commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joe...@apache.org
Subject svn commit: r425537 - in /jakarta/commons/sandbox/id/trunk/src: java/org/apache/commons/id/task/ java/org/apache/commons/id/uuid/ test/org/apache/commons/id/test/ test/org/apache/commons/id/uuid/ test/org/apache/commons/id/uuid/task/
Date Tue, 25 Jul 2006 22:15:09 GMT
Author: joehni
Date: Tue Jul 25 15:15:09 2006
New Revision: 425537

URL: http://svn.apache.org/viewvc?rev=425537&view=rev
Log:
Fix non-working VersionFourGenerator with SecureRandom (SANDBOX-151).
Add missing test for UUIDTask.

Added:
    jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/VersionFourGeneratorTest.java
  (with props)
    jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/task/
    jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/task/UUIDTaskTest.java
  (with props)
Removed:
    jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/task/
Modified:
    jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/uuid/VersionFourGenerator.java
    jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java

Modified: jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/uuid/VersionFourGenerator.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/uuid/VersionFourGenerator.java?rev=425537&r1=425536&r2=425537&view=diff
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/uuid/VersionFourGenerator.java
(original)
+++ jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/uuid/VersionFourGenerator.java
Tue Jul 25 15:15:09 2006
@@ -92,10 +92,7 @@
          * @return a new version four UUID that was generated by either a <code>Random</code>
or <code>SecureRandom</code>.
          */
         public Object nextIdentifier(boolean secure)  {
-            if (secure) {
-                return nextUUID(true);
-            }
-            return nextUUID(false);
+            return nextUUID(secure);
         }
 
         /**
@@ -130,11 +127,11 @@
                     } catch (NoSuchProviderException nspe) {
                         secure = false; //Fail back to default PRNG/Random
                     }
-                    secureRandom.nextBytes(raw);
                 }
-            }
-
-            if (!secure) {
+                if (secureRandom != null) { 
+                    secureRandom.nextBytes(raw); 
+                }                
+            } else {
                 regularRandom.nextBytes(raw);
             }
 

Modified: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java?rev=425537&r1=425536&r2=425537&view=diff
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java
(original)
+++ jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/test/AssertSerialization.java
Tue Jul 25 15:15:09 2006
@@ -185,7 +185,7 @@
      * @author J&ouml;rg Schaible
      * @since 1.0
      */
-    public static class SerializationTest extends TestCase {
+    protected static class SerializationTest extends TestCase {
         private final Context context;
         private final ByteArrayOutputStream baos;
 

Added: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/VersionFourGeneratorTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/VersionFourGeneratorTest.java?rev=425537&view=auto
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/VersionFourGeneratorTest.java
(added)
+++ jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/VersionFourGeneratorTest.java
Tue Jul 25 15:15:09 2006
@@ -0,0 +1,50 @@
+/*
+ * 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.uuid;
+
+import junit.framework.TestCase;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Commons-Uid team
+ * @version $Id$
+ */
+public class VersionFourGeneratorTest extends TestCase {
+
+    public void testCanGenerateUUIDsWithStandardRandomizer() {
+        Set set = new HashSet();
+        VersionFourGenerator generator = new VersionFourGenerator();
+        // following assertions are valid, if all UUIDs are unique
+        assertTrue(set.add(generator.nextIdentifier()));
+        assertTrue(set.add(generator.nextIdentifier()));
+        assertTrue(set.add(generator.nextIdentifier()));
+        assertTrue(set.add(generator.nextIdentifier()));
+        assertTrue(set.add(generator.nextIdentifier()));
+    }
+
+    public void testCanGenerateUUIDsWithSecureRandomizer() {
+        Set set = new HashSet();
+        VersionFourGenerator generator = new VersionFourGenerator();
+        // following assertions are valid, if all UUIDs are unique
+        assertTrue(set.add(generator.nextIdentifier(true)));
+        assertTrue(set.add(generator.nextIdentifier(true)));
+        assertTrue(set.add(generator.nextIdentifier(true)));
+        assertTrue(set.add(generator.nextIdentifier(true)));
+        assertTrue(set.add(generator.nextIdentifier(true)));
+    }
+}

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

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

Added: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/task/UUIDTaskTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/task/UUIDTaskTest.java?rev=425537&view=auto
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/task/UUIDTaskTest.java
(added)
+++ jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/task/UUIDTaskTest.java
Tue Jul 25 15:15:09 2006
@@ -0,0 +1,107 @@
+/*
+ * 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.uuid.task;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.id.uuid.UUID;
+import org.apache.commons.id.uuid.state.ReadOnlyResourceStateImpl;
+import org.apache.commons.id.uuid.task.UUIDTask.UUIDVersion;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+
+import java.util.Properties;
+
+/**
+ * @author Commons-Id team
+ */
+public class UUIDTaskTest extends TestCase {
+
+    private Project project;
+    private UUIDVersion uuidVersion;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        project = new Project();
+        uuidVersion = new UUIDTask.UUIDVersion();
+    }
+
+    public void testVersion1() {
+        final Properties sysProps = new Properties(System.getProperties());
+        try {
+            System.setProperty(ReadOnlyResourceStateImpl.CONFIG_FILENAME_KEY, "uuid1.state");
+            uuidVersion.setValue("VERSION_ONE");
+            final UUIDTask uuidTask = new UUIDTask();
+            uuidTask.setProject(project);
+            uuidTask.setVersion(uuidVersion);
+            uuidTask.execute();
+            final UUID uuid = new UUID(uuidTask.getProject().getProperty("uuid"));
+            assertEquals(UUID.VERSION_ONE, uuid.version());
+        } finally {
+            System.setProperties(sysProps);
+        }
+    }
+
+    public void testVersion2() {
+        try {
+            uuidVersion.setValue("VERSION_TWO");
+            final UUIDTask uuidTask = new UUIDTask();
+            uuidTask.setProject(project);
+            uuidTask.setVersion(uuidVersion);
+            fail("Thrown " + BuildException.class.getName() + " expected");
+        } catch (final BuildException e) {
+            assertTrue(e.getMessage().indexOf("VERSION_TWO") >= 0);
+        }
+    }
+
+    public void testVersion3() {
+        final UUID namespace = new UUID();
+        uuidVersion.setValue("VERSION_THREE");
+        final UUIDTask uuidTask = new UUIDTask();
+        uuidTask.setProject(project);
+        uuidTask.setVersion(uuidVersion);
+        uuidTask.setNamespace(namespace.toUrn());
+        uuidTask.setName("jakarta.apache.org");
+        uuidTask.execute();
+        final UUID uuid = new UUID(uuidTask.getProject().getProperty("uuid"));
+        assertEquals(UUID.VERSION_THREE, uuid.version());
+        assertEquals(UUID.nameUUIDFromString("jakarta.apache.org", namespace, UUID.MD5_ENCODING),
uuid);
+    }
+
+    public void testVersion4() {
+        uuidVersion.setValue("VERSION_FOUR");
+        final UUIDTask uuidTask = new UUIDTask();
+        uuidTask.setProject(project);
+        uuidTask.setVersion(uuidVersion);
+        uuidTask.execute();
+        final UUID uuid = new UUID(uuidTask.getProject().getProperty("uuid"));
+        assertEquals(UUID.VERSION_FOUR, uuid.version());
+    }
+
+    public void testVersion5() {
+        final UUID namespace = new UUID();
+        uuidVersion.setValue("VERSION_FIVE");
+        final UUIDTask uuidTask = new UUIDTask();
+        uuidTask.setProject(project);
+        uuidTask.setVersion(uuidVersion);
+        uuidTask.setNamespace(namespace.toUrn());
+        uuidTask.setName("jakarta.apache.org");
+        uuidTask.execute();
+        final UUID uuid = new UUID(uuidTask.getProject().getProperty("uuid"));
+        assertEquals(UUID.VERSION_FIVE, uuid.version());
+        assertEquals(UUID.nameUUIDFromString("jakarta.apache.org", namespace, UUID.SHA1_ENCODING),
uuid);
+    }
+}

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

Propchange: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/uuid/task/UUIDTaskTest.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