001: /*
002: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package com.hp.hpl.jena.shared.uuid.test;
008:
009: import com.hp.hpl.jena.shared.uuid.*;
010:
011: import junit.framework.TestCase;
012:
013: public class TestUUID extends TestCase {
014:
015: public void testNilUUID1() {
016: JenaUUID u = JenaUUID.nil();
017: assertTrue(u.getVariant() == 0);
018: assertTrue(u.getVersion() == 0);
019: assertTrue(u == UUID_nil.getNil());
020: }
021:
022: public void testNilUUID2() {
023: JenaUUID u = JenaUUID.nil();
024: assertTrue(u.isNil());
025: }
026:
027: public void testNilUUID3() {
028: JenaUUID u = JenaUUID.nil();
029: String s = u.asString();
030: assertEquals(s, UUID_nil.getNilString());
031: }
032:
033: public void testTime1() {
034: JenaUUID u = JenaUUID.generate();
035: assertTrue(u.getVersion() == UUID_V1.version);
036: assertTrue(u.getVariant() == UUID_V1.variant);
037: }
038:
039: public void testTime2() {
040: JenaUUID u = JenaUUID.generate();
041: check(u);
042: }
043:
044: public void testTime3() {
045: UUID_V1 u1 = new UUID_V1_Gen().generateV1();
046: UUID_V1 u2 = UUID_V1_Gen.generate(u1.getVersion(), u1
047: .getVariant(), u1.getTimestamp(),
048: u1.getClockSequence(), u1.getNode());
049:
050: assertEquals(u1.getVersion(), u2.getVersion());
051: assertEquals(u1.getVariant(), u2.getVariant());
052: assertEquals(u1.getTimestamp(), u2.getTimestamp());
053: assertEquals(u1.getClockSequence(), u2.getClockSequence());
054: assertEquals(u1.getNode(), u2.getNode());
055: }
056:
057: public void testTime4() {
058: UUID_V1 u1 = new UUID_V1_Gen().generateV1();
059: UUID_V1 u2 = UUID_V1_Gen.generate(u1.getVersion(), u1
060: .getVariant(), u1.getTimestamp(),
061: u1.getClockSequence(), u1.getNode());
062: assertEquals(u1, u2);
063: assertEquals(u1.asString(), u2.asString());
064: }
065:
066: public void testTime5() {
067: UUID_V1 u1 = new UUID_V1_Gen().generateV1();
068: UUID_V1 u2 = UUID_V1_Gen.generate(u1.getVersion(), u1
069: .getVariant(), u1.getTimestamp(),
070: u1.getClockSequence(), u1.getNode());
071: assertEquals(u1.asString(), u2.asString());
072: }
073:
074: public void testTime6() {
075: JenaUUID u1 = JenaUUID.generate();
076: JenaUUID u2 = JenaUUID.generate();
077: assertFalse(u1.equals(u2));
078: }
079:
080: UUIDFactory randomFactory = new UUID_V4_Gen();
081:
082: public void testRandom1() {
083: JenaUUID u = randomFactory.generate();
084: assertEquals(u.getVersion(), UUID_V4.version);
085: assertEquals(u.getVariant(), UUID_V4.variant);
086: }
087:
088: public void testRandom2() {
089: JenaUUID u = randomFactory.generate();
090: check(u);
091: }
092:
093: public void testRandom3() {
094: JenaUUID u = randomFactory.generate();
095: check(u.asString());
096: }
097:
098: public void testRandom4() {
099: JenaUUID u1 = randomFactory.generate();
100: JenaUUID u2 = randomFactory.generate();
101: assertFalse(u1.equals(u2));
102: }
103:
104: public void testEquals1() {
105: JenaUUID u1 = JenaUUID.generate();
106: JenaUUID u2 = JenaUUID.parse(u1.asString());
107: assertNotSame(u1, u2);
108: assertEquals(u1, u2);
109: JenaUUID u3 = JenaUUID.generate();
110: assertFalse(u1.equals(u3));
111: assertFalse(u3.equals(u1));
112: assertFalse(u2.equals(u3));
113: assertFalse(u3.equals(u2));
114: }
115:
116: public void testEquals2() {
117: JenaUUID u1 = randomFactory.generate();
118: JenaUUID u2 = JenaUUID.parse(u1.asString());
119: assertNotSame(u1, u2);
120: assertEquals(u1, u2);
121: JenaUUID u3 = randomFactory.generate();
122: assertFalse(u1.equals(u3));
123: assertFalse(u3.equals(u1));
124: assertFalse(u2.equals(u3));
125: assertFalse(u3.equals(u2));
126: }
127:
128: public void testHash1() {
129: JenaUUID u1 = JenaUUID.generate();
130: JenaUUID u2 = JenaUUID.parse(u1.asString());
131: assertNotSame(u1, u2);
132: assertEquals(u1.hashCode(), u2.hashCode());
133: JenaUUID u3 = JenaUUID.generate();
134: // Time/increment based so should be different
135: assertFalse(u1.hashCode() == u3.hashCode());
136: }
137:
138: public void testHash2() {
139: JenaUUID u1 = randomFactory.generate();
140: JenaUUID u2 = JenaUUID.parse(u1.asString());
141: assertNotSame(u1, u2);
142: assertEquals(u1.hashCode(), u2.hashCode());
143: }
144:
145: public void testMisc1() {
146: check("8609C81E-EE1F-4D5A-B202-3EB13AD01823");
147: check("uuid:DB77450D-9FA8-45D4-A7BC-04411D14E384");
148: check("UUID:C0B9FE13-179F-413D-8A5B-5004DB8E5BB2");
149: check("urn:8609C81E-EE1F-4D5A-B202-3EB13AD01823");
150: check("urn:uuid:70A80F61-77BC-4821-A5E2-2A406ACC35DD");
151: }
152:
153: private void check(String uuidString) {
154: JenaUUID uuid = JenaUUID.parse(uuidString);
155: //assertFalse(u.equals(JenaUUID.nil())) ;
156: String s2 = uuid.asString();
157:
158: String s = uuidString;
159: if (s.matches("[uU][rR][nN]:"))
160: s = s.substring(4);
161: if (s.startsWith("[uU][uU][iI][dD]:"))
162: s = s.substring(5);
163:
164: assertTrue(uuidString.equalsIgnoreCase(s));
165: }
166:
167: private void check(JenaUUID uuid) {
168: String s = uuid.asString();
169: if (uuid.isNil()) {
170: assertEquals(JenaUUID.strNil(), s);
171: return;
172: }
173: JenaUUID uuid2 = JenaUUID.parse(s);
174: assertTrue(uuid2.equals(uuid));
175: }
176: }
177:
178: /*
179: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
180: * All rights reserved.
181: *
182: * Redistribution and use in source and binary forms, with or without
183: * modification, are permitted provided that the following conditions
184: * are met:
185: * 1. Redistributions of source code must retain the above copyright
186: * notice, this list of conditions and the following disclaimer.
187: * 2. Redistributions in binary form must reproduce the above copyright
188: * notice, this list of conditions and the following disclaimer in the
189: * documentation and/or other materials provided with the distribution.
190: * 3. The name of the author may not be used to endorse or promote products
191: * derived from this software without specific prior written permission.
192: *
193: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
194: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
195: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
198: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
201: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
202: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
203: */
|