KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > T_UUIDFactory


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_UUIDFactory
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.services;
23
24 import org.apache.derbyTesting.unitTests.harness.T_Generic;
25 import org.apache.derbyTesting.unitTests.harness.T_Fail;
26
27 import org.apache.derby.catalog.UUID;
28
29 import org.apache.derby.iapi.services.monitor.Monitor;
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
33
34 import org.apache.derby.iapi.services.uuid.UUIDFactory;
35
36 /**
37     Test to ensure a implementation of the UUID module
38     implements the protocol correctly.
39 */

40
41 public class T_UUIDFactory extends T_Generic {
42
43     protected UUIDFactory factory;
44     boolean resultSoFar;
45
46     public T_UUIDFactory() {
47         super();
48     }
49
50     protected String JavaDoc getModuleToTestProtocolName() {
51
52         return "A.Dummy.Name";
53     }
54
55     /**
56         Run all the tests, each test that starts with 'S' is a single user
57         test, each test that starts with 'M' is a multi-user test.
58
59         @exception T_Fail The test failed in some way.
60     */

61     protected void runTests() throws T_Fail {
62
63         factory = Monitor.getMonitor().getUUIDFactory();
64         if (factory == null) {
65             throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " module not started.");
66         }
67
68         if (!testUUID())
69             throw T_Fail.testFailMsg("testUUID indicated failure");
70     }
71
72
73     /*
74     ** Tests
75     */

76
77     protected boolean testUUID() {
78         resultSoFar = true;
79
80         UUID uuid1 = factory.createUUID();
81         UUID uuid2 = factory.createUUID();
82
83         if (uuid1.equals(uuid2)){
84             // Resolve: format this with a message factory
85
String JavaDoc message =
86                 "UUID factory created matching UUIDS '%0' and '%1'";
87             out.printlnWithHeader(message);
88             resultSoFar = false;
89         }
90
91         if (!uuid1.equals(uuid1)){
92             // Resolve: format this with a message factory
93
String JavaDoc message =
94                 "UUID '%0' does not equal itself";
95             resultSoFar = false;
96         }
97
98         if (uuid1.hashCode() != uuid1.hashCode()){
99             // Resolve: format this with a message factory
100
String JavaDoc message =
101                 "UUID '%0' does not hash to the same thing twice.";
102             out.printlnWithHeader(message);
103             resultSoFar = false;
104         }
105
106         // Check that we can go from UUID to string and back.
107

108         String JavaDoc suuid1 = uuid1.toString();
109         UUID uuid3 = factory.recreateUUID(suuid1);
110         if (!uuid3.equals(uuid1)){
111             // Resolve: format this with a message factory
112
String JavaDoc message =
113                 "Couldn't recreate UUID: "
114                 + uuid3.toString()
115                 + " != "
116                 + uuid1.toString();
117             out.printlnWithHeader(message);
118             resultSoFar = false;
119         }
120
121         // Check that we can transform from string to UUID and back
122
// for a few "interesting" UUIDs.
123

124         // This one came from GUIDGEN.EXE.
125
testUUIDConversions(out, "7878FCD0-DA09-11d0-BAFE-0060973F0942");
126
127         // Interesting bit patterns.
128
testUUIDConversions(out, "80706050-4030-2010-8070-605040302010");
129         testUUIDConversions(out, "f0e0d0c0-b0a0-9080-7060-504030201000");
130         testUUIDConversions(out, "00000000-0000-0000-0000-000000000000");
131         testUUIDConversions(out, "ffffffff-ffff-ffff-ffff-ffffffffffff");
132
133         // A couple self-generated ones for good measure.
134
testUUIDConversions(out, factory.createUUID().toString());
135         testUUIDConversions(out, factory.createUUID().toString());
136
137         return resultSoFar;
138     
139     }
140
141     private void testUUIDConversions(HeaderPrintWriter out, String JavaDoc uuidstring)
142     {
143         UUID uuid = factory.recreateUUID(uuidstring);
144         if (!uuidstring.equalsIgnoreCase(uuid.toString())){
145             // Resolve: format this with a message factory
146
String JavaDoc message =
147                 "Couldn't recreate UUID String: "
148                 + uuidstring
149                 + " != "
150                 + uuid.toString();
151             out.printlnWithHeader(message);
152             resultSoFar = false;
153         }
154
155         byte[] uuidByteArray = uuid.toByteArray();
156         UUID uuid_b = factory.recreateUUID(uuidByteArray);
157         if (!uuid_b.equals(uuid))
158         {
159             // Resolve: format this with a message factory
160
String JavaDoc badByteArrayString = "";
161             for (int ix = 0; ix < 16; ix++)
162             {
163                 badByteArrayString +=
164                     Integer.toHexString(0x00ff&uuidByteArray[ix])+".";
165             }
166
167             String JavaDoc message =
168                 "Conversion error: "
169                 + uuidstring
170                 + " != "
171                 + badByteArrayString;
172             out.printlnWithHeader(message);
173             resultSoFar = false;
174         }
175     }
176 }
177
Popular Tags