001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.sqldataloader.total.test;
019:
020: import junit.extensions.ActiveTestSuite;
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: import java.math.BigDecimal;
026: import java.sql.PreparedStatement;
027: import java.sql.SQLException;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.Set;
031: import java.util.TreeMap;
032:
033: import org.sape.carbon.core.component.Lookup;
034: import org.sape.carbon.core.component.lifecycle.StateTransitionException;
035: import org.sape.carbon.core.config.InvalidConfigurationException;
036: import org.sape.carbon.services.cache.Cache;
037: import org.sape.carbon.services.sql.StatementFactory;
038: import org.sape.carbon.services.sql.StatementFactoryException;
039: import org.sape.carbon.services.sqldataloader.test.TestTwoColumnBean;
040: import org.sape.carbon.services.sqldataloader.test.TestThreeColumnBean;
041: import org.sape.carbon.services.sqldataloader.test.TestComparator;
042:
043: /**
044: * Template for junit test harness.
045: * This class is responsible for testing SqlBeanDataLoader
046: * @since carbon 2.1
047: * @author Akash Tayal, May 2003
048: * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2003/09/30 02:08:19 $)
049: * <br>Copyright 2003 Sapient
050: */
051: public class TotalSqlBeanDataLoaderTest extends TestCase {
052: public TotalSqlBeanDataLoaderTest(String testName) {
053: super (testName);
054: }
055:
056: private static final String STATEMENT_FACTORY = "/sql/sqldataloader/test/SqlBeanDataLoaderStatementFactory";
057:
058: private static final String DROP_TABLE_ONE_LOOKUP = "DropTableBeanData";
059: private static final String CREATE_TABLE_ONE_LOOKUP = "CreateTableBeanData";
060: private static final String INSERT_TABLE_ONE_LOOKUP_DATA = "InsertBeanData";
061:
062: // These values are used to store the test data for running this test
063: private static final Object KEY1 = new BigDecimal("1");
064: private static final Object KEY2 = new BigDecimal("2");
065: private static final Object KEY3 = new BigDecimal("3");
066: private static final String KEY1_COLUMN1 = "This is cached row 1 column 1";
067: private static final String KEY1_COLUMN2 = "This is cached row 1 column 2";
068: private static final String KEY2_COLUMN1 = "This is cached row 2 column 1";
069: private static final String KEY2_COLUMN2 = "This is cached row 2 column 2";
070: private static final String KEY3_COLUMN1 = "This is cached row 3 column 1";
071: private static final String KEY3_COLUMN2 = "This is cached row 3 column 2";
072:
073: // Populating expected two column map for validating the map retreived
074: // from Cache.
075: private static Map expectedTwoColumnMap() {
076: TreeMap expectedTwoColumnResultMap = new TreeMap();
077: TestTwoColumnBean twoColumnBean = new TestTwoColumnBean();
078: twoColumnBean.setKey((BigDecimal) KEY1);
079: twoColumnBean.setValue(KEY1_COLUMN1);
080: expectedTwoColumnResultMap.put(KEY1, twoColumnBean);
081: twoColumnBean = new TestTwoColumnBean();
082: twoColumnBean.setKey((BigDecimal) KEY2);
083: twoColumnBean.setValue(KEY2_COLUMN1);
084: expectedTwoColumnResultMap.put(KEY2, twoColumnBean);
085: twoColumnBean = new TestTwoColumnBean();
086: twoColumnBean.setKey((BigDecimal) KEY3);
087: twoColumnBean.setValue(KEY3_COLUMN1);
088: expectedTwoColumnResultMap.put(KEY3, twoColumnBean);
089: return expectedTwoColumnResultMap;
090: }
091:
092: // Populating expected inverted two column map for validating the map
093: // retreived from Cache.
094: private static Map expectedInvertedTwoColumnMap() {
095: TestComparator comparator = new TestComparator();
096: TreeMap expectedTwoColumnResultMap = new TreeMap(comparator);
097: TestTwoColumnBean twoColumnBean = new TestTwoColumnBean();
098: twoColumnBean.setKey((BigDecimal) KEY3);
099: twoColumnBean.setValue(KEY3_COLUMN1);
100: expectedTwoColumnResultMap.put(KEY3, twoColumnBean);
101: twoColumnBean = new TestTwoColumnBean();
102: twoColumnBean.setKey((BigDecimal) KEY2);
103: twoColumnBean.setValue(KEY2_COLUMN1);
104: expectedTwoColumnResultMap.put(KEY2, twoColumnBean);
105: twoColumnBean = new TestTwoColumnBean();
106: twoColumnBean.setKey((BigDecimal) KEY1);
107: twoColumnBean.setValue(KEY1_COLUMN1);
108: expectedTwoColumnResultMap.put(KEY1, twoColumnBean);
109: return expectedTwoColumnResultMap;
110: }
111:
112: // Populating expected three column map for validating the map retreived
113: // from Cache.
114: private static Map expectedThreeColumnMap() {
115: TreeMap expectedThreeColumnResultMap = new TreeMap();
116: TestThreeColumnBean threeColumnBean = new TestThreeColumnBean();
117: threeColumnBean.setKey((BigDecimal) KEY1);
118: threeColumnBean.setColumn1Value(KEY1_COLUMN1);
119: threeColumnBean.setColumn2Value(KEY1_COLUMN2);
120: expectedThreeColumnResultMap.put(KEY1, threeColumnBean);
121: threeColumnBean = new TestThreeColumnBean();
122: threeColumnBean.setKey((BigDecimal) KEY2);
123: threeColumnBean.setColumn1Value(KEY2_COLUMN1);
124: threeColumnBean.setColumn2Value(KEY2_COLUMN2);
125: expectedThreeColumnResultMap.put(KEY2, threeColumnBean);
126: threeColumnBean = new TestThreeColumnBean();
127: threeColumnBean.setKey((BigDecimal) KEY3);
128: threeColumnBean.setColumn1Value(KEY3_COLUMN1);
129: threeColumnBean.setColumn2Value(KEY3_COLUMN2);
130: expectedThreeColumnResultMap.put(KEY3, threeColumnBean);
131: return expectedThreeColumnResultMap;
132: }
133:
134: /**
135: * Method cleanUpBeforeStart. Cleans up database table before the test
136: * specific data is loaded.
137: */
138: public void cleanUpBeforeStart() {
139: PreparedStatement preparedStatement = null;
140: try {
141: StatementFactory sf = (StatementFactory) Lookup
142: .getInstance().fetchComponent(STATEMENT_FACTORY);
143:
144: try {
145: preparedStatement = sf
146: .createPreparedStatement(DROP_TABLE_ONE_LOOKUP);
147: preparedStatement.executeUpdate();
148: } catch (SQLException se) {
149: //eat the exception
150: }
151: } catch (StatementFactoryException sfe) {
152: fail("Clean up before start operation failed due to " + sfe);
153: }
154: }
155:
156: /**
157: * Method createTestData. Creates test table and populates the data.
158: */
159: public void createTestData() {
160: PreparedStatement preparedStatement = null;
161: int result;
162: try {
163: StatementFactory sf = (StatementFactory) Lookup
164: .getInstance().fetchComponent(STATEMENT_FACTORY);
165:
166: preparedStatement = sf
167: .createPreparedStatement(CREATE_TABLE_ONE_LOOKUP);
168: preparedStatement.executeUpdate();
169:
170: preparedStatement = sf
171: .createPreparedStatement(INSERT_TABLE_ONE_LOOKUP_DATA);
172: preparedStatement.setObject(1, KEY1);
173: preparedStatement.setString(2, KEY1_COLUMN1);
174: preparedStatement.setString(3, KEY1_COLUMN2);
175: result = preparedStatement.executeUpdate();
176: if (result != 1) {
177: fail("Insert operation Failed for row 1");
178: }
179:
180: preparedStatement = sf
181: .createPreparedStatement(INSERT_TABLE_ONE_LOOKUP_DATA);
182: preparedStatement.setObject(1, KEY2);
183: preparedStatement.setString(2, KEY2_COLUMN1);
184: preparedStatement.setString(3, KEY2_COLUMN2);
185: result = preparedStatement.executeUpdate();
186: if (result != 1) {
187: fail("Insert operation Failed for row 2");
188: }
189:
190: preparedStatement = sf
191: .createPreparedStatement(INSERT_TABLE_ONE_LOOKUP_DATA);
192: preparedStatement.setObject(1, KEY3);
193: preparedStatement.setString(2, KEY3_COLUMN1);
194: preparedStatement.setString(3, KEY3_COLUMN2);
195: result = preparedStatement.executeUpdate();
196: if (result != 1) {
197: fail("Insert operation Failed for row 2");
198: }
199: preparedStatement.close();
200: } catch (StatementFactoryException sfe) {
201: fail("Cannot create Test Tables:" + sfe);
202: } catch (SQLException se) {
203: fail("Cannot create Test Tables" + se);
204: }
205: }
206:
207: private static final String DATA_LOADER_WITHOUT_MAP_IMPL = "/sql/sqldataloader/total/testSqlBeanDataLoaderWithoutMapImpl";
208:
209: /**
210: * Test default implementation used for Map
211: */
212: public void testDefaultImplMap() {
213: Cache actualTwoColumnMap = (Cache) Lookup.getInstance()
214: .fetchComponent(DATA_LOADER_WITHOUT_MAP_IMPL);
215:
216: assertEquals("Expected and actual maps are not same",
217: expectedTwoColumnMap(), actualTwoColumnMap);
218: }
219:
220: private static final String DATA_LOADER_WITHOUT_COMPARATOR = "/sql/sqldataloader/total/testSqlBeanDataLoaderWithoutComparator";
221:
222: /**
223: * Test implementation for Map without comparator
224: */
225: public void testMapImplWithoutComparator() {
226: Cache actualTwoColumnMap = (Cache) Lookup.getInstance()
227: .fetchComponent(DATA_LOADER_WITHOUT_COMPARATOR);
228:
229: assertTrue("Expected and actual maps are not same",
230: compareSequencedMap(expectedTwoColumnMap(),
231: actualTwoColumnMap));
232: }
233:
234: private static final String DATA_LOADER_WITH_COMPARATOR = "/sql/sqldataloader/total/testSqlBeanDataLoaderWithComparator";
235:
236: /**
237: * Test implementation for Map with comparator
238: */
239: public void testMapImplWithComparator() {
240: Cache actualTwoColumnMap = (Cache) Lookup.getInstance()
241: .fetchComponent(DATA_LOADER_WITH_COMPARATOR);
242: assertTrue("Expected and actual maps are not same",
243: compareSequencedMap(expectedInvertedTwoColumnMap(),
244: actualTwoColumnMap));
245: }
246:
247: // Compares the maps not only for similar key and values but also
248: // for ordering.
249: private boolean compareSequencedMap(Map expectedMap, Map actualMap) {
250: boolean returnValue = false;
251: if (expectedMap.keySet().equals(actualMap.keySet())) {
252: Set entrySetExpectedMap = expectedMap.entrySet();
253: Set entrySetActualMap = actualMap.entrySet();
254: Iterator iteratorExpectedMap = entrySetExpectedMap
255: .iterator();
256: Iterator iteratorActualMap = entrySetActualMap.iterator();
257:
258: while ((iteratorExpectedMap.hasNext())
259: && (iteratorActualMap.hasNext())) {
260: Map.Entry entryExpectedMap = (Map.Entry) iteratorExpectedMap
261: .next();
262: Map.Entry entryActualMap = (Map.Entry) iteratorActualMap
263: .next();
264: if ((entryExpectedMap.getKey().equals(entryActualMap
265: .getKey()))
266: && (entryExpectedMap.getValue()
267: .equals(entryActualMap.getValue()))) {
268: returnValue = true;
269: } else {
270: return false;
271: }
272: }
273: }
274: return returnValue;
275: }
276:
277: private static final String DATA_LOADER_THREE_COLUMN = "/sql/sqldataloader/total/testSqlBeanDataLoaderThreeColumn";
278:
279: /**
280: * Test for population of 3 column bean class
281: */
282: public void testThreeColumnBeanClass() {
283: Cache actualThreeColumnMap = (Cache) Lookup.getInstance()
284: .fetchComponent(DATA_LOADER_THREE_COLUMN);
285: assertTrue("Expected and actual maps are not same",
286: compareSequencedMap(expectedThreeColumnMap(),
287: actualThreeColumnMap));
288: }
289:
290: private static final String INVALID_QUERY_DATA_LOADER = "/sql/sqldataloader/total/testInvalidQuerySqlBeanDataLoader";
291:
292: /**
293: * Test invalid configuration key column / data load query
294: */
295: public void testInvalidConfig() {
296: try {
297: Cache localCache = (Cache) Lookup.getInstance()
298: .fetchComponent(INVALID_QUERY_DATA_LOADER);
299: fail("Invalid query executed successfully");
300: } catch (InvalidConfigurationException ice) {
301: // expected
302: } catch (StateTransitionException ste) {
303: // expected
304: }
305: }
306:
307: private static final String INVALID_BEAN_ATTRIBUTE_DATA_LOADER = "/sql/sqldataloader/total/testInvalidBeanAttributeSqlBeanDataLoader";
308:
309: /**
310: * Test for invalid bean attribute configured
311: */
312: public void testInvalidBeanAttribute() {
313: try {
314: Cache localCache = (Cache) Lookup.getInstance()
315: .fetchComponent(INVALID_BEAN_ATTRIBUTE_DATA_LOADER);
316: fail("Invalid bean attribute populated successfully");
317: } catch (InvalidConfigurationException ice) {
318: // expected
319: } catch (StateTransitionException ste) {
320: // expected
321: }
322: }
323:
324: /**
325: * Method called by jUnit to get all the tests in this test case.
326: * @return Test the suite of tests in this test case
327: */
328: public static Test suite() {
329: TestSuite masterSuite = new TestSuite();
330:
331: // These two methods are added as part of initial setup before rest
332: // of the cases are executed.
333: // These methods are responsible for cleaning and setting up the
334: // table and data for testing SqlBeanDataLoader
335: masterSuite.addTest(new TotalSqlBeanDataLoaderTest(
336: "cleanUpBeforeStart"));
337: masterSuite.addTest(new TotalSqlBeanDataLoaderTest(
338: "createTestData"));
339: // add single threaded tests
340: Test singleThreadedTests = getSingleThreadedTests();
341: if (singleThreadedTests != null) {
342: masterSuite.addTest(singleThreadedTests);
343: }
344: // add multi threaded tests
345: Test multiThreadedTests = getMultiThreadedTests();
346: if (multiThreadedTests != null) {
347: masterSuite.addTest(multiThreadedTests);
348: }
349: return masterSuite;
350: }
351:
352: /**
353: * This method is used within the suite method to get all of the single
354: * threaded tests. Add all your single threaded tests in this method with
355: * a line like: suite.addTest(new CacheServiceTest("testFunction1"));
356: * @return Test the suite of single threaded tests in this test case
357: */
358: private static Test getSingleThreadedTests() {
359: TestSuite mySuite = new TestSuite();
360: mySuite.addTest(new TotalSqlBeanDataLoaderTest(
361: "testDefaultImplMap"));
362: mySuite.addTest(new TotalSqlBeanDataLoaderTest(
363: "testMapImplWithoutComparator"));
364: mySuite.addTest(new TotalSqlBeanDataLoaderTest(
365: "testMapImplWithComparator"));
366: mySuite.addTest(new TotalSqlBeanDataLoaderTest(
367: "testThreeColumnBeanClass"));
368: mySuite.addTest(new TotalSqlBeanDataLoaderTest(
369: "testInvalidConfig"));
370: mySuite.addTest(new TotalSqlBeanDataLoaderTest(
371: "testInvalidBeanAttribute"));
372: return mySuite;
373: }
374:
375: /**
376: * This method is used within the suite method to get all of the multi
377: * threaded tests. Add all your multi threaded tests in this method with
378: * a line like: addTest(suite, "testFunction1", 5);
379: * Currently there are no multi-threaded tests for SqlBeanDataLoaderTest
380: * @return Test the suite of multi-threaded tests in this test case
381: */
382: private static Test getMultiThreadedTests() {
383: TestSuite suite = new ActiveTestSuite();
384: final int THREAD_COUNT = 10;
385:
386: return suite;
387: }
388:
389: /**
390: * This method will add the give test to the give suite the specified
391: * number of times. This is best used for multi-threaded tests where
392: * suite is an instance of ActiveTestSuite and you want to run the same
393: * test in multiple threads.
394: * @param suite the suite to add the test to.
395: * @param testName the name of the test to add.
396: * @param number the number of times to add the test to the suite
397: */
398: private static void addTest(TestSuite suite, String testName,
399: int number) {
400: for (int count = 0; count < number; count++) {
401: suite.addTest(new TotalSqlBeanDataLoaderTest(testName));
402: }
403: }
404:
405: }
|