1 /** 2 * Copyright (c) 2008, http://www.snakeyaml.org 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.yaml.snakeyaml.types; 17 18 import java.io.IOException; 19 import java.util.HashMap; 20 import java.util.Map; 21 22 /** 23 * @see <a href="http://yaml.org/type/binary.html"></a> 24 */ 25 public class BinaryTagTest extends AbstractTest { 26 String line1 = "R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5"; 27 String line2 = "OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+"; 28 String line3 = "+f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC"; 29 String line4 = "AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="; 30 String content = line1 + line2 + line3 + line4; 31 32 public void testBinary() { 33 byte[] binary = (byte[]) getMapValue("canonical: !!binary " + content, "canonical"); 34 assertEquals((byte) 'G', binary[0]); 35 assertEquals((byte) 'I', binary[1]); 36 assertEquals((byte) 'F', binary[2]); 37 assertEquals((byte) '8', binary[3]); 38 assertEquals((byte) '9', binary[4]); 39 } 40 41 public void testBinary2() { 42 byte[] binary = (byte[]) load("!!binary \"MQ==\""); 43 assertEquals(1, binary.length); 44 assertEquals((byte) '1', binary[0]); 45 } 46 47 public void testBinaryTag() { 48 byte[] binary = (byte[]) getMapValue("canonical: !<tag:yaml.org,2002:binary> " + content, 49 "canonical"); 50 assertEquals((byte) 'G', binary[0]); 51 assertEquals((byte) 'I', binary[1]); 52 assertEquals((byte) 'F', binary[2]); 53 assertEquals((byte) '8', binary[3]); 54 assertEquals((byte) '9', binary[4]); 55 } 56 57 public void testBinaryOut() throws IOException { 58 byte[] data = "GIF89\tbi\u0003\u0000nary\n\u001Fimage\n".getBytes("ISO-8859-1"); 59 Map<String, String> map = new HashMap<String, String>(); 60 String value = new String(data, "ISO-8859-1"); 61 map.put("canonical", value); 62 String output = dump(map); 63 assertEquals("canonical: !!binary |-\n R0lGODkJYmkDAG5hcnkKH2ltYWdlCg==\n", output); 64 } 65 66 public void testByteArray() { 67 byte[] data = { 8, 14, 15, 10, 126, 32, 65, 65, 65 }; 68 String output = dump(data); 69 assertEquals("!!binary |-\n CA4PCn4gQUFB\n", output); 70 byte[] parsed = (byte[]) load(output); 71 assertEquals(data.length, parsed.length); 72 for (int i = 0; i < data.length; i++) { 73 assertEquals(data[i], parsed[i]); 74 } 75 } 76 } 77