]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
caa2ab9ba4949f2de60c272f418c6ebc4b560836
[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  * 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.
26  */
27
28 package org.collectionspace.services.id.test;
29
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;
34
35
36 /**
37  * NumericIDGeneratorPartTest
38  *
39  * Test class for NumericIDGeneratorPart.
40  *
41  * $LastChangedRevision$
42  * $LastChangedDate$
43  */
44 public class NumericIDGeneratorPartTest extends TestCase {
45
46     SequenceIDGeneratorPart part;
47
48     public void testInitialID() {
49         part = new NumericIDGeneratorPart("0");
50         assertEquals("0", part.getInitialID());
51
52         part = new NumericIDGeneratorPart("25");
53         assertEquals("25", part.getInitialID());
54     }
55
56     public void testNullInitialValue() {
57         try {
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.
62         }
63     }
64
65     public void testNonLongParseableInitialValue() {
66         try {
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.
71         }
72     }
73
74     public void testNonLongParseableMaxLength() {
75         try {
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.
80         }
81     }
82
83     public void testNegativeInitialValue() {
84        try {
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.
89         }
90     }
91
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());
100
101         part = new NumericIDGeneratorPart("25");
102         assertEquals("25", part.getCurrentID());
103     }
104
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());
111     
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());
117         
118         part = new NumericIDGeneratorPart();
119         assertEquals("1", part.newID());
120         assertEquals("2", part.newID());
121         assertEquals("3", part.newID());
122     }
123
124     public void testNewIDOverflow() {
125
126         try {
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.
135         }
136
137         // Tests default MAX_LENGTH value of 6 decimal places
138         try {
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.
147         }
148             
149     }
150
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"));
158
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(""));
173     }    
174     
175     // @TODO: Add more tests of boundary conditions, exceptions ...
176  
177 }