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.lowlevel;
    18
    19  import java.io.StringReader;
    20  import java.util.ArrayList;
    21  import java.util.HashMap;
    22  import java.util.List;
    23  import java.util.Map;
    24
    25  import junit.framework.TestCase;
    26
    27  import org.yaml.snakeyaml.Yaml;
    28  import org.yaml.snakeyaml.events.Event;
    29  import org.yaml.snakeyaml.events.ScalarEvent;
    30  import org.yaml.snakeyaml.nodes.Node;
    31
    32  public class LowLevelApiTest extends TestCase {
    33
    34      public void testLowLevel() {
    35          List<Object> list = new ArrayList<Object>();
    36          list.add(1);
    37          list.add("abc");
    38          Map<String, String> map = new HashMap<String, String>();
    39          map.put("name", "Tolstoy");
    40          map.put("book", "War and People");
    41          list.add(map);
    42          Yaml yaml = new Yaml();
    43          String etalon = yaml.dump(list);
    44          // System.out.println(etalon);
    45          //
    46          Node node = yaml.represent(list);
    47          // System.out.println(node);
    48          assertEquals(
    49                  "Representation tree from an object and from its YAML document must be the same.",
    50                  yaml.compose(new StringReader(etalon)).toString(), node.toString());
    51          //
    52          List<Event> events = yaml.serialize(node);
    53          int i = 0;
    54          for (Event etalonEvent : yaml.parse(new StringReader(etalon))) {
    55              Event ev1 = events.get(i++);
    56              assertEquals(etalonEvent.getClass(), ev1.getClass());
    57              if (etalonEvent instanceof ScalarEvent) {
    58                  ScalarEvent scalar1 = (ScalarEvent) etalonEvent;
    59                  ScalarEvent scalar2 = (ScalarEvent) ev1;
    60                  assertEquals(scalar1.getAnchor(), scalar2.getAnchor());
    61                  assertEquals(scalar1.getValue(), scalar2.getValue());
    62              }
    63          }
    64          assertEquals(i, events.size());
    65      }
    66  }