]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-235,CSPACE-237: Fixed a number of minor errors in ID generator and ID part...
authorAron Roberts <aron@socrates.berkeley.edu>
Wed, 1 Jul 2009 01:13:08 +0000 (01:13 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Wed, 1 Jul 2009 01:13:08 +0000 (01:13 +0000)
services/id/service/src/main/java/org/collectionspace/services/id/AlphabeticIDGenerator.java
services/id/service/src/main/java/org/collectionspace/services/id/IDGenerator.java
services/id/service/src/main/java/org/collectionspace/services/id/IDPart.java
services/id/service/src/main/java/org/collectionspace/services/id/NumericIDGenerator.java
services/id/service/src/main/java/org/collectionspace/services/id/StringIDGenerator.java
services/id/service/src/main/java/org/collectionspace/services/id/YearIDGenerator.java
services/id/service/src/test/java/org/collectionspace/services/id/NumericIDPartTest.java
services/id/service/src/test/java/org/collectionspace/services/id/StringIDPartTest.java
services/id/service/src/test/java/org/collectionspace/services/id/YearIDPartTest.java

index 3083800dd98508783f43e4af94154d3fcb22bfb7..e3b4cab73ee59b44b3a09606df1856e708e2104d 100644 (file)
 
 // @TODO: Add Javadoc comments
 
-// @TODO: When auto expanding, we'll need to set a maximum length to which the
-// generated IDs can grow, likely as an additional parameter to be
+// @TODO: When auto expanding, we'll need to allow setting of a maximum length to
+// which the generated IDs can grow, likely as an additional parameter to be
 // passed to a constructor, with a default value hard-coded in the class.
+// If that length is exceeded, nextID() should throw an IllegalStateException.
 
 // @TODO: Consider handling escaped characters or sequences which represent Unicode
 // code points, both in the start and end characters of the sequence, and in the initial value.
@@ -242,7 +243,7 @@ public class AlphabeticIDGenerator implements IDGenerator {
   //
   // See the TODOs at the top of this class for additional
   // functionality that needs to be implemented.
-  public synchronized String nextID() {
+  public synchronized String nextID() throws IllegalStateException {
 
                // Get next values for each character, from right to left
                // (least significant to most significant).
@@ -297,10 +298,10 @@ public class AlphabeticIDGenerator implements IDGenerator {
                return sb.toString();
        }
 
-       public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+       public synchronized boolean isValidID(String value) {
 
-               if ( value == null || value == "") {
-                       throw new IllegalArgumentException("ID to validate must not be null or empty");
+               if (value == null || value.equals("")) {
+                       return false;
                }
 
                Pattern pattern = Pattern.compile(getRegex());
index fa054a769c5918a839d3b44c5f5232256fcfa747..f9c69b2e67ad59f7a69d0b1de224c8e6bbb53f75 100644 (file)
@@ -38,14 +38,14 @@ public interface IDGenerator {
        public String getCurrentID();
        
        // Sets the current value of an ID.
-       public void setCurrentID(String value);
+       public void setCurrentID(String value) throws IllegalArgumentException;
 
        // Resets an ID to its initial value.
        public void resetID();
 
        // Returns the next ID in the sequence, and sets
        // the current value to that ID.
-       public String nextID();
+       public String nextID() throws IllegalStateException;
 
        // Validates an ID against a pattern of generated IDs.
        public boolean isValidID(String value);
index 25d7265cf5c0309e78d950611bb6524a97b1af13..f70c421751deaf29ca040c1a2b15217924033c46 100644 (file)
@@ -58,7 +58,7 @@ public abstract class IDPart {
        }
 
        // Sets the current value of the ID associated with this IDPart.
-       public synchronized void setCurrentID(String value) {
+       public synchronized void setCurrentID(String value) throws IllegalArgumentException {
                generator.setCurrentID(value);
        }
 
index 05ea816bf9736c47feb11c32cd5f08295928b1e1..c2c6933e484549ef627b01cfa4323d03b732e503 100644 (file)
@@ -55,6 +55,10 @@ public class NumericIDGenerator implements IDGenerator {
        // Constructor.
        public NumericIDGenerator(String initialValue, String maxLength)
                throws IllegalArgumentException {
+
+               if (initialValue == null || initialValue.equals("")) {
+                       throw new IllegalArgumentException("Initial ID value must not be null or empty");
+               }
                
                try {
                        long l = Long.parseLong(initialValue.trim());
@@ -69,6 +73,10 @@ public class NumericIDGenerator implements IDGenerator {
                        throw new IllegalArgumentException("Initial ID value must be parseable as a number");
                }
 
+               if (maxLength == null || maxLength.equals("")) {
+                       throw new IllegalArgumentException("Initial ID value must not be null or empty");
+               }
+
                try {
                        this.maxLength = Integer.parseInt(maxLength);
                } catch (NumberFormatException e) {
@@ -90,10 +98,15 @@ public class NumericIDGenerator implements IDGenerator {
 
          // @TODO Much of this code is copied from the main constructor,
          // and may be ripe for refactoring.
+
+               if (value == null || value.equals("")) {
+                       throw new IllegalArgumentException("ID value must not be null or empty");
+               }
+               
                try {
                        long l = Long.parseLong(value.trim());
                        if ( l < 0 ) {
-                               throw new IllegalArgumentException("Initial ID value should be zero (0) or greater");
+                               throw new IllegalArgumentException("ID value should be zero (0) or greater");
                        }
                        this.currentValue = l;
                        this.initialValue = l;
index 6ad0b94d5d693cfa162daa7ecaf2a0cb74443f92..68d62cf20b57148aad79a32e00d8f341a366a705 100644 (file)
@@ -40,7 +40,7 @@ public class StringIDGenerator implements IDGenerator {
        
        public StringIDGenerator(String initialValue) throws IllegalArgumentException {
 
-               if ( initialValue == null || initialValue == "") {
+               if (initialValue == null || initialValue.equals("")) {
                        throw new IllegalArgumentException("Initial ID value must not be null or empty");
                }
                
@@ -58,7 +58,7 @@ public class StringIDGenerator implements IDGenerator {
        }
 
        public synchronized void setCurrentID(String value) throws IllegalArgumentException {
-               if ( initialValue == null || initialValue == "") {
+               if (value == null || value.equals("")) {
                        throw new IllegalArgumentException("ID value must not be null or empty");
                }
                this.currentValue = value;
@@ -72,10 +72,10 @@ public class StringIDGenerator implements IDGenerator {
                return this.currentValue;
   }
 
-       public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+       public synchronized boolean isValidID(String value) {
 
-               if ( value == null || value == "") {
-                       throw new IllegalArgumentException("ID to validate must not be null or empty");
+               if (value == null || value.equals("")) {
+                       return false;
                }
 
                Pattern pattern = Pattern.compile(getRegex());
index b098dfdcec2b85fc3b992d79b9cdcc027451795d..e6cddab42668973c804a0afe5e76b811987bc8ee 100644 (file)
@@ -66,7 +66,7 @@ public class YearIDGenerator implements IDGenerator {
        
        public YearIDGenerator(String initialValue) throws IllegalArgumentException {
 
-               if ( initialValue == null || initialValue == "") {
+               if (initialValue == null || initialValue.equals("")) {
                        throw new IllegalArgumentException("Initial ID value must not be null or empty");
                }
                
@@ -92,7 +92,7 @@ public class YearIDGenerator implements IDGenerator {
          // @TODO This code is copied from the main constructor,
          // and thus there may be an opportunity for refactoring.
 
-               if ( value == null || value == "") {
+               if (value == null || value.equals("")) {
                        throw new IllegalArgumentException("ID value must not be null or empty");
                }
                
@@ -124,8 +124,8 @@ public class YearIDGenerator implements IDGenerator {
 
        public synchronized boolean isValidID(String value) throws IllegalArgumentException {
 
-               if ( value == null || value == "") {
-                       throw new IllegalArgumentException("ID to validate must not be null or empty");
+               if (value == null || value.equals("")) {
+                       return false;
                }
 
                Pattern pattern = Pattern.compile(getRegex());
index a21cd88a2b4f49b059faef2363fff63214042cc2..374fd147420124338e3e9b30516289ee6c8b8911 100644 (file)
@@ -71,7 +71,7 @@ public class NumericIDPartTest extends TestCase {
                        
        }
 
-       public void testresetID() {
+       public void testResetID() {
        
                part = new NumericIDPart("25");
                assertEquals("26", part.nextID());
@@ -139,6 +139,17 @@ public class NumericIDPartTest extends TestCase {
 
        }
 
+       public void testNegativeInitialValue() {
+
+               try {
+                       part = new NumericIDPart("-1");
+                       fail("Should have thrown IllegalArgumentException here");
+               } catch (IllegalArgumentException expected) {
+                       // This Exception should be thrown, and thus the test should pass.
+               }
+       }
+
+
        public void testIsValidID() {
        
                part = new NumericIDPart("1");
@@ -164,6 +175,12 @@ public class NumericIDPartTest extends TestCase {
 
                part = new NumericIDPart("1", "3");
                assertFalse(part.isValidID("not a parseable long"));
+
+               part = new NumericIDPart("1", "3");
+               assertFalse(part.isValidID(null));
+
+               part = new NumericIDPart("1", "3");
+               assertFalse(part.isValidID(""));
        
        }       
        
index 8a4b0ebfbc99655c82f55569284f4984c5e6d2fe..54ef72b89586b4608eabb7718b759a7e5bfc3009 100644 (file)
@@ -98,20 +98,15 @@ public class StringIDPartTest extends TestCase {
 
                part = new StringIDPart("T");
                assertFalse(part.isValidID("TE"));
-       
-       }       
 
-       public void testNullValidationValue() {
-       
-               try {
-                       part = new StringIDPart("-");
-                       assertFalse(part.isValidID(null));
-                       fail("Should have thrown IllegalArgumentException here");
-               } catch (IllegalArgumentException expected) {
-                       // This Exception should be thrown, and thus the test should pass.
-               }
+    part = new StringIDPart("-");
+    assertFalse(part.isValidID(null));
 
+    part = new StringIDPart("-");
+    assertFalse(part.isValidID(""));
+       
        }       
+
        // @TODO: Add more tests of boundary conditions, exceptions ...
  
 }
index 60e4b37cce9d5ec0a56ae0ee2176cb872a04a478..90d6bfb754fce6c417f92446541967b51de3ba53 100644 (file)
@@ -50,29 +50,60 @@ public class YearIDPartTest extends TestCase {
                assertEquals(year, part.getCurrentID());
 
        }
-       
 
-/*
+       public void testSetCurrentID() {
+
+               part = new YearIDPart("1999");
+               part.setCurrentID("1999");
+               assertEquals("1999", part.getCurrentID());
+               part.setCurrentID("2000");
+               assertEquals("2000", part.getCurrentID());
+
+       }
+
+       public void testSetCurrentIDNullOrEmpty() {
+
+               part = new YearIDPart();
+
+               try {
+                 part.setCurrentID(null);
+                       fail("Should have thrown IllegalArgumentException here");
+               } catch (IllegalArgumentException expected) {
+                       // This Exception should be thrown, and thus the test should pass.
+               }
+
+               try {
+                 part.setCurrentID("");
+                       fail("Should have thrown IllegalArgumentException here");
+               } catch (IllegalArgumentException expected) {
+                       // This Exception should be thrown, and thus the test should pass.
+               }
+
+  }
+       
        public void testNextID() {
-               part = new YearIDPart("XYZ");           
-               assertEquals("XYZ", part.nextID());                     
+       
+               part = new YearIDPart("1999");          
+               assertEquals("1999", part.nextID());            
+               
        }
 
        public void testresetID() {
        
-               part = new YearIDPart(".");
-               assertEquals(".", part.nextID());
+               part = new YearIDPart("1999");
+               assertEquals("1999", part.nextID());
                part.resetID();
-               assertEquals(".", part.nextID());
+               assertEquals("1999", part.getCurrentID());
                        
        }
 
        public void testInitialID() {
-               part = new YearIDPart("-");
-               assertEquals("-", part.getInitialID());
+       
+               part = new YearIDPart("1999");
+               assertEquals("1999", part.getInitialID());
+               
        }
 
-*/
 
        public void testNullInitialValue() {
        
@@ -109,20 +140,15 @@ public class YearIDPartTest extends TestCase {
                
                part = new YearIDPart();
                assertFalse(part.isValidID("non-numeric value"));
-               
+
+    part = new YearIDPart();
+    assertFalse(part.isValidID(null));
+    
+    part = new YearIDPart();
+    assertFalse(part.isValidID(""));
+
        }
 
-       public void testNullValidationValue() {
-       
-               try {
-                       part = new YearIDPart();
-                       assertFalse(part.isValidID(null));
-                       fail("Should have thrown IllegalArgumentException here");
-               } catch (IllegalArgumentException expected) {
-                       // This Exception should be thrown, and thus the test should pass.
-               }
-               
-       }       
        // @TODO: Add more tests of boundary conditions, exceptions ...
  
 }