]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
594f9181716ae217340b448e9bd9a72782720b49
[tmp/jakarta-migration.git] /
1 /**     
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23
24 package org.collectionspace.services.id.test;
25
26 import org.collectionspace.services.common.repository.BadRequestException;
27 import org.collectionspace.services.id.AlphabeticIDGeneratorPart;
28 import org.collectionspace.services.id.SequenceIDGeneratorPart;
29
30 import static org.junit.Assert.fail;
31 import junit.framework.TestCase;
32
33 /**     
34  * AlphabeticIDGeneratorPartTest
35  *
36  * Test class for AlphabeticIDGeneratorPart.
37  *
38  * $LastChangedRevision$
39  * $LastChangedDate$
40  */
41 public class AlphabeticIDGeneratorPartTest extends TestCase {
42
43         SequenceIDGeneratorPart part;
44         
45         public void testNextIDLowercase() throws BadRequestException {
46
47                 part = new AlphabeticIDGeneratorPart("a");
48                 assertEquals("a", part.newID());
49                 assertEquals("b", part.newID());
50                 assertEquals("c", part.newID());
51
52                 part = new AlphabeticIDGeneratorPart("x");
53                 assertEquals("x", part.newID());
54                 assertEquals("y", part.newID());
55                 assertEquals("z", part.newID());
56
57 }
58
59         public void testnewIDLowercase2Chars() throws BadRequestException {
60
61                 part = new AlphabeticIDGeneratorPart("aa");
62                 assertEquals("aa", part.newID());
63                 assertEquals("ab", part.newID());
64                 assertEquals("ac", part.newID());
65
66                 part = new AlphabeticIDGeneratorPart("zx");
67                 assertEquals("zx", part.newID());
68                 assertEquals("zy", part.newID());
69                 assertEquals("zz", part.newID());
70
71         }
72
73         public void testnewIDLowercase2CharsRolloverFirst() throws BadRequestException {
74
75                 part = new AlphabeticIDGeneratorPart("ay");
76                 assertEquals("ay", part.newID());
77                 assertEquals("az", part.newID());
78                 assertEquals("ba", part.newID());
79                 assertEquals("bb", part.newID());
80
81     }
82         
83         public void testnewIDUppercase() throws BadRequestException {
84                 
85                 part = new AlphabeticIDGeneratorPart("A", "Z", "A");
86                 assertEquals("A", part.newID());
87                 assertEquals("B", part.newID());
88                 assertEquals("C", part.newID());
89
90                 part = new AlphabeticIDGeneratorPart("A", "Z", "X");
91                 assertEquals("X", part.newID());
92                 assertEquals("Y", part.newID());
93                 assertEquals("Z", part.newID());
94
95     }
96
97         public void testnewIDUppercase2Chars() throws BadRequestException {
98
99                 part = new AlphabeticIDGeneratorPart("A", "Z", "AA");
100                 assertEquals("AA", part.newID());
101                 assertEquals("AB", part.newID());
102                 assertEquals("AC", part.newID());
103
104                 part = new AlphabeticIDGeneratorPart("A", "Z", "ZX");
105                 assertEquals("ZX", part.newID());
106                 assertEquals("ZY", part.newID());
107                 assertEquals("ZZ", part.newID());
108                         
109         }
110
111         public void testnewIDUppercase2CharsRolloverFirst() throws BadRequestException {
112
113                 part = new AlphabeticIDGeneratorPart("A", "Z", "AY");
114                 assertEquals("AY", part.newID());
115                 assertEquals("AZ", part.newID());
116                 assertEquals("BA", part.newID());
117                 assertEquals("BB", part.newID());
118
119   }
120
121         public void testInitialLowercase() throws BadRequestException {
122                 
123                 part = new AlphabeticIDGeneratorPart("aaa");
124                 assertEquals("aaa", part.getInitialID());
125                 
126         }
127
128         public void testInitialUppercase() throws BadRequestException {
129                 
130                 part = new AlphabeticIDGeneratorPart("A", "Z", "AZ");
131                 assertEquals("AZ", part.getInitialID());
132                 
133         }
134
135         public void testCurrentLowercase() throws BadRequestException {
136                 
137                 part = new AlphabeticIDGeneratorPart("aaa");
138                 assertEquals("aaa", part.getCurrentID());
139                 assertEquals("aaa", part.newID());
140                 assertEquals("aab", part.newID());
141                 assertEquals("aac", part.newID());
142                 assertEquals("aac", part.getCurrentID());
143                 assertEquals("aad", part.newID());
144                 
145         }
146
147         public void testCurrentUppercase() throws BadRequestException {
148                 
149                 part = new AlphabeticIDGeneratorPart("A", "Z", "A");
150                 assertEquals("A", part.getCurrentID());
151                 assertEquals("A", part.newID());
152                 assertEquals("B", part.newID());
153                 assertEquals("C", part.newID());
154                 assertEquals("C", part.getCurrentID());
155                 assertEquals("D", part.newID());
156                 
157         }       
158
159         public void testOverflowLowercase() throws BadRequestException {
160         
161         part = new AlphabeticIDGeneratorPart("zx");
162         assertEquals("zx", part.newID());
163         assertEquals("zy", part.newID());
164         assertEquals("zz", part.newID());
165         assertEquals("aaa", part.newID());
166                 
167         }
168
169         public void testOverflowUppercase() throws BadRequestException {
170         
171         part = new AlphabeticIDGeneratorPart("A", "Z", "X");
172         assertEquals("X", part.newID());
173         assertEquals("Y", part.newID());
174         assertEquals("Z", part.newID());
175         assertEquals("AA", part.newID());
176                 
177         }
178
179         public void testNonAlphabeticInitialValue() {
180                 try {
181                         part = new AlphabeticIDGeneratorPart("&*432");
182                         fail("Should have thrown BadRequestException here");
183                 } catch (BadRequestException expected) {
184                         // This Exception should be thrown, and thus the test should pass.
185                 }
186         }
187
188         public void testNullInitialValue() {
189                 try {
190                         part = new AlphabeticIDGeneratorPart(null);
191                         fail("Should have thrown BadRequestException here");
192                 } catch (BadRequestException expected) {
193                         // This Exception should be thrown, and thus the test should pass.
194                 }
195         }
196
197         public void testEmptyStringInitialValue() {
198                 try {
199                         part = new AlphabeticIDGeneratorPart("");
200                         fail("Should have thrown BadRequestException here");
201                 } catch (BadRequestException expected) {
202                         // This Exception should be thrown, and thus the test should pass.
203                 }
204         }
205
206         public void testAllSpaceCharsInitialValue() {
207                 try {
208                         part = new AlphabeticIDGeneratorPart("  ");
209                         fail("Should have thrown BadRequestException here");
210                 } catch (BadRequestException expected) {
211                         // This Exception should be thrown, and thus the test should pass.
212                 }
213         }
214
215         public void testIsValidIDDefaultSeries() throws BadRequestException {
216         
217                 part = new AlphabeticIDGeneratorPart();
218
219                 assertTrue(part.isValidID("a"));
220                 assertTrue(part.isValidID("z"));
221
222                 assertFalse(part.isValidID("A"));
223                 assertFalse(part.isValidID("123"));
224                 
225         }
226
227         public void testIsValidIDConstrainedLowerCaseSeries() throws BadRequestException {
228         
229                 part = new AlphabeticIDGeneratorPart("a", "f", "a");
230                 
231                 assertTrue(part.isValidID("a"));
232                 assertTrue(part.isValidID("b"));
233                 assertTrue(part.isValidID("f"));
234
235                 assertFalse(part.isValidID("g"));
236                 assertFalse(part.isValidID("z"));
237                 assertFalse(part.isValidID("A"));
238                 assertFalse(part.isValidID("123"));
239                 
240         }
241
242         public void testIsValidIDConstrainedUppercaseSeries() throws BadRequestException {
243         
244                 part = new AlphabeticIDGeneratorPart("A", "F", "A");
245
246                 assertTrue(part.isValidID("A"));
247                 assertTrue(part.isValidID("B"));
248                 assertTrue(part.isValidID("F"));
249
250                 assertFalse(part.isValidID("G"));
251                 assertFalse(part.isValidID("Z"));
252                 assertFalse(part.isValidID("a"));
253                 assertFalse(part.isValidID("123"));
254                 
255         }
256
257         // @TODO: Add more tests of boundary conditions, exceptions ...
258  
259 }