|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
UniqueGenerator.java | - | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.util;
|
|
58 |
|
|
59 |
import java.net.InetAddress;
|
|
60 |
import java.net.UnknownHostException;
|
|
61 |
|
|
62 |
import junit.framework.TestCase;
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Generates a quasi-unique id for a test case.
|
|
66 |
*
|
|
67 |
* @author <a href="mailto:ndlesiecki@apache.org>Nicholas Lesiecki</a>
|
|
68 |
* @author <a href="mailto:cmlenz@apache.org>Christopher Lenz</a>
|
|
69 |
*
|
|
70 |
* @version $Id: UniqueGenerator.java,v 1.3 2003/07/12 12:58:09 vmassol Exp $
|
|
71 |
*/
|
|
72 |
public class UniqueGenerator |
|
73 |
{ |
|
74 |
/**
|
|
75 |
* Counter with synchronized access to prevent possibly
|
|
76 |
* identical ids from two threads requesting an id in the
|
|
77 |
* same millisecond.
|
|
78 |
*/
|
|
79 |
private static byte count = 0; |
|
80 |
|
|
81 |
/**
|
|
82 |
* Lock for count.
|
|
83 |
*/
|
|
84 |
private static Object lock = new Object(); |
|
85 |
|
|
86 |
/**
|
|
87 |
* The local IP address in hexadecimal format.
|
|
88 |
*/
|
|
89 |
private static String ipAddress; |
|
90 |
static
|
|
91 |
{ |
|
92 | 0 |
try
|
93 |
{ |
|
94 | 0 |
byte ip[] = InetAddress.getLocalHost().getAddress();
|
95 | 0 |
ipAddress = toHex(((ip[0] & 0xff) << 24) |
96 |
| ((ip[1] & 0xff) << 16) | ((ip[2] & 0xff) << 8) |
|
97 |
| (ip[3] & 0xff)); |
|
98 |
} |
|
99 |
catch (UnknownHostException e)
|
|
100 |
{ |
|
101 | 0 |
ipAddress = "";
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
/**
|
|
106 |
* Generates a unique identifier for a Cactus test.
|
|
107 |
*
|
|
108 |
* @param theTestCase The Test to generate a unique ID for
|
|
109 |
* @return The generated ID
|
|
110 |
*/
|
|
111 | 0 |
public static String generate(TestCase theTestCase) |
112 |
{ |
|
113 | 0 |
long time = System.currentTimeMillis();
|
114 | 0 |
synchronized (lock)
|
115 |
{ |
|
116 | 0 |
time += count++; |
117 |
} |
|
118 | 0 |
return generate(theTestCase, time);
|
119 |
} |
|
120 |
|
|
121 |
/**
|
|
122 |
* Generates a unique identifier for a Cactus test.
|
|
123 |
*
|
|
124 |
* @param theTestCase The Test to generate a unique ID for
|
|
125 |
* @param theTime The time component to include in the generated ID
|
|
126 |
* @return The generated ID
|
|
127 |
*/
|
|
128 | 0 |
public static String generate(TestCase theTestCase, |
129 |
long theTime)
|
|
130 |
{ |
|
131 | 0 |
String id = ipAddress; |
132 | 0 |
id += "-" + toHex(theTime);
|
133 | 0 |
id += "-" + toHex(System.identityHashCode(theTestCase));
|
134 | 0 |
id += toHex(theTestCase.getName().hashCode()); |
135 | 0 |
return id;
|
136 |
} |
|
137 |
|
|
138 |
/**
|
|
139 |
* Returns the hexadecimal representation of an integer as string.
|
|
140 |
*
|
|
141 |
* @param theValue The integer value
|
|
142 |
* @return The integer value as string of hexadecimal digits
|
|
143 |
*/
|
|
144 | 0 |
private static String toHex(long theValue) |
145 |
{ |
|
146 | 0 |
return Long.toString(theValue, 16).toUpperCase();
|
147 |
} |
|
148 |
|
|
149 |
} |
|
150 |
|
|