]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
751202acc7eb9be1e028862cc60e81efc2d03fbd
[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
18 package org.collectionspace.services.id;
19
20 import static org.junit.Assert.fail;
21 import junit.framework.TestCase;
22
23 /**     
24  * AlphabeticIDGeneratorPartTest
25  *
26  * Test class for AlphabeticIDGeneratorPart.
27  *
28  * $LastChangedRevision$
29  * $LastChangedDate$
30  */
31 public class AlphabeticIDGeneratorPartTest extends TestCase {
32
33         IDGeneratorPart part;
34         
35         public void testNextIDLowercase() {
36
37                 part = new AlphabeticIDGeneratorPart("a");
38                 assertEquals("b", part.nextID());
39                 assertEquals("c", part.nextID());
40
41                 part = new AlphabeticIDGeneratorPart("x");
42                 assertEquals("y", part.nextID());
43                 assertEquals("z", part.nextID());
44
45 }
46
47         public void testnextIDLowercase2Chars() {
48
49                 part = new AlphabeticIDGeneratorPart("aa");
50                 assertEquals("ab", part.nextID());
51                 assertEquals("ac", part.nextID());
52
53                 part = new AlphabeticIDGeneratorPart("zx");
54                 assertEquals("zy", part.nextID());
55                 assertEquals("zz", part.nextID());
56
57         }
58
59         public void testnextIDLowercase2CharsRolloverFirst() {
60
61                 part = new AlphabeticIDGeneratorPart("ay");
62                 assertEquals("az", part.nextID());
63                 assertEquals("ba", part.nextID());
64                 assertEquals("bb", part.nextID());
65
66   }
67         
68         public void testnextIDUppercase() {
69                 
70                 part = new AlphabeticIDGeneratorPart("A", "Z", "A");
71                 assertEquals("B", part.nextID());
72                 assertEquals("C", part.nextID());
73
74                 part = new AlphabeticIDGeneratorPart("A", "Z", "X");
75                 assertEquals("Y", part.nextID());
76                 assertEquals("Z", part.nextID());
77
78 }
79
80         public void testnextIDUppercase2Chars() {
81
82                 part = new AlphabeticIDGeneratorPart("A", "Z", "AA");
83                 assertEquals("AB", part.nextID());
84                 assertEquals("AC", part.nextID());
85
86                 part = new AlphabeticIDGeneratorPart("A", "Z", "ZX");
87                 assertEquals("ZY", part.nextID());
88                 assertEquals("ZZ", part.nextID());
89                         
90         }
91
92         public void testnextIDUppercase2CharsRolloverFirst() {
93
94                 part = new AlphabeticIDGeneratorPart("A", "Z", "AY");
95                 assertEquals("AZ", part.nextID());
96                 assertEquals("BA", part.nextID());
97                 assertEquals("BB", part.nextID());
98
99   }
100   
101         public void testresetIDLowercase() {
102                 
103                 part = new AlphabeticIDGeneratorPart("zx");
104                 assertEquals("zy", part.nextID());
105                 assertEquals("zz", part.nextID());
106                 part.resetID();
107                 assertEquals("zx", part.getCurrentID());
108         
109         }
110
111         public void testresetIDUppercase() {
112                 
113                 part = new AlphabeticIDGeneratorPart("A", "Z", "RA");
114                 assertEquals("RB", part.nextID());
115                 assertEquals("RC", part.nextID());
116                 part.resetID();
117                 assertEquals("RB", part.nextID());
118         
119         }
120         
121         public void testInitialLowercase() {
122                 
123                 part = new AlphabeticIDGeneratorPart("aaa");
124                 assertEquals("aaa", part.getInitialID());
125                 
126         }
127
128         public void testInitialUppercase() {
129                 
130                 part = new AlphabeticIDGeneratorPart("A", "Z", "AZ");
131                 assertEquals("AZ", part.getInitialID());
132                 
133         }
134
135         public void testCurrentLowercase() {
136                 
137                 part = new AlphabeticIDGeneratorPart("aaa");
138                 assertEquals("aaa", part.getCurrentID());
139                 assertEquals("aab", part.nextID());
140                 assertEquals("aac", part.nextID());
141                 assertEquals("aac", part.getCurrentID());
142                 assertEquals("aad", part.nextID());
143                 
144         }
145
146         public void testCurrentUppercase() {
147                 
148                 part = new AlphabeticIDGeneratorPart("A", "Z", "A");
149                 assertEquals("A", part.getCurrentID());
150                 assertEquals("B", part.nextID());
151                 assertEquals("C", part.nextID());
152                 assertEquals("C", part.getCurrentID());
153                 assertEquals("D", part.nextID());
154                 
155         }       
156         
157         public void testOverflowLowercase() {
158         
159     part = new AlphabeticIDGeneratorPart("zx");
160     assertEquals("zy", part.nextID());
161     assertEquals("zz", part.nextID());
162     assertEquals("aaa", part.nextID());
163                 
164         }
165
166         public void testOverflowUppercase() {
167         
168     part = new AlphabeticIDGeneratorPart("A", "Z", "X");
169     assertEquals("Y", part.nextID());
170     assertEquals("Z", part.nextID());
171     assertEquals("AA", part.nextID());
172                 
173         }
174
175         public void testNonAlphabeticInitialValue() {
176                 try {
177                         part = new AlphabeticIDGeneratorPart("&*432");
178                         fail("Should have thrown IllegalArgumentException here");
179                 } catch (IllegalArgumentException expected) {
180                         // This Exception should be thrown, and thus the test should pass.
181                 }
182         }
183
184         public void testNullInitialValue() {
185                 try {
186                         part = new AlphabeticIDGeneratorPart(null);
187                         fail("Should have thrown IllegalArgumentException here");
188                 } catch (IllegalArgumentException expected) {
189                         // This Exception should be thrown, and thus the test should pass.
190                 }
191         }
192
193         public void testEmptyStringInitialValue() {
194                 try {
195                         part = new AlphabeticIDGeneratorPart("");
196                         fail("Should have thrown IllegalArgumentException here");
197                 } catch (IllegalArgumentException expected) {
198                         // This Exception should be thrown, and thus the test should pass.
199                 }
200         }
201
202         public void testAllSpaceCharsInitialValue() {
203                 try {
204                         part = new AlphabeticIDGeneratorPart("  ");
205                         fail("Should have thrown IllegalArgumentException here");
206                 } catch (IllegalArgumentException expected) {
207                         // This Exception should be thrown, and thus the test should pass.
208                 }
209         }
210
211         public void testIsValidIDDefaultSeries() {
212         
213                 part = new AlphabeticIDGeneratorPart();
214
215                 assertTrue(part.isValidID("a"));
216                 assertTrue(part.isValidID("z"));
217
218                 assertFalse(part.isValidID("A"));
219                 assertFalse(part.isValidID("123"));
220                 
221         }
222
223         public void testIsValidIDConstrainedLowerCaseSeries() {
224         
225                 part = new AlphabeticIDGeneratorPart("a", "f", "a");
226                 
227                 assertTrue(part.isValidID("a"));
228                 assertTrue(part.isValidID("b"));
229                 assertTrue(part.isValidID("f"));
230
231                 assertFalse(part.isValidID("g"));
232                 assertFalse(part.isValidID("z"));
233                 assertFalse(part.isValidID("A"));
234                 assertFalse(part.isValidID("123"));
235                 
236         }
237
238         public void testIsValidIDConstrainedUppercaseSeries() {
239         
240                 part = new AlphabeticIDGeneratorPart("A", "F", "A");
241
242                 assertTrue(part.isValidID("A"));
243                 assertTrue(part.isValidID("B"));
244                 assertTrue(part.isValidID("F"));
245
246                 assertFalse(part.isValidID("G"));
247                 assertFalse(part.isValidID("Z"));
248                 assertFalse(part.isValidID("a"));
249                 assertFalse(part.isValidID("123"));
250                 
251         }
252
253         // @TODO: Add more tests of boundary conditions, exceptions ...
254  
255 }