01: /*
02: * ========================================================================
03: *
04: * Copyright 2001-2003 The Apache Software Foundation.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: * ========================================================================
19: */
20: package org.apache.cactus.internal.util;
21:
22: import java.net.InetAddress;
23: import java.net.UnknownHostException;
24:
25: import junit.framework.TestCase;
26:
27: /**
28: * Generates a quasi-unique id for a test case.
29: *
30: * @version $Id: UniqueGenerator.java 238991 2004-05-22 11:34:50Z vmassol $
31: */
32: public class UniqueGenerator {
33: /**
34: * Counter with synchronized access to prevent possibly
35: * identical ids from two threads requesting an id in the
36: * same millisecond.
37: */
38: private static byte count = 0;
39:
40: /**
41: * Lock for count.
42: */
43: private static Object lock = new Object();
44:
45: /**
46: * The local IP address in hexadecimal format.
47: */
48: private static String ipAddress;
49: static {
50: try {
51: byte ip[] = InetAddress.getLocalHost().getAddress();
52: ipAddress = toHex(((ip[0] & 0xff) << 24)
53: | ((ip[1] & 0xff) << 16) | ((ip[2] & 0xff) << 8)
54: | (ip[3] & 0xff));
55: } catch (UnknownHostException e) {
56: ipAddress = "";
57: }
58: }
59:
60: /**
61: * Generates a unique identifier for a Cactus test.
62: *
63: * @param theTestCase The Test to generate a unique ID for
64: * @return The generated ID
65: */
66: public static String generate(TestCase theTestCase) {
67: long time = System.currentTimeMillis();
68: synchronized (lock) {
69: time += count++;
70: }
71: return generate(theTestCase, time);
72: }
73:
74: /**
75: * Generates a unique identifier for a Cactus test.
76: *
77: * @param theTestCase The Test to generate a unique ID for
78: * @param theTime The time component to include in the generated ID
79: * @return The generated ID
80: */
81: public static String generate(TestCase theTestCase, long theTime) {
82: String id = ipAddress;
83: id += "-" + toHex(theTime);
84: id += "-" + toHex(System.identityHashCode(theTestCase));
85: id += toHex(theTestCase.getName().hashCode());
86: return id;
87: }
88:
89: /**
90: * Returns the hexadecimal representation of an integer as string.
91: *
92: * @param theValue The integer value
93: * @return The integer value as string of hexadecimal digits
94: */
95: private static String toHex(long theValue) {
96: return Long.toString(theValue, 16).toUpperCase();
97: }
98:
99: }
|