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 static org.junit.Assert.fail;
31 import junit.framework.TestCase;
32 import org.collectionspace.services.id.NumericIDGeneratorPart;
33 import org.collectionspace.services.id.SequenceIDGeneratorPart;
37 * NumericIDGeneratorPartTest
39 * Test class for NumericIDGeneratorPart.
41 * $LastChangedRevision$
44 public class NumericIDGeneratorPartTest extends TestCase {
46 SequenceIDGeneratorPart part;
48 public void testInitialID() {
49 part = new NumericIDGeneratorPart("0");
50 assertEquals("0", part.getInitialID());
52 part = new NumericIDGeneratorPart("25");
53 assertEquals("25", part.getInitialID());
56 public void testNullInitialValue() {
58 part = new NumericIDGeneratorPart(null);
59 fail("Should have thrown IllegalArgumentException here");
60 } catch (IllegalArgumentException expected) {
61 // This Exception should be thrown, and thus the test should pass.
65 public void testNonLongParseableInitialValue() {
67 part = new NumericIDGeneratorPart("not a long parseable value");
68 fail("Should have thrown IllegalArgumentException here");
69 } catch (IllegalArgumentException expected) {
70 // This Exception should be thrown, and thus the test should pass.
74 public void testNonLongParseableMaxLength() {
76 part = new NumericIDGeneratorPart("1", "not an int parseable value");
77 fail("Should have thrown IllegalArgumentException here");
78 } catch (IllegalArgumentException expected) {
79 // This Exception should be thrown, and thus the test should pass.
83 public void testNegativeInitialValue() {
85 part = new NumericIDGeneratorPart("-1");
86 fail("Should have thrown IllegalArgumentException here");
87 } catch (IllegalArgumentException expected) {
88 // This Exception should be thrown, and thus the test should pass.
92 public void testGetCurrentID() {
93 part = new NumericIDGeneratorPart("0");
94 assertEquals("0", part.getCurrentID());
95 assertEquals("0", part.newID());
96 assertEquals("1", part.newID());
97 assertEquals("2", part.newID());
98 assertEquals("2", part.getCurrentID());
99 assertEquals("3", part.newID());
101 part = new NumericIDGeneratorPart("25");
102 assertEquals("25", part.getCurrentID());
105 public void testNewID() {
106 part = new NumericIDGeneratorPart("0");
107 assertEquals("0", part.newID());
108 assertEquals("1", part.newID());
109 assertEquals("2", part.newID());
110 assertEquals("3", part.newID());
112 part = new NumericIDGeneratorPart("25");
113 assertEquals("25", part.newID());
114 assertEquals("26", part.newID());
115 assertEquals("27", part.newID());
116 assertEquals("28", part.newID());
118 part = new NumericIDGeneratorPart();
119 assertEquals("1", part.newID());
120 assertEquals("2", part.newID());
121 assertEquals("3", part.newID());
124 public void testNewIDOverflow() {
127 part = new NumericIDGeneratorPart("997", "3");
128 assertEquals("997", part.newID());
129 assertEquals("998", part.newID());
130 assertEquals("999", part.newID());
131 assertEquals("1000", part.newID());
132 fail("Should have thrown IllegalStateException here");
133 } catch (IllegalStateException expected) {
134 // This Exception should be thrown, and thus the test should pass.
137 // Tests default MAX_LENGTH value of 6 decimal places
139 part = new NumericIDGeneratorPart("999997");
140 assertEquals("999997", part.newID());
141 assertEquals("999998", part.newID());
142 assertEquals("999999", part.newID());
143 assertEquals("1000000", part.newID());
144 fail("Should have thrown IllegalStateException here");
145 } catch (IllegalStateException expected) {
146 // This Exception should be thrown, and thus the test should pass.
151 public void testIsValidID() {
152 part = new NumericIDGeneratorPart("1");
153 assertTrue(part.isValidID("1"));
154 part = new NumericIDGeneratorPart("1");
155 assertTrue(part.isValidID("123"));
156 part = new NumericIDGeneratorPart("1");
157 assertTrue(part.isValidID("123456"));
159 part = new NumericIDGeneratorPart("1");
160 assertFalse(part.isValidID("1234567"));
161 part = new NumericIDGeneratorPart("1", "3");
162 assertTrue(part.isValidID("123"));
163 part = new NumericIDGeneratorPart("1", "3");
164 assertFalse(part.isValidID("1234"));
165 part = new NumericIDGeneratorPart("1");
166 assertFalse(part.isValidID("not a parseable long"));
167 part = new NumericIDGeneratorPart("1", "3");
168 assertFalse(part.isValidID("not a parseable long"));
169 part = new NumericIDGeneratorPart("1", "3");
170 assertFalse(part.isValidID(null));
171 part = new NumericIDGeneratorPart("1", "3");
172 assertFalse(part.isValidID(""));
175 // @TODO: Add more tests of boundary conditions, exceptions ...