]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-336,CSPACE-321: Moved unit tests of the JDBC implementation of the ID service...
authorAron Roberts <aron@socrates.berkeley.edu>
Wed, 12 Aug 2009 01:48:04 +0000 (01:48 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Wed, 12 Aug 2009 01:48:04 +0000 (01:48 +0000)
services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java
services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java

index d85c358c6bd430bfaeba6ac2149d7b1b18db23c1..8b182af49c22fd362230d6c0fe3004f6f778521a 100644 (file)
@@ -111,7 +111,7 @@ public class IDServiceJdbcImpl implements IDService {
   /**
    * Constructor (no-argument).
    */ 
-  public void IDServiceJdbcImpl() {
+  public void IDServiceJdbcImpl() throws IllegalStateException {
   
     // @TODO Decide when and how to fail at startup, or else to correct
     // failure conditions automatically, when preconditions are not met.
@@ -119,11 +119,15 @@ public class IDServiceJdbcImpl implements IDService {
     // Currently, errors at initialization are merely informative and
     // result in exceptions that can be persistently logged.
     
-    init();
+    try {
+      init();
+    } catch (IllegalStateException e) {
+      throw e;
+    }
     
   }
   
-  // @TODO init() and hasTable() are currently UNTESTED as of 2009-08-11T13:00-0700.
+  // @TODO init() is currently UNTESTED as of 2009-08-11T13:00-0700.
 
   //////////////////////////////////////////////////////////////////////
   /**
@@ -184,11 +188,11 @@ public class IDServiceJdbcImpl implements IDService {
         "Error instantiating JDBC driver class '" +
         JDBC_DRIVER_CLASSNAME +
         "' to set up database connection.");
-     } catch (IllegalAccessException e) {
-      throw new IllegalStateException(
-        "Error accessing JDBC driver class '" +
-        JDBC_DRIVER_CLASSNAME +
-        "' to set up database connection.");
+    } catch (IllegalAccessException e) {
+    throw new IllegalStateException(
+      "Error accessing JDBC driver class '" +
+      JDBC_DRIVER_CLASSNAME +
+      "' to set up database connection.");
     }
       
   }
@@ -300,7 +304,7 @@ public class IDServiceJdbcImpl implements IDService {
 
     IDPattern pattern;
     try {
-      pattern = IDPatternSerializer.deserialize(serializedGenerator);
+      pattern = IDGeneratorSerializer.deserialize(serializedGenerator);
     } catch (IllegalArgumentException e) {
            throw e;
     }
@@ -481,7 +485,7 @@ public class IDServiceJdbcImpl implements IDService {
 
     String serializedGenerator = "";
     try {
-      serializedGenerator = IDPatternSerializer.serialize(pattern);
+      serializedGenerator = IDGeneratorSerializer.serialize(pattern);
     } catch (IllegalArgumentException e) {
            throw e;
     }
@@ -620,7 +624,7 @@ public class IDServiceJdbcImpl implements IDService {
 
     String serializedGenerator = "";
     try {
-      serializedGenerator = IDPatternSerializer.serialize(pattern);
+      serializedGenerator = IDGeneratorSerializer.serialize(pattern);
     } catch (IllegalArgumentException e) {
            throw e;
     }
@@ -691,7 +695,7 @@ public class IDServiceJdbcImpl implements IDService {
                            
                          IDPattern pattern;
         try {
-          pattern = IDPatternSerializer.deserialize(serializedGenerator);
+          pattern = IDGeneratorSerializer.deserialize(serializedGenerator);
         } catch (IllegalArgumentException e) {
           throw e;
         }
index 2f749caf74ff08bddbd31c021fa5adc61bd85131..dca62a08b9509c9546eab7944679783ceb48092a 100644 (file)
@@ -19,10 +19,8 @@ package org.collectionspace.services.id.test;
 
 import org.collectionspace.services.id.*;
 
-import junit.framework.TestCase;
-import static org.junit.Assert.*;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.testng.Assert;
+import org.testng.annotations.Test;
 
 /**    
  * IDServiceJdbcImplTest
@@ -33,7 +31,7 @@ import org.junit.Test;
  * $LastChangedRevision: 302 $
  * $LastChangedDate$
  */
-public class IDServiceJdbcImplTest extends TestCase {
+public class IDServiceJdbcImplTest {
 
   // *IMPORTANT*
   // @TODO This class is in an early state of a refactoring to
@@ -48,63 +46,60 @@ public class IDServiceJdbcImplTest extends TestCase {
   
   IDServiceJdbcImpl jdbc = new IDServiceJdbcImpl();
   IDService service = jdbc;
-
+  
+  final static String TABLE_NAME = "id_generators";
        final static String DEFAULT_CSID = "TEST-1";
 
-  @BeforeClass
-  public static void setUpOnce() { 
-    // @TODO Check for service preconditions before running tests.   
-  }
-  
   @Test
-  public void testPlaceholder() {
-    // Placeholder test to avoid "org.testng.TestNGException:
-    // Failure in JUnit mode ...: could not create/run JUnit test suite"
-    // errors until we add working tests to this class.
+  public void hasRequiredDatabaseTable() {
+    Assert.assertTrue(
+      jdbc.hasTable(TABLE_NAME), 
+      "Table '" + TABLE_NAME + "' must exist in database. " + 
+      "Please first run SQL setup script, 'create_id_generators_table.sql', in 'id' project.");
   }
 
-  @Test
-  public void testAddIDGenerator() {
+  @Test(dependsOnMethods = {"hasRequiredDatabaseTable"})
+  public void addIDGenerator() {
     jdbc.addIDGenerator(DEFAULT_CSID, getSpectrumEntryNumberGenerator());
   }
 
-  @Test
-  public void testReadIDGenerator() {
+  @Test(dependsOnMethods = {"addIDGenerator"})
+  public void readIDGenerator() {
 
     serializedGenerator = jdbc.getIDGenerator(DEFAULT_CSID);
-    pattern = IDPatternSerializer.deserialize(serializedGenerator);
-    assertEquals(DEFAULT_CSID, pattern.getCsid());
+    pattern = IDGeneratorSerializer.deserialize(serializedGenerator);
+    Assert.assertEquals(DEFAULT_CSID, pattern.getCsid());
     
   }
 
-  @Test
-  public void testUpdateIDGenerator() {
+  @Test(dependsOnMethods = {"addIDGenerator"})
+  public void updateIDGenerator() {
 
     final String NEW_DESCRIPTION = "new description";
     
     // Retrieve an existing generator, deserialize it,
     // update its contents, serialize it, and write it back.
     serializedGenerator = jdbc.getIDGenerator(DEFAULT_CSID);
-    pattern = IDPatternSerializer.deserialize(serializedGenerator);
+    pattern = IDGeneratorSerializer.deserialize(serializedGenerator);
     pattern.setDescription(NEW_DESCRIPTION);
-    serializedGenerator = IDPatternSerializer.serialize(pattern);
+    serializedGenerator = IDGeneratorSerializer.serialize(pattern);
     
     jdbc.updateIDGenerator(DEFAULT_CSID, serializedGenerator);
     
     serializedGenerator = jdbc.getIDGenerator(DEFAULT_CSID);
-    pattern = IDPatternSerializer.deserialize(serializedGenerator);
+    pattern = IDGeneratorSerializer.deserialize(serializedGenerator);
     
-    assertEquals(NEW_DESCRIPTION, pattern.getDescription());
+    Assert.assertEquals(NEW_DESCRIPTION, pattern.getDescription());
     
   }
 
-  @Test
-  public void testDeleteIDGenerator() {
+  @Test(dependsOnMethods = {"addIDGenerator", "readIDGenerator"})
+  public void deleteIDGenerator() {
     jdbc.deleteIDGenerator(DEFAULT_CSID);
   }
  
-  @Test
-       public void testNewIDValidPattern() {
+  @Test(dependsOnMethods = {"addIDGenerator", "deleteIDGenerator"})
+       public void newIDValidPattern() {
        
     csid = DEFAULT_CSID;
     
@@ -116,9 +111,9 @@ public class IDServiceJdbcImplTest extends TestCase {
     
     jdbc.addIDGenerator(csid, getSpectrumEntryNumberGenerator());
 
-    assertEquals("E1", service.newID("TEST-1"));
-    assertEquals("E2", service.newID("TEST-1"));
-    assertEquals("E3", service.newID("TEST-1"));
+    Assert.assertEquals("E1", service.newID("TEST-1"));
+    Assert.assertEquals("E2", service.newID("TEST-1"));
+    Assert.assertEquals("E3", service.newID("TEST-1"));
     
     try {
       jdbc.deleteIDGenerator(csid);
@@ -129,9 +124,9 @@ public class IDServiceJdbcImplTest extends TestCase {
     jdbc.addIDGenerator(csid, getChinAccessionNumberGenerator());
 
     String currentYear = YearIDGenerator.getCurrentYear();
-    assertEquals(currentYear + ".1.1", service.newID("TEST-1"));
-    assertEquals(currentYear + ".1.2", service.newID("TEST-1"));
-    assertEquals(currentYear + ".1.3", service.newID("TEST-1"));
+    Assert.assertEquals(currentYear + ".1.1", service.newID("TEST-1"));
+    Assert.assertEquals(currentYear + ".1.2", service.newID("TEST-1"));
+    Assert.assertEquals(currentYear + ".1.3", service.newID("TEST-1"));
 
     try {
       jdbc.deleteIDGenerator(csid);
@@ -145,16 +140,10 @@ public class IDServiceJdbcImplTest extends TestCase {
   // 1. The ID Service is running and accessible to this test; and
   // 2. There is no ID pattern retrievable through that service
   //    with the identifier 'non-existent identifier'.
-  @Test
-       public void testNextIDInvalidPattern() {
-       
-               try {
-      nextId = service.newID("non-existent identifier");
-                       fail("Should have thrown IllegalArgumentException here");
-               } catch (IllegalArgumentException expected) {
-                       // This Exception should be thrown, and thus the test should pass.
-               }
-               
+  @Test(dependsOnMethods = {"hasRequiredDatabaseTable"}, 
+    expectedExceptions = IllegalArgumentException.class)
+       public void newIDInvalidPattern() {
+    nextId = service.newID("non-existent identifier");         
        }
 
   // ---------------------------------------------------------------
@@ -171,7 +160,7 @@ public class IDServiceJdbcImplTest extends TestCase {
     pattern.add(new StringIDPart("E"));
     pattern.add(new NumericIDPart("1"));
     
-    return IDPatternSerializer.serialize(pattern);
+    return IDGeneratorSerializer.serialize(pattern);
     
   }
 
@@ -186,7 +175,7 @@ public class IDServiceJdbcImplTest extends TestCase {
     pattern.add(new StringIDPart("."));
     pattern.add(new NumericIDPart("1"));    
 
-    return IDPatternSerializer.serialize(pattern);
+    return IDGeneratorSerializer.serialize(pattern);
     
   }