From cb7051a38c1498b9d804f6755b6a7a7a245363d3 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Thu, 5 Nov 2009 03:55:01 +0000 Subject: [PATCH] CSPACE-534: Replaced inappropriate references to IllegalArgumentExceptions with BadRequestExceptions throughout much of the ID service. Also minor, unrelated optimizations in YearIDGeneratorPart. --- .../id/AlphabeticIDGeneratorPart.java | 29 ++++++------ .../services/id/IDGeneratorPart.java | 4 +- .../services/id/IDGeneratorSerializer.java | 19 ++++---- .../services/id/IDServiceJdbcImpl.java | 10 ++-- .../services/id/NumericIDGeneratorPart.java | 30 ++++++------ .../services/id/SettableIDGenerator.java | 21 ++++++--- .../services/id/StringIDGeneratorPart.java | 9 ++-- .../services/id/UUIDGeneratorPart.java | 16 +++++-- .../services/id/YearIDGeneratorPart.java | 27 ++++++----- .../test/AlphabeticIDGeneratorPartTest.java | 47 ++++++++++--------- .../services/id/test/BaseIDGeneratorTest.java | 23 ++++----- .../id/test/IDGeneratorSerializerTest.java | 16 ++++--- .../id/test/IDServiceJdbcImplTest.java | 6 +-- .../id/test/NumericIDGeneratorPartTest.java | 27 ++++++----- .../id/test/StringIDGeneratorPartTest.java | 17 +++---- .../id/test/YearIDGeneratorPartTest.java | 23 ++++----- 16 files changed, 178 insertions(+), 146 deletions(-) diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/AlphabeticIDGeneratorPart.java b/services/id/service/src/main/java/org/collectionspace/services/id/AlphabeticIDGeneratorPart.java index 83a1aab6d..f23a30550 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/AlphabeticIDGeneratorPart.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/AlphabeticIDGeneratorPart.java @@ -53,10 +53,11 @@ package org.collectionspace.services.id; import java.util.Vector; - import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.collectionspace.services.common.repository.BadRequestException; + /** * AlphabeticIDGeneratorPart * @@ -90,7 +91,7 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { * * Additionally defaults to an initial value of "a". */ - public AlphabeticIDGeneratorPart() throws IllegalArgumentException { + public AlphabeticIDGeneratorPart() throws BadRequestException { this(DEFAULT_START_CHAR, DEFAULT_END_CHAR, DEFAULT_INITIAL_VALUE); } @@ -106,7 +107,7 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { * member of the valid alphabetic sequence. */ public AlphabeticIDGeneratorPart(String initial) - throws IllegalArgumentException { + throws BadRequestException { this(DEFAULT_START_CHAR, DEFAULT_END_CHAR, initial); } @@ -121,12 +122,12 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { * member of the valid aphabetic sequence. */ public AlphabeticIDGeneratorPart(String sequenceStart, String sequenceEnd, - String initial) throws IllegalArgumentException { + String initial) throws BadRequestException { // Validate and store the start character in the character sequence. if (sequenceStart == null || sequenceStart.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "Start character in the character sequence must not be " + "null or empty"); } @@ -136,7 +137,7 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { } else if (false) { // Handle representations of Unicode code points here } else { - throw new IllegalArgumentException( + throw new BadRequestException( "Start character must be one character in length"); // "Start character must be one character in length or // a Unicode value such as '\u0000'"); @@ -145,7 +146,7 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { // Validate and store the end character in the character sequence. if (sequenceEnd == null || sequenceEnd.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "End character in the character sequence must not be " + "null or empty"); } @@ -155,14 +156,14 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { } else if (false) { // Handle representations of Unicode code points here } else { - throw new IllegalArgumentException( + throw new BadRequestException( "End character must be one character in length"); // "End character must be one character in length or // a Unicode value such as '\u0000'"); } if (this.endChar <= this.startChar) { - throw new IllegalArgumentException( + throw new BadRequestException( "End (last) character in the character sequence must be " + "greater than the start character"); } @@ -170,7 +171,7 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { // Validate and store the initial value of this identifier. if (initial == null || initial.equals("")) { - throw new IllegalArgumentException("Initial value must not be " + + throw new BadRequestException("Initial value must not be " + "null or empty"); } @@ -193,7 +194,7 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { this.initialValue.add(new Character(ch)); // Otherwise, we've detected a character not in the sequence. } else { - throw new IllegalArgumentException( + throw new BadRequestException( "character " + "\'" + ch + "\'" + " is not valid"); } @@ -207,13 +208,13 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { } @Override - public void setCurrentID(String value) throws IllegalArgumentException { + public void setCurrentID(String value) throws BadRequestException { // @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("Initial value must not be " + + throw new BadRequestException("Initial value must not be " + "null or empty"); } @@ -237,7 +238,7 @@ public class AlphabeticIDGeneratorPart implements SequenceIDGeneratorPart { v.add(new Character(ch)); // Otherwise, we've detected a character not in the sequence. } else { - throw new IllegalArgumentException( + throw new BadRequestException( "character " + "\'" + ch + "\'" + " is not valid"); } diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDGeneratorPart.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDGeneratorPart.java index 4193c4eb1..901fbb01a 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDGeneratorPart.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDGeneratorPart.java @@ -29,6 +29,8 @@ */ package org.collectionspace.services.id; +import org.collectionspace.services.common.repository.BadRequestException; + public interface IDGeneratorPart { /** @@ -45,7 +47,7 @@ public interface IDGeneratorPart { * * @return The current value of the ID. */ - public void setCurrentID(String value) throws IllegalArgumentException; + public void setCurrentID(String value) throws BadRequestException; /** * Returns a new ID. diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDGeneratorSerializer.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDGeneratorSerializer.java index eb14b3008..d03faa992 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDGeneratorSerializer.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDGeneratorSerializer.java @@ -27,6 +27,9 @@ import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.XStreamException; import com.thoughtworks.xstream.io.xml.DomDriver; +import org.collectionspace.services.common.repository.BadRequestException; + + /** * IDGeneratorSerializer * @@ -53,13 +56,13 @@ public class IDGeneratorSerializer { * * @return A serialized representation of that ID generator. * - * @throws IllegalArgumentException if the ID generator cannot be serialized. + * @throws BadRequestException if the ID generator cannot be serialized. */ public static String serialize(SettableIDGenerator generator) - throws IllegalArgumentException { + throws BadRequestException { if (generator == null) { - throw new IllegalArgumentException("ID generator cannot be null."); + throw new BadRequestException("ID generator cannot be null."); } XStream xstream = new XStream(new DomDriver()); @@ -68,7 +71,7 @@ public class IDGeneratorSerializer { try { serializedGenerator = xstream.toXML(generator); } catch (XStreamException e) { - throw new IllegalArgumentException( + throw new BadRequestException( "Could not convert ID generator to XML for storage in database."); } @@ -85,13 +88,13 @@ public class IDGeneratorSerializer { * * @return The ID generator deserialized as a Java object. * - * @throws IllegalArgumentException if the ID generator cannot be deserialized. + * @throws BadRequestException if the ID generator cannot be deserialized. */ public static SettableIDGenerator deserialize(String serializedGenerator) - throws IllegalArgumentException { + throws BadRequestException { if (serializedGenerator == null || serializedGenerator.equals("")) { - throw new IllegalArgumentException("ID generator cannot be null or empty."); + throw new BadRequestException("ID generator cannot be null or empty."); } XStream xstream = new XStream(new DomDriver()); @@ -100,7 +103,7 @@ public class IDGeneratorSerializer { try { generator = (SettableIDGenerator) xstream.fromXML(serializedGenerator); } catch (XStreamException e) { - throw new IllegalArgumentException( + throw new BadRequestException( "Could not understand or parse this representation of an ID generator."); } diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java index d63e2d552..535552655 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java @@ -86,9 +86,7 @@ import java.sql.Statement; // May at some point instead use // org.jboss.resteasy.spi.NotFoundException -import java.util.ArrayList; import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; import org.collectionspace.services.common.repository.BadRequestException; import org.collectionspace.services.common.repository.DocumentNotFoundException; @@ -430,8 +428,8 @@ public class IDServiceJdbcImpl implements IDService { String serializedGenerator = ""; try { serializedGenerator = IDGeneratorSerializer.serialize(generator); - } catch (IllegalArgumentException e) { - throw new BadRequestException(e); + } catch (BadRequestException e) { + throw e; } try { @@ -726,8 +724,8 @@ public class IDServiceJdbcImpl implements IDService { String serializedGenerator = ""; try { serializedGenerator = IDGeneratorSerializer.serialize(generator); - } catch (IllegalArgumentException e) { - throw new BadRequestException(e); + } catch (BadRequestException e) { + throw e; } try { diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/NumericIDGeneratorPart.java b/services/id/service/src/main/java/org/collectionspace/services/id/NumericIDGeneratorPart.java index d5a1a6fd3..38621cbea 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/NumericIDGeneratorPart.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/NumericIDGeneratorPart.java @@ -30,6 +30,8 @@ package org.collectionspace.services.id; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.collectionspace.services.common.repository.BadRequestException; + /** * NumericIDGeneratorPart * @@ -52,7 +54,7 @@ public class NumericIDGeneratorPart implements SequenceIDGeneratorPart { /** * Constructor using defaults for initial value and maximum length. */ - public NumericIDGeneratorPart() throws IllegalArgumentException { + public NumericIDGeneratorPart() throws BadRequestException { this(Long.toString(DEFAULT_INITIAL_VALUE), Long.toString(DEFAULT_MAX_LENGTH)); } @@ -63,7 +65,7 @@ public class NumericIDGeneratorPart implements SequenceIDGeneratorPart { * @param initialValue The initial value of the numeric ID. */ public NumericIDGeneratorPart(String initialValue) - throws IllegalArgumentException { + throws BadRequestException { this(initialValue, Long.toString(DEFAULT_MAX_LENGTH)); } @@ -75,27 +77,27 @@ public class NumericIDGeneratorPart implements SequenceIDGeneratorPart { * @param maxLength The maximum String length for generated IDs. */ public NumericIDGeneratorPart(String initialValue, String maxLength) - throws IllegalArgumentException { + throws BadRequestException { if (maxLength == null || maxLength.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "Initial ID value must not be null or empty"); } try { this.maxLength = Long.parseLong(maxLength); } catch (NumberFormatException e) { - throw new IllegalArgumentException( + throw new BadRequestException( "Maximum ID length must be parseable as a number"); } if (initialValue == null || initialValue.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "Initial ID value must not be null or empty"); } try { long initVal = Long.parseLong(initialValue.trim()); if ( initVal < 0 ) { - throw new IllegalArgumentException( + throw new BadRequestException( "Initial ID value should be zero (0) or greater"); } String initValStr = Long.toString(initVal); @@ -106,10 +108,10 @@ public class NumericIDGeneratorPart implements SequenceIDGeneratorPart { } this.initialValue = initVal; } catch (NullPointerException e) { - throw new IllegalArgumentException( + throw new BadRequestException( "Initial ID value should not be null"); } catch (NumberFormatException e) { - throw new IllegalArgumentException( + throw new BadRequestException( "Initial ID value must be parseable as a number"); } @@ -121,20 +123,20 @@ public class NumericIDGeneratorPart implements SequenceIDGeneratorPart { } @Override - public void setCurrentID(String value) throws IllegalArgumentException { + public void setCurrentID(String value) throws BadRequestException { // @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( + throw new BadRequestException( "ID value must not be null or empty"); } try { long currVal = Long.parseLong(value.trim()); if ( currVal < 0 ) { - throw new IllegalArgumentException( + throw new BadRequestException( "ID value should be zero (0) or greater"); } String currValStr = Long.toString(currVal); @@ -145,10 +147,10 @@ public class NumericIDGeneratorPart implements SequenceIDGeneratorPart { } this.currentValue = currVal; } catch (NullPointerException e) { - throw new IllegalArgumentException( + throw new BadRequestException( "ID value should not be null"); } catch (NumberFormatException e) { - throw new IllegalArgumentException( + throw new BadRequestException( "ID value must be parseable as a number"); } } diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/SettableIDGenerator.java b/services/id/service/src/main/java/org/collectionspace/services/id/SettableIDGenerator.java index bf70510d7..05bd355f4 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/SettableIDGenerator.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/SettableIDGenerator.java @@ -29,8 +29,11 @@ package org.collectionspace.services.id; import java.util.Vector; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.collectionspace.services.common.repository.BadRequestException; /** * SettableIDGenerator @@ -91,7 +94,7 @@ public class SettableIDGenerator extends BaseIDGenerator { * @return The current value of this ID. */ public String getCurrentID(String value) - throws IllegalArgumentException { + throws BadRequestException { if (value == null) return value; @@ -125,7 +128,7 @@ public class SettableIDGenerator extends BaseIDGenerator { // If the supplied ID doesn't partly match the pattern, // throw an Exception. if (matchedParts == 0) { - throw new IllegalArgumentException("Supplied ID does not match " + + throw new BadRequestException("Supplied ID does not match " + "this ID pattern."); } @@ -136,7 +139,7 @@ public class SettableIDGenerator extends BaseIDGenerator { // throw an Exception. (This error condition should likely // never be reached, but it's here as a guard.) if (! matcher.matches()) { - throw new IllegalArgumentException("Supplied ID does not match " + + throw new BadRequestException("Supplied ID does not match " + "this ID pattern."); } @@ -146,7 +149,11 @@ public class SettableIDGenerator extends BaseIDGenerator { IDGeneratorPart currentPart; for (int i = 1; i <= matchedParts; i++) { currentPart = this.parts.get(i - 1); - currentPart.setCurrentID(matcher.group(i)); + try { + currentPart.setCurrentID(matcher.group(i)); + } catch (BadRequestException ex) { + throw new BadRequestException("Error setting current ID in ID part."); + } } // Obtain the initial value of the next IDGeneratorPart, and @@ -189,10 +196,10 @@ public class SettableIDGenerator extends BaseIDGenerator { * @return An new ID. */ public String newID(String value) - throws IllegalStateException, IllegalArgumentException { + throws IllegalStateException, BadRequestException { if (value == null) { - throw new IllegalArgumentException("Supplied ID cannot be null."); + throw new BadRequestException("Supplied ID cannot be null."); } Pattern pattern = Pattern.compile(getRegex()); @@ -201,7 +208,7 @@ public class SettableIDGenerator extends BaseIDGenerator { // If the supplied ID doesn't entirely match the pattern, // throw an Exception. if (! matcher.matches()) { - throw new IllegalArgumentException( + throw new BadRequestException( "Supplied ID does not match this ID pattern."); } diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/StringIDGeneratorPart.java b/services/id/service/src/main/java/org/collectionspace/services/id/StringIDGeneratorPart.java index c7abc67ed..fc2e5179d 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/StringIDGeneratorPart.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/StringIDGeneratorPart.java @@ -30,6 +30,7 @@ package org.collectionspace.services.id; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import org.collectionspace.services.common.repository.BadRequestException; /** * StringIDGeneratorPart @@ -46,10 +47,10 @@ public class StringIDGeneratorPart implements IDGeneratorPart, private String currentValue = null; public StringIDGeneratorPart(String initialValue) - throws IllegalArgumentException { + throws BadRequestException { if (initialValue == null || initialValue.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "Initial ID value must not be null or empty"); } @@ -68,9 +69,9 @@ public class StringIDGeneratorPart implements IDGeneratorPart, return this.currentValue; } - public void setCurrentID(String value) throws IllegalArgumentException { + public void setCurrentID(String value) throws BadRequestException { if (value == null || value.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "ID value must not be null or empty"); } this.currentValue = value; diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/UUIDGeneratorPart.java b/services/id/service/src/main/java/org/collectionspace/services/id/UUIDGeneratorPart.java index 83ec2c7a2..80a3f94fa 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/UUIDGeneratorPart.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/UUIDGeneratorPart.java @@ -24,8 +24,11 @@ package org.collectionspace.services.id; import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.collectionspace.services.common.repository.BadRequestException; /** * UUIDGeneratorPart @@ -65,13 +68,13 @@ public class UUIDGeneratorPart implements IDGeneratorPart { } @Override - public void setCurrentID(String idValue) throws IllegalArgumentException { + public void setCurrentID(String idValue) throws BadRequestException { if (idValue == null || idValue.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "Supplied UUID must not be null or empty"); } if (! isValidID(idValue)) { - throw new IllegalArgumentException( + throw new BadRequestException( "Supplied UUID '" + idValue + "' is not valid."); } this.currentValue = idValue; @@ -89,7 +92,12 @@ public class UUIDGeneratorPart implements IDGeneratorPart { @Override public String newID() { String newID = UUID.randomUUID().toString(); - setCurrentID(newID); + try { + setCurrentID(newID); + } catch (BadRequestException ex) { + // Do nothing. There will be no adverse consequences if we + // can't set the current ID here. + } return newID; } diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/YearIDGeneratorPart.java b/services/id/service/src/main/java/org/collectionspace/services/id/YearIDGeneratorPart.java index f5ec3d036..d036e490a 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/YearIDGeneratorPart.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/YearIDGeneratorPart.java @@ -43,10 +43,13 @@ package org.collectionspace.services.id; import java.util.Calendar; import java.util.GregorianCalendar; - +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.collectionspace.services.common.repository.BadRequestException; + /** * YearIDGeneratorPart * @@ -64,25 +67,23 @@ public class YearIDGeneratorPart implements IDGeneratorPart { // four-digit Gregorian Calendar year dates. final static String REGEX_PATTERN = "(\\d{4})"; - public YearIDGeneratorPart() throws IllegalArgumentException { - this.currentValue = getCurrentYear(); + public YearIDGeneratorPart() { } - public YearIDGeneratorPart(String yearValue) - throws IllegalArgumentException { + public YearIDGeneratorPart(String yearValue) throws BadRequestException { setCurrentID(yearValue); } @Override - public void setCurrentID(String yearValue) throws IllegalArgumentException { + public void setCurrentID(String yearValue) throws BadRequestException { if (yearValue == null || yearValue.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "Supplied year must not be null or empty"); } // NOTE: Validation currently is based on a // hard coded year format and calendar system. if (! isValidID(yearValue)) { - throw new IllegalArgumentException( + throw new BadRequestException( "Supplied year '" + yearValue + "' is not valid."); } this.currentValue = yearValue; @@ -90,15 +91,17 @@ public class YearIDGeneratorPart implements IDGeneratorPart { @Override public String getCurrentID() { - if (this.currentValue == null || this.currentValue.equals("")) { - setCurrentID(getCurrentYear()); + String currentYear = ""; + if (this.currentValue == null || this.currentValue.trim().isEmpty()) { + currentYear = getCurrentYear(); + } else { + currentYear = this.currentValue; } - return this.currentValue; + return currentYear; } @Override public String newID() { - setCurrentID(getCurrentYear()); return getCurrentID(); } diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/AlphabeticIDGeneratorPartTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/AlphabeticIDGeneratorPartTest.java index 9fffb9d70..594f91817 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/AlphabeticIDGeneratorPartTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/AlphabeticIDGeneratorPartTest.java @@ -23,6 +23,7 @@ package org.collectionspace.services.id.test; +import org.collectionspace.services.common.repository.BadRequestException; import org.collectionspace.services.id.AlphabeticIDGeneratorPart; import org.collectionspace.services.id.SequenceIDGeneratorPart; @@ -41,7 +42,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { SequenceIDGeneratorPart part; - public void testNextIDLowercase() { + public void testNextIDLowercase() throws BadRequestException { part = new AlphabeticIDGeneratorPart("a"); assertEquals("a", part.newID()); @@ -55,7 +56,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testnewIDLowercase2Chars() { + public void testnewIDLowercase2Chars() throws BadRequestException { part = new AlphabeticIDGeneratorPart("aa"); assertEquals("aa", part.newID()); @@ -69,7 +70,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testnewIDLowercase2CharsRolloverFirst() { + public void testnewIDLowercase2CharsRolloverFirst() throws BadRequestException { part = new AlphabeticIDGeneratorPart("ay"); assertEquals("ay", part.newID()); @@ -79,7 +80,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testnewIDUppercase() { + public void testnewIDUppercase() throws BadRequestException { part = new AlphabeticIDGeneratorPart("A", "Z", "A"); assertEquals("A", part.newID()); @@ -93,7 +94,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testnewIDUppercase2Chars() { + public void testnewIDUppercase2Chars() throws BadRequestException { part = new AlphabeticIDGeneratorPart("A", "Z", "AA"); assertEquals("AA", part.newID()); @@ -107,7 +108,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testnewIDUppercase2CharsRolloverFirst() { + public void testnewIDUppercase2CharsRolloverFirst() throws BadRequestException { part = new AlphabeticIDGeneratorPart("A", "Z", "AY"); assertEquals("AY", part.newID()); @@ -117,21 +118,21 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testInitialLowercase() { + public void testInitialLowercase() throws BadRequestException { part = new AlphabeticIDGeneratorPart("aaa"); assertEquals("aaa", part.getInitialID()); } - public void testInitialUppercase() { + public void testInitialUppercase() throws BadRequestException { part = new AlphabeticIDGeneratorPart("A", "Z", "AZ"); assertEquals("AZ", part.getInitialID()); } - public void testCurrentLowercase() { + public void testCurrentLowercase() throws BadRequestException { part = new AlphabeticIDGeneratorPart("aaa"); assertEquals("aaa", part.getCurrentID()); @@ -143,7 +144,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testCurrentUppercase() { + public void testCurrentUppercase() throws BadRequestException { part = new AlphabeticIDGeneratorPart("A", "Z", "A"); assertEquals("A", part.getCurrentID()); @@ -155,7 +156,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testOverflowLowercase() { + public void testOverflowLowercase() throws BadRequestException { part = new AlphabeticIDGeneratorPart("zx"); assertEquals("zx", part.newID()); @@ -165,7 +166,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testOverflowUppercase() { + public void testOverflowUppercase() throws BadRequestException { part = new AlphabeticIDGeneratorPart("A", "Z", "X"); assertEquals("X", part.newID()); @@ -178,8 +179,8 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { public void testNonAlphabeticInitialValue() { try { part = new AlphabeticIDGeneratorPart("&*432"); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } @@ -187,8 +188,8 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { public void testNullInitialValue() { try { part = new AlphabeticIDGeneratorPart(null); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } @@ -196,8 +197,8 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { public void testEmptyStringInitialValue() { try { part = new AlphabeticIDGeneratorPart(""); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } @@ -205,13 +206,13 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { public void testAllSpaceCharsInitialValue() { try { part = new AlphabeticIDGeneratorPart(" "); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } - public void testIsValidIDDefaultSeries() { + public void testIsValidIDDefaultSeries() throws BadRequestException { part = new AlphabeticIDGeneratorPart(); @@ -223,7 +224,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testIsValidIDConstrainedLowerCaseSeries() { + public void testIsValidIDConstrainedLowerCaseSeries() throws BadRequestException { part = new AlphabeticIDGeneratorPart("a", "f", "a"); @@ -238,7 +239,7 @@ public class AlphabeticIDGeneratorPartTest extends TestCase { } - public void testIsValidIDConstrainedUppercaseSeries() { + public void testIsValidIDConstrainedUppercaseSeries() throws BadRequestException { part = new AlphabeticIDGeneratorPart("A", "F", "A"); diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/BaseIDGeneratorTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/BaseIDGeneratorTest.java index 9383955fc..7e218d492 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/BaseIDGeneratorTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/BaseIDGeneratorTest.java @@ -25,6 +25,7 @@ package org.collectionspace.services.id.test; import java.util.Vector; import junit.framework.TestCase; +import org.collectionspace.services.common.repository.BadRequestException; import org.collectionspace.services.id.*; /** @@ -40,7 +41,7 @@ public class BaseIDGeneratorTest extends TestCase { final static String CURRENT_YEAR = YearIDGeneratorPart.getCurrentYear(); - // Note: tests may fail with IllegalArgumentException + // Note: tests may fail with BadRequestException // if any initialization of new IDParts fails // due to invalid arguments passed to their constructors. @@ -56,7 +57,7 @@ public class BaseIDGeneratorTest extends TestCase { // Test adding parts and retrieving current IDs - public void testCurrentIDViaVector() { + public void testCurrentIDViaVector() throws BadRequestException { Vector parts = new Vector(); BaseIDGenerator gen; @@ -98,7 +99,7 @@ public class BaseIDGeneratorTest extends TestCase { assertEquals("2009.1-a", gen.getCurrentID()); } - public void testCurrentIDViaAdd() { + public void testCurrentIDViaAdd() throws BadRequestException { generator.clear(); generator.add(new YearIDGeneratorPart("2009")); generator.add(new StringIDGeneratorPart(".")); @@ -118,7 +119,7 @@ public class BaseIDGeneratorTest extends TestCase { // Test generating new IDs from a single part - public void testNewAlphabeticLowercaseID() { + public void testNewAlphabeticLowercaseID() throws BadRequestException { generator.clear(); generator.add(new AlphabeticIDGeneratorPart("a")); assertEquals("a", generator.newID()); @@ -145,7 +146,7 @@ public class BaseIDGeneratorTest extends TestCase { assertEquals("aza", generator.newID()); } - public void testNewAlphabeticUppercaseID() { + public void testNewAlphabeticUppercaseID() throws BadRequestException { generator.clear(); generator.add(new AlphabeticIDGeneratorPart("A", "Z", "A")); assertEquals("A", generator.newID()); @@ -172,7 +173,7 @@ public class BaseIDGeneratorTest extends TestCase { assertEquals("AZA", generator.newID()); } - public void testNewNumericID() { + public void testNewNumericID() throws BadRequestException { generator.clear(); generator.add(new NumericIDGeneratorPart("1")); assertEquals("1", generator.newID()); @@ -180,7 +181,7 @@ public class BaseIDGeneratorTest extends TestCase { assertEquals("3", generator.newID()); } - public void testNewStringID() { + public void testNewStringID() throws BadRequestException { generator.clear(); generator.add(new StringIDGeneratorPart("PREFIX")); assertEquals("PREFIX", generator.newID()); @@ -216,7 +217,7 @@ public class BaseIDGeneratorTest extends TestCase { // Test generating new IDs from multiple, mixed parts - public void testNewMixedID() { + public void testNewMixedID() throws BadRequestException { generator.clear(); generator.add(new YearIDGeneratorPart()); generator.add(new StringIDGeneratorPart(".")); @@ -253,7 +254,7 @@ public class BaseIDGeneratorTest extends TestCase { assertEquals(CURRENT_YEAR + ".1.3", generator.newID()); } - public void testNewMixedIDWithTrailingConstantStringID() { + public void testNewMixedIDWithTrailingConstantStringID() throws BadRequestException { generator.clear(); generator.add(new YearIDGeneratorPart()); generator.add(new StringIDGeneratorPart(".")); @@ -288,7 +289,7 @@ public class BaseIDGeneratorTest extends TestCase { assertFalse(generator.isValidID("10000")); } - public void testValidMixedID() { + public void testValidMixedID() throws BadRequestException { generator.clear(); generator.add(new YearIDGeneratorPart("2009")); generator.add(new StringIDGeneratorPart(".")); @@ -309,7 +310,7 @@ public class BaseIDGeneratorTest extends TestCase { assertFalse(generator.isValidID("2009ZZ-AND-1")); } - public void testGetRegex() { + public void testGetRegex() throws BadRequestException { generator.clear(); generator.add(new YearIDGeneratorPart("2009")); generator.add(new StringIDGeneratorPart(".")); diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/IDGeneratorSerializerTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/IDGeneratorSerializerTest.java index 1231518ed..156d36894 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/IDGeneratorSerializerTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/IDGeneratorSerializerTest.java @@ -25,6 +25,8 @@ package org.collectionspace.services.id.test; import org.collectionspace.services.id.*; +import org.collectionspace.services.common.repository.BadRequestException; + import junit.framework.TestCase; import static org.junit.Assert.*; @@ -51,7 +53,7 @@ public class IDGeneratorSerializerTest extends TestCase { // @TODO We may want to canonicalize (or otherwise normalize) the // expected and actual XML in these tests, to avoid failures resulting // from differences in whitespace, etc. - public void testSerializeIDGenerator() { + public void testSerializeIDGenerator() throws BadRequestException { SettableIDGenerator tempGenerator = new SettableIDGenerator(); assertEquals(DEFAULT_SERIALIZED_ID_GENERATOR, IDGeneratorSerializer.serialize(tempGenerator)); @@ -60,8 +62,8 @@ public class IDGeneratorSerializerTest extends TestCase { public void testSerializeNullIDGenerator() { try { String serializedPattern = IDGeneratorSerializer.serialize(null); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } @@ -80,8 +82,8 @@ public class IDGeneratorSerializerTest extends TestCase { try { SettableIDGenerator tempGenerator = IDGeneratorSerializer.deserialize(null); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } @@ -90,8 +92,8 @@ public class IDGeneratorSerializerTest extends TestCase { try { IDGenerator tempGenerator = IDGeneratorSerializer.deserialize(""); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java index 6d9ce14e3..b3f7e3c57 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java @@ -80,7 +80,7 @@ public class IDServiceJdbcImplTest { @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator"}) public void readIDGenerator() throws DocumentNotFoundException, - IllegalArgumentException, IllegalStateException { + BadRequestException, IllegalStateException { instance = jdbc.readIDGenerator(DEFAULT_CSID); Assert.assertTrue(instance != null); @@ -204,7 +204,7 @@ public class IDServiceJdbcImplTest { // @TODO Read test patterns from external configuration. - public String getSpectrumEntryNumberGenerator() { + public String getSpectrumEntryNumberGenerator() throws BadRequestException { generator = new SettableIDGenerator(); generator.add(new StringIDGeneratorPart("E")); @@ -214,7 +214,7 @@ public class IDServiceJdbcImplTest { } - public String getChinAccessionNumberGenerator() { + public String getChinAccessionNumberGenerator() throws BadRequestException { generator = new SettableIDGenerator(); generator.add(new YearIDGeneratorPart()); diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/NumericIDGeneratorPartTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/NumericIDGeneratorPartTest.java index caa2ab9ba..c21bbf9a3 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/NumericIDGeneratorPartTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/NumericIDGeneratorPartTest.java @@ -27,6 +27,7 @@ package org.collectionspace.services.id.test; +import org.collectionspace.services.common.repository.BadRequestException; import static org.junit.Assert.fail; import junit.framework.TestCase; import org.collectionspace.services.id.NumericIDGeneratorPart; @@ -45,7 +46,7 @@ public class NumericIDGeneratorPartTest extends TestCase { SequenceIDGeneratorPart part; - public void testInitialID() { + public void testInitialID() throws BadRequestException { part = new NumericIDGeneratorPart("0"); assertEquals("0", part.getInitialID()); @@ -56,8 +57,8 @@ public class NumericIDGeneratorPartTest extends TestCase { public void testNullInitialValue() { try { part = new NumericIDGeneratorPart(null); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } @@ -65,8 +66,8 @@ public class NumericIDGeneratorPartTest extends TestCase { public void testNonLongParseableInitialValue() { try { part = new NumericIDGeneratorPart("not a long parseable value"); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } @@ -74,8 +75,8 @@ public class NumericIDGeneratorPartTest extends TestCase { public void testNonLongParseableMaxLength() { try { part = new NumericIDGeneratorPart("1", "not an int parseable value"); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } @@ -83,13 +84,13 @@ public class NumericIDGeneratorPartTest extends TestCase { public void testNegativeInitialValue() { try { part = new NumericIDGeneratorPart("-1"); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } - public void testGetCurrentID() { + public void testGetCurrentID() throws BadRequestException { part = new NumericIDGeneratorPart("0"); assertEquals("0", part.getCurrentID()); assertEquals("0", part.newID()); @@ -102,7 +103,7 @@ public class NumericIDGeneratorPartTest extends TestCase { assertEquals("25", part.getCurrentID()); } - public void testNewID() { + public void testNewID() throws BadRequestException { part = new NumericIDGeneratorPart("0"); assertEquals("0", part.newID()); assertEquals("1", part.newID()); @@ -121,7 +122,7 @@ public class NumericIDGeneratorPartTest extends TestCase { assertEquals("3", part.newID()); } - public void testNewIDOverflow() { + public void testNewIDOverflow() throws BadRequestException { try { part = new NumericIDGeneratorPart("997", "3"); @@ -148,7 +149,7 @@ public class NumericIDGeneratorPartTest extends TestCase { } - public void testIsValidID() { + public void testIsValidID() throws BadRequestException { part = new NumericIDGeneratorPart("1"); assertTrue(part.isValidID("1")); part = new NumericIDGeneratorPart("1"); diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/StringIDGeneratorPartTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/StringIDGeneratorPartTest.java index 5cdbe788b..9a14be2c4 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/StringIDGeneratorPartTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/StringIDGeneratorPartTest.java @@ -24,6 +24,7 @@ package org.collectionspace.services.id.test; import junit.framework.TestCase; +import org.collectionspace.services.common.repository.BadRequestException; import org.collectionspace.services.id.StoredValueIDGeneratorPart; import org.collectionspace.services.id.StringIDGeneratorPart; @@ -42,8 +43,8 @@ public class StringIDGeneratorPartTest extends TestCase { public void testNullInitialValue() { try { part = new StringIDGeneratorPart(null); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } @@ -51,30 +52,30 @@ public class StringIDGeneratorPartTest extends TestCase { public void testEmptyInitialValue() { try { part = new StringIDGeneratorPart(""); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } } - public void testGetInitialID() { + public void testGetInitialID() throws BadRequestException { part = new StringIDGeneratorPart("-"); assertEquals("-", part.getInitialID()); } - public void testGetCurrentID() { + public void testGetCurrentID() throws BadRequestException { part = new StringIDGeneratorPart("- -"); assertEquals("- -", part.getCurrentID()); } - public void testNewID() { + public void testNewID() throws BadRequestException { part = new StringIDGeneratorPart("E"); assertEquals("E", part.newID()); part = new StringIDGeneratorPart("XYZ"); assertEquals("XYZ", part.newID()); } - public void testIsValidID() { + public void testIsValidID() throws BadRequestException, BadRequestException { part = new StringIDGeneratorPart("-"); assertTrue(part.isValidID("-")); part = new StringIDGeneratorPart("TE"); diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/YearIDGeneratorPartTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/YearIDGeneratorPartTest.java index 40b03cb17..3073dc667 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/YearIDGeneratorPartTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/YearIDGeneratorPartTest.java @@ -23,6 +23,7 @@ package org.collectionspace.services.id.test; +import org.collectionspace.services.common.repository.BadRequestException; import static org.junit.Assert.fail; import java.util.Calendar; import java.util.GregorianCalendar; @@ -50,7 +51,7 @@ public class YearIDGeneratorPartTest extends TestCase { } - public void testCurrentID() { + public void testCurrentID() throws BadRequestException { part = new YearIDGeneratorPart(); assertEquals(getCurrentYear(), part.getCurrentID()); @@ -60,7 +61,7 @@ public class YearIDGeneratorPartTest extends TestCase { } - public void testSetCurrentID() { + public void testSetCurrentID() throws BadRequestException { part = new YearIDGeneratorPart("1999"); part.setCurrentID("1999"); @@ -76,15 +77,15 @@ public class YearIDGeneratorPartTest extends TestCase { try { part.setCurrentID(null); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } try { part.setCurrentID(""); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } @@ -126,12 +127,12 @@ public class YearIDGeneratorPartTest extends TestCase { } */ - public void testNullInitialValue() { + public void testNullInitialValue() throws BadRequestException { try { part = new YearIDGeneratorPart(null); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } @@ -141,8 +142,8 @@ public class YearIDGeneratorPartTest extends TestCase { try { part = new YearIDGeneratorPart(""); - fail("Should have thrown IllegalArgumentException here"); - } catch (IllegalArgumentException expected) { + fail("Should have thrown BadRequestException here"); + } catch (BadRequestException expected) { // This Exception should be thrown, and thus the test should pass. } -- 2.47.3