|
| 1 | + |
1 | 2 | package com.esotericsoftware.yamlbeans; |
2 | 3 |
|
3 | | -import org.junit.Test; |
| 4 | +import static junit.framework.Assert.*; |
4 | 5 |
|
5 | 6 | import java.util.HashMap; |
6 | 7 | import java.util.List; |
7 | 8 | import java.util.Map; |
8 | 9 |
|
9 | | -import static junit.framework.Assert.assertEquals; |
10 | | -import static junit.framework.Assert.assertNull; |
11 | | -import static junit.framework.Assert.assertTrue; |
| 10 | +import org.junit.Test; |
12 | 11 |
|
13 | 12 | public class SafeYamlConfigTest { |
14 | | - |
15 | | - |
16 | | - private static final String TESTOBJECT_TAG = "!com.esotericsoftware.yamlbeans.SafeYamlConfigTest$TestObject"; |
17 | | - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
18 | | - |
19 | | - |
20 | | - @Test |
21 | | - public void testDeserializationOfClassTag() throws YamlException { |
22 | | - SafeYamlConfig yamlConfig = new SafeYamlConfig(); |
23 | | - StringBuilder yamlData = new StringBuilder(); |
24 | | - yamlData.append(TESTOBJECT_TAG).append(LINE_SEPARATOR) |
25 | | - .append("a: test").append(LINE_SEPARATOR); |
26 | | - YamlReader reader = new YamlReader(yamlData.toString(),yamlConfig); |
27 | | - Object data = reader.read(); |
28 | | - assertTrue(data instanceof HashMap); |
29 | | - Map dataMap = (Map) data; |
30 | | - assertTrue(dataMap.containsKey("a")); |
31 | | - assertEquals("test",dataMap.get("a")); |
32 | | - } |
33 | | - |
34 | | - |
35 | | - |
36 | | - @Test |
37 | | - public void testIgnoreAnchor() throws YamlException { |
38 | | - SafeYamlConfig yamlConfig = new SafeYamlConfig(); |
39 | | - StringBuilder yamlData = new StringBuilder(); |
40 | | - yamlData.append("oldest friend:").append(LINE_SEPARATOR) |
41 | | - .append(" &1 !contact").append(LINE_SEPARATOR) |
42 | | - .append(" name: Bob").append(LINE_SEPARATOR) |
43 | | - .append(" age: 29").append(LINE_SEPARATOR) |
44 | | - .append("best friend: *1").append(LINE_SEPARATOR); |
45 | | - YamlReader reader = new YamlReader(yamlData.toString(),yamlConfig); |
46 | | - Object data = reader.read(); |
47 | | - assertTrue(data instanceof HashMap); |
48 | | - Map dataMap = (Map) data; |
49 | | - assertTrue(dataMap.containsKey("oldest friend")); |
50 | | - Map old = (Map) dataMap.get("oldest friend"); |
51 | | - assertTrue(old.containsKey("name")); |
52 | | - assertEquals("Bob",old.get("name")); |
53 | | - assertNull(dataMap.get("best friend")); |
54 | | - } |
55 | | - |
56 | | - |
57 | | - static class TestObject { |
58 | | - private String a; |
59 | | - public int age; |
60 | | - public String name; |
61 | | - public Object object; |
62 | | - public List<Object> objects; |
63 | | - |
64 | | - private TestObject() { |
65 | | - } |
66 | | - |
67 | | - public TestObject(String a) { |
68 | | - this.a = a; |
69 | | - } |
70 | | - |
71 | | - public String getA() { |
72 | | - return a; |
73 | | - } |
74 | | - |
75 | | - public void setA(String a) { |
76 | | - this.a = a; |
77 | | - } |
78 | | - } |
| 13 | + private static final String TESTOBJECT_TAG = "!com.esotericsoftware.yamlbeans.SafeYamlConfigTest$TestObject"; |
| 14 | + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
| 15 | + |
| 16 | + @Test |
| 17 | + public void testDeserializationOfClassTag () throws YamlException { |
| 18 | + SafeYamlConfig yamlConfig = new SafeYamlConfig(); |
| 19 | + StringBuilder yamlData = new StringBuilder(); |
| 20 | + yamlData.append(TESTOBJECT_TAG).append(LINE_SEPARATOR).append("a: test").append(LINE_SEPARATOR); |
| 21 | + YamlReader reader = new YamlReader(yamlData.toString(), yamlConfig); |
| 22 | + Object data = reader.read(); |
| 23 | + assertTrue(data instanceof HashMap); |
| 24 | + Map dataMap = (Map)data; |
| 25 | + assertTrue(dataMap.containsKey("a")); |
| 26 | + assertEquals("test", dataMap.get("a")); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testIgnoreAnchor () throws YamlException { |
| 31 | + SafeYamlConfig yamlConfig = new SafeYamlConfig(); |
| 32 | + StringBuilder yamlData = new StringBuilder(); |
| 33 | + yamlData.append("oldest friend:").append(LINE_SEPARATOR).append(" &1 !contact").append(LINE_SEPARATOR) |
| 34 | + .append(" name: Bob").append(LINE_SEPARATOR).append(" age: 29").append(LINE_SEPARATOR).append("best friend: *1") |
| 35 | + .append(LINE_SEPARATOR); |
| 36 | + YamlReader reader = new YamlReader(yamlData.toString(), yamlConfig); |
| 37 | + Object data = reader.read(); |
| 38 | + assertTrue(data instanceof HashMap); |
| 39 | + Map dataMap = (Map)data; |
| 40 | + assertTrue(dataMap.containsKey("oldest friend")); |
| 41 | + Map old = (Map)dataMap.get("oldest friend"); |
| 42 | + assertTrue(old.containsKey("name")); |
| 43 | + assertEquals("Bob", old.get("name")); |
| 44 | + assertNull(dataMap.get("best friend")); |
| 45 | + } |
| 46 | + |
| 47 | + static class TestObject { |
| 48 | + private String a; |
| 49 | + public int age; |
| 50 | + public String name; |
| 51 | + public Object object; |
| 52 | + public List<Object> objects; |
| 53 | + |
| 54 | + private TestObject () { |
| 55 | + } |
| 56 | + |
| 57 | + public TestObject (String a) { |
| 58 | + this.a = a; |
| 59 | + } |
| 60 | + |
| 61 | + public String getA () { |
| 62 | + return a; |
| 63 | + } |
| 64 | + |
| 65 | + public void setA (String a) { |
| 66 | + this.a = a; |
| 67 | + } |
| 68 | + } |
79 | 69 | } |
0 commit comments