1 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 ; 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.TreeMap ; 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 51 public class TotalSqlBeanDataLoaderTest extends TestCase { 52 public TotalSqlBeanDataLoaderTest(String testName) { 53 super(testName); 54 } 55 56 private static final String STATEMENT_FACTORY = 57 "/sql/sqldataloader/test/SqlBeanDataLoaderStatementFactory"; 58 59 60 private static final String DROP_TABLE_ONE_LOOKUP = 61 "DropTableBeanData"; 62 private static final String CREATE_TABLE_ONE_LOOKUP = 63 "CreateTableBeanData"; 64 private static final String INSERT_TABLE_ONE_LOOKUP_DATA = 65 "InsertBeanData"; 66 67 private static final Object KEY1 = new BigDecimal ("1"); 69 private static final Object KEY2 = new BigDecimal ("2"); 70 private static final Object KEY3 = new BigDecimal ("3"); 71 private static final String KEY1_COLUMN1 = "This is cached row 1 column 1"; 72 private static final String KEY1_COLUMN2 = "This is cached row 1 column 2"; 73 private static final String KEY2_COLUMN1 = "This is cached row 2 column 1"; 74 private static final String KEY2_COLUMN2 = "This is cached row 2 column 2"; 75 private static final String KEY3_COLUMN1 = "This is cached row 3 column 1"; 76 private static final String KEY3_COLUMN2 = "This is cached row 3 column 2"; 77 78 private static Map expectedTwoColumnMap() { 81 TreeMap expectedTwoColumnResultMap = new TreeMap (); 82 TestTwoColumnBean twoColumnBean = new TestTwoColumnBean(); 83 twoColumnBean.setKey((BigDecimal )KEY1); 84 twoColumnBean.setValue(KEY1_COLUMN1); 85 expectedTwoColumnResultMap.put(KEY1,twoColumnBean); 86 twoColumnBean = new TestTwoColumnBean(); 87 twoColumnBean.setKey((BigDecimal )KEY2); 88 twoColumnBean.setValue(KEY2_COLUMN1); 89 expectedTwoColumnResultMap.put(KEY2,twoColumnBean); 90 twoColumnBean = new TestTwoColumnBean(); 91 twoColumnBean.setKey((BigDecimal )KEY3); 92 twoColumnBean.setValue(KEY3_COLUMN1); 93 expectedTwoColumnResultMap.put(KEY3,twoColumnBean); 94 return expectedTwoColumnResultMap; 95 } 96 97 private static Map expectedInvertedTwoColumnMap() { 100 TestComparator comparator = new TestComparator(); 101 TreeMap expectedTwoColumnResultMap = new TreeMap (comparator); 102 TestTwoColumnBean twoColumnBean = new TestTwoColumnBean(); 103 twoColumnBean.setKey((BigDecimal )KEY3); 104 twoColumnBean.setValue(KEY3_COLUMN1); 105 expectedTwoColumnResultMap.put(KEY3,twoColumnBean); 106 twoColumnBean = new TestTwoColumnBean(); 107 twoColumnBean.setKey((BigDecimal )KEY2); 108 twoColumnBean.setValue(KEY2_COLUMN1); 109 expectedTwoColumnResultMap.put(KEY2,twoColumnBean); 110 twoColumnBean = new TestTwoColumnBean(); 111 twoColumnBean.setKey((BigDecimal )KEY1); 112 twoColumnBean.setValue(KEY1_COLUMN1); 113 expectedTwoColumnResultMap.put(KEY1,twoColumnBean); 114 return expectedTwoColumnResultMap; 115 } 116 117 private static Map expectedThreeColumnMap() { 120 TreeMap expectedThreeColumnResultMap = new TreeMap (); 121 TestThreeColumnBean threeColumnBean = new TestThreeColumnBean(); 122 threeColumnBean.setKey((BigDecimal )KEY1); 123 threeColumnBean.setColumn1Value(KEY1_COLUMN1); 124 threeColumnBean.setColumn2Value(KEY1_COLUMN2); 125 expectedThreeColumnResultMap.put(KEY1,threeColumnBean); 126 threeColumnBean = new TestThreeColumnBean(); 127 threeColumnBean.setKey((BigDecimal )KEY2); 128 threeColumnBean.setColumn1Value(KEY2_COLUMN1); 129 threeColumnBean.setColumn2Value(KEY2_COLUMN2); 130 expectedThreeColumnResultMap.put(KEY2,threeColumnBean); 131 threeColumnBean = new TestThreeColumnBean(); 132 threeColumnBean.setKey((BigDecimal )KEY3); 133 threeColumnBean.setColumn1Value(KEY3_COLUMN1); 134 threeColumnBean.setColumn2Value(KEY3_COLUMN2); 135 expectedThreeColumnResultMap.put(KEY3,threeColumnBean); 136 return expectedThreeColumnResultMap; 137 } 138 139 140 144 public void cleanUpBeforeStart() { 145 PreparedStatement 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 se) { 155 } 157 } catch (StatementFactoryException sfe) { 158 fail("Clean up before start operation failed due to "+sfe); 159 } 160 } 161 162 163 166 public void createTestData() { 167 PreparedStatement 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 se) { 210 fail("Cannot create Test Tables"+se); 211 } 212 } 213 214 private static final String DATA_LOADER_WITHOUT_MAP_IMPL = 215 "/sql/sqldataloader/total/testSqlBeanDataLoaderWithoutMapImpl"; 216 217 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 DATA_LOADER_WITHOUT_COMPARATOR = 230 "/sql/sqldataloader/total/testSqlBeanDataLoaderWithoutComparator"; 231 232 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 DATA_LOADER_WITH_COMPARATOR = 245 "/sql/sqldataloader/total/testSqlBeanDataLoaderWithComparator"; 246 247 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 private boolean compareSequencedMap(Map expectedMap, Map actualMap) { 263 boolean returnValue = false; 264 if (expectedMap.keySet().equals(actualMap.keySet())) { 265 Set entrySetExpectedMap = expectedMap.entrySet(); 266 Set entrySetActualMap = actualMap.entrySet(); 267 Iterator iteratorExpectedMap = entrySetExpectedMap.iterator(); 268 Iterator iteratorActualMap = entrySetActualMap.iterator(); 269 270 while ((iteratorExpectedMap.hasNext()) && 271 (iteratorActualMap.hasNext())) { 272 Map.Entry entryExpectedMap = 273 (Map.Entry ) iteratorExpectedMap.next(); 274 Map.Entry entryActualMap = 275 (Map.Entry ) 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 DATA_LOADER_THREE_COLUMN = 289 "/sql/sqldataloader/total/testSqlBeanDataLoaderThreeColumn"; 290 291 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 INVALID_QUERY_DATA_LOADER = 303 "/sql/sqldataloader/total/testInvalidQuerySqlBeanDataLoader"; 304 305 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 } catch (StateTransitionException ste) { 316 } 318 } 319 320 private static final String INVALID_BEAN_ATTRIBUTE_DATA_LOADER = 321 "/sql/sqldataloader/total/testInvalidBeanAttributeSqlBeanDataLoader"; 322 323 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 } catch (StateTransitionException ste) { 334 } 336 } 337 338 339 343 public static Test suite() { 344 TestSuite masterSuite = new TestSuite(); 345 346 masterSuite.addTest(new TotalSqlBeanDataLoaderTest("cleanUpBeforeStart")); 351 masterSuite.addTest(new TotalSqlBeanDataLoaderTest("createTestData")); 352 Test singleThreadedTests = getSingleThreadedTests(); 354 if (singleThreadedTests != null) { 355 masterSuite.addTest(singleThreadedTests); 356 } 357 Test multiThreadedTests = getMultiThreadedTests(); 359 if (multiThreadedTests != null) { 360 masterSuite.addTest(multiThreadedTests); 361 } 362 return masterSuite; 363 } 364 365 366 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 392 private static Test getMultiThreadedTests() { 393 TestSuite suite = new ActiveTestSuite(); 394 final int THREAD_COUNT = 10; 395 396 return suite; 397 } 398 399 408 private static void addTest(TestSuite suite, String testName, int number) { 409 for (int count = 0; count < number; count++) { 410 suite.addTest(new TotalSqlBeanDataLoaderTest(testName)); 411 } 412 } 413 414 } 415 | Popular Tags |