1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.id.serial;
18  
19  import junit.framework.TestCase;
20  
21  /**
22   * Tests the org.apache.commons.id.serial.PrefixedLeftPaddedNumericGenerator class.
23   *
24   * @author Commons-Id team
25   * @version $Id$
26   */
27  public class PrefixedLeftPaddedNumericGeneratorTest extends TestCase {
28  
29      public void testConstructor() {
30          PrefixedLeftPaddedNumericGenerator plpng0 = new PrefixedLeftPaddedNumericGenerator("foo", true, 15);
31          PrefixedLeftPaddedNumericGenerator plpng1 = new PrefixedLeftPaddedNumericGenerator("foo", false, 15);
32          PrefixedLeftPaddedNumericGenerator plpng2 = new PrefixedLeftPaddedNumericGenerator("foo", true, 4);
33          PrefixedLeftPaddedNumericGenerator plpng3 = new PrefixedLeftPaddedNumericGenerator("", true, 15);
34  
35          try
36          {
37              PrefixedLeftPaddedNumericGenerator plpag = new PrefixedLeftPaddedNumericGenerator(null, true, 15);
38              fail("ctr(null,,) expected IllegalArgumentException");
39          }
40          catch (NullPointerException e)
41          {
42              // expected
43          }
44  
45          try
46          {
47              PrefixedLeftPaddedNumericGenerator plpag = new PrefixedLeftPaddedNumericGenerator("foo", true, 0);
48              fail("ctr(,,0) expected IllegalArgumentException");
49          }
50          catch (IllegalArgumentException e)
51          {
52              // expected
53          }
54  
55          try
56          {
57              PrefixedLeftPaddedNumericGenerator plpag = new PrefixedLeftPaddedNumericGenerator("foo", true, -1);
58              fail("ctr(,,-1) expected IllegalArgumentException");
59          }
60          catch (IllegalArgumentException e)
61          {
62              // expected
63          }
64  
65          try
66          {
67              PrefixedLeftPaddedNumericGenerator plpag = new PrefixedLeftPaddedNumericGenerator("foo", true, 3); // 3 == "foo".length()
68              fail("ctr(,,3) expected IllegalArgumentException");
69          }
70          catch (IllegalArgumentException e)
71          {
72              // expected
73          }
74  
75          try
76          {
77              PrefixedLeftPaddedNumericGenerator plpag = new PrefixedLeftPaddedNumericGenerator("foo", true, 2); // 2 < "foo".length()
78              fail("ctr(,,2) expected IllegalArgumentException");
79          }
80          catch (IllegalArgumentException e)
81          {
82              // expected
83          }
84      }
85  
86      public void testPrefixedNumericGenerator() {
87          PrefixedLeftPaddedNumericGenerator plpng = new PrefixedLeftPaddedNumericGenerator("foo", true, 15);
88          assertNotNull("plpng not null");
89          assertTrue("plpng wrap == true", plpng.isWrap());
90          assertEquals("plpng prefix == foo", "foo", plpng.getPrefix());
91          assertTrue("plpng maxLength > prefix length", plpng.maxLength() > "foo".length());
92          assertTrue("plpng minLength > prefix length", plpng.minLength() > "foo".length());
93          assertTrue("plpng size > prefix length", plpng.getSize() > "foo".length());
94  
95          plpng.setWrap(false);
96          assertTrue("plpng wrap == false", plpng.isWrap() == false);
97  
98          plpng.setWrap(true);
99          assertTrue("plpng wrap == true", plpng.isWrap());
100     }
101 
102     public void testIdentifiers() {
103         PrefixedLeftPaddedNumericGenerator plpng = new PrefixedLeftPaddedNumericGenerator("foo", true, 15);
104         assertEquals("foo000000000001", plpng.nextStringIdentifier());
105         assertEquals("foo000000000002", plpng.nextStringIdentifier());
106         assertEquals("foo000000000003", plpng.nextIdentifier());
107         assertEquals("foo000000000004", plpng.nextIdentifier());
108         assertEquals("foo000000000005", plpng.nextStringIdentifier());
109         assertEquals("foo000000000006", plpng.nextStringIdentifier());
110         assertEquals("foo000000000007", plpng.nextStringIdentifier());
111         assertEquals("foo000000000008", plpng.nextStringIdentifier());
112         assertEquals("foo000000000009", plpng.nextStringIdentifier());
113         assertEquals("foo000000000010", plpng.nextStringIdentifier());
114         assertEquals("foo000000000011", plpng.nextStringIdentifier());
115     }
116 
117     public void testWrap() {
118         PrefixedLeftPaddedNumericGenerator wrap = new PrefixedLeftPaddedNumericGenerator("foo", true, 4);
119         assertEquals("foo1", wrap.nextStringIdentifier());
120         assertEquals("foo2", wrap.nextStringIdentifier());
121         assertEquals("foo3", wrap.nextIdentifier());
122         assertEquals("foo4", wrap.nextIdentifier());
123         assertEquals("foo5", wrap.nextStringIdentifier());
124         assertEquals("foo6", wrap.nextStringIdentifier());
125         assertEquals("foo7", wrap.nextStringIdentifier());
126         assertEquals("foo8", wrap.nextStringIdentifier());
127         assertEquals("foo9", wrap.nextStringIdentifier()); // last identifier
128         assertEquals("foo0", wrap.nextStringIdentifier()); // wrap
129         assertEquals("foo1", wrap.nextStringIdentifier());
130 
131         try
132         {
133             PrefixedLeftPaddedNumericGenerator noWrap = new PrefixedLeftPaddedNumericGenerator("foo", false, 4);
134             assertEquals("foo1", noWrap.nextStringIdentifier());
135             assertEquals("foo2", noWrap.nextStringIdentifier());
136             assertEquals("foo3", noWrap.nextIdentifier());
137             assertEquals("foo4", noWrap.nextIdentifier());
138             assertEquals("foo5", noWrap.nextStringIdentifier());
139             assertEquals("foo6", noWrap.nextStringIdentifier());
140             assertEquals("foo7", noWrap.nextStringIdentifier());
141             assertEquals("foo8", noWrap.nextStringIdentifier());
142             assertEquals("foo9", noWrap.nextStringIdentifier()); // last identifier
143             noWrap.nextStringIdentifier(); // attempt to wrap
144 
145             fail("noWrap.nextStringIdentifier expected IllegalStateException");
146         }
147         catch (IllegalStateException e)
148         {
149             // expected
150         }
151     }
152 }