Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 91013 invoked from network); 16 Jan 2006 22:31:56 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 16 Jan 2006 22:31:56 -0000 Received: (qmail 20836 invoked by uid 500); 16 Jan 2006 22:31:54 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 20558 invoked by uid 500); 16 Jan 2006 22:31:52 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 20547 invoked by uid 500); 16 Jan 2006 22:31:52 -0000 Received: (qmail 20544 invoked by uid 99); 16 Jan 2006 22:31:52 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 Jan 2006 14:31:52 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 16 Jan 2006 14:31:51 -0800 Received: (qmail 90869 invoked by uid 65534); 16 Jan 2006 22:31:31 -0000 Message-ID: <20060116223131.90868.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commons-cvs@jakarta.apache.org From: joehni@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 @@ /** - * TimeBasedAlphanumericIdentifierGenerator is an Identifier Generator that generates + * TimeBasedAlphanumericIdentifierGenerator 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. - *

* 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; + /** + * serialVersionUID is the serializable UID for the binary version of the class. + */ + private static final long serialVersionUID = 20060116L; + /** + * padding 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"); + /** + * UTC is the UTC {@link TimeZone} instance. + */ + private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); + /** + * last is the marker, when the counter was resetted the last time. + */ private static long last = 0; + /** + * counter counts the number of generated identifiers since the last reset. + */ private static long counter = 0; - final private int postfixSize; + /** + * postfixSize 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. + *

+ * Note: This class is intented to be moved to a commons-test component later. + *

+ * + * @author Jö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. + *

+ * Note: This class is intented to be moved to a commons-test component later. + *

+ * + * @author Jö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