|
| 1 | +package jdk.sandbox.demo; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.api.RepeatedTest; |
| 5 | +import org.junit.jupiter.api.parallel.Execution; |
| 6 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 7 | + |
| 8 | +import java.util.UUID; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.concurrent.ConcurrentHashMap; |
| 12 | +import java.util.concurrent.CountDownLatch; |
| 13 | +import java.util.concurrent.atomic.AtomicInteger; |
| 14 | +import java.util.regex.Pattern; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.*; |
| 17 | + |
| 18 | +class UUIDGeneratorTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + void testTimeThenRandomGeneratesUUID() { |
| 22 | + UUID uuid = UUIDGenerator.timeThenRandom(); |
| 23 | + assertNotNull(uuid); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void testTimeThenRandomGeneratesUniqueUUIDs() { |
| 28 | + Set<UUID> uuids = new HashSet<>(); |
| 29 | + for (int i = 0; i < 1000; i++) { |
| 30 | + UUID uuid = UUIDGenerator.timeThenRandom(); |
| 31 | + assertTrue(uuids.add(uuid), "Generated duplicate UUID: " + uuid); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void testTimeThenRandomIncreasingOrder() { |
| 37 | + UUID prev = UUIDGenerator.timeThenRandom(); |
| 38 | + for (int i = 0; i < 100; i++) { |
| 39 | + UUID current = UUIDGenerator.timeThenRandom(); |
| 40 | + // MSB should be increasing (time+counter) |
| 41 | + assertTrue(current.getMostSignificantBits() >= prev.getMostSignificantBits(), |
| 42 | + "UUIDs should be time-ordered"); |
| 43 | + prev = current; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testUniqueThenTimeGeneratesUUID() { |
| 49 | + long uniqueMsb = 0x123456789ABCDEF0L; |
| 50 | + UUID uuid = UUIDGenerator.uniqueThenTime(uniqueMsb); |
| 51 | + assertNotNull(uuid); |
| 52 | + assertEquals(uniqueMsb, uuid.getMostSignificantBits()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testUniqueThenTimeWithDifferentUserIds() { |
| 57 | + long userId1 = 1L; |
| 58 | + long userId2 = 2L; |
| 59 | + |
| 60 | + UUID uuid1 = UUIDGenerator.uniqueThenTime(userId1); |
| 61 | + UUID uuid2 = UUIDGenerator.uniqueThenTime(userId2); |
| 62 | + |
| 63 | + assertNotEquals(uuid1, uuid2); |
| 64 | + assertEquals(userId1, uuid1.getMostSignificantBits()); |
| 65 | + assertEquals(userId2, uuid2.getMostSignificantBits()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testFormatAsUUIDLowercase() { |
| 70 | + UUID uuid = new UUID(0x123456789ABCDEF0L, 0xFEDCBA9876543210L); |
| 71 | + String formatted = UUIDGenerator.formatAsUUID(uuid); |
| 72 | + |
| 73 | + assertEquals(36, formatted.length()); |
| 74 | + assertTrue(formatted.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"), |
| 75 | + "Should match UUID format: " + formatted); |
| 76 | + assertEquals("12345678-9abc-def0-fedc-ba9876543210", formatted); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testFormatAsUUIDUppercase() { |
| 81 | + UUID uuid = new UUID(0x123456789ABCDEF0L, 0xFEDCBA9876543210L); |
| 82 | + String formatted = UUIDGenerator.formatAsUUID(uuid, true); |
| 83 | + |
| 84 | + assertEquals(36, formatted.length()); |
| 85 | + assertTrue(formatted.matches("[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"), |
| 86 | + "Should match uppercase UUID format: " + formatted); |
| 87 | + assertEquals("12345678-9ABC-DEF0-FEDC-BA9876543210", formatted); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void testFormatAsUUIDDefaultIsLowercase() { |
| 92 | + UUID uuid = new UUID(0x123456789ABCDEF0L, 0xFEDCBA9876543210L); |
| 93 | + String formatted = UUIDGenerator.formatAsUUID(uuid); |
| 94 | + String formattedExplicit = UUIDGenerator.formatAsUUID(uuid, false); |
| 95 | + |
| 96 | + assertEquals(formatted, formattedExplicit); |
| 97 | + assertEquals(formatted, formatted.toLowerCase()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void testFormatAsDenseKeyLength() { |
| 102 | + UUID uuid = UUIDGenerator.timeThenRandom(); |
| 103 | + String denseKey = UUIDGenerator.formatAsDenseKey(uuid); |
| 104 | + |
| 105 | + assertEquals(22, denseKey.length(), "Dense key should be 22 characters"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testFormatAsDenseKeyAlphanumeric() { |
| 110 | + UUID uuid = UUIDGenerator.timeThenRandom(); |
| 111 | + String denseKey = UUIDGenerator.formatAsDenseKey(uuid); |
| 112 | + |
| 113 | + assertTrue(denseKey.matches("[0-9A-Za-z]+"), |
| 114 | + "Dense key should only contain alphanumeric characters"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void testFormatAsDenseKeyUniqueness() { |
| 119 | + Set<String> denseKeys = new HashSet<>(); |
| 120 | + for (int i = 0; i < 1000; i++) { |
| 121 | + UUID uuid = UUIDGenerator.timeThenRandom(); |
| 122 | + String denseKey = UUIDGenerator.formatAsDenseKey(uuid); |
| 123 | + assertTrue(denseKeys.add(denseKey), "Generated duplicate dense key: " + denseKey); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void testFormatAsDenseKeyZeroPadding() { |
| 129 | + // Test with UUID that has many leading zeros |
| 130 | + UUID uuid = new UUID(0L, 1L); |
| 131 | + String denseKey = UUIDGenerator.formatAsDenseKey(uuid); |
| 132 | + |
| 133 | + assertEquals(22, denseKey.length(), "Dense key should be zero-padded to 22 characters"); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testFormatAsDenseKeyLexicographicOrdering() { |
| 138 | + // Time-ordered UUIDs should produce lexicographically ordered dense keys |
| 139 | + UUID uuid1 = UUIDGenerator.timeThenRandom(); |
| 140 | + String key1 = UUIDGenerator.formatAsDenseKey(uuid1); |
| 141 | + |
| 142 | + // Wait a bit to ensure time advances |
| 143 | + try { |
| 144 | + Thread.sleep(2); |
| 145 | + } catch (InterruptedException e) { |
| 146 | + Thread.currentThread().interrupt(); |
| 147 | + } |
| 148 | + |
| 149 | + UUID uuid2 = UUIDGenerator.timeThenRandom(); |
| 150 | + String key2 = UUIDGenerator.formatAsDenseKey(uuid2); |
| 151 | + |
| 152 | + assertTrue(key1.compareTo(key2) <= 0, |
| 153 | + "Dense keys should maintain lexicographic ordering for time-ordered UUIDs"); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void testLazyRandomInitialization() { |
| 158 | + // This test verifies that the LazyRandom pattern works correctly |
| 159 | + // by generating multiple UUIDs and ensuring they are all different |
| 160 | + Set<UUID> uuids = new HashSet<>(); |
| 161 | + for (int i = 0; i < 100; i++) { |
| 162 | + UUID uuid = UUIDGenerator.timeThenRandom(); |
| 163 | + assertTrue(uuids.add(uuid), "LazyRandom should generate unique random values"); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void testThreadSafetyOfLazyRandom() throws InterruptedException { |
| 169 | + final int threadCount = 10; |
| 170 | + final int uuidsPerThread = 100; |
| 171 | + final CountDownLatch startLatch = new CountDownLatch(1); |
| 172 | + final CountDownLatch doneLatch = new CountDownLatch(threadCount); |
| 173 | + final Set<UUID> allUuids = ConcurrentHashMap.newKeySet(); |
| 174 | + final AtomicInteger duplicateCount = new AtomicInteger(0); |
| 175 | + |
| 176 | + for (int i = 0; i < threadCount; i++) { |
| 177 | + new Thread(() -> { |
| 178 | + try { |
| 179 | + startLatch.await(); // Wait for all threads to be ready |
| 180 | + for (int j = 0; j < uuidsPerThread; j++) { |
| 181 | + UUID uuid = UUIDGenerator.timeThenRandom(); |
| 182 | + if (!allUuids.add(uuid)) { |
| 183 | + duplicateCount.incrementAndGet(); |
| 184 | + } |
| 185 | + } |
| 186 | + } catch (InterruptedException e) { |
| 187 | + Thread.currentThread().interrupt(); |
| 188 | + } finally { |
| 189 | + doneLatch.countDown(); |
| 190 | + } |
| 191 | + }).start(); |
| 192 | + } |
| 193 | + |
| 194 | + startLatch.countDown(); // Start all threads |
| 195 | + doneLatch.await(); // Wait for all threads to complete |
| 196 | + |
| 197 | + assertEquals(0, duplicateCount.get(), "Should not generate duplicate UUIDs in multi-threaded scenario"); |
| 198 | + assertEquals(threadCount * uuidsPerThread, allUuids.size()); |
| 199 | + } |
| 200 | + |
| 201 | + @RepeatedTest(10) |
| 202 | + void testSequenceCounterWraparound() { |
| 203 | + // Generate many UUIDs quickly to test sequence counter |
| 204 | + Set<UUID> uuids = new HashSet<>(); |
| 205 | + for (int i = 0; i < 1000; i++) { |
| 206 | + UUID uuid = UUIDGenerator.timeThenRandom(); |
| 207 | + assertTrue(uuids.add(uuid), "Should generate unique UUIDs even with sequence counter"); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + void testTimeCounterBitsFormat() { |
| 213 | + // Test that timeCounterBits produces reasonable values |
| 214 | + long bits1 = UUIDGenerator.timeCounterBits(); |
| 215 | + long bits2 = UUIDGenerator.timeCounterBits(); |
| 216 | + |
| 217 | + // Should be increasing or equal (if same millisecond) |
| 218 | + assertTrue(bits2 >= bits1, "Time counter bits should be non-decreasing"); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + void testUniqueThenTimePreservesUniqueBits() { |
| 223 | + long uniqueMsb = 0xFFFFFFFFFFFFFFFL; |
| 224 | + UUID uuid = UUIDGenerator.uniqueThenTime(uniqueMsb); |
| 225 | + |
| 226 | + assertEquals(uniqueMsb, uuid.getMostSignificantBits(), |
| 227 | + "uniqueThenTime should preserve the unique MSB"); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + void testFormatAsDenseKeyDeterministic() { |
| 232 | + UUID uuid = new UUID(0x123456789ABCDEF0L, 0xFEDCBA9876543210L); |
| 233 | + String key1 = UUIDGenerator.formatAsDenseKey(uuid); |
| 234 | + String key2 = UUIDGenerator.formatAsDenseKey(uuid); |
| 235 | + |
| 236 | + assertEquals(key1, key2, "Same UUID should produce same dense key"); |
| 237 | + } |
| 238 | +} |
0 commit comments