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.constructor; 17 18 import java.util.List; 19 20 import junit.framework.TestCase; 21 22 import org.yaml.snakeyaml.DumperOptions; 23 import org.yaml.snakeyaml.DumperOptions.FlowStyle; 24 import org.yaml.snakeyaml.TypeDescription; 25 import org.yaml.snakeyaml.Util; 26 import org.yaml.snakeyaml.Yaml; 27 28 public class ArrayTagsTest extends TestCase { 29 30 public void testDefaultRepresenter() { 31 CarWithArray car = new CarWithArray(); 32 car.setPlate("12-XP-F4"); 33 Wheel[] wheels = new Wheel[5]; 34 for (int i = 1; i < 6; i++) { 35 Wheel wheel = new Wheel(); 36 wheel.setId(i); 37 wheels[i - 1] = wheel; 38 } 39 car.setWheels(wheels); 40 assertEquals(Util.getLocalResource("constructor/cararray-with-tags-flow-auto.yaml"), 41 new Yaml().dump(car)); 42 } 43 44 public void testFlowBlock() { 45 CarWithArray car = new CarWithArray(); 46 car.setPlate("12-XP-F4"); 47 Wheel[] wheels = new Wheel[5]; 48 for (int i = 1; i < 6; i++) { 49 Wheel wheel = new Wheel(); 50 wheel.setId(i); 51 wheels[i - 1] = wheel; 52 } 53 car.setWheels(wheels); 54 DumperOptions options = new DumperOptions(); 55 options.setDefaultFlowStyle(FlowStyle.BLOCK); 56 Yaml yaml = new Yaml(options); 57 assertEquals(Util.getLocalResource("constructor/cararray-with-tags.yaml"), yaml.dump(car)); 58 } 59 60 public void testLoadClassTag() { 61 Constructor constructor = new Constructor(); 62 constructor.addTypeDescription(new TypeDescription(Car.class, "!car")); 63 Yaml yaml = new Yaml(constructor); 64 Car car = (Car) yaml.load(Util.getLocalResource("constructor/car-without-tags.yaml")); 65 assertEquals("12-XP-F4", car.getPlate()); 66 List<Wheel> wheels = car.getWheels(); 67 assertNotNull(wheels); 68 assertEquals(5, wheels.size()); 69 } 70 71 public void testNullDescription() { 72 Constructor constructor = new Constructor(); 73 try { 74 constructor.addTypeDescription(null); 75 fail("Description is required."); 76 } catch (Exception e) { 77 assertEquals("TypeDescription is required.", e.getMessage()); 78 } 79 } 80 81 public void testLoadClassNoRoot() { 82 Constructor constructor = new Constructor(new TypeDescription(CarWithArray.class)); 83 Yaml yaml = new Yaml(constructor); 84 CarWithArray car = (CarWithArray) yaml.load(Util 85 .getLocalResource("constructor/car-no-root-class.yaml")); 86 assertEquals("12-XP-F4", car.getPlate()); 87 Wheel[] wheels = car.getWheels(); 88 assertNotNull(wheels); 89 assertEquals(5, wheels.length); 90 } 91 92 public static class CarWithArray { 93 private String plate; 94 private Wheel[] wheels; 95 96 public String getPlate() { 97 return plate; 98 } 99 100 public void setPlate(String plate) { 101 this.plate = plate; 102 } 103 104 public Wheel[] getWheels() { 105 return wheels; 106 } 107 108 public void setWheels(Wheel[] wheels) { 109 this.wheels = wheels; 110 } 111 } 112 } 113