-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTestNessUUID.java
More file actions
294 lines (252 loc) · 10.6 KB
/
TestNessUUID.java
File metadata and controls
294 lines (252 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Copyright (C) 2013 Ness Computing, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nesscomputing.uuid;
import java.util.UUID;
import org.junit.Assert;
import org.junit.Test;
public class TestNessUUID {
private final String uuid = "6f32f693-c7b5-11e1-afa7-88af2abc9a66";
private final String caseSensitivity = "Dd000000-0000-0000-0000-000000000000";
private final String overflow = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
private final String zero = "00000000-0000-0000-0000-000000000000";
private final String badLength = "00000000-f0000-0000-0000-000000000000";
private final String hyphen1 = "00000000f0000-0000-0000-000000000000";
private final String hyphen2 = "00000000-0000f0000-0000-000000000000";
private final String hyphen3 = "00000000-0000-0000f0000-000000000000";
private final String hyphen4 = "00000000-0000-0000-0000f000000000000";
private final String invalid1 = "00000000-0000-0-00-0000-000000000000";
private final String invalid2 = "0000g000-0000-0000-0000-000000000000";
private final String invalid3 = "00000000-00g0-0000-0000-000000000000";
private final String invalid4 = "00000000-0000-0g00-0000-000000000000";
private final String invalid5 = "00000000-0000-0000-00g0-000000000000";
private final String invalid6 = "00000000-0000-0000-0000-0000000000g0";
@Test
public void testBasic()
{
Assert.assertEquals(UUID.fromString(uuid), NessUUID.fromString(uuid));
Assert.assertEquals(UUID.fromString(caseSensitivity), NessUUID.fromString(caseSensitivity));
Assert.assertEquals(UUID.fromString(overflow), NessUUID.fromString(overflow));
Assert.assertEquals(UUID.fromString(zero), NessUUID.fromString(zero));
}
@Test
public void testLength() {
final UUID uuid = NessUUID.fromString(badLength);
Assert.assertEquals(0, uuid.getMostSignificantBits());
Assert.assertEquals(0, uuid.getLeastSignificantBits());
}
@Test
public void testHyphen() {
testEx(hyphen1);
testEx(hyphen2);
testEx(hyphen3);
testEx(hyphen4);
}
@Test
public void invalid() {
testEx(invalid1);
testEx(invalid2);
testEx(invalid3);
testEx(invalid4);
testEx(invalid5);
testEx(invalid6);
}
@Test
public void testSimpleToString()
{
String[] uuids = new String[] {
"01234567-89ab-cdef-0000-0123456789ab",
"ffffffff-ffff-ffff-ffff-ffffffffffff",
"00000000-0000-0000-0000-000000000000"
};
for (String uuid : uuids) {
Assert.assertEquals(uuid, NessUUID.toString(UUID.fromString(uuid)));
}
}
@Test
public void test100ToString()
{
final long PRIME = 514229;
for (long msb = 0; msb < 10; msb++)
{
for (long lsb = msb; lsb < msb + 10; lsb++)
{
UUID uuid = new UUID(msb * PRIME, lsb * PRIME * PRIME);
Assert.assertEquals(uuid.toString(), NessUUID.toString(uuid));
}
}
}
// makes testing multiple exceptions less verbose
private void testEx(String str) {
try {
NessUUID.fromString(hyphen1);
} catch (IllegalArgumentException e) {
return;
}
Assert.fail(str);
}
/*
* Add the luni tests from http://svn.apache.org/viewvc/harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/UUIDTest.java?revision=929252
*/
/**
* @see UUID#fromString(String)
*/
@Test
public void test_fromString() {
UUID actual = NessUUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
UUID expected = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
Assert.assertEquals(expected, actual);
Assert.assertEquals(2, actual.variant());
Assert.assertEquals(1, actual.version());
Assert.assertEquals(130742845922168750L, actual.timestamp());
Assert.assertEquals(10085, actual.clockSequence());
Assert.assertEquals(690568981494L, actual.node());
actual = NessUUID.fromString("00000000-0000-1000-8000-000000000000");
expected = new UUID(0x0000000000001000L, 0x8000000000000000L);
Assert.assertEquals(expected, actual);
Assert.assertEquals(2, actual.variant());
Assert.assertEquals(1, actual.version());
Assert.assertEquals(0L, actual.timestamp());
Assert.assertEquals(0, actual.clockSequence());
Assert.assertEquals(0L, actual.node());
try {
NessUUID.fromString(null);
Assert.fail("No NPE");
} catch (NullPointerException e) {}
try {
NessUUID.fromString("");
Assert.fail("No IAE");
} catch (IllegalArgumentException e) {}
try {
NessUUID.fromString("f81d4fae_7dec-11d0-a765-00a0c91e6bf6");
Assert.fail("No IAE");
} catch (IllegalArgumentException e) {}
try {
NessUUID.fromString("f81d4fae-7dec_11d0-a765-00a0c91e6bf6");
Assert.fail("No IAE");
} catch (IllegalArgumentException e) {}
try {
NessUUID.fromString("f81d4fae-7dec-11d0_a765-00a0c91e6bf6");
Assert.fail("No IAE");
} catch (IllegalArgumentException e) {}
try {
NessUUID.fromString("f81d4fae-7dec-11d0-a765_00a0c91e6bf6");
Assert.fail("No IAE");
} catch (IllegalArgumentException e) {}
}
/**
* @tests java.util.UUID#fromString(String)
*/
@Test
public void test_fromString_LString_Exception() {
UUID uuid = NessUUID.fromString("0-0-0-0-0");
try {
uuid = NessUUID.fromString("0-0-0-0-");
Assert.fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
uuid = NessUUID.fromString("-0-0-0-0-0");
Assert.fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
uuid = NessUUID.fromString("-0-0-0-0");
Assert.fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
uuid = NessUUID.fromString("-0-0-0-");
Assert.fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
uuid = NessUUID.fromString("0--0-0-0");
Assert.fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
uuid = NessUUID.fromString("0-0-0-0-");
Assert.fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
uuid = NessUUID.fromString("-1-0-0-0-0");
Assert.fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
uuid = NessUUID.fromString("123456789-0-0-0-0");
Assert.assertEquals(0x2345678900000000L, uuid.getMostSignificantBits());
Assert.assertEquals(0x0L, uuid.getLeastSignificantBits());
uuid = NessUUID.fromString("111123456789-0-0-0-0");
Assert.assertEquals(0x2345678900000000L, uuid.getMostSignificantBits());
Assert.assertEquals(0x0L, uuid.getLeastSignificantBits());
uuid = NessUUID.fromString("7fffffffffffffff-0-0-0-0");
Assert.assertEquals(0xffffffff00000000L, uuid.getMostSignificantBits());
Assert.assertEquals(0x0L, uuid.getLeastSignificantBits());
try {
uuid = NessUUID.fromString("80000000000000000-0-0-0-0");
Assert.fail("should throw IllegalArgumentException that contains NumberFormatException");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getCause() != null && e.getCause() instanceof NumberFormatException);
}
uuid = UUID
.fromString("7fffffffffffffff-7fffffffffffffff-7fffffffffffffff-0-0");
Assert.assertEquals(0xffffffffffffffffL, uuid.getMostSignificantBits());
Assert.assertEquals(0x0L, uuid.getLeastSignificantBits());
uuid = NessUUID.fromString("0-0-0-7fffffffffffffff-7fffffffffffffff");
Assert.assertEquals(0x0L, uuid.getMostSignificantBits());
Assert.assertEquals(0xffffffffffffffffL, uuid.getLeastSignificantBits());
try {
NessUUID.fromString("0-0-0-80000000000000000-0");
Assert.fail("should throw IllegalArgumentException that contains NumberFormatException");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getCause() != null && e.getCause() instanceof NumberFormatException);
}
try {
NessUUID.fromString("0-0-0-0-80000000000000000");
Assert.fail("should throw IllegalArgumentException that contains NumberFormatException");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getCause() != null && e.getCause() instanceof NumberFormatException);
}
}
@Test
public void test_toString() {
UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
String actual = NessUUID.toString(uuid);
Assert.assertEquals("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", actual);
uuid = new UUID(0x0000000000001000L, 0x8000000000000000L);
actual = NessUUID.toString(uuid);
Assert.assertEquals("00000000-0000-1000-8000-000000000000", actual);
}
@Test
public void testDecode()
{
doDecodeTest("-7fffffffffffffff-", 0x7fffffffffffffffL);
doDecodeTest("-8000000000000001-", 0x8000000000000001L);
}
private void doDecodeTest(String value, long expected)
{
long result = NessUUID.decode(value, new int [] { value.indexOf('-'), value.lastIndexOf('-') }, 0);
Assert.assertEquals(expected, result);
}
}