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.DatabaseConnection;
32  import org.dbunit.database.IDatabaseConnection;
33  import org.dbunit.database.QueryDataSet;
34  import org.dbunit.dataset.Column;
35  import org.dbunit.dataset.CompositeDataSet;
36  import org.dbunit.dataset.DefaultDataSet;
37  import org.dbunit.dataset.DefaultTable;
38  import org.dbunit.dataset.datatype.DataType;
39  import org.dbunit.dataset.xml.FlatXmlDataSet;
40  import org.dbunit.testutil.TestUtils;
41  
42  
43  /**
44   * Test Case for issue #1721870
45   * @author Sebastien Le Callonnec
46   * @version $Revision: 1162 $
47   * @since Mar 11, 2008
48   */
49  public class CompositeDataSetIterationTest extends TestCase {
50  
51  	private Connection jdbcConnection;
52  	private final String sqlFile = "hypersonic_simple_dataset.sql"; 
53  	private IDatabaseConnection connection;
54  
55  	protected void setUp() throws Exception {
56  		super.setUp();
57  		this.jdbcConnection = HypersonicEnvironment.createJdbcConnection("mem:tempdb");
58  		HypersonicEnvironment.executeDdlFile(TestUtils.getFile("sql/" + sqlFile), jdbcConnection);
59  		this.connection = new DatabaseConnection(jdbcConnection);
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 }