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.extensions.compactnotation;
    18
    19  import java.util.Iterator;
    20  import java.util.List;
    21  import java.util.Map;
    22
    23  import junit.framework.TestCase;
    24
    25  import org.yaml.snakeyaml.Util;
    26  import org.yaml.snakeyaml.Yaml;
    27  import org.yaml.snakeyaml.constructor.Constructor;
    28
    29  public class CompactConstructorExampleTest extends TestCase {
    30
    31      private Object load(String fileName) {
    32          CompactConstructor compact = new CompactConstructor();
    33          Yaml yaml = new Yaml(compact);
    34          String doc = Util.getLocalResource("compactnotation/" + fileName);
    35          Object obj = yaml.load(doc);
    36          assertNotNull(obj);
    37          return obj;
    38      }
    39
    40      public void test1() {
    41          Object obj = load("example1.yaml");
    42          assertEquals(new Container(), obj);
    43      }
    44
    45      public void test2() {
    46          Object obj = load("example2.yaml");
    47          assertEquals(new Container("title"), obj);
    48      }
    49
    50      public void test3() {
    51          Container obj = (Container) load("example3.yaml");
    52          assertEquals(new Container("title3"), obj);
    53          assertEquals("title3", obj.getTitle());
    54          assertEquals("parent", obj.getName());
    55          assertEquals("123", obj.getId());
    56      }
    57
    58      public void test4() {
    59          Object obj = load("example4.yaml");
    60          // System.out.println(obj);
    61          Container container = (Container) obj;
    62          assertNotNull(obj);
    63          assertEquals(new Container("title4"), obj);
    64          assertEquals("title4", container.getTitle());
    65          assertEquals("child4", container.getName());
    66          assertEquals("444", container.getId());
    67      }
    68
    69      public void test5() {
    70          Object obj = load("example5.yaml");
    71          // System.out.println(obj);
    72          Container container = (Container) obj;
    73          assertNotNull(obj);
    74          assertEquals(new Container("title4"), obj);
    75          assertEquals("title4", container.getTitle());
    76          assertEquals("child5", container.getName());
    77          assertEquals("ID555", container.getId());
    78      }
    79
    80      public void test6() {
    81          Object obj = load("example6.yaml");
    82          // System.out.println(obj);
    83          Container container = (Container) obj;
    84          assertNotNull(obj);
    85          assertEquals(new Container("title4"), obj);
    86          assertEquals("title4", container.getTitle());
    87          assertEquals("child6", container.getName());
    88          assertEquals("ID6", container.getId());
    89      }
    90
    91      public void test7() {
    92          Object obj = load("example7.yaml");
    93          // System.out.println(obj);
    94          Container container = (Container) obj;
    95          assertNotNull(obj);
    96          assertEquals(new Container("The title"), obj);
    97          assertEquals("The title", container.getTitle());
    98          assertEquals("child7", container.getName());
    99          assertEquals("id7", container.getId());
    100     }
    101
    102     @SuppressWarnings("unchecked")
    103     // TODO it is unclear how the result should look like for CON
    104     public void test9() {
    105         Map<String, Object> map = (Map<String, Object>) load("example9.yaml");
    106         assertEquals(1, map.size());
    107         Map<Container, Map<String, String>> containers = (Map<Container, Map<String, String>>) map
    108                 .get("something");
    109         // System.out.println(obj);
    110         assertEquals(2, containers.size());
    111         for (Container c : containers.keySet()) {
    112             assertTrue(c.getId().matches("id\\d"));
    113             assertEquals(1, containers.get(c).size());
    114         }
    115     }
    116
    117     @SuppressWarnings("unchecked")
    118     public void test10() {
    119         Map<String, Object> map = (Map<String, Object>) load("example10.yaml");
    120         assertEquals(1, map.size());
    121         List<Container> containers = (List<Container>) map.get("something");
    122         // System.out.println(obj);
    123         assertEquals(3, containers.size());
    124         for (Container c : containers) {
    125             assertTrue(c.toString(), c.getId().matches("id\\d+"));
    126             assertTrue(c.toString(), c.getName().matches("child\\d+"));
    127             // System.out.println(c);
    128         }
    129     }
    130
    131     public void test11withoutPackageNames() {
    132         Constructor compact = new PackageCompactConstructor(
    133                 "org.yaml.snakeyaml.extensions.compactnotation");
    134         Yaml yaml = new Yaml(compact);
    135         String doc = Util.getLocalResource("compactnotation/example11.yaml");
    136         Box box = (Box) yaml.load(doc);
    137         assertNotNull(box);
    138         assertEquals("id11", box.getId());
    139         assertEquals("Main box", box.getName());
    140         Item top = box.getTop();
    141         assertEquals("id003", top.getId());
    142         assertEquals("25.0", top.getPrice());
    143         assertEquals("parrot", top.getName());
    144         Item bottom = box.getBottom();
    145         assertEquals("id004", bottom.getId());
    146         assertEquals("3.5", bottom.getPrice());
    147         assertEquals("sweet", bottom.getName());
    148     }
    149
    150     public void test12withList() {
    151         Constructor compact = new TableCompactConstructor(
    152                 "org.yaml.snakeyaml.extensions.compactnotation");
    153         Yaml yaml = new Yaml(compact);
    154         String doc = Util.getLocalResource("compactnotation/example12.yaml");
    155         Table table = (Table) yaml.load(doc);
    156         assertNotNull(table);
    157         assertEquals("id12", table.getId());
    158         assertEquals("A table", table.getName());
    159         List<Row> rows = table.getRows();
    160         assertEquals(3, rows.size());
    161         Iterator<Row> iter = rows.iterator();
    162         Row first = iter.next();
    163         assertEquals("id111", first.getId());
    164         assertEquals("I think; therefore I am.", first.getDescription());
    165         assertEquals(0.125, first.getRatio(), 0.000000001);
    166         assertEquals(15, first.getSize());
    167         Row second = iter.next();
    168         assertEquals("id222", second.getId());
    169         assertEquals("We do not need new lines here, just replace them all with spaces\n",
    170                 second.getDescription());
    171         assertEquals(0.333, second.getRatio(), 0.000000001);
    172         assertEquals(17, second.getSize());
    173         Row third = iter.next();
    174         assertEquals("id333", third.getId());
    175         assertEquals(
    176                 "Please preserve all\nthe lines because they may be\nimportant, but do not include the last one !!!",
    177                 third.getDescription());
    178         assertEquals(0.88, third.getRatio(), 0.000000001);
    179         assertEquals(52, third.getSize());
    180     }
    181 }