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.mru.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: import java.util.HashMap;
033: import java.util.ArrayList;
034: import java.util.List;
035:
036: import org.sape.carbon.core.component.Lookup;
037: import org.sape.carbon.core.component.lifecycle.StateTransitionException;
038: import org.sape.carbon.core.config.InvalidConfigurationException;
039: import org.sape.carbon.core.config.Config;
040: import org.sape.carbon.services.cache.Cache;
041: import org.sape.carbon.services.sql.StatementFactory;
042: import org.sape.carbon.services.sql.StatementFactoryException;
043: import org.sape.carbon.services.sqldataloader.test.TestComparator;
044: import org.sape.carbon.services.sqldataloader.test.TestThreeColumnBean;
045: import org.sape.carbon.services.sqldataloader.test.TestTwoColumnBean;
046:
047: /**
048: * Template for junit test harness.
049: * This class is responsible for testing SqlBeanDataLoader
050: * @since carbon 2.1
051: * @author Akash Tayal, May 2003
052: * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2003/09/30 02:08:19 $)
053: * <br>Copyright 2003 Sapient
054: */
055: public class SqlBeanMapDataLoaderTest extends TestCase {
056: public SqlBeanMapDataLoaderTest(String testName) {
057: super (testName);
058: }
059:
060: private static final String STATEMENT_FACTORY = "/sql/sqldataloader/test/SqlMapBeanDataLoaderStatementFactory";
061:
062: private static final String DROP_TABLE_ONE_LOOKUP = "DropTableBeanData";
063: private static final String CREATE_TABLE_ONE_LOOKUP = "CreateTableBeanData";
064: private static final String INSERT_TABLE_ONE_LOOKUP_DATA = "InsertBeanData";
065:
066: private static List data = new ArrayList();
067:
068: private static final int COUNT = 50;
069: private static final int GROUP = 5;
070: // Create a list of lists where each child list contains the data for one
071: // bean.
072:
073: static {
074: for (int i = 0; i < COUNT; i++) {
075: ArrayList list = new ArrayList();
076:
077: BigDecimal key = new BigDecimal(i);
078: BigDecimal group = new BigDecimal(i % GROUP);
079: String col2 = new String("Test data for key: " + key
080: + " group: " + group);
081:
082: list.add(key);
083: list.add(group);
084: list.add(col2);
085:
086: data.add(list);
087: }
088: }
089:
090: /**
091: * Method cleanUpBeforeStart. Cleans up database table before the test
092: * specific data is loaded.
093: */
094: public void cleanUpBeforeStart() {
095: PreparedStatement preparedStatement = null;
096: try {
097: StatementFactory sf = (StatementFactory) Lookup
098: .getInstance().fetchComponent(STATEMENT_FACTORY);
099:
100: try {
101: preparedStatement = sf
102: .createPreparedStatement(DROP_TABLE_ONE_LOOKUP);
103: preparedStatement.executeUpdate();
104: } catch (SQLException se) {
105: //eat the exception
106: }
107: } catch (StatementFactoryException sfe) {
108: fail("Clean up before start operation failed due to " + sfe);
109: }
110: }
111:
112: /**
113: * Method createTestData. Creates test table and populates the data.
114: */
115: public void createTestData() throws SQLException,
116: StatementFactoryException {
117: PreparedStatement preparedStatement = null;
118: int result;
119: try {
120: StatementFactory sf = (StatementFactory) Lookup
121: .getInstance().fetchComponent(STATEMENT_FACTORY);
122:
123: preparedStatement = sf
124: .createPreparedStatement(CREATE_TABLE_ONE_LOOKUP);
125: preparedStatement.executeUpdate();
126:
127: preparedStatement = sf
128: .createPreparedStatement(INSERT_TABLE_ONE_LOOKUP_DATA);
129:
130: for (int i = 0; i < data.size(); i++) {
131: List beanItem = (List) data.get(i);
132: preparedStatement.setBigDecimal(1,
133: (BigDecimal) beanItem.get(0));
134: preparedStatement.setBigDecimal(2,
135: (BigDecimal) beanItem.get(1));
136: preparedStatement
137: .setString(3, (String) beanItem.get(2));
138: result = preparedStatement.executeUpdate();
139: if (result != 1) {
140: fail("Unable to insert test data");
141: }
142: }
143:
144: preparedStatement.close();
145: } finally {
146: if (preparedStatement != null) {
147: try {
148: preparedStatement.getConnection().close();
149: } catch (SQLException se) {
150: }
151: }
152: }
153: }
154:
155: private static final String DATALOADER_THREE_COLUMN = "/sql/sqldataloader/mru/TestSqlMapDataloader_ThreeColumn";
156:
157: /**
158: * Test default implementation used for Map
159: */
160: public void testThreeColumnWithMap() {
161: Cache cache = (Cache) Lookup.getInstance().fetchComponent(
162: DATALOADER_THREE_COLUMN);
163:
164: for (int i = 0; i < GROUP; i++) {
165:
166: HashMap result = (HashMap) cache.get(new BigDecimal(i));
167:
168: //System.out.println("Values for: " + i);
169: for (Iterator mapIter = result.entrySet().iterator(); mapIter
170: .hasNext();) {
171: Map.Entry entry = (Map.Entry) mapIter.next();
172: Object o1 = (Object) entry.getKey();
173: Object o2 = (Object) entry.getValue();
174: //System.out.println("\t" + o1 + " = " + o2);
175: }
176:
177: // TODO GH: test for correct values
178: }
179:
180: }
181:
182: // Compares the maps not only for similar key and values but also
183: // for ordering.
184: private boolean compareSequencedMap(Map expectedMap, Map actualMap) {
185: boolean returnValue = false;
186: if (expectedMap.keySet().equals(actualMap.keySet())) {
187: Set entrySetExpectedMap = expectedMap.entrySet();
188: Set entrySetActualMap = actualMap.entrySet();
189: Iterator iteratorExpectedMap = entrySetExpectedMap
190: .iterator();
191: Iterator iteratorActualMap = entrySetActualMap.iterator();
192:
193: while ((iteratorExpectedMap.hasNext())
194: && (iteratorActualMap.hasNext())) {
195: Map.Entry entryExpectedMap = (Map.Entry) iteratorExpectedMap
196: .next();
197: Map.Entry entryActualMap = (Map.Entry) iteratorActualMap
198: .next();
199: if ((entryExpectedMap.getKey().equals(entryActualMap
200: .getKey()))
201: && (entryExpectedMap.getValue()
202: .equals(entryActualMap.getValue()))) {
203: returnValue = true;
204: } else {
205: return false;
206: }
207: }
208: }
209: return returnValue;
210: }
211:
212: /**
213: * Method called by jUnit to get all the tests in this test case.
214: * @return Test the suite of tests in this test case
215: */
216: public static Test suite() {
217: TestSuite masterSuite = new TestSuite(
218: "SqlBeanMapDataloaderTest");
219:
220: // These two methods are added as part of initial setup before rest
221: // of the cases are executed.
222: // These methods are responsible for cleaning and setting up the
223: // table and data for testing SqlBeanDataLoader
224: masterSuite.addTest(new SqlBeanMapDataLoaderTest(
225: "cleanUpBeforeStart"));
226: masterSuite.addTest(new SqlBeanMapDataLoaderTest(
227: "createTestData"));
228:
229: // add single threaded tests
230: Test singleThreadedTests = getSingleThreadedTests();
231: if (singleThreadedTests != null) {
232: masterSuite.addTest(singleThreadedTests);
233: }
234: // add multi threaded tests
235: Test multiThreadedTests = getMultiThreadedTests();
236: if (multiThreadedTests != null) {
237: masterSuite.addTest(multiThreadedTests);
238: }
239: return masterSuite;
240: }
241:
242: /**
243: * This method is used within the suite method to get all of the single
244: * threaded tests. Add all your single threaded tests in this method with
245: * a line like: suite.addTest(new CacheServiceTest("testFunction1"));
246: * @return Test the suite of single threaded tests in this test case
247: */
248: private static Test getSingleThreadedTests() {
249: TestSuite mySuite = new TestSuite("SingleThreadedTests");
250: mySuite.addTest(new SqlBeanMapDataLoaderTest(
251: "testThreeColumnWithMap"));
252: return mySuite;
253: }
254:
255: /**
256: * This method is used within the suite method to get all of the multi
257: * threaded tests. Add all your multi threaded tests in this method with
258: * a line like: addTest(suite, "testFunction1", 5);
259: * Currently there are no multi-threaded tests for SqlBeanDataLoaderTest
260: * @return Test the suite of multi-threaded tests in this test case
261: */
262: private static Test getMultiThreadedTests() {
263: TestSuite suite = new ActiveTestSuite();
264: final int THREAD_COUNT = 10;
265:
266: return null;
267: }
268:
269: /**
270: * This method will add the give test to the give suite the specified
271: * number of times. This is best used for multi-threaded tests where
272: * suite is an instance of ActiveTestSuite and you want to run the same
273: * test in multiple threads.
274: * @param suite the suite to add the test to.
275: * @param testName the name of the test to add.
276: * @param number the number of times to add the test to the suite
277: */
278: private static void addTest(TestSuite suite, String testName,
279: int number) {
280: for (int count = 0; count < number; count++) {
281: suite.addTest(new SqlBeanMapDataLoaderTest(testName));
282: }
283: }
284:
285: }
|