]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c21bbf9a3658aa3288b4efb01dc397a7a5fffa7e
[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 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;
35
36
37 /**
38  * NumericIDGeneratorPartTest
39  *
40  * Test class for NumericIDGeneratorPart.
41  *
42  * $LastChangedRevision$
43  * $LastChangedDate$
44  */
45 public class NumericIDGeneratorPartTest extends TestCase {
46
47     SequenceIDGeneratorPart part;
48
49     public void testInitialID() throws BadRequestException {
50         part = new NumericIDGeneratorPart("0");
51         assertEquals("0", part.getInitialID());
52
53         part = new NumericIDGeneratorPart("25");
54         assertEquals("25", part.getInitialID());
55     }
56
57     public void testNullInitialValue() {
58         try {
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.
63         }
64     }
65
66     public void testNonLongParseableInitialValue() {
67         try {
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.
72         }
73     }
74
75     public void testNonLongParseableMaxLength() {
76         try {
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.
81         }
82     }
83
84     public void testNegativeInitialValue() {
85        try {
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.
90         }
91     }
92
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());
101
102         part = new NumericIDGeneratorPart("25");
103         assertEquals("25", part.getCurrentID());
104     }
105
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());
112     
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());
118         
119         part = new NumericIDGeneratorPart();
120         assertEquals("1", part.newID());
121         assertEquals("2", part.newID());
122         assertEquals("3", part.newID());
123     }
124
125     public void testNewIDOverflow() throws BadRequestException {
126
127         try {
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.
136         }
137
138         // Tests default MAX_LENGTH value of 6 decimal places
139         try {
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.
148         }
149             
150     }
151
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"));
159
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(""));
174     }    
175     
176     // @TODO: Add more tests of boundary conditions, exceptions ...
177  
178 }