View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.dataset;
23  
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.sql.Connection;
27  
28  import junit.framework.TestCase;
29  
30  import org.dbunit.HypersonicEnvironment;
31  import org.dbunit.database.DatabaseConfig;
32  import org.dbunit.database.DatabaseConnection;
33  import org.dbunit.database.IDatabaseConnection;
34  import org.dbunit.database.QueryDataSet;
35  import org.dbunit.dataset.Column;
36  import org.dbunit.dataset.CompositeDataSet;
37  import org.dbunit.dataset.DefaultDataSet;
38  import org.dbunit.dataset.DefaultTable;
39  import org.dbunit.dataset.datatype.DataType;
40  import org.dbunit.dataset.xml.FlatXmlDataSet;
41  import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
42  import org.dbunit.testutil.TestUtils;
43  
44  
45  /**
46   * Test Case for issue #1721870
47   * @author Sebastien Le Callonnec
48   * @version $Revision$
49   * @since Mar 11, 2008
50   */
51  public class CompositeDataSetIterationTest extends TestCase {
52  
53  	private Connection jdbcConnection;
54  	private final String sqlFile = "hypersonic_simple_dataset.sql"; 
55  	private IDatabaseConnection connection;
56  
57  	protected void setUp() throws Exception {
58  		super.setUp();
59  		this.jdbcConnection = HypersonicEnvironment.createJdbcConnection("mem:tempdb");
60  		HypersonicEnvironment.executeDdlFile(TestUtils.getFile("sql/" + sqlFile), jdbcConnection);
61  		this.connection = new DatabaseConnection(jdbcConnection);
62  		DatabaseConfig config = connection.getConfig();
63  	    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
64  	            new HsqldbDataTypeFactory());
65  	}
66  	
67  	  protected void tearDown() throws Exception {
68  		super.tearDown();
69  
70  		HypersonicEnvironment.shutdown(this.jdbcConnection);
71  		this.jdbcConnection.close();
72  	}
73  
74  	public void testMe() throws Exception {
75  		
76  		// 1. QueryDataSet
77  		QueryDataSet queryDataSet = new QueryDataSet(connection);
78  		queryDataSet.addTable("B", "select * from B");
79  		queryDataSet.addTable("C", "select * from C");
80  
81  		// 2. Hard-coded data set
82  		DefaultDataSet plainDataSet = new DefaultDataSet();
83  
84  		Column id   = new Column("id",   DataType.DOUBLE);
85  		Column name = new Column("name", DataType.VARCHAR);
86  
87  		Column[] cols = { id, name };
88  
89  		DefaultTable aTable = new DefaultTable("D", cols);
90  		Object[] row1 = { new Long(1), "D1" };
91  		Object[] row2 = { new Long(2), "D2" };
92  
93  		aTable.addRow(row1);
94  		aTable.addRow(row2);
95  
96  		plainDataSet.addTable(aTable);
97  
98  		// 3. Composite
99  		CompositeDataSet compositeDataSet = new CompositeDataSet(queryDataSet, plainDataSet);
100 
101 		// 4. Write
102 		try {
103 			FlatXmlDataSet.write(compositeDataSet, new FileOutputStream("target/full.xml"));
104 		} catch (Exception e) {
105 			fail(e.getMessage());
106 		}
107 	}
108 }