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