KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > sqldataloader > mru > test > SqlBeanMapDataLoaderTest


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.mru.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 import java.util.HashMap JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.sape.carbon.core.component.Lookup;
37 import org.sape.carbon.core.component.lifecycle.StateTransitionException;
38 import org.sape.carbon.core.config.InvalidConfigurationException;
39 import org.sape.carbon.core.config.Config;
40 import org.sape.carbon.services.cache.Cache;
41 import org.sape.carbon.services.sql.StatementFactory;
42 import org.sape.carbon.services.sql.StatementFactoryException;
43 import org.sape.carbon.services.sqldataloader.test.TestComparator;
44 import org.sape.carbon.services.sqldataloader.test.TestThreeColumnBean;
45 import org.sape.carbon.services.sqldataloader.test.TestTwoColumnBean;
46
47 /**
48  * Template for junit test harness.
49  * This class is responsible for testing SqlBeanDataLoader
50  * @since carbon 2.1
51  * @author Akash Tayal, May 2003
52  * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2003/09/30 02:08:19 $)
53  * <br>Copyright 2003 Sapient
54  */

55 public class SqlBeanMapDataLoaderTest extends TestCase {
56     public SqlBeanMapDataLoaderTest(String JavaDoc testName) {
57         super(testName);
58     }
59
60     private static final String JavaDoc STATEMENT_FACTORY =
61         "/sql/sqldataloader/test/SqlMapBeanDataLoaderStatementFactory";
62
63
64     private static final String JavaDoc DROP_TABLE_ONE_LOOKUP =
65         "DropTableBeanData";
66     private static final String JavaDoc CREATE_TABLE_ONE_LOOKUP =
67         "CreateTableBeanData";
68     private static final String JavaDoc INSERT_TABLE_ONE_LOOKUP_DATA =
69         "InsertBeanData";
70
71     private static List JavaDoc data = new ArrayList JavaDoc();
72
73     private static final int COUNT = 50;
74     private static final int GROUP = 5;
75     // Create a list of lists where each child list contains the data for one
76
// bean.
77

78     static {
79         for (int i = 0; i < COUNT; i++) {
80             ArrayList JavaDoc list = new ArrayList JavaDoc();
81
82             BigDecimal JavaDoc key = new BigDecimal JavaDoc(i);
83             BigDecimal JavaDoc group = new BigDecimal JavaDoc(i % GROUP);
84             String JavaDoc col2 = new String JavaDoc("Test data for key: " + key + " group: " + group);
85
86             list.add(key);
87             list.add(group);
88             list.add(col2);
89
90             data.add(list);
91         }
92     }
93
94
95
96     /**
97      * Method cleanUpBeforeStart. Cleans up database table before the test
98      * specific data is loaded.
99      */

100     public void cleanUpBeforeStart() {
101         PreparedStatement JavaDoc preparedStatement=null;
102         try {
103             StatementFactory sf = (StatementFactory)
104             Lookup.getInstance().fetchComponent(STATEMENT_FACTORY);
105
106             try {
107                 preparedStatement =
108                     sf.createPreparedStatement(DROP_TABLE_ONE_LOOKUP);
109                 preparedStatement.executeUpdate();
110             } catch (SQLException JavaDoc se) {
111                 //eat the exception
112
}
113         } catch (StatementFactoryException sfe) {
114             fail("Clean up before start operation failed due to "+sfe);
115         }
116     }
117
118
119     /**
120      * Method createTestData. Creates test table and populates the data.
121      */

122     public void createTestData() throws SQLException JavaDoc, StatementFactoryException {
123         PreparedStatement JavaDoc preparedStatement=null;
124         int result;
125         try {
126             StatementFactory sf = (StatementFactory)
127                 Lookup.getInstance().fetchComponent(STATEMENT_FACTORY);
128
129             preparedStatement =
130                 sf.createPreparedStatement(CREATE_TABLE_ONE_LOOKUP);
131             preparedStatement.executeUpdate();
132
133
134             preparedStatement =
135                 sf.createPreparedStatement(INSERT_TABLE_ONE_LOOKUP_DATA);
136
137
138             for (int i = 0; i < data.size(); i++) {
139                 List JavaDoc beanItem = (List JavaDoc) data.get(i);
140                 preparedStatement.setBigDecimal(1, (BigDecimal JavaDoc) beanItem.get(0));
141                 preparedStatement.setBigDecimal(2, (BigDecimal JavaDoc) beanItem.get(1));
142                 preparedStatement.setString(3, (String JavaDoc) beanItem.get(2));
143                 result = preparedStatement.executeUpdate();
144                 if (result != 1) {
145                     fail("Unable to insert test data");
146                 }
147             }
148
149             preparedStatement.close();
150         } finally {
151             if (preparedStatement != null) {
152                 try {
153                     preparedStatement.getConnection().close();
154                 } catch (SQLException JavaDoc se) { }
155             }
156         }
157     }
158
159
160     private static final String JavaDoc DATALOADER_THREE_COLUMN =
161         "/sql/sqldataloader/mru/TestSqlMapDataloader_ThreeColumn";
162
163     /**
164      * Test default implementation used for Map
165      */

166     public void testThreeColumnWithMap() {
167         Cache cache =
168             (Cache) Lookup.getInstance().fetchComponent(
169                 DATALOADER_THREE_COLUMN);
170
171         for (int i = 0; i < GROUP; i++) {
172
173             HashMap JavaDoc result = (HashMap JavaDoc) cache.get(new BigDecimal JavaDoc(i));
174
175             //System.out.println("Values for: " + i);
176
for (Iterator JavaDoc mapIter = result.entrySet().iterator(); mapIter.hasNext();) {
177                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) mapIter.next();
178                 Object JavaDoc o1 = (Object JavaDoc) entry.getKey();
179                 Object JavaDoc o2 = (Object JavaDoc) entry.getValue();
180                 //System.out.println("\t" + o1 + " = " + o2);
181
}
182
183             // TODO GH: test for correct values
184
}
185
186     }
187
188     // Compares the maps not only for similar key and values but also
189
// for ordering.
190
private boolean compareSequencedMap(Map JavaDoc expectedMap, Map JavaDoc actualMap) {
191         boolean returnValue = false;
192         if (expectedMap.keySet().equals(actualMap.keySet())) {
193             Set JavaDoc entrySetExpectedMap = expectedMap.entrySet();
194             Set JavaDoc entrySetActualMap = actualMap.entrySet();
195             Iterator JavaDoc iteratorExpectedMap = entrySetExpectedMap.iterator();
196             Iterator JavaDoc iteratorActualMap = entrySetActualMap.iterator();
197
198             while ((iteratorExpectedMap.hasNext()) &&
199                 (iteratorActualMap.hasNext())) {
200                 Map.Entry JavaDoc entryExpectedMap =
201                     (Map.Entry JavaDoc) iteratorExpectedMap.next();
202                 Map.Entry JavaDoc entryActualMap =
203                     (Map.Entry JavaDoc) iteratorActualMap.next();
204                 if ((entryExpectedMap.getKey().equals(entryActualMap.getKey()))
205                     && (entryExpectedMap.getValue().equals(
206                         entryActualMap.getValue()))) {
207                     returnValue = true;
208                 } else {
209                     return false;
210                 }
211             }
212         }
213         return returnValue;
214     }
215
216
217
218     /**
219      * Method called by jUnit to get all the tests in this test case.
220      * @return Test the suite of tests in this test case
221      */

222     public static Test suite() {
223         TestSuite masterSuite = new TestSuite("SqlBeanMapDataloaderTest");
224
225         // These two methods are added as part of initial setup before rest
226
// of the cases are executed.
227
// These methods are responsible for cleaning and setting up the
228
// table and data for testing SqlBeanDataLoader
229
masterSuite.addTest(new SqlBeanMapDataLoaderTest("cleanUpBeforeStart"));
230         masterSuite.addTest(new SqlBeanMapDataLoaderTest("createTestData"));
231
232         // add single threaded tests
233
Test singleThreadedTests = getSingleThreadedTests();
234         if (singleThreadedTests != null) {
235             masterSuite.addTest(singleThreadedTests);
236         }
237         // add multi threaded tests
238
Test multiThreadedTests = getMultiThreadedTests();
239         if (multiThreadedTests != null) {
240             masterSuite.addTest(multiThreadedTests);
241         }
242         return masterSuite;
243     }
244
245
246     /**
247      * This method is used within the suite method to get all of the single
248      * threaded tests. Add all your single threaded tests in this method with
249      * a line like: suite.addTest(new CacheServiceTest("testFunction1"));
250      * @return Test the suite of single threaded tests in this test case
251      */

252     private static Test getSingleThreadedTests() {
253         TestSuite mySuite = new TestSuite("SingleThreadedTests");
254         mySuite.addTest(new SqlBeanMapDataLoaderTest("testThreeColumnWithMap"));
255         return mySuite;
256     }
257
258     /**
259      * This method is used within the suite method to get all of the multi
260      * threaded tests. Add all your multi threaded tests in this method with
261      * a line like: addTest(suite, "testFunction1", 5);
262      * Currently there are no multi-threaded tests for SqlBeanDataLoaderTest
263      * @return Test the suite of multi-threaded tests in this test case
264      */

265     private static Test getMultiThreadedTests() {
266         TestSuite suite = new ActiveTestSuite();
267         final int THREAD_COUNT = 10;
268
269         return null;
270     }
271
272     /**
273      * This method will add the give test to the give suite the specified
274      * number of times. This is best used for multi-threaded tests where
275      * suite is an instance of ActiveTestSuite and you want to run the same
276      * test in multiple threads.
277      * @param suite the suite to add the test to.
278      * @param testName the name of the test to add.
279      * @param number the number of times to add the test to the suite
280      */

281     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
282         for (int count = 0; count < number; count++) {
283             suite.addTest(new SqlBeanMapDataLoaderTest(testName));
284         }
285     }
286
287 }
288
Popular Tags