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.issues.issue67;
18  
19  import junit.framework.TestCase;
20  
21  import org.yaml.snakeyaml.LoaderOptions;
22  import org.yaml.snakeyaml.Util;
23  import org.yaml.snakeyaml.Yaml;
24  import org.yaml.snakeyaml.nodes.Tag;
25  import org.yaml.snakeyaml.representer.Representer;
26  import org.yaml.snakeyaml.scanner.ScannerException;
27  
28  public class NonAsciiCharsInClassNameTest extends TestCase {
29      private String PREFIX = "!!org.yaml.snakeyaml.issues.issue67.NonAsciiCharsInClassNameTest$";
30  
31      public void testDump() {
32          Académico obj = new Académico();
33          obj.setId(1);
34          obj.setName("Foo bar baz");
35          Yaml yaml = new Yaml();
36          String result = yaml.dump(obj);
37          assertEquals(PREFIX + "Acad%C3%A9mico {\n  id: 1, name: Foo bar baz}\n", result);
38      }
39  
40      public void testLoad() {
41          Yaml yaml = new Yaml();
42          Académico obj = (Académico) yaml.load(PREFIX + "Acad%C3%A9mico {id: 3, name: Foo bar}");
43          assertEquals(3, obj.getId());
44          assertEquals("Foo bar", obj.getName());
45      }
46  
47      public void testLoadInvalidPattern() {
48          try {
49              Yaml yaml = new Yaml();
50              yaml.load(PREFIX + "Acad%WZ%A9mico {id: 3, name: Foo bar}");
51              fail("Illegal hex characters in escape (%) pattern must not be accepted.");
52          } catch (Exception e) {
53              assertEquals(
54                      "while scanning a tag; expected URI escape sequence of 2 hexadecimal numbers, but found W(87) and Z(90)",
55                      e.getMessage());
56          }
57      }
58  
59      public void testLoadInvalidPatternTooShort() {
60          try {
61              LoaderOptions options = new LoaderOptions();
62              Yaml yaml = new Yaml(options);
63              yaml.load(PREFIX + "Acad%9%A9mico {id: 3, name: Foo bar}");
64              fail("Illegal hex characters in escape (%) pattern must not be accepted.");
65          } catch (ScannerException e) {
66              assertEquals(
67                      "while scanning a tag; expected URI escape sequence of 2 hexadecimal numbers, but found 9(57) and %(37)",
68                      e.getMessage());
69              assertEquals(Util.getLocalResource("issues/issue67-error1.txt"), e.toString());
70          }
71      }
72  
73      public void testLoadInvalidUtf8() {
74          try {
75              LoaderOptions options = new LoaderOptions();
76              Yaml yaml = new Yaml(options);
77              yaml.load(PREFIX + "Acad%C0mico {id: 3, name: Foo bar}");
78              fail("Illegal UTF-8 must not be accepted.");
79          } catch (ScannerException e) {
80              assertEquals("while scanning a tag; expected URI in UTF-8: Input length = 1",
81                      e.getMessage());
82              assertEquals(Util.getLocalResource("issues/issue67-error2.txt"), e.toString());
83          }
84      }
85  
86      public static class Académico {
87          public int getId() {
88              return id;
89          }
90  
91          public void setId(int id) {
92              this.id = id;
93          }
94  
95          public String getName() {
96              return name;
97          }
98  
99          public void setName(String name) {
100             this.name = name;
101         }
102 
103         private int id;
104         private String name;
105     }
106 
107     public void testDumpCustomTag() {
108         Académico obj = new Académico();
109         obj.setId(123);
110         obj.setName("Foo bar 123");
111         Representer repr = new Representer();
112         repr.addClassTag(Académico.class, new Tag("!foo"));
113         Yaml yaml = new Yaml(repr);
114         String result = yaml.dump(obj);
115         assertEquals("!foo {id: 123, name: Foo bar 123}\n", result);
116     }
117 
118     public void testDumpEscapedTag() {
119         Académico obj = new Académico();
120         obj.setId(123);
121         obj.setName("Foo bar 123");
122         Representer repr = new Representer();
123         repr.addClassTag(Académico.class, new Tag("!Académico"));
124         Yaml yaml = new Yaml(repr);
125         String result = yaml.dump(obj);
126         assertEquals("!Acad%C3%A9mico {id: 123, name: Foo bar 123}\n", result);
127     }
128 
129     public void testTag() {
130         Tag tag = new Tag("!java/javabean:foo.Bar");
131         assertEquals("!java/javabean:foo.Bar", tag.getValue());
132     }
133 }