Author: psteitz
Date: Sat Apr 2 19:25:02 2005
New Revision: 159870
URL: http://svn.apache.org/viewcvs?view=rev&rev=159870
Log:
Added Prefixed string generators.
Bz# 34254
Contributed by Michael Heuer
Added:
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedAlphanumericGenerator.java
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGenerator.java
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedNumericGenerator.java
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedAlphanumericGeneratorTest.java
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGeneratorTest.java
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedNumericGeneratorTest.java
Added:
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedAlphanumericGenerator.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedAlphanumericGenerator.java?view=auto&rev=159870
==============================================================================
---
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedAlphanumericGenerator.java
(added)
+++
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedAlphanumericGenerator.java
Sat Apr 2 19:25:02 2005
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.id.serial;
+
+/**
+ * <p><code>PrefixedAlphanumericGenerator</code> is an Identifier Generator
+ * that generates an incrementing number in base 36 with a prefix as a String
+ * object.</p>
+ *
+ * <p>All generated ids have the same length (prefixed and padded with 0's
+ * on the left), which is determined by the <code>size</code> parameter passed
+ * to the constructor.<p>
+ *
+ * <p>The <code>wrap</code> property determines whether or not the sequence
wraps
+ * when it reaches the largest value that can be represented in
<code>size</code>
+ * base 36 digits. If <code>wrap</code> is false and the the maximum
representable
+ * value is exceeded, an IllegalStateException is thrown.</p>
+ *
+ * @author Commons-Id team
+ * @version $Id$
+ */
+public class PrefixedAlphanumericGenerator extends AlphanumericGenerator {
+
+ /** Prefix. */
+ private final String prefix;
+
+
+ /**
+ * Create a new prefixed alphanumeric generator with the specified prefix.
+ *
+ * @param prefix prefix, must not be null
+ * @param wrap should the factory wrap when it reaches the maximum
+ * value that can be represented in <code>size</code> base 36 digits
+ * (or throw an exception)
+ * @param size the size of the identifier, including prefix length
+ * @throws IllegalArgumentException if prefix is null or size less prefix
+ * length is not at least one
+ */
+ public PrefixedAlphanumericGenerator(String prefix, boolean wrap, int
size) {
+ super(wrap, size - ((prefix == null) ? 0 : prefix.length()));
+
+ if (prefix == null) {
+ throw new IllegalArgumentException("prefix must not be null");
+ }
+ if (size <= prefix.length()) {
+ throw new IllegalArgumentException("size less prefix length must
be at least one");
+ }
+ this.prefix = prefix;
+ }
+
+
+ /**
+ * Return the prefix for this prefixed alphanumeric generator.
+ *
+ * @return the prefix for this prefixed alphanumeric generator
+ */
+ public String getPrefix() {
+ return prefix;
+ }
+
+ /** @see AlphanumericGenerator */
+ public long maxLength() {
+ return super.maxLength() + prefix.length();
+ }
+
+ /** @see AlphanumericGenerator */
+ public long minLength() {
+ return super.minLength() + prefix.length();
+ }
+
+ /** @see AlphanumericGenerator */
+ public int getSize() {
+ return super.getSize() + prefix.length();
+ }
+
+ /** @see AlphanumericGenerator */
+ public String nextStringIdentifier() {
+ StringBuffer sb = new StringBuffer(prefix);
+ sb.append(super.nextStringIdentifier());
+ return sb.toString();
+ }
+}
Added:
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGenerator.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGenerator.java?view=auto&rev=159870
==============================================================================
---
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGenerator.java
(added)
+++
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGenerator.java
Sat Apr 2 19:25:02 2005
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.id.serial;
+
+import org.apache.commons.id.AbstractStringIdentifierGenerator;
+
+/**
+ * <p><code>PrefixedLeftPaddedNumericGenerator</code> is an Identifier
Generator
+ * that generates a left-padded incrementing number with a prefix as a String
object.</p>
+ *
+ * <p>All generated ids have the same length (prefixed and padded with 0's
+ * on the left), which is determined by the <code>size</code> parameter passed
+ * to the constructor.<p>
+ *
+ * <p>The <code>wrap</code> property determines whether or not the sequence
wraps
+ * when it reaches the largest value that can be represented in
<code>size</code>
+ * base 10 digits. If <code>wrap</code> is false and the the maximum
representable
+ * value is exceeded, an IllegalStateException is thrown.</p>
+ *
+ * @author Commons-Id team
+ * @version $Id$
+ */
+public class PrefixedLeftPaddedNumericGenerator extends
AbstractStringIdentifierGenerator {
+
+ /** Prefix. */
+ private final String prefix;
+
+ /** Should the counter wrap. */
+ private boolean wrap = true;
+
+ /** The counter. */
+ private char[] count = null;
+
+ /** '9' char. */
+ private static final char NINE_CHAR = '9';
+
+
+ /**
+ * Create a new prefixed left-padded numeric generator with the specified
prefix.
+ *
+ * @param prefix prefix, must not be null or empty
+ * @param wrap should the factory wrap when it reaches the maximum
+ * value that can be represented in <code>size</code> base 10 digits
+ * (or throw an exception)
+ * @param size the size of the identifier, including prefix length
+ * @throws IllegalArgumentException if prefix is null or size less prefix
+ * length is less than 1
+ */
+ public PrefixedLeftPaddedNumericGenerator(String prefix, boolean wrap, int
size) {
+ super();
+
+ if (prefix == null) {
+ throw new IllegalArgumentException("prefix must not be null");
+ }
+ if (size < 1) {
+ throw new IllegalArgumentException("size must be at least one");
+ }
+ if (size <= prefix.length()) {
+ throw new IllegalArgumentException("size less prefix length must
be at least one");
+ }
+ this.wrap = wrap;
+ this.prefix = prefix;
+
+ int countLength = size - prefix.length();
+ this.count = new char[countLength];
+ for (int i = 0; i < countLength; i++) {
+ count[i] = '0';
+ }
+ }
+
+
+ /**
+ * Return the prefix for this prefixed numeric generator.
+ *
+ * @return the prefix for this prefixed numeric generator
+ */
+ public String getPrefix() {
+ return prefix;
+ }
+
+ /**
+ * <p>Returns the maximum length (number or characters) for an identifier
+ * from this sequence.</p>
+ *
+ * @return the maximum identifier length
+ */
+ public long maxLength() {
+ return this.count.length + prefix.length();
+ }
+
+ /**
+ * <p>Returns the minimum length (number of characters) for an identifier
+ * from this sequence.</p>
+ *
+ * @return the minimum identifier length
+ */
+ public long minLength() {
+ return this.count.length + prefix.length();
+ }
+
+ /**
+ * Returns the (constant) size of the strings generated by this generator.
+ *
+ * @return the size of generated identifiers
+ */
+ public int getSize() {
+ return this.count.length + prefix.length();
+ }
+
+ /**
+ * Getter for property wrap.
+ *
+ * @return <code>true</code> iff this generator is set up to wrap
+ */
+ public boolean isWrap() {
+ return wrap;
+ }
+
+ /**
+ * Setter for property wrap.
+ *
+ * @param wrap should the factory wrap when it reaches the maximum
+ * value that can be represented in <code>size</code> base 10 digits
+ * (or throw an exception)
+ */
+ public void setWrap(boolean wrap) {
+ this.wrap = wrap;
+ }
+
+ /** @see AbstractStringIdentifierGenerator */
+ public String nextStringIdentifier() {
+ for (int i = count.length - 1; i >= 0; i--) {
+ switch (count[i]) {
+ case NINE_CHAR: // 9
+ count[i] = '0';
+ if (i == 0 && !wrap) {
+ throw new IllegalStateException
+ ("The maximum number of identifiers has been reached");
+ }
+ break;
+
+ default:
+ count[i]++;
+ i = -1;
+ break;
+ }
+ }
+
+ StringBuffer sb = new StringBuffer(prefix);
+ sb.append(count);
+ return sb.toString();
+ }
+}
Added:
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedNumericGenerator.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedNumericGenerator.java?view=auto&rev=159870
==============================================================================
---
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedNumericGenerator.java
(added)
+++
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/PrefixedNumericGenerator.java
Sat Apr 2 19:25:02 2005
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.id.serial;
+
+/**
+ * <p><code>PrefixedNumericGenerator</code> is an Identifier Generator
+ * that generates an incrementing number with a prefix as a String object.</p>
+ *
+ * <p>If the <code>wrap</code> argument passed to the constructor is set to
+ * <code>true</code>, the sequence will wrap, returning negative values when
+ * <code>Long.MAX_VALUE</code> reached; otherwise an
+ * <code>IllegalStateException</code> will be thrown.</p>
+ *
+ * @author Commons-Id team
+ * @version $Id$
+ */
+public class PrefixedNumericGenerator extends NumericGenerator {
+
+ /** Prefix. */
+ private final String prefix;
+
+
+ /**
+ * Create a new prefixed numeric generator with the specified prefix.
+ *
+ * @param prefix prefix, must not be null
+ * @param wrap should the factory wrap when it reaches the maximum
+ * long value (or throw an exception)
+ * @param initialValue the initial long value to start at
+ * @throws IllegalArgumentException if prefix is null
+ */
+ public PrefixedNumericGenerator(String prefix, boolean wrap, long
initialValue) {
+ super(wrap, initialValue);
+
+ if (prefix == null) {
+ throw new IllegalArgumentException("prefix must not be null");
+ }
+ this.prefix = prefix;
+ }
+
+
+ /**
+ * Return the prefix for this prefixed numeric generator.
+ *
+ * @return the prefix for this prefixed numeric generator
+ */
+ public String getPrefix() {
+ return prefix;
+ }
+
+ /** @see NumericGenerator */
+ public long maxLength() {
+ return super.maxLength() + prefix.length();
+ }
+
+ /** @see NumericGenerator */
+ public long minLength() {
+ return super.minLength() + prefix.length();
+ }
+
+ /** @see NumericGenerator */
+ public String nextStringIdentifier() {
+ StringBuffer sb = new StringBuffer(prefix);
+ sb.append(super.nextStringIdentifier());
+ return sb.toString();
+ }
+}
Added:
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedAlphanumericGeneratorTest.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedAlphanumericGeneratorTest.java?view=auto&rev=159870
==============================================================================
---
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedAlphanumericGeneratorTest.java
(added)
+++
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedAlphanumericGeneratorTest.java
Sat Apr 2 19:25:02 2005
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.id.serial;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the org.apache.commons.id.serial.PrefixedAlphanumericGenerator class.
+ *
+ * @author Commons-Id team
+ * @version $Id$
+ */
+public class PrefixedAlphanumericGeneratorTest extends TestCase {
+
+ public void testConstructor() {
+ PrefixedAlphanumericGenerator pag0 = new
PrefixedAlphanumericGenerator("foo", true, 15);
+ PrefixedAlphanumericGenerator pag1 = new
PrefixedAlphanumericGenerator("foo", false, 15);
+ PrefixedAlphanumericGenerator pag3 = new
PrefixedAlphanumericGenerator("foo", true, 4);
+ PrefixedAlphanumericGenerator pag4 = new
PrefixedAlphanumericGenerator("", true, 15);
+
+ try
+ {
+ PrefixedAlphanumericGenerator pag = new
PrefixedAlphanumericGenerator(null, true, 15);
+ fail("ctr(null,,) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+
+ try
+ {
+ PrefixedAlphanumericGenerator pag = new
PrefixedAlphanumericGenerator("foo", true, 0);
+ fail("ctr(,,0) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+
+ try
+ {
+ PrefixedAlphanumericGenerator pag = new
PrefixedAlphanumericGenerator("foo", true, -1);
+ fail("ctr(,,-1) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+
+ try
+ {
+ PrefixedAlphanumericGenerator pag = new
PrefixedAlphanumericGenerator("foo", true, 3); // 3 == "foo".length()
+ fail("ctr(,,3) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+
+ try
+ {
+ PrefixedAlphanumericGenerator pag = new
PrefixedAlphanumericGenerator("foo", true, 2); // 2 < "foo".length()
+ fail("ctr(,,2) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testPrefixedNumericGenerator() {
+ PrefixedAlphanumericGenerator pag = new
PrefixedAlphanumericGenerator("foo", true, 15);
+ assertNotNull("pag not null", pag);
+ assertTrue("pag wrap == true", pag.isWrap());
+ assertEquals("pag prefix == foo", "foo", pag.getPrefix());
+ assertTrue("pag maxLength > prefix length", pag.maxLength() >
"foo".length());
+ assertTrue("pag minLength > prefix length", pag.minLength() >
"foo".length());
+ assertTrue("pag size > prefix length", pag.getSize() > "foo".length());
+
+ pag.setWrap(false);
+ assertTrue("pag wrap == false", pag.isWrap() == false);
+
+ pag.setWrap(true);
+ assertTrue("pag wrap == true", pag.isWrap());
+ }
+
+ public void testIdentifiers() {
+ PrefixedAlphanumericGenerator pag = new
PrefixedAlphanumericGenerator("foo", true, 15);
+ assertEquals("foo000000000001", pag.nextStringIdentifier());
+ assertEquals("foo000000000002", pag.nextStringIdentifier());
+ assertEquals("foo000000000003", pag.nextIdentifier());
+ assertEquals("foo000000000004", pag.nextIdentifier());
+ assertEquals("foo000000000005", pag.nextStringIdentifier());
+ assertEquals("foo000000000006", pag.nextStringIdentifier());
+ assertEquals("foo000000000007", pag.nextStringIdentifier());
+ assertEquals("foo000000000008", pag.nextStringIdentifier());
+ assertEquals("foo000000000009", pag.nextStringIdentifier());
+ assertEquals("foo00000000000a", pag.nextStringIdentifier());
+ assertEquals("foo00000000000b", pag.nextStringIdentifier());
+ }
+
+ public void testWrap() {
+ PrefixedAlphanumericGenerator wrap = new
PrefixedAlphanumericGenerator("foo", true, 4);
+ assertEquals("foo1", wrap.nextStringIdentifier());
+ for (int i = 0; i < 33; i++) {
+ wrap.nextStringIdentifier();
+ }
+ assertEquals("fooz", wrap.nextStringIdentifier()); // last identifier
+ assertEquals("foo0", wrap.nextStringIdentifier()); // wrap
+ assertEquals("foo1", wrap.nextStringIdentifier());
+
+ try
+ {
+ PrefixedAlphanumericGenerator noWrap = new
PrefixedAlphanumericGenerator("foo", false, 4);
+ for (int i = 0; i < 34; i++) {
+ noWrap.nextStringIdentifier();
+ }
+ assertEquals("fooz", noWrap.nextStringIdentifier()); // last
identifier
+ noWrap.nextStringIdentifier(); // attempt to wrap
+
+ fail("noWrap.nextStringIdentifier expected IllegalStateException");
+ }
+ catch (IllegalStateException e)
+ {
+ // expected
+ }
+ }
+}
Added:
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGeneratorTest.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGeneratorTest.java?view=auto&rev=159870
==============================================================================
---
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGeneratorTest.java
(added)
+++
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedLeftPaddedNumericGeneratorTest.java
Sat Apr 2 19:25:02 2005
@@ -0,0 +1,151 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.id.serial;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the org.apache.commons.id.serial.PrefixedLeftPaddedNumericGenerator
class.
+ *
+ * @author Commons-Id team
+ * @version $Id$
+ */
+public class PrefixedLeftPaddedNumericGeneratorTest extends TestCase {
+
+ public void testConstructor() {
+ PrefixedLeftPaddedNumericGenerator plpng0 = new
PrefixedLeftPaddedNumericGenerator("foo", true, 15);
+ PrefixedLeftPaddedNumericGenerator plpng1 = new
PrefixedLeftPaddedNumericGenerator("foo", false, 15);
+ PrefixedLeftPaddedNumericGenerator plpng2 = new
PrefixedLeftPaddedNumericGenerator("foo", true, 4);
+ PrefixedLeftPaddedNumericGenerator plpng3 = new
PrefixedLeftPaddedNumericGenerator("", true, 15);
+
+ try
+ {
+ PrefixedLeftPaddedNumericGenerator plpag = new
PrefixedLeftPaddedNumericGenerator(null, true, 15);
+ fail("ctr(null,,) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+
+ try
+ {
+ PrefixedLeftPaddedNumericGenerator plpag = new
PrefixedLeftPaddedNumericGenerator("foo", true, 0);
+ fail("ctr(,,0) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+
+ try
+ {
+ PrefixedLeftPaddedNumericGenerator plpag = new
PrefixedLeftPaddedNumericGenerator("foo", true, -1);
+ fail("ctr(,,-1) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+
+ try
+ {
+ PrefixedLeftPaddedNumericGenerator plpag = new
PrefixedLeftPaddedNumericGenerator("foo", true, 3); // 3 == "foo".length()
+ fail("ctr(,,3) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+
+ try
+ {
+ PrefixedLeftPaddedNumericGenerator plpag = new
PrefixedLeftPaddedNumericGenerator("foo", true, 2); // 2 < "foo".length()
+ fail("ctr(,,2) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testPrefixedNumericGenerator() {
+ PrefixedLeftPaddedNumericGenerator plpng = new
PrefixedLeftPaddedNumericGenerator("foo", true, 15);
+ assertNotNull("plpng not null");
+ assertTrue("plpng wrap == true", plpng.isWrap());
+ assertEquals("plpng prefix == foo", "foo", plpng.getPrefix());
+ assertTrue("plpng maxLength > prefix length", plpng.maxLength() >
"foo".length());
+ assertTrue("plpng minLength > prefix length", plpng.minLength() >
"foo".length());
+ assertTrue("plpng size > prefix length", plpng.getSize() >
"foo".length());
+
+ plpng.setWrap(false);
+ assertTrue("plpng wrap == false", plpng.isWrap() == false);
+
+ plpng.setWrap(true);
+ assertTrue("plpng wrap == true", plpng.isWrap());
+ }
+
+ public void testIdentifiers() {
+ PrefixedLeftPaddedNumericGenerator plpng = new
PrefixedLeftPaddedNumericGenerator("foo", true, 15);
+ assertEquals("foo000000000001", plpng.nextStringIdentifier());
+ assertEquals("foo000000000002", plpng.nextStringIdentifier());
+ assertEquals("foo000000000003", plpng.nextIdentifier());
+ assertEquals("foo000000000004", plpng.nextIdentifier());
+ assertEquals("foo000000000005", plpng.nextStringIdentifier());
+ assertEquals("foo000000000006", plpng.nextStringIdentifier());
+ assertEquals("foo000000000007", plpng.nextStringIdentifier());
+ assertEquals("foo000000000008", plpng.nextStringIdentifier());
+ assertEquals("foo000000000009", plpng.nextStringIdentifier());
+ assertEquals("foo000000000010", plpng.nextStringIdentifier());
+ assertEquals("foo000000000011", plpng.nextStringIdentifier());
+ }
+
+ public void testWrap() {
+ PrefixedLeftPaddedNumericGenerator wrap = new
PrefixedLeftPaddedNumericGenerator("foo", true, 4);
+ assertEquals("foo1", wrap.nextStringIdentifier());
+ assertEquals("foo2", wrap.nextStringIdentifier());
+ assertEquals("foo3", wrap.nextIdentifier());
+ assertEquals("foo4", wrap.nextIdentifier());
+ assertEquals("foo5", wrap.nextStringIdentifier());
+ assertEquals("foo6", wrap.nextStringIdentifier());
+ assertEquals("foo7", wrap.nextStringIdentifier());
+ assertEquals("foo8", wrap.nextStringIdentifier());
+ assertEquals("foo9", wrap.nextStringIdentifier()); // last identifier
+ assertEquals("foo0", wrap.nextStringIdentifier()); // wrap
+ assertEquals("foo1", wrap.nextStringIdentifier());
+
+ try
+ {
+ PrefixedLeftPaddedNumericGenerator noWrap = new
PrefixedLeftPaddedNumericGenerator("foo", false, 4);
+ assertEquals("foo1", noWrap.nextStringIdentifier());
+ assertEquals("foo2", noWrap.nextStringIdentifier());
+ assertEquals("foo3", noWrap.nextIdentifier());
+ assertEquals("foo4", noWrap.nextIdentifier());
+ assertEquals("foo5", noWrap.nextStringIdentifier());
+ assertEquals("foo6", noWrap.nextStringIdentifier());
+ assertEquals("foo7", noWrap.nextStringIdentifier());
+ assertEquals("foo8", noWrap.nextStringIdentifier());
+ assertEquals("foo9", noWrap.nextStringIdentifier()); // last
identifier
+ noWrap.nextStringIdentifier(); // attempt to wrap
+
+ fail("noWrap.nextStringIdentifier expected IllegalStateException");
+ }
+ catch (IllegalStateException e)
+ {
+ // expected
+ }
+ }
+}
Added:
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedNumericGeneratorTest.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedNumericGeneratorTest.java?view=auto&rev=159870
==============================================================================
---
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedNumericGeneratorTest.java
(added)
+++
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/PrefixedNumericGeneratorTest.java
Sat Apr 2 19:25:02 2005
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.id.serial;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the org.apache.commons.id.serial.PrefixedNumericGenerator class.
+ *
+ * @author Commons-Id team
+ * @version $Id$
+ */
+public class PrefixedNumericGeneratorTest extends TestCase {
+
+ public void testConstructor() {
+ PrefixedNumericGenerator png0 = new PrefixedNumericGenerator("foo",
true, 0l);
+ PrefixedNumericGenerator png1 = new PrefixedNumericGenerator("foo",
false, 0l);
+ PrefixedNumericGenerator png2 = new PrefixedNumericGenerator("foo",
true, Long.MAX_VALUE);
+ PrefixedNumericGenerator png3 = new PrefixedNumericGenerator("foo",
true, Long.MIN_VALUE);
+ PrefixedNumericGenerator png4 = new PrefixedNumericGenerator("", true,
0l);
+
+ try
+ {
+ PrefixedNumericGenerator png = new PrefixedNumericGenerator(null,
true, 0l);
+ fail("ctr(null,,) expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testPrefixedNumericGenerator() {
+ PrefixedNumericGenerator png = new PrefixedNumericGenerator("foo",
true, 0l);
+ assertNotNull("png not null");
+ assertTrue("png wrap == true", png.isWrap());
+ assertEquals("png prefix == foo", "foo", png.getPrefix());
+ assertTrue("png maxLength > prefix length", png.maxLength() >
"foo".length());
+ assertTrue("png minLength > prefix length", png.minLength() >
"foo".length());
+
+ png.setWrap(false);
+ assertTrue("png wrap == false", png.isWrap() == false);
+
+ png.setWrap(true);
+ assertTrue("png wrap == true", png.isWrap());
+ }
+
+ public void testIdentifiers() {
+ PrefixedNumericGenerator png = new PrefixedNumericGenerator("foo",
true, 0l);
+ assertEquals("foo0", png.nextStringIdentifier());
+ assertEquals("foo1", png.nextStringIdentifier());
+ assertEquals("foo2", png.nextIdentifier());
+ assertEquals("foo3", png.nextIdentifier());
+ assertEquals("foo4", png.nextStringIdentifier());
+ }
+
+ public void testWrap() {
+ PrefixedNumericGenerator wrap = new PrefixedNumericGenerator("foo",
true, Long.MAX_VALUE);
+ wrap.nextStringIdentifier(); // last long identifier
+ wrap.nextStringIdentifier(); // wrap
+ wrap.nextStringIdentifier();
+
+ try
+ {
+ PrefixedNumericGenerator noWrap = new
PrefixedNumericGenerator("foo", false, Long.MAX_VALUE);
+ noWrap.nextStringIdentifier(); // last long identifier
+ noWrap.nextStringIdentifier(); // attempt to wrap
+ fail("noWrap.nextStringIdentifier expected IllegalStateException");
+ }
+ catch (IllegalStateException e)
+ {
+ // expected
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]