<version>1.1.5</version>
</dependency>
- <dependency>
+ <!-- Cobertura, a tool for identifying the extent of code coverage in test classes -->
+ <dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.2</version>
</dependency>
- <dependency>
- <groupId>net.sourceforge.cobertura</groupId>
- <artifactId>cobertura</artifactId>
- <version>1.9</version>
- </dependency>
</dependencies>
</plugin>
</plugins>
</build>
-
- <reports>
- <report>maven-cobertura-plugin</report>
- </reports>
</project>
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
// Set the base path component for URLs that access this service.
@Path("/ids")
+
// Identify the default MIME media types consumed and produced by this service.
-@Consumes("application/xml")
-@Produces("application/xml")
+@Consumes(MediaType.APPLICATION_XML)
+@Produces(MediaType.APPLICATION_XML)
+
public class IDResource {
final Logger logger = LoggerFactory.getLogger(IDResource.class);
- // Richard's comment in the CollectionObject Resource class, from which
- // this class was derived: This should be a DI wired by a container like
- // Spring, Seam, or EJB3.
+ // Per Richard's comment in the CollectionObject Resource class, from which
+ // this class was derived: "This should be a DI wired by a container like
+ // Spring, Seam, or EJB3."
+
final static IDService service = new IDServiceJdbcImpl();
//////////////////////////////////////////////////////////////////////
@Produces("text/plain")
public Response getNextID(@PathParam("csid") String csid) {
- Response response = null;
+ verbose("> in getNextID");
+
+ // Unless the 'response' variable is explicitly initialized here, the
+ // compiler gives the error: "variable response might not have been initialized."
+ Response response = null;
+ response = response.ok().build();
String nextId = "";
try {
+ // Retrieve the next ID for the requested pattern,
+ // and return it in the entity body of the response.
nextId = service.nextID(csid);
+ if (nextId == null || nextId.equals("")) {
+ response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+ .entity("ID Service returned null or empty ID").type(MediaType.TEXT_PLAIN).build();
+ return response;
+ }
+
response = Response.status(Response.Status.OK)
- .entity(nextId).type("text/plain").build();
-
- // @TODO: Return our XML-based error results format.
+ .entity(nextId).type(MediaType.TEXT_PLAIN).build();
+
+ // @TODO: Return an XML-based error results format with the
+ // responses below.
// @TODO: An IllegalStateException often indicates an overflow
// of an IDPart. Consider whether returning a 400 Bad Request
- // status code is warranted, or whether returning some other
+ // status code is still warranted, or whether returning some other
// status would be more appropriate.
} catch (IllegalStateException ise) {
- response = Response.status(Response.Status.BAD_REQUEST)
- .entity(ise.getMessage()).type("text/plain").build();
-
+ response = Response.status(Response.Status.BAD_REQUEST)
+ .entity(ise.getMessage()).type(MediaType.TEXT_PLAIN).build();
+
} catch (IllegalArgumentException iae) {
response = Response.status(Response.Status.BAD_REQUEST)
- .entity(iae.getMessage()).type("text/plain").build();
-
+ .entity(iae.getMessage()).type(MediaType.TEXT_PLAIN).build();
+
+ // This is guard code that should never be reached.
} catch (Exception e) {
- response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
- .entity(e.getMessage()).type("text/plain").build();
+ response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+ .entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
}
- return response;
-
+ return response;
+
}
//////////////////////////////////////////////////////////////////////
package org.collectionspace.services;
import org.collectionspace.services.IDService;
+// The following import statement has been left open-ended
+// to accommodate future ID generation components.
import org.collectionspace.services.id.*;
public class IDServiceJdbcImpl implements IDService {
// a candidate ID.
public String nextID(String csid) throws
IllegalArgumentException, IllegalStateException {
-
+
IDPattern pattern;
String nextId = "";
+ if (csid == null || csid.equals("")) {
+ throw new IllegalArgumentException(
+ "Identifier for ID pattern must not be null or empty.");
+ }
+
try {
pattern = getIDPattern(csid);
} catch (IllegalArgumentException e ) {
throw e;
}
+ // Guard code - should not be needed.
+ if (pattern == null) {
+ throw new IllegalArgumentException(
+ "Pattern with ID " + "\'" + csid + "\'" + " could not be found.");
+ }
+
// Get the next ID associated with the pattern,
// setting its current ID to the just-generated ID.
//
// Returns an IDPattern.
public IDPattern getIDPattern(String csid) throws IllegalArgumentException {
-
- IDPattern pattern = new IDPattern();
+
+ IDPattern pattern;
// @TODO: Replace hard-coded IDs and related logic - currently
// used here for bootstrapping and initial testing - with actual
// Retrieve the pattern. (In this example, we're
// simply hard-coding its construction here.)
+ pattern = new IDPattern();
pattern.add(new StringIDPart("E"));
- pattern.add(new NumericIDPart());
+ pattern.add(new NumericIDPart("0"));
return pattern;
// Retrieve the pattern.(In this example, we're
// simply hard-coding its construction here.)
+ pattern = new IDPattern();
pattern.add(new YearIDPart());
pattern.add(new StringIDPart("."));
- pattern.add(new NumericIDPart());
+ pattern.add(new NumericIDPart("1"));
pattern.add(new StringIDPart("."));
- pattern.add(new NumericIDPart());
+ pattern.add(new NumericIDPart("0"));
return pattern;
}
}
-
+
}
return this.currentValue;
}
- public String getCurrentYear() {
+ public static String getCurrentYear() {
Calendar cal = GregorianCalendar.getInstance();
int y = cal.get(Calendar.YEAR);
return Integer.toString(y);
--- /dev/null
+/*
+ * IDServiceJdbcImplTest
+ *
+ * Test class for the ID Service's JDBC implementation class, IDServiceJdbcImpl.
+ *
+ * This document is a part of the source code and related artifacts
+ * for CollectionSpace, an open source collections management system
+ * for museums and related institutions:
+ *
+ * http://www.collectionspace.org
+ * http://wiki.collectionspace.org
+ *
+ * Copyright © 2009 Regents of the University of California
+ *
+ * Licensed under the Educational Community License (ECL), Version 2.0.
+ * You may not use this file except in compliance with this License.
+ *
+ * You may obtain a copy of the ECL 2.0 License at
+ * https://source.collectionspace.org/collection-space/LICENSE.txt
+ *
+ * $LastChangedBy: aron $
+ * $LastChangedRevision: 302 $
+ * $LastChangedDate$
+ */
+
+package org.collectionspace.services.test;
+
+import org.collectionspace.services.IDService;
+import org.collectionspace.services.IDServiceJdbcImpl;
+import org.collectionspace.services.id.YearIDGenerator;
+
+//import org.collectionspace.services.id.Id;
+//import org.collectionspace.services.id.IdList;
+//import org.collectionspace.services.id.IdPattern;
+//import org.collectionspace.services.id.IdPatternList;
+
+import junit.framework.TestCase;
+import static org.junit.Assert.*;
+
+public class IDServiceJdbcImplTest extends TestCase {
+
+ String currentYear = YearIDGenerator.getCurrentYear();
+ String nextId;
+ IDService service;
+
+ protected void setUp() {
+
+ nextId = "";
+ service = new IDServiceJdbcImpl();
+
+ }
+
+ public void testNextIDValidPattern() {
+
+ assertEquals("E1", service.nextID("1"));
+ assertEquals(currentYear + ".1.1", service.nextID("2"));
+
+ }
+
+ public void testNextIDNotValidPattern() {
+
+ try {
+ nextId = service.nextID("3");
+ fail("Should have thrown IllegalArgumentException here");
+ } catch (IllegalArgumentException expected) {
+ // This Exception should be thrown, and thus the test should pass.
+ }
+
+ }
+
+}
pattern.add(new StringIDPart("-"));
pattern.add(new AlphabeticIDPart("a"));
assertEquals("2009.1-a", pattern.getCurrentID());
+
+ pattern = new IDPattern();
+ pattern.add(new YearIDPart("2009"));
+ pattern.add(new StringIDPart("."));
+ pattern.add(new NumericIDPart("0"));
+ pattern.add(new StringIDPart("."));
+ pattern.add(new NumericIDPart("0"));
+ assertEquals("2009.0.0", pattern.getCurrentID());
}
pattern.add(new NumericIDPart("1005"));
assertEquals("T1006", pattern.nextID());
assertEquals("T1007", pattern.nextID());
+
+ pattern = new IDPattern();
+ pattern.add(new YearIDPart("2009"));
+ pattern.add(new StringIDPart("."));
+ pattern.add(new NumericIDPart("1"));
+ pattern.add(new StringIDPart("."));
+ pattern.add(new NumericIDPart("1"));
+ assertEquals("2009.1.2", pattern.nextID());
}
assertEquals("2009.3-c", pattern.nextID("2009.3-b"));
}
-
+
public void testEmptyPartsListCurrentID() {
pattern = new IDPattern();
assertEquals("26", part.nextID());
assertEquals("27", part.nextID());
assertEquals("28", part.nextID());
+
+ part = new NumericIDPart();
+ assertEquals("2", part.nextID());
}
IDPart part;
public void testNextID() {
+
+ part = new StringIDPart("E");
+ assertEquals("E", part.nextID());
+
part = new StringIDPart("XYZ");
- assertEquals("XYZ", part.nextID());
+ assertEquals("XYZ", part.nextID());
+
}
public void testresetID() {