KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > sqldataloader > total > test > TotalSqlBeanDataLoaderTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.sqldataloader.total.test;
19
20 import junit.extensions.ActiveTestSuite;
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import java.math.BigDecimal JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.TreeMap JavaDoc;
32
33 import org.sape.carbon.core.component.Lookup;
34 import org.sape.carbon.core.component.lifecycle.StateTransitionException;
35 import org.sape.carbon.core.config.InvalidConfigurationException;
36 import org.sape.carbon.services.cache.Cache;
37 import org.sape.carbon.services.sql.StatementFactory;
38 import org.sape.carbon.services.sql.StatementFactoryException;
39 import org.sape.carbon.services.sqldataloader.test.TestTwoColumnBean;
40 import org.sape.carbon.services.sqldataloader.test.TestThreeColumnBean;
41 import org.sape.carbon.services.sqldataloader.test.TestComparator;
42
43 /**
44  * Template for junit test harness.
45  * This class is responsible for testing SqlBeanDataLoader
46  * @since carbon 2.1
47  * @author Akash Tayal, May 2003
48  * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2003/09/30 02:08:19 $)
49  * <br>Copyright 2003 Sapient
50  */

51 public class TotalSqlBeanDataLoaderTest extends TestCase {
52     public TotalSqlBeanDataLoaderTest(String JavaDoc testName) {
53         super(testName);
54     }
55
56     private static final String JavaDoc STATEMENT_FACTORY =
57         "/sql/sqldataloader/test/SqlBeanDataLoaderStatementFactory";
58
59
60     private static final String JavaDoc DROP_TABLE_ONE_LOOKUP =
61         "DropTableBeanData";
62     private static final String JavaDoc CREATE_TABLE_ONE_LOOKUP =
63         "CreateTableBeanData";
64     private static final String JavaDoc INSERT_TABLE_ONE_LOOKUP_DATA =
65         "InsertBeanData";
66
67     // These values are used to store the test data for running this test
68
private static final Object JavaDoc KEY1 = new BigDecimal JavaDoc("1");
69     private static final Object JavaDoc KEY2 = new BigDecimal JavaDoc("2");
70     private static final Object JavaDoc KEY3 = new BigDecimal JavaDoc("3");
71     private static final String JavaDoc KEY1_COLUMN1 = "This is cached row 1 column 1";
72     private static final String JavaDoc KEY1_COLUMN2 = "This is cached row 1 column 2";
73     private static final String JavaDoc KEY2_COLUMN1 = "This is cached row 2 column 1";
74     private static final String JavaDoc KEY2_COLUMN2 = "This is cached row 2 column 2";
75     private static final String JavaDoc KEY3_COLUMN1 = "This is cached row 3 column 1";
76     private static final String JavaDoc KEY3_COLUMN2 = "This is cached row 3 column 2";
77
78     // Populating expected two column map for validating the map retreived
79
// from Cache.
80
private static Map JavaDoc expectedTwoColumnMap() {
81         TreeMap JavaDoc expectedTwoColumnResultMap = new TreeMap JavaDoc();
82         TestTwoColumnBean twoColumnBean = new TestTwoColumnBean();
83         twoColumnBean.setKey((BigDecimal JavaDoc)KEY1);
84         twoColumnBean.setValue(KEY1_COLUMN1);
85         expectedTwoColumnResultMap.put(KEY1,twoColumnBean);
86         twoColumnBean = new TestTwoColumnBean();
87         twoColumnBean.setKey((BigDecimal JavaDoc)KEY2);
88         twoColumnBean.setValue(KEY2_COLUMN1);
89         expectedTwoColumnResultMap.put(KEY2,twoColumnBean);
90         twoColumnBean = new TestTwoColumnBean();
91         twoColumnBean.setKey((BigDecimal JavaDoc)KEY3);
92         twoColumnBean.setValue(KEY3_COLUMN1);
93         expectedTwoColumnResultMap.put(KEY3,twoColumnBean);
94         return expectedTwoColumnResultMap;
95     }
96
97     // Populating expected inverted two column map for validating the map
98
// retreived from Cache.
99
private static Map JavaDoc expectedInvertedTwoColumnMap() {
100         TestComparator comparator = new TestComparator();
101         TreeMap JavaDoc expectedTwoColumnResultMap = new TreeMap JavaDoc(comparator);
102         TestTwoColumnBean twoColumnBean = new TestTwoColumnBean();
103         twoColumnBean.setKey((BigDecimal JavaDoc)KEY3);
104         twoColumnBean.setValue(KEY3_COLUMN1);
105         expectedTwoColumnResultMap.put(KEY3,twoColumnBean);
106         twoColumnBean = new TestTwoColumnBean();
107         twoColumnBean.setKey((BigDecimal JavaDoc)KEY2);
108         twoColumnBean.setValue(KEY2_COLUMN1);
109         expectedTwoColumnResultMap.put(KEY2,twoColumnBean);
110         twoColumnBean = new TestTwoColumnBean();
111         twoColumnBean.setKey((BigDecimal JavaDoc)KEY1);
112         twoColumnBean.setValue(KEY1_COLUMN1);
113         expectedTwoColumnResultMap.put(KEY1,twoColumnBean);
114         return expectedTwoColumnResultMap;
115     }
116
117     // Populating expected three column map for validating the map retreived
118
// from Cache.
119
private static Map JavaDoc expectedThreeColumnMap() {
120         TreeMap JavaDoc expectedThreeColumnResultMap = new TreeMap JavaDoc();
121         TestThreeColumnBean threeColumnBean = new TestThreeColumnBean();
122         threeColumnBean.setKey((BigDecimal JavaDoc)KEY1);
123         threeColumnBean.setColumn1Value(KEY1_COLUMN1);
124         threeColumnBean.setColumn2Value(KEY1_COLUMN2);
125         expectedThreeColumnResultMap.put(KEY1,threeColumnBean);
126         threeColumnBean = new TestThreeColumnBean();
127         threeColumnBean.setKey((BigDecimal JavaDoc)KEY2);
128         threeColumnBean.setColumn1Value(KEY2_COLUMN1);
129         threeColumnBean.setColumn2Value(KEY2_COLUMN2);
130         expectedThreeColumnResultMap.put(KEY2,threeColumnBean);
131         threeColumnBean = new TestThreeColumnBean();
132         threeColumnBean.setKey((BigDecimal JavaDoc)KEY3);
133         threeColumnBean.setColumn1Value(KEY3_COLUMN1);
134         threeColumnBean.setColumn2Value(KEY3_COLUMN2);
135         expectedThreeColumnResultMap.put(KEY3,threeColumnBean);
136         return expectedThreeColumnResultMap;
137     }
138
139
140     /**
141      * Method cleanUpBeforeStart. Cleans up database table before the test
142      * specific data is loaded.
143      */

144     public void cleanUpBeforeStart() {
145         PreparedStatement JavaDoc preparedStatement=null;
146         try {
147             StatementFactory sf = (StatementFactory)
148             Lookup.getInstance().fetchComponent(STATEMENT_FACTORY);
149
150             try {
151                 preparedStatement =
152                     sf.createPreparedStatement(DROP_TABLE_ONE_LOOKUP);
153                 preparedStatement.executeUpdate();
154             } catch (SQLException JavaDoc se) {
155                 //eat the exception
156
}
157         } catch (StatementFactoryException sfe) {
158             fail("Clean up before start operation failed due to "+sfe);
159         }
160     }
161
162
163     /**
164      * Method createTestData. Creates test table and populates the data.
165      */

166     public void createTestData() {
167         PreparedStatement JavaDoc preparedStatement=null;
168         int result;
169         try {
170             StatementFactory sf = (StatementFactory)
171                 Lookup.getInstance().fetchComponent(STATEMENT_FACTORY);
172
173             preparedStatement =
174                 sf.createPreparedStatement(CREATE_TABLE_ONE_LOOKUP);
175             preparedStatement.executeUpdate();
176
177             preparedStatement =
178                 sf.createPreparedStatement(INSERT_TABLE_ONE_LOOKUP_DATA);
179             preparedStatement.setObject(1,KEY1);
180             preparedStatement.setString(2,KEY1_COLUMN1);
181             preparedStatement.setString(3,KEY1_COLUMN2);
182             result = preparedStatement.executeUpdate();
183             if (result!=1) {
184                 fail("Insert operation Failed for row 1");
185             }
186
187             preparedStatement =
188                 sf.createPreparedStatement(INSERT_TABLE_ONE_LOOKUP_DATA);
189             preparedStatement.setObject(1,KEY2);
190             preparedStatement.setString(2,KEY2_COLUMN1);
191             preparedStatement.setString(3,KEY2_COLUMN2);
192             result = preparedStatement.executeUpdate();
193             if (result!=1) {
194                 fail("Insert operation Failed for row 2");
195             }
196
197             preparedStatement =
198                 sf.createPreparedStatement(INSERT_TABLE_ONE_LOOKUP_DATA);
199             preparedStatement.setObject(1,KEY3);
200             preparedStatement.setString(2,KEY3_COLUMN1);
201             preparedStatement.setString(3,KEY3_COLUMN2);
202             result = preparedStatement.executeUpdate();
203             if (result!=1) {
204                 fail("Insert operation Failed for row 2");
205             }
206             preparedStatement.close();
207         } catch (StatementFactoryException sfe) {
208             fail("Cannot create Test Tables:"+sfe);
209         } catch (SQLException JavaDoc se) {
210             fail("Cannot create Test Tables"+se);
211         }
212     }
213
214     private static final String JavaDoc DATA_LOADER_WITHOUT_MAP_IMPL =
215         "/sql/sqldataloader/total/testSqlBeanDataLoaderWithoutMapImpl";
216
217     /**
218      * Test default implementation used for Map
219      */

220     public void testDefaultImplMap() {
221         Cache actualTwoColumnMap =
222             (Cache) Lookup.getInstance().fetchComponent(
223                 DATA_LOADER_WITHOUT_MAP_IMPL);
224
225         assertEquals("Expected and actual maps are not same",
226             expectedTwoColumnMap(), actualTwoColumnMap);
227     }
228
229     private static final String JavaDoc DATA_LOADER_WITHOUT_COMPARATOR =
230         "/sql/sqldataloader/total/testSqlBeanDataLoaderWithoutComparator";
231
232     /**
233      * Test implementation for Map without comparator
234      */

235     public void testMapImplWithoutComparator() {
236         Cache actualTwoColumnMap =
237             (Cache) Lookup.getInstance().fetchComponent(
238                 DATA_LOADER_WITHOUT_COMPARATOR);
239
240         assertTrue("Expected and actual maps are not same",
241             compareSequencedMap(expectedTwoColumnMap(), actualTwoColumnMap));
242     }
243
244     private static final String JavaDoc DATA_LOADER_WITH_COMPARATOR =
245         "/sql/sqldataloader/total/testSqlBeanDataLoaderWithComparator";
246
247     /**
248      * Test implementation for Map with comparator
249      */

250     public void testMapImplWithComparator() {
251         Cache actualTwoColumnMap =
252             (Cache) Lookup.getInstance().fetchComponent(
253                 DATA_LOADER_WITH_COMPARATOR);
254         assertTrue("Expected and actual maps are not same",
255             compareSequencedMap(
256                 expectedInvertedTwoColumnMap(),
257                 actualTwoColumnMap));
258     }
259
260     // Compares the maps not only for similar key and values but also
261
// for ordering.
262
private boolean compareSequencedMap(Map JavaDoc expectedMap, Map JavaDoc actualMap) {
263         boolean returnValue = false;
264         if (expectedMap.keySet().equals(actualMap.keySet())) {
265             Set JavaDoc entrySetExpectedMap = expectedMap.entrySet();
266             Set JavaDoc entrySetActualMap = actualMap.entrySet();
267             Iterator JavaDoc iteratorExpectedMap = entrySetExpectedMap.iterator();
268             Iterator JavaDoc iteratorActualMap = entrySetActualMap.iterator();
269
270             while ((iteratorExpectedMap.hasNext()) &&
271                 (iteratorActualMap.hasNext())) {
272                 Map.Entry JavaDoc entryExpectedMap =
273                     (Map.Entry JavaDoc) iteratorExpectedMap.next();
274                 Map.Entry JavaDoc entryActualMap =
275                     (Map.Entry JavaDoc) iteratorActualMap.next();
276                 if ((entryExpectedMap.getKey().equals(entryActualMap.getKey()))
277                     && (entryExpectedMap.getValue().equals(
278                         entryActualMap.getValue()))) {
279                     returnValue = true;
280                 } else {
281                     return false;
282                 }
283             }
284         }
285         return returnValue;
286     }
287
288     private static final String JavaDoc DATA_LOADER_THREE_COLUMN =
289         "/sql/sqldataloader/total/testSqlBeanDataLoaderThreeColumn";
290
291     /**
292      * Test for population of 3 column bean class
293      */

294     public void testThreeColumnBeanClass() {
295         Cache actualThreeColumnMap =(Cache) Lookup.getInstance().
296             fetchComponent(DATA_LOADER_THREE_COLUMN);
297         assertTrue("Expected and actual maps are not same",
298             compareSequencedMap(expectedThreeColumnMap(),
299                 actualThreeColumnMap));
300     }
301
302     private static final String JavaDoc INVALID_QUERY_DATA_LOADER =
303         "/sql/sqldataloader/total/testInvalidQuerySqlBeanDataLoader";
304
305     /**
306      * Test invalid configuration key column / data load query
307      */

308     public void testInvalidConfig() {
309         try {
310             Cache localCache =(Cache) Lookup.getInstance().
311                 fetchComponent(INVALID_QUERY_DATA_LOADER);
312             fail("Invalid query executed successfully");
313         } catch (InvalidConfigurationException ice) {
314             // expected
315
} catch (StateTransitionException ste) {
316            // expected
317
}
318     }
319
320     private static final String JavaDoc INVALID_BEAN_ATTRIBUTE_DATA_LOADER =
321         "/sql/sqldataloader/total/testInvalidBeanAttributeSqlBeanDataLoader";
322
323     /**
324      * Test for invalid bean attribute configured
325      */

326     public void testInvalidBeanAttribute() {
327         try {
328             Cache localCache =(Cache) Lookup.getInstance().
329                 fetchComponent(INVALID_BEAN_ATTRIBUTE_DATA_LOADER);
330             fail("Invalid bean attribute populated successfully");
331         } catch (InvalidConfigurationException ice) {
332             // expected
333
} catch (StateTransitionException ste) {
334             // expected
335
}
336     }
337
338
339     /**
340      * Method called by jUnit to get all the tests in this test case.
341      * @return Test the suite of tests in this test case
342      */

343     public static Test suite() {
344         TestSuite masterSuite = new TestSuite();
345
346         // These two methods are added as part of initial setup before rest
347
// of the cases are executed.
348
// These methods are responsible for cleaning and setting up the
349
// table and data for testing SqlBeanDataLoader
350
masterSuite.addTest(new TotalSqlBeanDataLoaderTest("cleanUpBeforeStart"));
351         masterSuite.addTest(new TotalSqlBeanDataLoaderTest("createTestData"));
352         // add single threaded tests
353
Test singleThreadedTests = getSingleThreadedTests();
354         if (singleThreadedTests != null) {
355             masterSuite.addTest(singleThreadedTests);
356         }
357         // add multi threaded tests
358
Test multiThreadedTests = getMultiThreadedTests();
359         if (multiThreadedTests != null) {
360             masterSuite.addTest(multiThreadedTests);
361         }
362         return masterSuite;
363     }
364
365
366     /**
367      * This method is used within the suite method to get all of the single
368      * threaded tests. Add all your single threaded tests in this method with
369      * a line like: suite.addTest(new CacheServiceTest("testFunction1"));
370      * @return Test the suite of single threaded tests in this test case
371      */

372     private static Test getSingleThreadedTests() {
373         TestSuite mySuite = new TestSuite();
374         mySuite.addTest(new TotalSqlBeanDataLoaderTest("testDefaultImplMap"));
375         mySuite.addTest(new TotalSqlBeanDataLoaderTest(
376             "testMapImplWithoutComparator"));
377         mySuite.addTest(new TotalSqlBeanDataLoaderTest(
378             "testMapImplWithComparator"));
379         mySuite.addTest(new TotalSqlBeanDataLoaderTest("testThreeColumnBeanClass"));
380         mySuite.addTest(new TotalSqlBeanDataLoaderTest("testInvalidConfig"));
381         mySuite.addTest(new TotalSqlBeanDataLoaderTest("testInvalidBeanAttribute"));
382         return mySuite;
383     }
384
385     /**
386      * This method is used within the suite method to get all of the multi
387      * threaded tests. Add all your multi threaded tests in this method with
388      * a line like: addTest(suite, "testFunction1", 5);
389      * Currently there are no multi-threaded tests for SqlBeanDataLoaderTest
390      * @return Test the suite of multi-threaded tests in this test case
391      */

392     private static Test getMultiThreadedTests() {
393         TestSuite suite = new ActiveTestSuite();
394         final int THREAD_COUNT = 10;
395
396         return suite;
397     }
398
399     /**
400      * This method will add the give test to the give suite the specified
401      * number of times. This is best used for multi-threaded tests where
402      * suite is an instance of ActiveTestSuite and you want to run the same
403      * test in multiple threads.
404      * @param suite the suite to add the test to.
405      * @param testName the name of the test to add.
406      * @param number the number of times to add the test to the suite
407      */

408     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
409         for (int count = 0; count < number; count++) {
410             suite.addTest(new TotalSqlBeanDataLoaderTest(testName));
411         }
412     }
413
414 }
415
Popular Tags