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 junit.framework.TestCase;
25  import org.dbunit.DdlExecutor;
26  import org.dbunit.HypersonicEnvironment;
27  import org.dbunit.database.DatabaseConfig;
28  import org.dbunit.database.DatabaseConnection;
29  import org.dbunit.database.IDatabaseConnection;
30  import org.dbunit.database.QueryDataSet;
31  import org.dbunit.dataset.datatype.DataType;
32  import org.dbunit.dataset.xml.FlatXmlDataSet;
33  import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
34  import org.dbunit.testutil.TestUtils;
35  
36  import java.io.FileOutputStream;
37  import java.sql.Connection;
38  
39  
40  /**
41   * Test Case for issue #1721870
42   * @author Sebastien Le Callonnec
43   * @version $Revision$
44   * @since Mar 11, 2008
45   */
46  public class CompositeDataSetIterationTest extends TestCase {
47  
48  	private Connection jdbcConnection;
49  	private final String sqlFile = "hypersonic_simple_dataset.sql"; 
50  	private IDatabaseConnection connection;
51  
52  	protected void setUp() throws Exception {
53  		super.setUp();
54  		this.jdbcConnection = HypersonicEnvironment.createJdbcConnection("mem:tempdb");
55  		DdlExecutor.executeDdlFile(TestUtils.getFile("sql/" + sqlFile), jdbcConnection);
56  		this.connection = new DatabaseConnection(jdbcConnection);
57  		DatabaseConfig config = connection.getConfig();
58  	    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
59  	            new HsqldbDataTypeFactory());
60  	}
61  	
62  	  protected void tearDown() throws Exception {
63  		super.tearDown();
64  
65  		HypersonicEnvironment.shutdown(this.jdbcConnection);
66  		this.jdbcConnection.close();
67  	}
68  
69  	public void testMe() throws Exception {
70  		
71  		// 1. QueryDataSet
72  		QueryDataSet queryDataSet = new QueryDataSet(connection);
73  		queryDataSet.addTable("B", "select * from B");
74  		queryDataSet.addTable("C", "select * from C");
75  
76  		// 2. Hard-coded data set
77  		DefaultDataSet plainDataSet = new DefaultDataSet();
78  
79  		Column id   = new Column("id",   DataType.DOUBLE);
80  		Column name = new Column("name", DataType.VARCHAR);
81  
82  		Column[] cols = { id, name };
83  
84  		DefaultTable aTable = new DefaultTable("D", cols);
85  		Object[] row1 = { new Long(1), "D1" };
86  		Object[] row2 = { new Long(2), "D2" };
87  
88  		aTable.addRow(row1);
89  		aTable.addRow(row2);
90  
91  		plainDataSet.addTable(aTable);
92  
93  		// 3. Composite
94  		CompositeDataSet compositeDataSet = new CompositeDataSet(queryDataSet, plainDataSet);
95  
96  		// 4. Write
97  		try {
98  			FlatXmlDataSet.write(compositeDataSet, new FileOutputStream("target/full.xml"));
99  		} catch (Exception e) {
100 			fail(e.getMessage());
101 		}
102 	}
103 }