aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/yaml/snakeyaml/constructor/BeanConstructorTest.java
blob: c2d3aa93f8795e23cabfe9ba912dd9588173f4a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.constructor;

import java.math.BigInteger;
import junit.framework.TestCase;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Util;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;

public class BeanConstructorTest extends TestCase {

  public void testPrimitivesConstructor() {
    Yaml yaml = new Yaml(new Constructor(TestBean1.class));
    String document = Util.getLocalResource("constructor/test-primitives1.yaml");
    TestBean1 result = yaml.load(document);
    assertNotNull(result);
    assertEquals(Byte.valueOf((byte) 1), result.getByteClass());
    assertEquals((byte) -3, result.getBytePrimitive());
    assertEquals(Short.valueOf((short) 0), result.getShortClass());
    assertEquals((short) -13, result.getShortPrimitive());
    assertEquals(Integer.valueOf(5), result.getInteger());
    assertEquals(17, result.getIntPrimitive());
    assertEquals("the text", result.getText());
    assertEquals("13", result.getId());
    assertEquals(Long.valueOf(11111111111L), result.getLongClass());
    assertEquals(9999999999L, result.getLongPrimitive());
    assertEquals(Boolean.TRUE, result.getBooleanClass());
    assertTrue(result.isBooleanPrimitive());
    assertEquals(Character.valueOf('2'), result.getCharClass());
    assertEquals('#', result.getCharPrimitive());
    assertEquals(new BigInteger("1234567890123456789012345678901234567890"),
        result.getBigInteger());
    assertEquals(Float.valueOf(2), result.getFloatClass());
    assertEquals(Float.valueOf(3.1416f), result.getFloatPrimitive());
    assertEquals(Double.valueOf(4), result.getDoubleClass());
    assertEquals(Double.valueOf(11200), result.getDoublePrimitive());
    assertEquals(1199836800000L, result.getDate().getTime());
    assertEquals("public", result.publicField);
    //
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    Yaml yamlToDump = new Yaml(options);
    String output = yamlToDump.dump(result);
    TestBean1 result2 = yaml.load(output);
    assertNotNull(result2);
    TestBean1 result3 = new Yaml().load(output);
    assertNotNull(result3);
  }

  public void testNoClassConstructor() {
    try {
      new Yaml(new Constructor((Class<? extends Object>) null));
      fail("Class must be provided.");
    } catch (NullPointerException e) {
      assertEquals("Root class must be provided.", e.getMessage());
    }
  }

  public void testNoClassConstructorString() throws ClassNotFoundException {
    try {
      new Yaml(new Constructor((String) null));
      fail("Class must be provided.");
    } catch (NullPointerException e) {
      assertEquals("Root type must be provided.", e.getMessage());
    }
  }

  public void testNoClassConstructorEmptyString() throws ClassNotFoundException {
    try {
      new Yaml(new Constructor(" "));
      fail("Class must be provided.");
    } catch (YAMLException e) {
      assertEquals("Root type must be provided.", e.getMessage());
    }
  }

  public void testCharacter() {
    Yaml yaml = new Yaml(new Constructor(TestBean1.class));
    String document = "charClass: id";
    try {
      yaml.load(document);
      fail("Only one char must be allowed.");
    } catch (Exception e) {
      assertTrue(e.getMessage(),
          e.getMessage().contains("Invalid node Character: 'id'; length: 2"));
    }
    document = "charClass: #";
    TestBean1 bean = yaml.load(document);
    assertNull("Null must be accepted.", bean.getCharClass());
    document = "charClass: ''";
    bean = yaml.load(document);
    assertNull("Null must be accepted.", bean.getCharClass());
    document = "charClass:\n";
    bean = yaml.load(document);
    assertNull("Null must be accepted.", bean.getCharClass());
    document = "charClass: 1\n";
    bean = yaml.load(document);
    assertEquals(Character.valueOf('1'), bean.getCharClass());
  }

  public void testNoEmptyConstructor() {
    Yaml yaml = new Yaml(new Constructor(TestBean2.class));
    String document = "text: qwerty";
    try {
      yaml.load(document);
      fail("No empty constructor available");
    } catch (Exception e) {
      assertTrue(e.getMessage(), e.getMessage().contains("NoSuchMethodException"));
    }
    TestBean2 bean = new TestBean2();
    assertEquals("", bean.getText());
  }

  private class TestBean2 {

    private String text;

    public TestBean2() {
      setText("");
    }

    public String getText() {
      return text;
    }

    public void setText(String text) {
      this.text = text;
    }
  }

  public void testPrivateMethod() {
    Yaml yaml = new Yaml(new Constructor(TestBean2.class));
    String document = "text: qwerty";
    try {
      yaml.load(document);
      fail("Private method cannot be called.");
    } catch (Exception e) {
      assertTrue(e.getMessage(), e.getMessage().contains("NoSuchMethodException"));
    }
  }

  public void testKeyNotScalar() {
    Yaml yaml = new Yaml(new Constructor(TestBean1.class));
    String document = "[1, 2]: qwerty";
    try {
      yaml.load(document);
      fail("Keys must be scalars.");
    } catch (Exception e) {
      assertTrue(e.getMessage(), e.getMessage().contains("Keys must be scalars but found"));
    }
  }

  public void testInvalidKey() {
    Yaml yaml = new Yaml(new Constructor(TestBean1.class));
    String document = "something: qwerty";
    try {
      yaml.load(document);
      fail("Non-existing property must fail.");
    } catch (Exception e) {
      assertTrue(e.getMessage(), e.getMessage().contains("Unable to find property 'something'"));
    }
  }

  public void testStaticField() {
    Yaml yaml = new Yaml(new Constructor(TestBean1.class));
    String document = "staticInteger: 123";
    try {
      yaml.load(document);
      fail("Staic variables must not be used.");
    } catch (Exception e) {
      assertTrue(e.getMessage(),
          e.getMessage().contains("Unable to find property 'staticInteger'"));
    }
  }

  public void testScalarContructor() {
    Yaml yaml = new Yaml(new Constructor(Parent1.class));
    String document = "id: 123\nchild: 25";
    Parent1 parent = yaml.load(document);
    assertEquals("123", parent.getId());
    Child1 child = parent.getChild();
    assertEquals(Integer.valueOf(25), child.getCode());
  }

  public void testScalarContructorException() {
    Yaml yaml = new Yaml(new Constructor(ExceptionParent.class));
    String document = "id: 123\nchild: 25";
    try {
      yaml.load(document);
      fail("ExceptionParent should not be created.");
    } catch (Exception e) {
      assertTrue(e.getMessage(), e.getMessage()
          .contains("Can't construct a java object for scalar tag:yaml.org,2002:int"));
    }
  }

  static public class ExceptionParent {

    private String id;
    private ExceptionChild child;

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public ExceptionChild getChild() {
      return child;
    }

    public void setChild(ExceptionChild child) {
      this.child = child;
    }

  }

  public static class ExceptionChild {

    private final Integer code;

    public ExceptionChild(Integer code) {
      throw new RuntimeException("ExceptionChild cannot be created.");
    }

    public Integer getCode() {
      return code;
    }
  }
}