View Javadoc

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   */
16  package org.yaml.snakeyaml.issues.issue149;
17  
18  import java.util.Iterator;
19  
20  import junit.framework.TestCase;
21  
22  import org.yaml.snakeyaml.TypeDescription;
23  import org.yaml.snakeyaml.Util;
24  import org.yaml.snakeyaml.Yaml;
25  import org.yaml.snakeyaml.constructor.Constructor;
26  import org.yaml.snakeyaml.nodes.Tag;
27  
28  public class GlobalDirectivesTest extends TestCase {
29      public void testOneDocument() {
30          String input = Util.getLocalResource("issues/issue149-one-document.yaml");
31          // System.out.println(input);
32          Constructor constr = new Constructor();
33          TypeDescription description = new TypeDescription(ComponentBean.class, new Tag(
34                  "tag:ualberta.ca,2012:29"));
35          constr.addTypeDescription(description);
36          Yaml yaml = new Yaml(constr);
37          Iterator<Object> parsed = yaml.loadAll(input).iterator();
38          ComponentBean bean = (ComponentBean) parsed.next();
39          assertEquals(0, bean.getProperty1());
40          assertEquals("aaa", bean.getProperty2());
41          assertFalse(parsed.hasNext());
42      }
43  
44      public void testDirectives() {
45          String input = Util.getLocalResource("issues/issue149-losing-directives.yaml");
46          // System.out.println(input);
47          Constructor constr = new Constructor();
48          TypeDescription description = new TypeDescription(ComponentBean.class, new Tag(
49                  "tag:ualberta.ca,2012:" + 29));
50          constr.addTypeDescription(description);
51          Yaml yaml = new Yaml(constr);
52          Iterator<Object> parsed = yaml.loadAll(input).iterator();
53          ComponentBean bean1 = (ComponentBean) parsed.next();
54          assertEquals(0, bean1.getProperty1());
55          assertEquals("aaa", bean1.getProperty2());
56          ComponentBean bean2 = (ComponentBean) parsed.next();
57          assertEquals(3, bean2.getProperty1());
58          assertEquals("bbb", bean2.getProperty2());
59          assertFalse(parsed.hasNext());
60      }
61  
62      public void testDirectives2() {
63          String input = Util.getLocalResource("issues/issue149-losing-directives-2.yaml");
64          // System.out.println(input);
65          Constructor constr = new Constructor();
66          TypeDescription description = new TypeDescription(ComponentBean.class, new Tag(
67                  "tag:ualberta.ca,2012:" + 29));
68          constr.addTypeDescription(description);
69          Yaml yaml = new Yaml(constr);
70          Iterator<Object> parsed = yaml.loadAll(input).iterator();
71          ComponentBean bean1 = (ComponentBean) parsed.next();
72          assertEquals(0, bean1.getProperty1());
73          assertEquals("aaa", bean1.getProperty2());
74          ComponentBean bean2 = (ComponentBean) parsed.next();
75          assertEquals(3, bean2.getProperty1());
76          assertEquals("bbb", bean2.getProperty2());
77          assertFalse(parsed.hasNext());
78      }
79  }