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