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.csv;
23  
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  import junit.framework.TestCase;
29  
30  import org.dbunit.Assertion;
31  import org.dbunit.dataset.DataSetException;
32  import org.dbunit.dataset.DataSetUtils;
33  import org.dbunit.dataset.IDataSet;
34  import org.dbunit.dataset.ITable;
35  import org.dbunit.testutil.TestUtils;
36  import org.dbunit.util.FileHelper;
37  
38  /**
39   * @author Lenny Marks (lenny@aps.org)
40   * @version $Revision$
41   * @since Sep 12, 2004 (pre 2.3)
42   */
43  public class CsvDataSetTest extends TestCase {
44  	protected static final File DATASET_DIR = TestUtils.getFile("csv/orders");
45      
46      public CsvDataSetTest(String s) {
47          super(s);
48      }
49  
50      public void testNullColumns() throws DataSetException {
51  		File csvDir = DATASET_DIR;
52  		
53      	CsvDataSet dataSet = new CsvDataSet(csvDir);
54      	
55      	ITable table = dataSet.getTable("orders");
56      	
57      	assertNull(table.getValue(4, "description"));
58      	
59      }
60  
61  	public void testWrite() throws Exception {
62  		
63  		IDataSet expectedDataSet = new CsvDataSet(DATASET_DIR);
64  			
65  		File tempDir = createTmpDir();
66  		try {
67  			//modified this test from FlatXmlDataSetTest
68  			CsvDataSetWriter.write(expectedDataSet, tempDir);
69  			
70  			File tableOrderingFile = new File(tempDir, CsvDataSet.TABLE_ORDERING_FILE);
71  			assertTrue(tableOrderingFile.exists());
72  			
73  			IDataSet actualDataSet = new CsvDataSet(tempDir);
74  			
75  			//verify table count
76  			assertEquals("table count", expectedDataSet.getTableNames().length,
77  			actualDataSet.getTableNames().length);
78  			
79  			//verify each table
80  			ITable[] expected = DataSetUtils.getTables(expectedDataSet);
81  			ITable[] actual = DataSetUtils.getTables(actualDataSet);
82  			assertEquals("table count", expected.length, actual.length);
83  			for (int i = 0; i < expected.length; i++) {
84  				String expectedName = expected[i].getTableMetaData().getTableName();
85  				String actualName = actual[i].getTableMetaData().getTableName();
86  				assertEquals("table name", expectedName, actualName);
87  				
88  				assertTrue("not same instance", expected[i] != actual[i]);
89  				Assertion.assertEquals(expected[i], actual[i]);
90  			}
91  			
92  		} finally {
93  			FileHelper.deleteDirectory(tempDir, true);
94  			
95  		}
96  		
97  		//assertFalse("temporary directory was not deleted", tempDir.exists());
98  	}
99  	
100 	private File createTmpDir() throws IOException {
101 		File tmpFile = File.createTempFile("CsvDataSetTest", "-csv");
102 		String fullPath = tmpFile.getAbsolutePath();
103 		tmpFile.delete();
104 		
105 		File tmpDir = new File(fullPath);
106 		if(!tmpDir.mkdir()) {
107 			throw new IOException("Failed to create tmpDir: " + fullPath);
108 		}
109 		
110 		return tmpDir;
111 	}
112 	
113 }
114 
115 
116 
117