]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9ecb44c92ed477a83536ebf5d0d88d59a4447bdd
[tmp/jakarta-migration.git] /
1 /*    
2  * NumericIDGeneratorPartTest
3  *
4  * Test class for NumericIDGeneratorPart.
5  *
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:
9  *
10  * http://www.collectionspace.org
11  * http://wiki.collectionspace.org
12  *
13  * Copyright © 2009 Regents of the University of California
14  *
15  * Licensed under the Educational Community License (ECL), Version 2.0.
16  * You may not use this file except in compliance with this License.
17  *
18  * You may obtain a copy of the ECL 2.0 License at
19  * https://source.collectionspace.org/collection-space/LICENSE.txt
20  */
21
22 package org.collectionspace.services.id;
23
24 import static org.junit.Assert.fail;
25 import junit.framework.TestCase;
26
27 /**
28  * NumericIDGeneratorPartTest
29  *
30  * Test class for NumericIDGeneratorPart.
31  *
32  * $LastChangedRevision$
33  * $LastChangedDate$
34  */
35 public class NumericIDGeneratorPartTest extends TestCase {
36
37     IDGeneratorPart part;
38
39     public void testNextID() {
40
41         part = new NumericIDGeneratorPart("0");        
42         assertEquals("1", part.nextID());
43         assertEquals("2", part.nextID());
44         assertEquals("3", part.nextID());
45     
46         part = new NumericIDGeneratorPart("25");
47         assertEquals("26", part.nextID());
48         assertEquals("27", part.nextID());
49         assertEquals("28", part.nextID());
50         
51         part = new NumericIDGeneratorPart();
52         assertEquals("2", part.nextID());
53             
54     }
55
56     public void testNextIDOverflow() {
57
58         try {
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.
66         }
67
68         // Tests default MAX_LENGTH value of 6 decimal places
69         try {
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.
77         }
78             
79     }
80
81     public void testResetID() {
82     
83         part = new NumericIDGeneratorPart("25");
84         assertEquals("26", part.nextID());
85         assertEquals("27", part.nextID());
86         assertEquals("28", part.nextID());
87         part.resetID();
88         assertEquals("26", part.nextID());
89             
90     }
91
92     public void testInitialID() {
93
94         part = new NumericIDGeneratorPart("0");
95         assertEquals("0", part.getInitialID());
96
97         part = new NumericIDGeneratorPart("25");
98         assertEquals("25", part.getInitialID());
99         
100     }
101
102     public void testCurrentID() {
103
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());
110
111         part = new NumericIDGeneratorPart("25");
112         assertEquals("25", part.getCurrentID());
113         
114     }
115     
116     public void testNullInitialValue() {
117     
118         try {
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.
123         }
124         
125     }
126
127     public void testNonLongParseableInitialValue() {
128     
129         try {
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.
134         }
135
136     }
137
138     public void testNonLongParseableMaxLength() {
139     
140         try {
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.
145         }
146
147     }
148
149     public void testNegativeInitialValue() {
150
151         try {
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.
156         }
157     }
158
159
160     public void testIsValidID() {
161     
162         part = new NumericIDGeneratorPart("1");
163         assertTrue(part.isValidID("1"));
164
165         part = new NumericIDGeneratorPart("1");
166         assertTrue(part.isValidID("123"));
167
168         part = new NumericIDGeneratorPart("1");
169         assertTrue(part.isValidID("123456"));
170
171         part = new NumericIDGeneratorPart("1");
172         assertFalse(part.isValidID("1234567"));
173         
174         part = new NumericIDGeneratorPart("1", "3");
175         assertTrue(part.isValidID("123"));
176         
177         part = new NumericIDGeneratorPart("1", "3");
178         assertFalse(part.isValidID("1234"));
179
180         part = new NumericIDGeneratorPart("1");
181         assertFalse(part.isValidID("not a parseable long"));
182
183         part = new NumericIDGeneratorPart("1", "3");
184         assertFalse(part.isValidID("not a parseable long"));
185
186         part = new NumericIDGeneratorPart("1", "3");
187         assertFalse(part.isValidID(null));
188
189         part = new NumericIDGeneratorPart("1", "3");
190         assertFalse(part.isValidID(""));
191     
192     }    
193     
194     // @TODO: Add more tests of boundary conditions, exceptions ...
195  
196 }