2 * NumericIDGeneratorPartTest
4 * Test class for NumericIDGeneratorPart.
6 * This document is a part of the source code and related artifacts
7 * for CollectionSpace, an open source collections management system
8 * for museums and related institutions:
10 * http://www.collectionspace.org
11 * http://wiki.collectionspace.org
13 * Copyright © 2009 Regents of the University of California
15 * Licensed under the Educational Community License (ECL), Version 2.0.
16 * You may not use this file except in compliance with this License.
18 * You may obtain a copy of the ECL 2.0 License at
19 * https://source.collectionspace.org/collection-space/LICENSE.txt
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
28 package org.collectionspace.services.id.test;
30 import org.collectionspace.services.common.repository.BadRequestException;
31 import static org.junit.Assert.fail;
32 import junit.framework.TestCase;
33 import org.collectionspace.services.id.NumericIDGeneratorPart;
34 import org.collectionspace.services.id.SequenceIDGeneratorPart;
38 * NumericIDGeneratorPartTest
40 * Test class for NumericIDGeneratorPart.
42 * $LastChangedRevision$
45 public class NumericIDGeneratorPartTest extends TestCase {
47 SequenceIDGeneratorPart part;
49 public void testInitialID() throws BadRequestException {
50 part = new NumericIDGeneratorPart("0");
51 assertEquals("0", part.getInitialID());
53 part = new NumericIDGeneratorPart("25");
54 assertEquals("25", part.getInitialID());
57 public void testNullInitialValue() {
59 part = new NumericIDGeneratorPart(null);
60 fail("Should have thrown BadRequestException here");
61 } catch (BadRequestException expected) {
62 // This Exception should be thrown, and thus the test should pass.
66 public void testNonLongParseableInitialValue() {
68 part = new NumericIDGeneratorPart("not a long parseable value");
69 fail("Should have thrown BadRequestException here");
70 } catch (BadRequestException expected) {
71 // This Exception should be thrown, and thus the test should pass.
75 public void testNonLongParseableMaxLength() {
77 part = new NumericIDGeneratorPart("1", "not an int parseable value");
78 fail("Should have thrown BadRequestException here");
79 } catch (BadRequestException expected) {
80 // This Exception should be thrown, and thus the test should pass.
84 public void testNegativeInitialValue() {
86 part = new NumericIDGeneratorPart("-1");
87 fail("Should have thrown BadRequestException here");
88 } catch (BadRequestException expected) {
89 // This Exception should be thrown, and thus the test should pass.
93 public void testGetCurrentID() throws BadRequestException {
94 part = new NumericIDGeneratorPart("0");
95 assertEquals("0", part.getCurrentID());
96 assertEquals("0", part.newID());
97 assertEquals("1", part.newID());
98 assertEquals("2", part.newID());
99 assertEquals("2", part.getCurrentID());
100 assertEquals("3", part.newID());
102 part = new NumericIDGeneratorPart("25");
103 assertEquals("25", part.getCurrentID());
106 public void testNewID() throws BadRequestException {
107 part = new NumericIDGeneratorPart("0");
108 assertEquals("0", part.newID());
109 assertEquals("1", part.newID());
110 assertEquals("2", part.newID());
111 assertEquals("3", part.newID());
113 part = new NumericIDGeneratorPart("25");
114 assertEquals("25", part.newID());
115 assertEquals("26", part.newID());
116 assertEquals("27", part.newID());
117 assertEquals("28", part.newID());
119 part = new NumericIDGeneratorPart();
120 assertEquals("1", part.newID());
121 assertEquals("2", part.newID());
122 assertEquals("3", part.newID());
125 public void testNewIDOverflow() throws BadRequestException {
128 part = new NumericIDGeneratorPart("997", "3");
129 assertEquals("997", part.newID());
130 assertEquals("998", part.newID());
131 assertEquals("999", part.newID());
132 assertEquals("1000", part.newID());
133 fail("Should have thrown IllegalStateException here");
134 } catch (IllegalStateException expected) {
135 // This Exception should be thrown, and thus the test should pass.
138 // Tests default MAX_LENGTH value of 6 decimal places
140 part = new NumericIDGeneratorPart("999997");
141 assertEquals("999997", part.newID());
142 assertEquals("999998", part.newID());
143 assertEquals("999999", part.newID());
144 assertEquals("1000000", part.newID());
145 fail("Should have thrown IllegalStateException here");
146 } catch (IllegalStateException expected) {
147 // This Exception should be thrown, and thus the test should pass.
152 public void testIsValidID() throws BadRequestException {
153 part = new NumericIDGeneratorPart("1");
154 assertTrue(part.isValidID("1"));
155 part = new NumericIDGeneratorPart("1");
156 assertTrue(part.isValidID("123"));
157 part = new NumericIDGeneratorPart("1");
158 assertTrue(part.isValidID("123456"));
160 part = new NumericIDGeneratorPart("1");
161 assertFalse(part.isValidID("1234567"));
162 part = new NumericIDGeneratorPart("1", "3");
163 assertTrue(part.isValidID("123"));
164 part = new NumericIDGeneratorPart("1", "3");
165 assertFalse(part.isValidID("1234"));
166 part = new NumericIDGeneratorPart("1");
167 assertFalse(part.isValidID("not a parseable long"));
168 part = new NumericIDGeneratorPart("1", "3");
169 assertFalse(part.isValidID("not a parseable long"));
170 part = new NumericIDGeneratorPart("1", "3");
171 assertFalse(part.isValidID(null));
172 part = new NumericIDGeneratorPart("1", "3");
173 assertFalse(part.isValidID(""));
176 // @TODO: Add more tests of boundary conditions, exceptions ...