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.issues.issue10; 17 18 import java.util.ArrayList; 19 import java.util.Iterator; 20 21 import junit.framework.TestCase; 22 23 import org.yaml.snakeyaml.Util; 24 import org.yaml.snakeyaml.Yaml; 25 26 public class BasicDumpTest extends TestCase { 27 28 public void testTag() { 29 DataSource base = new DataSource(); 30 JDBCDataSource baseJDBC = new JDBCDataSource(); 31 baseJDBC.setParent(base); 32 33 ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); 34 // trying expected order first 35 dataSources.add(base); 36 dataSources.add(baseJDBC); 37 38 DataSources ds = new DataSources(); 39 ds.setDataSources(dataSources); 40 41 Yaml yaml = new Yaml(); 42 String output = yaml.dump(ds); 43 44 String etalon = Util.getLocalResource("javabeans/issue10-1.yaml"); 45 assertEquals(etalon.trim(), output.trim()); 46 Object obj = yaml.load(output); 47 DataSources dsOut = (DataSources) obj; 48 Iterator<DataSource> iter = dsOut.getDataSources().iterator(); 49 assertFalse("Must be DataSource.", iter.next() instanceof JDBCDataSource); 50 assertTrue(iter.next() instanceof JDBCDataSource); 51 } 52 53 public void testTag2() { 54 DataSource base = new DataSource(); 55 JDBCDataSource baseJDBC = new JDBCDataSource(); 56 baseJDBC.setParent(base); 57 58 ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); 59 dataSources.add(base); 60 dataSources.add(baseJDBC); 61 62 DataSources ds = new DataSources(); 63 ds.setDataSources(dataSources); 64 65 Yaml yaml = new Yaml(); 66 String output = yaml.dumpAsMap(ds); 67 68 String etalon = Util.getLocalResource("javabeans/issue10-2.yaml"); 69 assertEquals(etalon.trim(), output.trim()); 70 } 71 72 /** 73 * different order does not require the global tag 74 */ 75 public void testTag3() { 76 DataSource base = new DataSource(); 77 JDBCDataSource baseJDBC = new JDBCDataSource(); 78 baseJDBC.setParent(base); 79 80 ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); 81 dataSources.add(baseJDBC); 82 dataSources.add(base); 83 84 DataSources ds = new DataSources(); 85 ds.setDataSources(dataSources); 86 87 Yaml yaml = new Yaml(); 88 String output = yaml.dumpAsMap(ds); 89 90 String etalon = Util.getLocalResource("javabeans/issue10-3.yaml"); 91 assertEquals(etalon.trim(), output.trim()); 92 // load 93 Yaml beanLoader = new Yaml(); 94 DataSources bean = beanLoader.loadAs(output, DataSources.class); 95 Iterator<DataSource> iter = bean.getDataSources().iterator(); 96 assertTrue(iter.next() instanceof JDBCDataSource); 97 assertFalse("Must be DataSource.", iter.next() instanceof JDBCDataSource); 98 } 99 } 100