From: Aron Roberts Date: Tue, 23 Jun 2009 02:07:47 +0000 (+0000) Subject: NOJIRA Added rudimentary validation methods to IDPart and to two IDPart Generators. X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=98bbc360ced611a1be3baea5042abaca4c0fd01d;p=tmp%2Fjakarta-migration.git NOJIRA Added rudimentary validation methods to IDPart and to two IDPart Generators. --- diff --git a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDGenerator.java b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDGenerator.java index 8fbea616d..ff52b9c6d 100644 --- a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDGenerator.java +++ b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDGenerator.java @@ -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; + } + } diff --git a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDPart.java b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDPart.java index ed55d83ed..9e961b3b9 100644 --- a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDPart.java +++ b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/AlphabeticIDPart.java @@ -29,5 +29,5 @@ public class AlphabeticIDPart extends IDPart { public AlphabeticIDPart(String startVal, String endVal, String baseVal) { super(new AlphabeticIDGenerator(startVal, endVal, baseVal)); }; - + } diff --git a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDGenerator.java b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDGenerator.java index e114592fa..1e5c35694 100644 --- a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDGenerator.java +++ b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDGenerator.java @@ -27,5 +27,9 @@ public interface IDGenerator { public String getCurrentID(); public String getNextID(); + + public boolean isValidID(String value); + + // public String getRegex(); } diff --git a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDPart.java b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDPart.java index 41c7d309e..3cc2016c7 100644 --- a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDPart.java +++ b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/IDPart.java @@ -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); + } } diff --git a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/NumericIDGenerator.java b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/NumericIDGenerator.java index 88dbfbca2..d1fece529 100644 --- a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/NumericIDGenerator.java +++ b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/NumericIDGenerator.java @@ -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; + } } diff --git a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/StringIDGenerator.java b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/StringIDGenerator.java index 42f16ce82..831ebb15a 100644 --- a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/StringIDGenerator.java +++ b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/StringIDGenerator.java @@ -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; + } } diff --git a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/YearIDGenerator.java b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/YearIDGenerator.java index bf3c93126..8e72661a2 100644 --- a/sandbox/aron/id/src/main/java/org/collectionspace/services/id/YearIDGenerator.java +++ b/sandbox/aron/id/src/main/java/org/collectionspace/services/id/YearIDGenerator.java @@ -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; + } } diff --git a/sandbox/aron/id/src/test/java/org/collectionspace/services/id/StringIDPartTest.java b/sandbox/aron/id/src/test/java/org/collectionspace/services/id/StringIDPartTest.java index 4c0af39a0..d0e40f697 100644 --- a/sandbox/aron/id/src/test/java/org/collectionspace/services/id/StringIDPartTest.java +++ b/sandbox/aron/id/src/test/java/org/collectionspace/services/id/StringIDPartTest.java @@ -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 ... } diff --git a/sandbox/aron/id/src/test/java/org/collectionspace/services/id/YearIDPartTest.java b/sandbox/aron/id/src/test/java/org/collectionspace/services/id/YearIDPartTest.java index 132eae4e9..2dee61c79 100644 --- a/sandbox/aron/id/src/test/java/org/collectionspace/services/id/YearIDPartTest.java +++ b/sandbox/aron/id/src/test/java/org/collectionspace/services/id/YearIDPartTest.java @@ -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 ... }