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;
    18
    19  import java.io.StringReader;
    20  import java.util.List;
    21
    22  import junit.framework.TestCase;
    23
    24  import org.yaml.snakeyaml.error.Mark;
    25  import org.yaml.snakeyaml.nodes.MappingNode;
    26  import org.yaml.snakeyaml.nodes.Node;
    27  import org.yaml.snakeyaml.nodes.NodeId;
    28  import org.yaml.snakeyaml.nodes.NodeTuple;
    29  import org.yaml.snakeyaml.nodes.ScalarNode;
    30  import org.yaml.snakeyaml.nodes.Tag;
    31  import org.yaml.snakeyaml.resolver.Resolver;
    32
    33  public class LoaderTest extends TestCase {
    34
    35      public void testCompose1() {
    36          Yaml loader = new Yaml();
    37          String yaml = "abc: 3";
    38          MappingNode node = (MappingNode) loader.compose(new StringReader(yaml));
    39          List<NodeTuple> nodes = node.getValue();
    40          assertEquals(1, nodes.size());
    41          NodeTuple pairs = nodes.get(0);
    42          ScalarNode key = (ScalarNode) pairs.getKeyNode();
    43          assertEquals(Tag.STR, key.getTag());
    44          assertEquals("abc", key.getValue());
    45          //
    46          ScalarNode value = (ScalarNode) pairs.getValueNode();
    47          assertEquals(Tag.INT, value.getTag());
    48          assertEquals("3", value.getValue());
    49          //
    50          assertEquals(
    51                  "<org.yaml.snakeyaml.nodes.MappingNode (tag=tag:yaml.org,2002:map, values={ key=<org.yaml.snakeyaml.nodes.ScalarNode (tag=tag:yaml.org,2002:str, value=abc)>; value=<NodeTuple keyNode=<org.yaml.snakeyaml.nodes.ScalarNode (tag=tag:yaml.org,2002:str, value=abc)>; valueNode=<org.yaml.snakeyaml.nodes.ScalarNode (tag=tag:yaml.org,2002:int, value=3)>> })>",
    52                  node.toString());
    53      }
    54
    55      public void testCompose2() {
    56          LoaderOptions options = new LoaderOptions();
    57          Yaml loader = new Yaml(options);
    58          String yaml = "3";
    59          ScalarNode node = (ScalarNode) loader.compose(new StringReader(yaml));
    60          assertEquals(Tag.INT, node.getTag());
    61          assertEquals("3", node.getValue());
    62          // not sure whether it should be null or 0
    63          assertEquals(new Character('\u0000'), node.getStyle());
    64          assertEquals(Object.class, node.getType());
    65          Mark mark = node.getStartMark();
    66          assertEquals(0, mark.getColumn());
    67          assertEquals(0, mark.getLine());
    68          assertEquals("<reader>", mark.getName());
    69          assertEquals("    3\n    ^", mark.get_snippet());
    70          assertEquals("<org.yaml.snakeyaml.nodes.ScalarNode (tag=tag:yaml.org,2002:int, value=3)>",
    71                  node.toString());
    72      }
    73
    74      public void testComposeAll() {
    75          Yaml loader = new Yaml();
    76          String yaml = "abc: 3\n---\n2\n---\n- qwe\n- asd\n";
    77          int counter = 0;
    78          for (Node node : loader.composeAll(new StringReader(yaml))) {
    79              assertNotNull(node);
    80              switch (counter++) {
    81              case 0:
    82                  assertEquals(NodeId.mapping, node.getNodeId());
    83                  break;
    84              case 1:
    85                  assertEquals(NodeId.scalar, node.getNodeId());
    86                  break;
    87              case 2:
    88                  assertEquals(NodeId.sequence, node.getNodeId());
    89                  break;
    90              default:
    91                  fail("Unexpected document.");
    92              }
    93          }
    94      }
    95
    96      @SuppressWarnings("deprecation")
    97      public void testDeprecated() {
    98          Yaml yaml = new Yaml(new Loader());
    99          yaml.load("123");
    100         yaml = new Yaml(new Loader(), new Dumper());
    101         yaml.load("123");
    102         yaml = new Yaml(new Loader(), new Dumper(), new Resolver());
    103         yaml.load("123");
    104     }
    105 }