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
22 package org.collectionspace.services.id;
24 import static org.junit.Assert.fail;
25 import junit.framework.TestCase;
28 * NumericIDGeneratorPartTest
30 * Test class for NumericIDGeneratorPart.
32 * $LastChangedRevision$
35 public class NumericIDGeneratorPartTest extends TestCase {
39 public void testNextID() {
41 part = new NumericIDGeneratorPart("0");
42 assertEquals("1", part.nextID());
43 assertEquals("2", part.nextID());
44 assertEquals("3", part.nextID());
46 part = new NumericIDGeneratorPart("25");
47 assertEquals("26", part.nextID());
48 assertEquals("27", part.nextID());
49 assertEquals("28", part.nextID());
51 part = new NumericIDGeneratorPart();
52 assertEquals("2", part.nextID());
56 public void testNextIDOverflow() {
59 part = new NumericIDGeneratorPart("997", "3");
60 assertEquals("998", part.nextID());
61 assertEquals("999", part.nextID());
62 assertEquals("1000", part.nextID());
63 fail("Should have thrown IllegalStateException here");
64 } catch (IllegalStateException expected) {
65 // This Exception should be thrown, and thus the test should pass.
68 // Tests default MAX_LENGTH value of 6 decimal places
70 part = new NumericIDGeneratorPart("999997");
71 assertEquals("999998", part.nextID());
72 assertEquals("999999", part.nextID());
73 assertEquals("1000000", part.nextID());
74 fail("Should have thrown IllegalStateException here");
75 } catch (IllegalStateException expected) {
76 // This Exception should be thrown, and thus the test should pass.
81 public void testResetID() {
83 part = new NumericIDGeneratorPart("25");
84 assertEquals("26", part.nextID());
85 assertEquals("27", part.nextID());
86 assertEquals("28", part.nextID());
88 assertEquals("26", part.nextID());
92 public void testInitialID() {
94 part = new NumericIDGeneratorPart("0");
95 assertEquals("0", part.getInitialID());
97 part = new NumericIDGeneratorPart("25");
98 assertEquals("25", part.getInitialID());
102 public void testCurrentID() {
104 part = new NumericIDGeneratorPart("0");
105 assertEquals("0", part.getCurrentID());
106 assertEquals("1", part.nextID());
107 assertEquals("2", part.nextID());
108 assertEquals("2", part.getCurrentID());
109 assertEquals("3", part.nextID());
111 part = new NumericIDGeneratorPart("25");
112 assertEquals("25", part.getCurrentID());
116 public void testNullInitialValue() {
119 part = new NumericIDGeneratorPart(null);
120 fail("Should have thrown IllegalArgumentException here");
121 } catch (IllegalArgumentException expected) {
122 // This Exception should be thrown, and thus the test should pass.
127 public void testNonLongParseableInitialValue() {
130 part = new NumericIDGeneratorPart("not a long parseable value");
131 fail("Should have thrown IllegalArgumentException here");
132 } catch (IllegalArgumentException expected) {
133 // This Exception should be thrown, and thus the test should pass.
138 public void testNonLongParseableMaxLength() {
141 part = new NumericIDGeneratorPart("1", "not an int parseable value");
142 fail("Should have thrown IllegalArgumentException here");
143 } catch (IllegalArgumentException expected) {
144 // This Exception should be thrown, and thus the test should pass.
149 public void testNegativeInitialValue() {
152 part = new NumericIDGeneratorPart("-1");
153 fail("Should have thrown IllegalArgumentException here");
154 } catch (IllegalArgumentException expected) {
155 // This Exception should be thrown, and thus the test should pass.
160 public void testIsValidID() {
162 part = new NumericIDGeneratorPart("1");
163 assertTrue(part.isValidID("1"));
165 part = new NumericIDGeneratorPart("1");
166 assertTrue(part.isValidID("123"));
168 part = new NumericIDGeneratorPart("1");
169 assertTrue(part.isValidID("123456"));
171 part = new NumericIDGeneratorPart("1");
172 assertFalse(part.isValidID("1234567"));
174 part = new NumericIDGeneratorPart("1", "3");
175 assertTrue(part.isValidID("123"));
177 part = new NumericIDGeneratorPart("1", "3");
178 assertFalse(part.isValidID("1234"));
180 part = new NumericIDGeneratorPart("1");
181 assertFalse(part.isValidID("not a parseable long"));
183 part = new NumericIDGeneratorPart("1", "3");
184 assertFalse(part.isValidID("not a parseable long"));
186 part = new NumericIDGeneratorPart("1", "3");
187 assertFalse(part.isValidID(null));
189 part = new NumericIDGeneratorPart("1", "3");
190 assertFalse(part.isValidID(""));
194 // @TODO: Add more tests of boundary conditions, exceptions ...