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.javabeans; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 import java.util.Map; 21 import java.util.TreeMap; 22 23 import junit.framework.TestCase; 24 25 import org.yaml.snakeyaml.DumperOptions; 26 import org.yaml.snakeyaml.DumperOptions.FlowStyle; 27 import org.yaml.snakeyaml.TypeDescription; 28 import org.yaml.snakeyaml.Util; 29 import org.yaml.snakeyaml.Yaml; 30 import org.yaml.snakeyaml.constructor.Constructor; 31 32 public class HouseTest extends TestCase { 33 /** 34 * no root global tag 35 */ testDump1()36 public void testDump1() { 37 House house = new House(); 38 FrontDoor frontDoor = new FrontDoor("qaz1", 5); 39 frontDoor.setKeytype("qwerty123"); 40 house.setFrontDoor(frontDoor); 41 List<Room> rooms = new ArrayList<Room>(); 42 rooms.add(new Room("Hall")); 43 rooms.add(new Room("Kitchen")); 44 house.setRooms(rooms); 45 Map<String, String> reminders = new TreeMap<String, String>(); 46 reminders.put("today", "do nothig"); 47 reminders.put("tomorrow", "go shoping"); 48 house.setReminders(reminders); 49 house.setNumber(1); 50 house.setStreet("Wall Street"); 51 Yaml beanDumper = new Yaml(); 52 String yaml = beanDumper.dumpAsMap(house); 53 String etalon = Util.getLocalResource("javabeans/house-dump1.yaml"); 54 assertEquals(etalon, yaml); 55 // load 56 Yaml beanLoader = new Yaml(); 57 House loadedHouse = beanLoader.loadAs(yaml, House.class); 58 assertNotNull(loadedHouse); 59 assertEquals("Wall Street", loadedHouse.getStreet()); 60 // dump again 61 String yaml3 = beanDumper.dumpAsMap(loadedHouse); 62 assertEquals(yaml, yaml3); 63 } 64 65 /** 66 * with global root class tag (global tag should be avoided) 67 */ testDump3()68 public void testDump3() { 69 House house = new House(); 70 FrontDoor frontDoor = new FrontDoor("qaz1", 5); 71 frontDoor.setKeytype("qwerty123"); 72 house.setFrontDoor(frontDoor); 73 List<Room> rooms = new ArrayList<Room>(); 74 rooms.add(new Room("Hall")); 75 rooms.add(new Room("Kitchen")); 76 house.setRooms(rooms); 77 Map<String, String> reminders = new TreeMap<String, String>(); 78 reminders.put("today", "do nothig"); 79 reminders.put("tomorrow", "go shoping"); 80 house.setReminders(reminders); 81 house.setNumber(1); 82 house.setStreet("Wall Street"); 83 Yaml beanDumper = new Yaml(); 84 String yaml = beanDumper.dumpAsMap(house); 85 String etalon = Util.getLocalResource("javabeans/house-dump3.yaml"); 86 assertEquals(etalon, yaml); 87 // load 88 TypeDescription description = new TypeDescription(House.class); 89 description.putListPropertyType("rooms", Room.class); 90 Yaml beanLoader = new Yaml(new Constructor(description)); 91 House loadedHouse = (House) beanLoader.load(yaml); 92 House loadedHouse2 = (House) beanLoader.loadAs(yaml, House.class); 93 assertNotNull(loadedHouse); 94 assertFalse(loadedHouse == loadedHouse2); 95 assertEquals("Wall Street", loadedHouse.getStreet()); 96 assertEquals(1, loadedHouse.getNumber()); 97 assertEquals(1, loadedHouse2.getNumber()); 98 FrontDoor fdoor = loadedHouse.getFrontDoor(); 99 assertEquals(frontDoor.getId(), fdoor.getId()); 100 assertEquals(frontDoor.getHeight(), fdoor.getHeight()); 101 assertEquals(frontDoor.getKeytype(), fdoor.getKeytype()); 102 assertEquals(frontDoor, fdoor); 103 assertEquals(reminders, loadedHouse.getReminders()); 104 List<Room> loadedRooms = loadedHouse.getRooms(); 105 assertEquals(rooms, loadedRooms); 106 // dump again 107 String yaml3 = beanDumper.dumpAsMap(loadedHouse); 108 assertEquals(yaml, yaml3); 109 } 110 111 /** 112 * with global root class tag (global tag should be avoided) 113 */ testDump2()114 public void testDump2() { 115 House house = new House(); 116 FrontDoor frontDoor = new FrontDoor("qaz1", 5); 117 frontDoor.setKeytype("qwerty123"); 118 house.setFrontDoor(frontDoor); 119 List<Room> rooms = new ArrayList<Room>(); 120 rooms.add(new Room("Hall")); 121 rooms.add(new Room("Kitchen")); 122 house.setRooms(rooms); 123 Map<String, String> reminders = new TreeMap<String, String>(); 124 reminders.put("today", "do nothig"); 125 reminders.put("tomorrow", "go shoping"); 126 house.setReminders(reminders); 127 house.setNumber(1); 128 house.setStreet("Wall Street"); 129 DumperOptions options = new DumperOptions(); 130 options.setDefaultFlowStyle(FlowStyle.BLOCK); 131 Yaml beanDumper = new Yaml(options); 132 String yaml = beanDumper.dump(house); 133 String etalon = Util.getLocalResource("javabeans/house-dump2.yaml"); 134 assertEquals(etalon, yaml); 135 // load 136 Yaml beanLoader = new Yaml(); 137 House loadedHouse = beanLoader.loadAs(yaml, House.class); 138 assertNotNull(loadedHouse); 139 assertEquals("Wall Street", loadedHouse.getStreet()); 140 // dump again 141 String yaml3 = beanDumper.dump(loadedHouse); 142 assertEquals(yaml, yaml3); 143 } 144 } 145