commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joe...@apache.org
Subject svn commit: r369592 - in /jakarta/commons/sandbox/id/trunk/src: java/org/apache/commons/id/serial/ test/org/apache/commons/id/serial/ test/org/apache/commons/id/test/
Date Mon, 16 Jan 2006 22:31:30 GMT
Author: joehni
Date: Mon Jan 16 14:31:20 2006
New Revision: 369592

URL: http://svn.apache.org/viewcvs?rev=369592&view=rev
Log:
Test serializability of TimeBasedAlphanumericIdentifierGenerator.

Added:
    jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/
    jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertIO.java   (with
props)
    jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java
  (with props)
Modified:
    jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGenerator.java
    jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java

Modified: jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGenerator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGenerator.java?rev=369592&r1=369591&r2=369592&view=diff
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGenerator.java
(original)
+++ jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGenerator.java
Mon Jan 16 14:31:20 2006
@@ -15,6 +15,7 @@
  */
 package org.apache.commons.id.serial;
 
+import java.io.Serializable;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.TimeZone;
@@ -23,10 +24,9 @@
 
 
 /**
- * <code>TimeBasedAlphanumericIdentifierGenerator</code> is an Identifier Generator
that generates
+ * <code>TimeBasedAlphanumericIdentifierGenerator</code> is an identifier generator
that generates
  * an alphanumeric identifier in base 36 as a String object from the current UTC time and
an
  * internal counter.
-
  * <p>
  * The generator guarantees that all generated ids have an increasing natural sort order
(even if
  * the time internally has an overflow). The implementation additionally guarantees, that
all
@@ -48,17 +48,37 @@
  * @author Commons-Id team
  * @version $Id$
  */
-public class TimeBasedAlphanumericIdentifierGenerator extends AbstractStringIdentifierGenerator
{
+public class TimeBasedAlphanumericIdentifierGenerator extends AbstractStringIdentifierGenerator
+        implements Serializable {
 
-    final private static char[] padding;
+    /**
+     * <code>serialVersionUID</code> is the serializable UID for the binary version
of the class.
+     */
+    private static final long serialVersionUID = 20060116L;
+    /**
+     * <code>padding</code> an array of '0' for improved padding performance.
+     */
+    private static final char[] padding;
     static {
         padding = new char[MAX_LONG_ALPHANUMERIC_VALUE_LENGTH];
         Arrays.fill(padding, '0');
     }
-    final private static TimeZone UTC = TimeZone.getTimeZone("UTC");
+    /**
+     * <code>UTC</code> is the UTC {@link TimeZone} instance.
+     */
+    private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
+    /**
+     * <code>last</code> is the marker, when the counter was resetted the last
time.
+     */
     private static long last = 0;
+    /**
+     * <code>counter</code> counts the number of generated identifiers since
the last reset.
+     */
     private static long counter = 0;
-    final private int postfixSize;
+    /**
+     * <code>postfixSize</code> size of the postfix, that contains the padded
counter in base 36.
+     */
+    private final int postfixSize;
     private final long offset;
 
     /**

Modified: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java?rev=369592&r1=369591&r2=369592&view=diff
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java
(original)
+++ jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java
Mon Jan 16 14:31:20 2006
@@ -26,13 +26,15 @@
 
 import org.apache.commons.id.IdentifierGenerator;
 import org.apache.commons.id.StringIdentifierGenerator;
+import org.apache.commons.id.test.AssertSerialization;
 
 import junit.framework.TestCase;
 
 
 /**
  * @author Commons-Uid team
- * @version $Id$
+ * @version $Id: TimeBasedAlphanumericIdentifierGeneratorTest.java 368918 2006-01-13 23:24:41Z
+ *          joehni $
  */
 public class TimeBasedAlphanumericIdentifierGeneratorTest extends TestCase {
 
@@ -219,5 +221,17 @@
 
         assertEquals(loop * idGenerators.length, idList.size());
         assertEquals(loop * idGenerators.length, new HashSet(idList).size());
+    }
+
+    /**
+     * Test serializability.
+     */
+    public void testSerializable() {
+        TimeBasedAlphanumericIdentifierGenerator idGenerator = new TimeBasedAlphanumericIdentifierGenerator(
+                1);
+        TimeBasedAlphanumericIdentifierGenerator serialized = (TimeBasedAlphanumericIdentifierGenerator)AssertSerialization
+                .assertSerializable(idGenerator);
+        assertEquals(idGenerator.maxLength(), serialized.maxLength());
+        assertEquals(idGenerator.minLength(), serialized.minLength());
     }
 }

Added: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertIO.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertIO.java?rev=369592&view=auto
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertIO.java (added)
+++ jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertIO.java Mon
Jan 16 14:31:20 2006
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2006 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.test;
+
+import junit.framework.Assert;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+
+/**
+ * {@link Assert} class for IO operations.
+ * <p>
+ * Note: This class is intented to be moved to a commons-test component later.
+ * </p>
+ * 
+ * @author J&ouml;rg Schaible
+ * @since 1.0
+ */
+public class AssertIO {
+
+    /**
+     * Assert two {@linkplain File files} to have equal content.
+     * 
+     * @param expected reference file
+     * @param current file to compare
+     * @since 1.0
+     */
+    public static void assertFileEquals(final File expected, final File current) {
+        assertFileEquals(null, expected, current);
+    }
+
+    /**
+     * Assert two {@linkplain File files} to have equal content.
+     * 
+     * @param message the error message
+     * @param expected reference file
+     * @param current file to compare
+     * @since 1.0
+     */
+    public static void assertFileEquals(
+            final String message, final File expected, final File current) {
+        try {
+            assertInputStreamEquals(
+                    new BufferedInputStream(new FileInputStream(expected)),
+                    new BufferedInputStream(new FileInputStream(current)));
+        } catch (final FileNotFoundException e) {
+            Assert.fail((message != null ? message + ": " : "") + e.getMessage());
+        }
+    }
+
+    /**
+     * Assert two {@linkplain InputStream input streams} to deliver equal content.
+     * 
+     * @param expected reference input
+     * @param current input to compare
+     * @since 1.0
+     */
+    public static void assertInputStreamEquals(final InputStream expected, final InputStream
current) {
+        assertInputStreamEquals(null, expected, current);
+    }
+
+    /**
+     * Assert two {@linkplain InputStream input streams} to deliver equal content.
+     * 
+     * @param message the error message
+     * @param expected reference input
+     * @param current input to compare
+     * @since 1.0
+     */
+    public static void assertInputStreamEquals(
+            final String message, final InputStream expected, final InputStream current)
{
+        long counter = 0;
+        int eByte, cByte;
+        try {
+            for (; (eByte = expected.read()) != -1; ++counter) {
+                cByte = current.read();
+                if (eByte != cByte) {
+                    Assert.assertEquals((message != null ? message + ": " : "")
+                            + "Stream not equal at position "
+                            + counter, eByte, cByte);
+                }
+            }
+        } catch (final IOException e) {
+            Assert.fail((message != null ? message + ": " : "") + e.getMessage());
+        }
+    }
+}

Propchange: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertIO.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertIO.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java?rev=369592&view=auto
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java
(added)
+++ jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java
Mon Jan 16 14:31:20 2006
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2006 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.test;
+
+import junit.framework.Assert;
+import junit.framework.AssertionFailedError;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+
+
+/**
+ * {@link Assert} class for serialization operations.
+ * <p>
+ * Note: This class is intented to be moved to a commons-test component later.
+ * </p>
+ * 
+ * @author J&ouml;rg Schaible
+ * @since 1.0
+ */
+public class AssertSerialization {
+
+    /**
+     * Assert an object to be {@linkplain Serializable}.
+     * 
+     * @param serializable object to serialize
+     * @return the serialized object
+     * @since 1.0
+     */
+    public static Object assertSerializable(final Serializable serializable) {
+        return assertSerializable(null, serializable);
+    }
+
+    /**
+     * Assert an object to be {@linkplain Serializable}.
+     * 
+     * @param message the error message
+     * @param serializable object to serialize
+     * @return the serialized object
+     * @since 1.0
+     */
+    public static Object assertSerializable(final String message, final Serializable serializable)
{
+        return assertSerializable(message, serializable, serializable.getClass());
+    }
+
+    /**
+     * Assert an object to be {@linkplain Serializable}.
+     * 
+     * @param serializable object to serialize
+     * @param expectedType the expected type of the result
+     * @return the serialized object
+     * @since 1.0
+     */
+    public static Object assertSerializable(
+            final Serializable serializable, final Class expectedType) {
+        return assertSerializable(null, serializable, expectedType);
+    }
+
+    /**
+     * Assert an object to be {@linkplain Serializable}.
+     * 
+     * @param message the error message
+     * @param serializable object to serialize
+     * @param expectedType the expected type of the result
+     * @return the serialized object
+     * @since 1.0
+     */
+    public static Object assertSerializable(
+            String message, final Serializable serializable, final Class expectedType) {
+        message = message != null ? message + ": " : "";
+        try {
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final ObjectOutputStream oos = new ObjectOutputStream(baos);
+            oos.writeObject(serializable);
+            oos.close();
+
+            final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+            final ObjectInputStream ois = new ObjectInputStream(bais);
+            final Object serialized = ois.readObject();
+            ois.close();
+            Assert.assertNotNull(message + "Serialized object is null", serialized);
+            Assert.assertSame(
+                    message + "Serialized object has wrong type", serialized.getClass(),
+                    expectedType);
+            return serialized;
+        } catch (final Exception e) {
+            throw new AssertionFailedError(message + e.getMessage());
+        }
+    }
+}

Propchange: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message