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