1 64 65 package com.jcorporate.expresso.core.utility; 66 67 import com.jcorporate.expresso.core.db.DBConnection; 68 import com.jcorporate.expresso.core.db.DBConnectionPool; 69 import com.jcorporate.expresso.core.db.DBException; 70 import com.jcorporate.expresso.core.dbobj.DBObject; 71 import com.jcorporate.expresso.core.dbobj.Schema; 72 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 73 import com.jcorporate.expresso.kernel.ConsoleInstallLog; 74 import com.jcorporate.expresso.kernel.InstallLog; 75 import com.jcorporate.expresso.services.dbobj.SchemaList; 76 import com.jcorporate.expresso.services.test.ExpressoTestCase; 77 import com.jcorporate.expresso.services.test.SchemaDeleter; 78 import com.jcorporate.expresso.services.test.TestSystemInitializer; 79 import junit.framework.TestSuite; 80 81 import java.io.ByteArrayInputStream ; 82 import java.io.ByteArrayOutputStream ; 83 import java.io.IOException ; 84 import java.io.ObjectInputStream ; 85 import java.io.ObjectOutputStream ; 86 import java.util.Enumeration ; 87 import java.util.TreeSet ; 88 import java.util.Vector ; 89 90 91 101 public class DBToolTests 102 extends ExpressoTestCase { 103 104 107 protected Vector schemaList = new Vector (); 108 static private boolean alreadyCleared = false; 109 static private boolean failedOne = false; 110 private InstallLog installLog = new ConsoleInstallLog(); 111 112 public DBToolTests(String name) 113 throws Exception { 114 super(name); 115 } 116 117 public static void main(String [] args) 118 throws java.lang.Exception { 119 120 junit.textui.TestRunner.run(suite()); 122 } 123 124 protected boolean isAlreadyCleared() { 125 return alreadyCleared; 126 } 127 128 protected void setAlreadyCleared(boolean newValue) { 129 alreadyCleared = newValue; 130 } 131 132 protected boolean isFailedOnce() { 133 return failedOne; 134 } 135 136 protected void setFailedOnce(boolean newValue) { 137 failedOne = newValue; 138 } 139 140 141 142 148 public static junit.framework.Test suite() 149 throws Exception { 150 151 TestSuite ts = new TestSuite("Schema Verification Tests"); 153 ts.addTest(new DBToolTests("testCreate")); 154 ts.addTest(new DBToolTests("testAddingSchemas")); 155 ts.addTest(new DBToolTests("testSetupSecurity")); 156 ts.addTest(new DBToolTests("testPopulateTables")); 157 ts.addTest(new DBToolTests("testSetupConfigValues")); 158 ts.addTest(new DBToolTests("testOtherSetups")); 159 ts.addTest(new DBToolTests("testVerify")); 160 161 return ts; 162 } 163 164 168 public void testCreate() { 169 try { 170 DBTool.createTables(installLog, schemaList, TestSystemInitializer.getTestContext()); 171 DBTool.compareTables(installLog, schemaList, TestSystemInitializer.getTestContext()); 172 } catch (Exception e) { 173 System.out.println(e.getMessage()); 174 e.printStackTrace(); 175 this.setFailedOnce(true); 176 fail(e.getMessage()); 177 } 178 } 179 180 184 public void testAddingSchemas() { 185 try { 186 if (!this.isFailedOnce()) { 187 for (Enumeration e = schemaList.elements(); 188 e.hasMoreElements();) { 189 Schema s = (Schema) e.nextElement(); 190 191 if (!s.getClass().getName().equals("com.jcorporate.expresso.core.ExpressoSchema")) { 192 SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 193 sl.setDataContext(TestSystemInitializer.getTestContext()); 194 sl.setField("SchemaClass", s.getClass().getName()); 195 sl.setField("Descrip", s.getDefaultDescription()); 196 sl.setField("ComponentCode", 197 s.getDefaultComponentCode()); 198 sl.add(); 199 } 200 } 201 } else { 202 fail("Already Failed Earlier Test - Unable to continue"); 203 } 204 } catch (Exception e) { 205 System.out.println(e.getMessage()); 206 e.printStackTrace(); 207 this.setFailedOnce(true); 208 fail(e.getMessage()); 209 } 210 } 211 212 216 public void testPopulateTables() { 217 try { 218 if (!this.isFailedOnce()) { 219 DBTool.populateTables(installLog, schemaList, TestSystemInitializer.getTestContext()); 220 } else { 221 fail("Already Failed Earlier Test - Unable to continue"); 222 } 223 } catch (Exception e) { 224 System.out.println(e.getMessage()); 225 e.printStackTrace(); 226 227 fail(e.getMessage()); 230 } 231 } 232 233 237 public void testSetupSecurity() { 238 try { 239 if (!this.isFailedOnce()) { 240 DBTool.setupSecurity(installLog, schemaList, TestSystemInitializer.getTestContext()); 241 } else { 242 fail("Already Failed Earlier Test - Unable to continue"); 243 } 244 } catch (Exception e) { 245 System.out.println(e.getMessage()); 246 e.printStackTrace(); 247 this.setFailedOnce(true); 248 fail(e.getMessage()); 249 } 250 } 251 252 255 public void testSetupConfigValues() { 256 try { 257 if (!this.isFailedOnce()) { 258 DBTool.setupConfig(installLog, schemaList, TestSystemInitializer.getTestContext()); 259 } else { 260 fail("Already Failed Earlier Test - Unable to continue"); 261 } 262 } catch (Exception e) { 263 System.out.println(e.getMessage()); 264 e.printStackTrace(); 265 this.setFailedOnce(true); 266 fail(e.getMessage()); 267 } 268 } 269 270 274 public void testOtherSetups() { 275 try { 276 if (!this.isFailedOnce()) { 277 DBTool.otherSetups(installLog, schemaList, TestSystemInitializer.getTestContext()); 278 } else { 279 fail("Already Failed Earlier Test - Unable to continue"); 280 } 281 } catch (Exception e) { 282 System.out.println(e.getMessage()); 283 e.printStackTrace(); 284 this.setFailedOnce(true); 285 fail(e.getMessage()); 286 } 287 } 288 289 295 public void testVerify() { 296 TreeSet tableNames = new TreeSet (); 297 DBConnectionPool myPool = null; 298 DBConnection myConnection = null; 299 300 try { 301 if (!this.isFailedOnce()) { 302 assertTrue("schemaList.size() > 0", schemaList.size() > 0); 303 304 Schema thisSchema = null; 305 306 for (Enumeration als = schemaList.elements(); 307 als.hasMoreElements();) { 308 thisSchema = (Schema) als.nextElement(); 309 System.out.println("Verifying Schema " + 310 thisSchema.getClass().getName()); 311 312 DBObject oneObject = null; 313 314 for (Enumeration dbo = thisSchema.getMembers(); 315 dbo.hasMoreElements();) { 316 oneObject = (DBObject) dbo.nextElement(); 317 oneObject.setDataContext(TestSystemInitializer.getTestContext()); 318 319 if (tableNames.contains(oneObject.getJDBCMetaData().getTargetTable())) { 321 322 if (oneObject.getClass().getName().equals( 328 com.jcorporate.expresso.services.dbobj.DefaultUserInfo.class.getName()) || 329 oneObject.getClass().getName().equals( 330 com.jcorporate.expresso.ext.dbobj.SingleDBUserInfo.class.getName())) { 331 } else { 332 fail("Table name: " + 333 oneObject.getJDBCMetaData().getTargetTable() + 334 " already exists due to another DBObject. Offending DBObject" + 335 oneObject.getClass().getName()); 336 } 337 } else { 338 tableNames.add(oneObject.getJDBCMetaData().getTargetTable()); 339 } 340 try { 342 oneObject.verify(); 343 } catch (DBException de) { 344 myPool.release(myConnection); 345 System.out.println(de.getMessage()); 346 de.printStackTrace(); 347 fail("Error in object " + oneObject.getJDBCMetaData().getName() + 348 ":" + de.getMessage()); 349 } 350 } 351 352 } 353 354 } else { 356 fail(); 357 } 358 } catch (Exception e) { 359 System.out.println(e.getMessage()); 360 e.printStackTrace(); 361 this.setFailedOnce(true); 362 363 if (myPool != null && myConnection != null) { 364 myPool.release(myConnection); 365 } 366 367 fail(e.getMessage()); 368 } 369 } 370 371 376 public void testSerialization() { 377 try { 378 assertTrue("schemaList.size() > 0", schemaList.size() > 0); 379 380 Schema thisSchema = null; 381 382 for (Enumeration als = schemaList.elements(); 383 als.hasMoreElements();) { 384 thisSchema = (Schema) als.nextElement(); 385 System.out.println("Testing Schema Serialization: " + 386 thisSchema.getClass().getName()); 387 388 DBObject oneObject = null; 389 390 for (Enumeration dbo = thisSchema.getMembers(); 391 dbo.hasMoreElements();) { 392 oneObject = (DBObject) dbo.nextElement(); 393 oneObject.setDataContext(TestSystemInitializer.getTestContext()); 394 395 try { 397 DBObject secondObject = (DBObject) runThroughSerializer(oneObject); 398 } catch (IOException ioe) { 399 System.out.println(ioe.getMessage()); 400 ioe.printStackTrace(); 401 fail("Error serializing object " + 402 oneObject.getJDBCMetaData().getName() + ":" + ioe.getMessage()); 403 } catch (DBException de) { 404 System.out.println(de.getMessage()); 405 de.printStackTrace(); 406 fail("Error in object " + oneObject.getJDBCMetaData().getName() + ":" + 407 de.getMessage()); 408 } 409 } 410 411 } 412 413 } catch (IOException ioe) { 414 ioe.printStackTrace(); 415 fail("Failed Serialization: " + ioe.getMessage()); 416 } catch (Exception e) { 417 System.out.println(e.getMessage()); 418 e.printStackTrace(); 419 fail(e.getMessage()); 420 } 421 } 422 423 431 private DBObject runThroughSerializer(DBObject testObject) 432 throws Exception { 433 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 434 ObjectOutputStream oos = new ObjectOutputStream (bos); 435 oos.writeObject(testObject); 436 437 ByteArrayInputStream bis = new ByteArrayInputStream (bos.toByteArray()); 438 ObjectInputStream ois = new ObjectInputStream (bis); 439 440 return (DBObject) ois.readObject(); 441 } 442 443 protected void setUp() 444 throws Exception { 445 DBConnectionPool.reInitialize(); 446 super.setUp(); 447 448 Class c = Class.forName("com.jcorporate.expresso.core.ExpressoSchema"); 449 schemaList.add(c.newInstance()); 450 451 if (this.isAlreadyCleared() == false) { 452 SchemaDeleter.setSchemas(schemaList); 453 SchemaDeleter.deleteSchemas(TestSystemInitializer.getTestContext()); 454 this.setAlreadyCleared(true); 455 } 456 } 457 } 458 | Popular Tags |