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