001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.services.T_UUIDFactory
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.services;
023:
024: import org.apache.derbyTesting.unitTests.harness.T_Generic;
025: import org.apache.derbyTesting.unitTests.harness.T_Fail;
026:
027: import org.apache.derby.catalog.UUID;
028:
029: import org.apache.derby.iapi.services.monitor.Monitor;
030: import org.apache.derby.iapi.error.StandardException;
031:
032: import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
033:
034: import org.apache.derby.iapi.services.uuid.UUIDFactory;
035:
036: /**
037: Test to ensure a implementation of the UUID module
038: implements the protocol correctly.
039: */
040:
041: public class T_UUIDFactory extends T_Generic {
042:
043: protected UUIDFactory factory;
044: boolean resultSoFar;
045:
046: public T_UUIDFactory() {
047: super ();
048: }
049:
050: protected String getModuleToTestProtocolName() {
051:
052: return "A.Dummy.Name";
053: }
054:
055: /**
056: Run all the tests, each test that starts with 'S' is a single user
057: test, each test that starts with 'M' is a multi-user test.
058:
059: @exception T_Fail The test failed in some way.
060: */
061: protected void runTests() throws T_Fail {
062:
063: factory = Monitor.getMonitor().getUUIDFactory();
064: if (factory == null) {
065: throw T_Fail.testFailMsg(getModuleToTestProtocolName()
066: + " module not started.");
067: }
068:
069: if (!testUUID())
070: throw T_Fail.testFailMsg("testUUID indicated failure");
071: }
072:
073: /*
074: ** Tests
075: */
076:
077: protected boolean testUUID() {
078: resultSoFar = true;
079:
080: UUID uuid1 = factory.createUUID();
081: UUID uuid2 = factory.createUUID();
082:
083: if (uuid1.equals(uuid2)) {
084: // Resolve: format this with a message factory
085: String message = "UUID factory created matching UUIDS '%0' and '%1'";
086: out.printlnWithHeader(message);
087: resultSoFar = false;
088: }
089:
090: if (!uuid1.equals(uuid1)) {
091: // Resolve: format this with a message factory
092: String message = "UUID '%0' does not equal itself";
093: resultSoFar = false;
094: }
095:
096: if (uuid1.hashCode() != uuid1.hashCode()) {
097: // Resolve: format this with a message factory
098: String message = "UUID '%0' does not hash to the same thing twice.";
099: out.printlnWithHeader(message);
100: resultSoFar = false;
101: }
102:
103: // Check that we can go from UUID to string and back.
104:
105: String suuid1 = uuid1.toString();
106: UUID uuid3 = factory.recreateUUID(suuid1);
107: if (!uuid3.equals(uuid1)) {
108: // Resolve: format this with a message factory
109: String message = "Couldn't recreate UUID: "
110: + uuid3.toString() + " != " + uuid1.toString();
111: out.printlnWithHeader(message);
112: resultSoFar = false;
113: }
114:
115: // Check that we can transform from string to UUID and back
116: // for a few "interesting" UUIDs.
117:
118: // This one came from GUIDGEN.EXE.
119: testUUIDConversions(out, "7878FCD0-DA09-11d0-BAFE-0060973F0942");
120:
121: // Interesting bit patterns.
122: testUUIDConversions(out, "80706050-4030-2010-8070-605040302010");
123: testUUIDConversions(out, "f0e0d0c0-b0a0-9080-7060-504030201000");
124: testUUIDConversions(out, "00000000-0000-0000-0000-000000000000");
125: testUUIDConversions(out, "ffffffff-ffff-ffff-ffff-ffffffffffff");
126:
127: // A couple self-generated ones for good measure.
128: testUUIDConversions(out, factory.createUUID().toString());
129: testUUIDConversions(out, factory.createUUID().toString());
130:
131: return resultSoFar;
132:
133: }
134:
135: private void testUUIDConversions(HeaderPrintWriter out,
136: String uuidstring) {
137: UUID uuid = factory.recreateUUID(uuidstring);
138: if (!uuidstring.equalsIgnoreCase(uuid.toString())) {
139: // Resolve: format this with a message factory
140: String message = "Couldn't recreate UUID String: "
141: + uuidstring + " != " + uuid.toString();
142: out.printlnWithHeader(message);
143: resultSoFar = false;
144: }
145:
146: byte[] uuidByteArray = uuid.toByteArray();
147: UUID uuid_b = factory.recreateUUID(uuidByteArray);
148: if (!uuid_b.equals(uuid)) {
149: // Resolve: format this with a message factory
150: String badByteArrayString = "";
151: for (int ix = 0; ix < 16; ix++) {
152: badByteArrayString += Integer
153: .toHexString(0x00ff & uuidByteArray[ix])
154: + ".";
155: }
156:
157: String message = "Conversion error: " + uuidstring + " != "
158: + badByteArrayString;
159: out.printlnWithHeader(message);
160: resultSoFar = false;
161: }
162: }
163: }
|