cmlenz 2003/06/29 06:52:02
Modified: framework/src/java/share/org/apache/cactus/util
UniqueGenerator.java
Log:
- add IP address to the generated ID (doesn't really hurt, and makes IDs unique
where multiple client machines are running tests against a single test server)
- as we already included the identity hash of the testcase-instance, we don't need
to also include the name of the test case
- format numbers as hex
- add a method that let's us override the time component, for better testability
Revision Changes Path
1.2 +54 -28
jakarta-cactus/framework/src/java/share/org/apache/cactus/util/UniqueGenerator.java
Index: UniqueGenerator.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/util/UniqueGenerator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- UniqueGenerator.java 26 Jun 2003 15:07:56 -0000 1.1
+++ UniqueGenerator.java 29 Jun 2003 13:52:02 -0000 1.2
@@ -56,12 +56,16 @@
*/
package org.apache.cactus.util;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
import org.apache.cactus.AbstractWebServerTestCase;
/**
* Generates a quasi-unique id for a test case.
*
* @author <a href="mailto:[EMAIL PROTECTED]>Nicholas Lesiecki</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]>Christopher Lenz</a>
*
* @version $Id$
*/
@@ -72,52 +76,74 @@
* identical ids from two threads requesting an id in the
* same millisecond.
*/
- private static int count = 0;
+ private static byte count = 0;
/**
* Lock for count.
*/
private static Object lock = new Object();
-
+
/**
- * @param theTestCase TestCase to generate a unique id for.
- * @return The unique id.
+ * The local IP address in hexadecimal format.
*/
- public static String generate(AbstractWebServerTestCase theTestCase)
+ private static String ipAddress;
+ static
{
- String id = String.valueOf(System.identityHashCode(theTestCase));
-
- synchronized (lock)
+ try
{
- id += count++;
+ byte ip[] = InetAddress.getLocalHost().getAddress();
+ ipAddress = toHex(((ip[0] & 0xff) << 24)
+ | ((ip[1] & 0xff) << 16) | ((ip[2] & 0xff) << 8)
+ | (ip[3] & 0xff));
+ }
+ catch (UnknownHostException e)
+ {
+ ipAddress = "";
}
- id += System.currentTimeMillis();
- id += fullNameHash(theTestCase);
- return id;
}
/**
- * @param theTestCase The TestCase to generate a hash for.
- * @return The hash code of the full name of the testCase.
+ * Generates a unique identifier for a Cactus test.
+ *
+ * @param theTestCase The Test to generate a unique ID for
+ * @return The generated ID
*/
- private static String fullNameHash(AbstractWebServerTestCase theTestCase)
+ public static String generate(AbstractWebServerTestCase theTestCase)
{
- String name;
- if (theTestCase.isWrappingATest())
- {
- name = theTestCase.getWrappedTestName();
- }
- else
+ long time = System.currentTimeMillis();
+ synchronized (lock)
{
- name = theTestCase.getClass().getName();
+ time += count++;
}
-
- //the test method
- name += theTestCase.getName();
-
- return String.valueOf(name.hashCode());
+ return generate(theTestCase, time);
}
+ /**
+ * Generates a unique identifier for a Cactus test.
+ *
+ * @param theTestCase The Test to generate a unique ID for
+ * @param theTime The time component to include in the generated ID
+ * @return The generated ID
+ */
+ public static String generate(AbstractWebServerTestCase theTestCase,
+ long theTime)
+ {
+ String id = ipAddress;
+ id += "-" + toHex(theTime);
+ id += "-" + toHex(System.identityHashCode(theTestCase));
+ id += toHex(theTestCase.getName().hashCode());
+ return id;
+ }
+ /**
+ * Returns the hexadecimal representation of an integer as string.
+ *
+ * @param theValue The integer value
+ * @return The integer value as string of hexadecimal digits
+ */
+ private static String toHex(long theValue)
+ {
+ return Long.toString(theValue, 16).toUpperCase();
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]