View Javadoc

1   /**
2    * Copyright (c) 2008-2011, 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  
17  package org.yaml.snakeyaml.types;
18  
19  import java.io.IOException;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  /**
24   * @see http://yaml.org/type/binary.html
25   */
26  public class BinaryTagTest extends AbstractTest {
27      String line1 = "R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5";
28      String line2 = "OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+";
29      String line3 = "+f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC";
30      String line4 = "AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=";
31      String content = line1 + line2 + line3 + line4;
32  
33      public void testBinary() throws IOException {
34          byte[] binary = (byte[]) getMapValue("canonical: !!binary " + content, "canonical");
35          assertEquals((byte) 'G', binary[0]);
36          assertEquals((byte) 'I', binary[1]);
37          assertEquals((byte) 'F', binary[2]);
38          assertEquals((byte) '8', binary[3]);
39          assertEquals((byte) '9', binary[4]);
40      }
41  
42      public void testBinary2() throws IOException {
43          byte[] binary = (byte[]) load("!!binary \"MQ==\"");
44          assertEquals(1, binary.length);
45          assertEquals((byte) '1', binary[0]);
46      }
47  
48      public void testBinaryTag() throws IOException {
49          byte[] binary = (byte[]) getMapValue("canonical: !<tag:yaml.org,2002:binary> " + content,
50                  "canonical");
51          assertEquals((byte) 'G', binary[0]);
52          assertEquals((byte) 'I', binary[1]);
53          assertEquals((byte) 'F', binary[2]);
54          assertEquals((byte) '8', binary[3]);
55          assertEquals((byte) '9', binary[4]);
56      }
57  
58      public void testBinaryOut() throws IOException {
59          byte[] data = "GIF89\tbi\u0003\u0000nary\n\u001Fimage\n".getBytes("ISO-8859-1");
60          Map<String, String> map = new HashMap<String, String>();
61          String value = new String(data, "ISO-8859-1");
62          map.put("canonical", value);
63          String output = dump(map);
64          assertEquals("canonical: !!binary |-\n  R0lGODkJYmkDAG5hcnkKH2ltYWdlCg==\n", output);
65      }
66  
67      public void testByteArray() throws IOException {
68          byte[] data = { 8, 14, 15, 10, 126, 32, 65, 65, 65 };
69          String output = dump(data);
70          assertEquals("!!binary |-\n  CA4PCn4gQUFB\n", output);
71          byte[] parsed = (byte[]) load(output);
72          assertEquals(data.length, parsed.length);
73          for (int i = 0; i < data.length; i++) {
74              assertEquals(data[i], parsed[i]);
75          }
76      }
77  }