001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.dbunit;
017:
018: import org.junit.After;
019: import static org.junit.Assert.*;
020: import org.junit.Before;
021: import org.junit.Test;
022: import org.unitils.UnitilsJUnit4;
023: import org.unitils.core.ConfigurationLoader;
024: import org.unitils.core.UnitilsException;
025: import static org.unitils.database.SQLUnitils.*;
026: import org.unitils.database.annotations.TestDataSource;
027: import org.unitils.dbunit.annotation.DataSet;
028: import org.unitils.dbunit.datasetfactory.impl.MultiSchemaXmlDataSetFactory;
029: import org.unitils.dbunit.datasetloadstrategy.impl.CleanInsertLoadStrategy;
030: import static org.unitils.reflectionassert.ReflectionAssert.assertLenEquals;
031:
032: import javax.sql.DataSource;
033: import java.io.File;
034: import java.util.Properties;
035: import java.util.Set;
036:
037: /**
038: * Test class for loading of data sets using the {@link DbUnitModule}.
039: *
040: * @author Filip Neven
041: * @author Tim Ducheyne
042: */
043: public class DbUnitModuleDataSetTest extends UnitilsJUnit4 {
044:
045: /* Tested object */
046: private DbUnitModule dbUnitModule;
047:
048: /* The dataSource */
049: @TestDataSource
050: private DataSource dataSource = null;
051:
052: /**
053: * Initializes the test fixture.
054: */
055: @Before
056: public void setUp() throws Exception {
057: Properties configuration = new ConfigurationLoader()
058: .loadConfiguration();
059: dbUnitModule = new DbUnitModule();
060: dbUnitModule.init(configuration);
061:
062: dropTestTable();
063: createTestTables();
064: }
065:
066: /**
067: * Clean-up test database.
068: */
069: @After
070: public void tearDown() throws Exception {
071: dropTestTable();
072: }
073:
074: /**
075: * Test for custom annotation on method-level overriding default annotation on class-level
076: */
077: @Test
078: public void testInsertTestData_customMethodDataSet()
079: throws Exception {
080: dbUnitModule.insertDataSet(DataSetTest.class
081: .getMethod("testMethod2"), new DataSetTest());
082: assertLoadedDataSet("CustomDataSet.xml");
083: }
084:
085: /**
086: * Test for default file that is not found
087: */
088: @Test
089: public void testInsertTestData_notFound() throws Exception {
090: try {
091: dbUnitModule.insertDataSet(DataSetTest.class
092: .getMethod("testNotFound1"), new DataSetTest());
093: fail("Expected UnitilsException");
094: } catch (UnitilsException e) {
095: //expected
096: }
097: }
098:
099: /**
100: * Test for custom file that is not found
101: */
102: @Test
103: public void testInsertTestData_customNotFound() throws Exception {
104: try {
105: dbUnitModule.insertDataSet(DataSetTest.class
106: .getMethod("testNotFound2"), new DataSetTest());
107: fail("Expected UnitilsException");
108: } catch (UnitilsException e) {
109: //expected
110: }
111: }
112:
113: /**
114: * Test for custom annotation on method-level and no annotation on class-level
115: */
116: @Test
117: public void testInsertTestData_noClassDataSetCustomMethodDataSet()
118: throws Exception {
119: dbUnitModule.insertDataSet(DataSetTestNoClassLevel.class
120: .getMethod("testMethod2"),
121: new DataSetTestNoClassLevel());
122: assertLoadedDataSet("CustomDataSet.xml");
123: }
124:
125: /**
126: * Test for no annotation on method-level and no annotation on class-level.
127: * No data set should have been loaded.
128: */
129: @Test
130: public void testInsertTestData_noClassAndMethodDataSet()
131: throws Exception {
132: dbUnitModule.insertDataSet(DataSetTestNoClassLevel.class
133: .getMethod("testMethod3"),
134: new DataSetTestNoClassLevel());
135: Set<String> datasets = getItemsAsStringSet(
136: "select dataset from test", dataSource);
137: assertTrue(datasets.isEmpty()); // nothing loaded
138: }
139:
140: /**
141: * Test for no annotation on method-level and custom annotation on class-level
142: */
143: @Test
144: public void testInsertTestData_customClassDataSet()
145: throws Exception {
146: dbUnitModule.insertDataSet(DataSetTestCustomClassLevel.class
147: .getMethod("testMethod1"),
148: new DataSetTestCustomClassLevel());
149: assertLoadedDataSet("CustomDataSet.xml");
150: }
151:
152: /**
153: * Test for a direct call to insertDataSet.
154: */
155: @Test
156: public void testInsertTestData_directCall() throws Exception {
157: File dataSetFile = new File(this .getClass().getResource(
158: "CustomDataSet.xml").getPath());
159: dbUnitModule.insertDataSet(dataSetFile,
160: MultiSchemaXmlDataSetFactory.class,
161: CleanInsertLoadStrategy.class);
162: assertLoadedDataSet("CustomDataSet.xml");
163: }
164:
165: /**
166: * Test loading a data set containing 2 elements both specifying different columns.
167: * This is a test for a bug in DbUnit that forces you to repeat all column names over and over again, even if
168: * they should just be left null.
169: */
170: @Test
171: public void testInsertTestData_elementsWithDifferentColumns()
172: throws Exception {
173: File dataSetFile = new File(this .getClass().getResource(
174: "DifferentColumnsDataSet.xml").getPath());
175: dbUnitModule.insertDataSet(dataSetFile,
176: MultiSchemaXmlDataSetFactory.class,
177: CleanInsertLoadStrategy.class);
178: assertLenEquals(3, getItemAsLong("select count(1) from test",
179: dataSource));
180: }
181:
182: // Tests for behavior when data is loaded for a superclass method, but the test instance is of a
183: // subclass type
184:
185: /**
186: * Test for a superclass method when the actual test instance is of a subtype, when the dataset
187: * annotation is on the superclass method
188: */
189: @Test
190: public void testInsertTestData_methodOnSuperClassButInstanceOfSubClass_dataSetAnnotationOnMethod()
191: throws Exception {
192: dbUnitModule.insertDataSet(DataSetTestSuperclass.class
193: .getMethod("annotatedTestMethod"),
194: new DataSetTestSubClass_dataSetAnnotationOnSubClass());
195: assertLoadedDataSet("DbUnitModuleDataSetTest$DataSetTestSubClass_dataSetAnnotationOnSubClass.xml");
196: }
197:
198: /**
199: * Test for a superclass method when the actual test instance is of a subtype, when the dataset
200: * annotation is on the superclass method
201: */
202: @Test
203: public void testInsertTestData_methodOnSuperClassButInstanceOfSubClass_dataSetAnnotationOnSuperclass()
204: throws Exception {
205: dbUnitModule
206: .insertDataSet(
207: DataSetTestSuperclass_classLevelAnnotation.class
208: .getMethod("testMethod"),
209: new DataSetTestSubClass_dataSetAnnotationOnSuperClass());
210: assertLoadedDataSet("DbUnitModuleDataSetTest$DataSetTestSubClass_dataSetAnnotationOnSuperClass.xml");
211: }
212:
213: /**
214: * Test for a superclass method when the actual test instance is of a subtype, when the dataset
215: * annotation is on the superclass method
216: */
217: @Test
218: public void testInsertTestData_methodOnSuperClassButInstanceOfSubClass_dataSetAnnotationOnSubclass()
219: throws Exception {
220: dbUnitModule.insertDataSet(DataSetTestSuperclass.class
221: .getMethod("testMethod"),
222: new DataSetTestSubClass_dataSetAnnotationOnSubClass());
223: assertLoadedDataSet("DbUnitModuleDataSetTest$DataSetTestSubClass_dataSetAnnotationOnSubClass.xml");
224: }
225:
226: /**
227: * Test for loading a dataset consisting of multiple dataset files
228: * <p/>
229: * todo: make possible to define records in the same table in the two datasets
230: */
231: @Test
232: public void testInsertTestData_multipleDataSets() throws Exception {
233: dbUnitModule.insertDataSet(DataSetTest.class
234: .getMethod("testMethodMultipleDataSets"),
235: new DataSetTest());
236: assertLoadedDataSet("dataSet1.xml");
237: assertLenEquals("dataSet2.xml", getItemAsString(
238: "select dataset from test1", dataSource));
239: }
240:
241: /**
242: * Utility method to assert that the correct data set was loaded.
243: *
244: * @param expectedDataSetName the name of the data set, not null
245: */
246: private void assertLoadedDataSet(String expectedDataSetName)
247: throws Exception {
248: String dataSet = getItemAsString("select dataset from test",
249: dataSource);
250: assertEquals(expectedDataSetName, dataSet);
251: }
252:
253: /**
254: * Utility method to create the test table.
255: */
256: private void createTestTables() {
257: executeUpdate(
258: "create table test (dataset varchar(100), anotherColumn varchar(100))",
259: dataSource);
260: executeUpdate("create table test1 (dataset varchar(100))",
261: dataSource);
262: }
263:
264: /**
265: * Removes the test database table
266: */
267: private void dropTestTable() {
268: executeUpdateQuietly("drop table test", dataSource);
269: executeUpdateQuietly("drop table test1", dataSource);
270: }
271:
272: /**
273: * Test class with a class level dataset
274: */
275: @DataSet
276: public class DataSetTest {
277:
278: public void testMethod1() {
279: }
280:
281: @DataSet("CustomDataSet.xml")
282: public void testMethod2() {
283: }
284:
285: @DataSet
286: public void testMethod3() {
287: }
288:
289: public void testNotFound1() {
290: }
291:
292: @DataSet("xxxxxx.xml")
293: public void testNotFound2() {
294: }
295:
296: @DataSet({"dataSet1.xml","dataSet2.xml"})
297: public void testMethodMultipleDataSets() {
298: }
299: }
300:
301: /**
302: * Test class without a class level dataset
303: */
304: public class DataSetTestNoClassLevel {
305:
306: @DataSet
307: public void testMethod1() {
308: }
309:
310: @DataSet("CustomDataSet.xml")
311: public void testMethod2() {
312: }
313:
314: public void testMethod3() {
315: }
316: }
317:
318: @DataSet("CustomDataSet.xml")
319: public class DataSetTestCustomClassLevel {
320:
321: public void testMethod1() {
322: }
323:
324: @DataSet
325: public void testMethod2() {
326: }
327: }
328:
329: public class DataSetTestSuperclass {
330:
331: public void testMethod() {
332: }
333:
334: @DataSet
335: public void annotatedTestMethod() {
336: }
337: }
338:
339: @DataSet
340: public class DataSetTestSuperclass_classLevelAnnotation {
341:
342: public void testMethod() {
343: }
344: }
345:
346: @DataSet
347: public class DataSetTestSubClass_dataSetAnnotationOnSubClass extends
348: DataSetTestSuperclass {
349: }
350:
351: public class DataSetTestSubClass_dataSetAnnotationOnSuperClass
352: extends DataSetTestSuperclass_classLevelAnnotation {
353: }
354:
355: }
|