]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
NOJIRA Added rudimentary validation methods to IDPart and to two IDPart Generators.
authorAron Roberts <aron@socrates.berkeley.edu>
Tue, 23 Jun 2009 02:07:47 +0000 (02:07 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Tue, 23 Jun 2009 02:07:47 +0000 (02:07 +0000)
sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDGenerator.java
sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDPart.java
sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDGenerator.java
sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDPart.java
sandbox/aron/id/src/main/java/org/collectionspace/services/id/NumericIDGenerator.java
sandbox/aron/id/src/main/java/org/collectionspace/services/id/StringIDGenerator.java
sandbox/aron/id/src/main/java/org/collectionspace/services/id/YearIDGenerator.java
sandbox/aron/id/src/test/java/org/collectionspace/services/id/StringIDPartTest.java
sandbox/aron/id/src/test/java/org/collectionspace/services/id/YearIDPartTest.java

index 8fbea616d66c24e19dd363c528f69260d505894e..ff52b9c6d5592f25e2b2070a03f5f5410745b527 100644 (file)
@@ -223,4 +223,9 @@ public class AlphabeticIDGenerator implements IDGenerator {
                return sb.toString();
        }
 
+       public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+               // Currently stubbed-out
+               return true;
+       }
+       
 }
index ed55d83edb41b8bb6aec0bc4460cdd43b63b0a54..9e961b3b9c9eef3d8e8b6ab4283564d201f4eb17 100644 (file)
@@ -29,5 +29,5 @@ public class AlphabeticIDPart extends IDPart {
        public AlphabeticIDPart(String startVal, String endVal, String baseVal) {
                super(new AlphabeticIDGenerator(startVal, endVal, baseVal));
        };
-               
+                       
 }
index e114592fa074c8068a3b79de01591dc9551222e2..1e5c35694664c47f8dd9f60a4a1190e3f68b5db4 100644 (file)
@@ -27,5 +27,9 @@ public interface IDGenerator {
        public String getCurrentID();
 
        public String getNextID();
+
+       public boolean isValidID(String value);
+
+       // public String getRegex();
                
 }
index 41c7d309e7c982e1258b2e17c32cec89491b5c2b..3cc2016c7995d4927d8f0ee12c140389f5c5b99f 100644 (file)
@@ -56,14 +56,19 @@ public abstract class IDPart {
        }
 
        // Returns the next value of this ID.
-       public synchronized String getNextID() {
-               try {
-                       return generator.getNextID();
-               } catch (IllegalStateException e) {
-                       throw e;
-               }
+       public synchronized String getNextID() throws IllegalStateException {
+               return generator.getNextID();
        }
 
-       // public boolean validate() {};
+       // Validates an instance of the ID, to
+       // identify if it matches this IDPart's pattern.
+       //
+       // Some IDParts may offer generic validation,
+       // while others may offer per-instance valiadation
+       // based on the values, series, etc. that are
+       // stored within those parts.
+       public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+               return generator.isValidID(value);
+       }
  
 }
index 88dbfbca25954e21893efd3452d54af7514cdc83..d1fece529440295b239867ad3576541f05a94d0c 100644 (file)
@@ -32,14 +32,14 @@ public class NumericIDGenerator implements IDGenerator {
                try {
                        long l = Long.parseLong(initialValue.trim());
                        if ( l < 0 ) {
-                               throw new IllegalArgumentException("Initial value should be zero (0) or greater");
+                               throw new IllegalArgumentException("Initial ID value should be zero (0) or greater");
                        }
                        this.currentValue = l;
                        this.initialValue = l;
                } catch (NullPointerException e) {
-                       throw new IllegalArgumentException("Initial value should not be null");
+                       throw new IllegalArgumentException("Initial ID value should not be null");
                } catch (NumberFormatException e) {
-                       throw new IllegalArgumentException("Initial value must be parseable as a number");
+                       throw new IllegalArgumentException("Initial ID value must be parseable as a number");
                }
        }
 
@@ -59,5 +59,10 @@ public class NumericIDGenerator implements IDGenerator {
                this.currentValue++;
                return Long.toString(this.currentValue);
        }
+
+       public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+               // Currently stubbed-out
+               return true;
+       }
        
 }
index 42f16ce82cee765e0c6e7a1be6b095ff25b4cb59..831ebb15a96ff0b68ed7d03fc457f42155d61c77 100644 (file)
@@ -22,6 +22,9 @@
 
 package org.collectionspace.services.id;
 
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 public class StringIDGenerator implements IDGenerator {
     
        private String initialValue = null;
@@ -30,7 +33,7 @@ public class StringIDGenerator implements IDGenerator {
        public StringIDGenerator(String initialValue) throws IllegalArgumentException {
 
                if ( initialValue == null || initialValue == "") {
-                       throw new IllegalArgumentException("Initial value must not be null or empty");
+                       throw new IllegalArgumentException("Initial ID value must not be null or empty");
                }
                
                this.initialValue = initialValue;
@@ -53,5 +56,26 @@ public class StringIDGenerator implements IDGenerator {
        public synchronized String getNextID() {
                return this.currentValue;
   }
+
+       public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+
+               if ( value == null || value == "") {
+                       throw new IllegalArgumentException("ID to validate must not be null or empty");
+               }
+
+               Pattern pattern = Pattern.compile(getRegex());
+               Matcher matcher = pattern.matcher(value);
+               if (matcher.matches()) {
+                       return true;
+               } else {
+                       return false;
+               }
+               
+       }
+
+       public synchronized String getRegex() {
+               String regex = "(" + this.initialValue + ")";
+               return regex;
+       }
        
 }
index bf3c93126f0f393a09d18a38ed81591484d0389a..8e72661a22bd264db2c4c0d8aee7895b61bcb4a4 100644 (file)
@@ -41,6 +41,9 @@ package org.collectionspace.services.id;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
 
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 public class YearIDGenerator implements IDGenerator {
     
        private String initialValue = null;
@@ -57,7 +60,7 @@ public class YearIDGenerator implements IDGenerator {
        public YearIDGenerator(String initialValue) throws IllegalArgumentException {
 
                if ( initialValue == null || initialValue == "") {
-                       throw new IllegalArgumentException("Initial value must not be null or empty");
+                       throw new IllegalArgumentException("Initial ID value must not be null or empty");
                }
                
                // @TODO: Add regex-based validation here, by calling a
@@ -95,5 +98,28 @@ public class YearIDGenerator implements IDGenerator {
     int y = cal.get(Calendar.YEAR);
                return Integer.toString(y);
        }       
+
+       public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+
+               if ( value == null || value == "") {
+                       throw new IllegalArgumentException("ID to validate must not be null or empty");
+               }
+
+               Pattern pattern = Pattern.compile(getRegex());
+               Matcher matcher = pattern.matcher(value);
+               if (matcher.matches()) {
+                       return true;
+               } else {
+                       return false;
+               }
+               
+       }
+
+       public synchronized String getRegex() {
+               // NOTE: Currently hard-coded to accept only a range of
+               // four-digit Gregorian Calendar year dates.
+               String regex = "(\\d\\d\\d\\d)";
+               return regex;
+       }
        
 }
index 4c0af39a0da905bb7ec98ba30aa0a6ad185e486f..d0e40f697112d6744fe7121bda232a04bc5f6c31 100644 (file)
@@ -70,7 +70,41 @@ public class StringIDPartTest extends TestCase {
                }
 
        }
+
+       public void testIsValidID() {
+       
+               part = new StringIDPart("-");
+               assertTrue(part.isValidID("-"));
+
+               part = new StringIDPart("-");
+               assertFalse(part.isValidID("--"));
+
+               // Test chars with special meaning in regexes.
+               part = new StringIDPart(".");
+               assertTrue(part.isValidID("."));
+
+               part = new StringIDPart("TE");
+               assertTrue(part.isValidID("TE"));
+
+               part = new StringIDPart("TE");
+               assertFalse(part.isValidID("T"));
+
+               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.
+               }
+
+       }       
        // @TODO: Add more tests of boundary conditions, exceptions ...
  
 }
index 132eae4e983e6ce6731b383f9c108b7ec1eae0f8..2dee61c798eb593f42446f5901b392278b4c753b 100644 (file)
@@ -88,7 +88,34 @@ public class YearIDPartTest extends TestCase {
                }
 
        }
+
+       public void testIsValidID() {
+       
+               part = new YearIDPart();
+               assertTrue(part.isValidID("2009"));
+
+               part = new YearIDPart();
+               assertFalse(part.isValidID("839"));
+
+               part = new YearIDPart();
+               assertFalse(part.isValidID("10100"));
+               
+               part = new YearIDPart();
+               assertFalse(part.isValidID("non-numeric value"));
+               
+       }
+
+       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 ...
  
 }