From 29b5628dd71a1566eec5e9a93e7cb32f523cdd78 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Sat, 28 Oct 2023 22:02:35 -0400 Subject: [PATCH] =?UTF-8?q?CODEBANK-5=20=E6=9B=B4=E6=96=B0=20maven-javadoc?= =?UTF-8?q?-plugin=20=E7=9A=84=E6=8F=92=E4=BB=B6=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=B8=BA=203.6.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core-java-modules/core-java-uuid/README.md | 8 + core-java-modules/core-java-uuid/pom.xml | 149 ++++++++++++++++ .../JavaUUIDCreatorBenchmark.java | 42 +++++ .../timebaseduuid/JavaUUIDCreatorExample.java | 13 ++ .../timebaseduuid/UUIDCreatorBenchmark.java | 42 +++++ .../timebaseduuid/UUIDCreatorExample.java | 13 ++ .../java/com/baeldung/uuid/UUIDGenerator.java | 164 ++++++++++++++++++ .../src/main/resources/log4j.properties | 6 + .../src/main/resources/log4j2.xml | 13 ++ .../baeldung/uuid/UUIDFromStringUnitTest.java | 62 +++++++ .../baeldung/uuid/UUIDGeneratorUnitTest.java | 84 +++++++++ .../baeldung/uuid/UUIDValidatorUnitTest.java | 29 ++++ .../src/test/resources/log4j.properties | 6 + .../src/test/resources/log4j2.xml | 13 ++ .../resources/log4jstructuraldp.properties | 9 + 15 files changed, 653 insertions(+) create mode 100644 core-java-modules/core-java-uuid/README.md create mode 100644 core-java-modules/core-java-uuid/pom.xml create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDGenerator.java create mode 100644 core-java-modules/core-java-uuid/src/main/resources/log4j.properties create mode 100644 core-java-modules/core-java-uuid/src/main/resources/log4j2.xml create mode 100644 core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDFromStringUnitTest.java create mode 100644 core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDGeneratorUnitTest.java create mode 100644 core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDValidatorUnitTest.java create mode 100644 core-java-modules/core-java-uuid/src/test/resources/log4j.properties create mode 100644 core-java-modules/core-java-uuid/src/test/resources/log4j2.xml create mode 100644 core-java-modules/core-java-uuid/src/test/resources/log4jstructuraldp.properties diff --git a/core-java-modules/core-java-uuid/README.md b/core-java-modules/core-java-uuid/README.md new file mode 100644 index 0000000000..bd7bd9d9da --- /dev/null +++ b/core-java-modules/core-java-uuid/README.md @@ -0,0 +1,8 @@ +## Core Java UUID + +### Relevant Articles: +- [Generating Alphanumeric UUID String in Java](https://www.baeldung.com/java-generate-alphanumeric-uuid) +- [Guide to UUID in Java](http://www.baeldung.com/java-uuid) +- [Validate UUID String in Java](https://www.baeldung.com/java-validate-uuid-string) +- [Generate the Same UUID From a String in Java](https://www.baeldung.com/java-generate-same-uuid-from-string) +- [Generating Time Based UUIDs](https://www.baeldung.com/java-generating-time-based-uuids) diff --git a/core-java-modules/core-java-uuid/pom.xml b/core-java-modules/core-java-uuid/pom.xml new file mode 100644 index 0000000000..030d24273c --- /dev/null +++ b/core-java-modules/core-java-uuid/pom.xml @@ -0,0 +1,149 @@ + + + 4.0.0 + core-java-uuid + core-java-uuid + jar + + + com.ossez.core-java-modules + core-java-modules + 0.0.2-SNAPSHOT + + + + + com.github.f4b6a3 + uuid-creator + 5.2.0 + + + com.fasterxml.uuid + java-uuid-generator + 4.1.0 + + + com.github.f4b6a3 + tsid-creator + 5.2.3 + + + + + core-java-uuid + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + + + org.codehaus.mojo + exec-maven-plugin + + java + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + -Xmx300m + -XX:+UseParallelGC + -classpath + + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + 17 + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*ManualTest.java + + + **/*IntegrationTest.java + **/*IntTest.java + + + + + + + json + + + + + org.codehaus.mojo + exec-maven-plugin + + + run-benchmarks + + none + + exec + + + test + java + + -classpath + + org.openjdk.jmh.Main + .* + + + + + + + + + + + + 3.6.0 + + + diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java new file mode 100644 index 0000000000..20b2c127bd --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java @@ -0,0 +1,42 @@ +package com.baeldung.timebaseduuid; + +import com.fasterxml.uuid.Generators; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +public class JavaUUIDCreatorBenchmark { + +public static void main(String[] args) throws InterruptedException { + + int threadCount = 128; + int iterationCount = 100_000; + Map uuidMap = new ConcurrentHashMap<>(); + AtomicLong collisionCount = new AtomicLong(); + long startNanos = System.nanoTime(); + CountDownLatch endLatch = new CountDownLatch(threadCount); + + for (long i = 0; i < threadCount; i++) { + long threadId = i; + new Thread(() -> { + for (long j = 0; j < iterationCount; j++) { + UUID uuid = Generators.timeBasedGenerator().generate(); + Long existingUUID = uuidMap.put(uuid, (threadId * iterationCount) + j); + if(existingUUID != null) { + collisionCount.incrementAndGet(); + } + } + endLatch.countDown(); + }).start(); + } + + endLatch.await(); + System.out.println(threadCount * iterationCount + " UUIDs generated, " + collisionCount + " collisions in " + + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos) + "ms"); +} +} + diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java new file mode 100644 index 0000000000..b59d7e236a --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java @@ -0,0 +1,13 @@ +package com.baeldung.timebaseduuid; + +import com.fasterxml.uuid.Generators; + +public class JavaUUIDCreatorExample { + + public static void main(String[] args) { + System.out.println("UUID Version 1: " + Generators.timeBasedGenerator().generate()); + System.out.println("UUID Version 6: " + Generators.timeBasedReorderedGenerator().generate()); + System.out.println("UUID Version 7: " + Generators.timeBasedEpochGenerator().generate()); + + } +} diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java new file mode 100644 index 0000000000..d93cd73a25 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java @@ -0,0 +1,42 @@ +package com.baeldung.timebaseduuid; + +import com.github.f4b6a3.uuid.UuidCreator; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +public class UUIDCreatorBenchmark { + + public static void main(String[] args) throws InterruptedException { + + int threadCount = 128; + int iterationCount = 100_000; + Map uuidMap = new ConcurrentHashMap<>(); + AtomicLong collisionCount = new AtomicLong(); + long startNanos = System.nanoTime(); + CountDownLatch endLatch = new CountDownLatch(threadCount); + + for (long i = 0; i < threadCount; i++) { + long threadId = i; + new Thread(() -> { + for (long j = 0; j < iterationCount; j++) { + UUID uuid = UuidCreator.getTimeBased(); + Long existingUUID = uuidMap.put(uuid, (threadId * iterationCount) + j); + if(existingUUID != null) { + collisionCount.incrementAndGet(); + } + } + endLatch.countDown(); + }).start(); + } + + endLatch.await(); + System.out.println(threadCount * iterationCount + " UUIDs generated, " + collisionCount + " collisions in " + + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos) + "ms"); + } +} diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java new file mode 100644 index 0000000000..fad2f55c93 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java @@ -0,0 +1,13 @@ +package com.baeldung.timebaseduuid; + +import com.github.f4b6a3.uuid.UuidCreator; + +public class UUIDCreatorExample { + + public static void main(String[] args) { + System.out.println("UUID Version 1: " + UuidCreator.getTimeBased()); + System.out.println("UUID Version 6: " + UuidCreator.getTimeOrdered()); + System.out.println("UUID Version 7: " + UuidCreator.getTimeOrderedEpoch()); + } +} + diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDGenerator.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDGenerator.java new file mode 100644 index 0000000000..68ff9ce1d4 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDGenerator.java @@ -0,0 +1,164 @@ +package com.baeldung.uuid; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.Random; +import java.util.UUID; + +public final class UUIDGenerator { + + private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); + + private UUIDGenerator() { + } + + /** + * Type 1 UUID Generation + */ + public static UUID generateType1UUID() { + + final long most64SigBits = get64MostSignificantBitsForVersion1(); + final long least64SigBits = get64LeastSignificantBitsForVersion1(); + + return new UUID(most64SigBits, least64SigBits); + } + + private static long get64LeastSignificantBitsForVersion1() { + final long random63BitLong = new Random().nextLong() & 0x3FFFFFFFFFFFFFFFL; + long variant3BitFlag = 0x8000000000000000L; + return random63BitLong | variant3BitFlag; + } + + private static long get64MostSignificantBitsForVersion1() { + final long currentTimeMillis = System.currentTimeMillis(); + final long time_low = (currentTimeMillis & 0x0000_0000_FFFF_FFFFL) << 32; + final long time_mid = ((currentTimeMillis >> 32) & 0xFFFF) << 16; + final long version = 1 << 12; + final long time_high = ((currentTimeMillis >> 48) & 0x0FFF); + return time_low | time_mid | version | time_high; + } + + /** + * Type 3 UUID Generation + */ + public static UUID generateType3UUID(String namespace, String name) { + + final byte[] nameSpaceBytes = bytesFromUUID(namespace); + final byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8); + final byte[] result = joinBytes(nameSpaceBytes, nameBytes); + + return UUID.nameUUIDFromBytes(result); + } + + /** + * Type 4 UUID Generation + */ + public static UUID generateType4UUID() { + return UUID.randomUUID(); + } + + /** + * Type 5 UUID Generation + */ + public static UUID generateType5UUID(String namespace, String name) { + + final byte[] nameSpaceBytes = bytesFromUUID(namespace); + final byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8); + final byte[] result = joinBytes(nameSpaceBytes, nameBytes); + + return type5UUIDFromBytes(result); + } + + public static UUID type5UUIDFromBytes(byte[] name) { + final MessageDigest md; + try { + md = MessageDigest.getInstance("SHA-1"); + } catch (NoSuchAlgorithmException exception) { + throw new InternalError("SHA-1 not supported", exception); + } + final byte[] bytes = Arrays.copyOfRange(md.digest(name), 0, 16); + bytes[6] &= 0x0f; /* clear version */ + bytes[6] |= 0x50; /* set to version 5 */ + bytes[8] &= 0x3f; /* clear variant */ + bytes[8] |= 0x80; /* set to IETF variant */ + return constructType5UUID(bytes); + } + + private static UUID constructType5UUID(byte[] data) { + long msb = 0; + long lsb = 0; + assert data.length == 16 : "data must be 16 bytes in length"; + + for (int i = 0; i < 8; i++) { + msb = (msb << 8) | (data[i] & 0xff); + } + + for (int i = 8; i < 16; i++) { + lsb = (lsb << 8) | (data[i] & 0xff); + } + return new UUID(msb, lsb); + } + + private static byte[] bytesFromUUID(String uuidHexString) { + final String normalizedUUIDHexString = uuidHexString.replace("-", ""); + + assert normalizedUUIDHexString.length() == 32; + + final byte[] bytes = new byte[16]; + for (int i = 0; i < 16; i++) { + final byte b = hexToByte(normalizedUUIDHexString.substring(i * 2, i * 2 + 2)); + bytes[i] = b; + } + return bytes; + } + + public static byte hexToByte(String hexString) { + final int firstDigit = Character.digit(hexString.charAt(0), 16); + final int secondDigit = Character.digit(hexString.charAt(1), 16); + return (byte) ((firstDigit << 4) + secondDigit); + } + + public static byte[] joinBytes(byte[] byteArray1, byte[] byteArray2) { + final int finalLength = byteArray1.length + byteArray2.length; + final byte[] result = new byte[finalLength]; + + System.arraycopy(byteArray1, 0, result, 0, byteArray1.length); + System.arraycopy(byteArray2, 0, result, byteArray1.length, byteArray2.length); + return result; + } + + public static UUID generateType5UUID(String name) { + + try { + + final byte[] bytes = name.getBytes(StandardCharsets.UTF_8); + final MessageDigest md = MessageDigest.getInstance("SHA-1"); + + final byte[] hash = md.digest(bytes); + + long msb = getLeastAndMostSignificantBitsVersion5(hash, 0); + long lsb = getLeastAndMostSignificantBitsVersion5(hash, 8); + // Set the version field + msb &= ~(0xfL << 12); + msb |= 5L << 12; + // Set the variant field to 2 + lsb &= ~(0x3L << 62); + lsb |= 2L << 62; + return new UUID(msb, lsb); + + } catch (NoSuchAlgorithmException e) { + throw new AssertionError(e); + } + } + + private static long getLeastAndMostSignificantBitsVersion5(final byte[] src, final int offset) { + long ans = 0; + for (int i = offset + 7; i >= offset; i -= 1) { + ans <<= 8; + ans |= src[i] & 0xffL; + } + return ans; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-uuid/src/main/resources/log4j.properties b/core-java-modules/core-java-uuid/src/main/resources/log4j.properties new file mode 100644 index 0000000000..621cf01735 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/resources/log4j.properties @@ -0,0 +1,6 @@ +log4j.rootLogger=DEBUG, A1 + +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n \ No newline at end of file diff --git a/core-java-modules/core-java-uuid/src/main/resources/log4j2.xml b/core-java-modules/core-java-uuid/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..a824bef9b0 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDFromStringUnitTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDFromStringUnitTest.java new file mode 100644 index 0000000000..381d8715d5 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDFromStringUnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.uuid; + +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class UUIDFromStringUnitTest { + @Test + void whenStringInUUIDFormat_thenFromStringWorks() { + String inputStr = "bbcc4621-d88f-4a94-ae2f-b38072bf5087"; + + UUID uuid = UUID.fromString(inputStr); + UUID uuid2 = UUID.fromString(inputStr); + UUID uuid3 = UUID.fromString(inputStr); + + assertEquals(inputStr, uuid.toString()); + + assertEquals(uuid, uuid2); + assertEquals(uuid, uuid3); + + } + + @Test + void whenStringNotInUUIDFormat_thenFromStringRaisesException() { + String inputStr = "I am not a standard UUID representation."; + assertThrows(IllegalArgumentException.class, () -> UUID.fromString(inputStr)); + } + + @Test + void whenStringInFreeFormat_thenNameUUIDFromBytesWorks() { + String inputStr = "I am not a standard UUID representation."; + + UUID uuid = UUID.nameUUIDFromBytes(inputStr.getBytes()); + UUID uuid2 = UUID.nameUUIDFromBytes(inputStr.getBytes()); + UUID uuid3 = UUID.nameUUIDFromBytes(inputStr.getBytes()); + + assertTrue(uuid != null); + + assertEquals(uuid, uuid2); + assertEquals(uuid, uuid3); + + assertEquals(3, uuid.version()); + } + + @Test + void whenStringInFreeFormat_thenGenerateVer5UUIDWorks() { + String inputStr = "I am not a standard UUID representation."; + + UUID uuid = UUIDGenerator.generateType5UUID(inputStr); + UUID uuid2 = UUIDGenerator.generateType5UUID(inputStr); + UUID uuid3 = UUIDGenerator.generateType5UUID(inputStr); + + assertEquals(5, uuid.version()); + + assertTrue(uuid != null); + + assertEquals(uuid, uuid2); + assertEquals(uuid, uuid3); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDGeneratorUnitTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDGeneratorUnitTest.java new file mode 100644 index 0000000000..3c85eee3ff --- /dev/null +++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDGeneratorUnitTest.java @@ -0,0 +1,84 @@ +package com.baeldung.uuid; + +import org.junit.jupiter.api.Test; + +import java.io.UnsupportedEncodingException; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class UUIDGeneratorUnitTest { + + private static final String NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8"; + private static final String NAMESPACE_DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8"; + + @Test + void shouldGenerateType1UUIDWithCorrectVersionAndVariant() { + UUID uuid = UUIDGenerator.generateType1UUID(); + assertEquals(36, uuid.toString().length()); + assertEquals(1, uuid.version()); + assertEquals(2, uuid.variant()); + } + + @Test + void shouldGenerateType1UUIDWithTheCurrentDate() { + UUID uuid = UUIDGenerator.generateType1UUID(); + long time = uuid.timestamp(); + LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()); + assertEquals(LocalDate.now(), dateTime.toLocalDate()); + } + + @Test + void version_3_UUID_is_correctly_generated_for_domain_baeldung_com() { + + UUID uuid = UUIDGenerator.generateType3UUID(NAMESPACE_DNS, "baeldung.com"); + + assertEquals("23785b78-0132-3ac6-aff6-cfd5be162139", uuid.toString()); + assertEquals(3, uuid.version()); + assertEquals(2, uuid.variant()); + } + + @Test + void version_3_UUID_is_correctly_generated_for_domain_d() { + + UUID uuid = UUIDGenerator.generateType3UUID(NAMESPACE_DNS, "d"); + + assertEquals("dbd41ecb-f466-33de-b309-1468addfc63b", uuid.toString()); + assertEquals(3, uuid.version()); + assertEquals(2, uuid.variant()); + } + + @Test + public void version_4_UUID_is_generated_with_correct_length_version_and_variant() { + + UUID uuid = UUIDGenerator.generateType4UUID(); + + assertEquals(36, uuid.toString().length()); + assertEquals(4, uuid.version()); + assertEquals(2, uuid.variant()); + } + + @Test + public void version_5_UUID_is_correctly_generated_for_domain_baeldung_com() throws UnsupportedEncodingException { + + UUID uuid = UUIDGenerator.generateType5UUID(NAMESPACE_URL, "baeldung.com"); + + assertEquals("aeff44a5-8a61-52b6-bcbe-c8e5bd7d0300", uuid.toString()); + assertEquals(5, uuid.version()); + assertEquals(2, uuid.variant()); + } + + @Test + public void version_5_UUID_is_correctly_generated_for_domain_baeldung_name() { + + UUID uuid = UUIDGenerator.generateType5UUID("baeldung.com"); + + assertEquals("efd5462b-b07a-52a3-94ea-bf575c0e0e75", uuid.toString()); + assertEquals(5, uuid.version()); + assertEquals(2, uuid.variant()); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDValidatorUnitTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDValidatorUnitTest.java new file mode 100644 index 0000000000..76ed644dd7 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDValidatorUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.uuid; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.UUID; +import java.util.regex.Pattern; + +public class UUIDValidatorUnitTest { + + @Test + public void whenValidUUIDStringIsValidated_thenValidationSucceeds() { + String validUUID = "26929514-237c-11ed-861d-0242ac120002"; + Assertions.assertEquals(UUID.fromString(validUUID).toString(), validUUID); + + String invalidUUID = "invalid-uuid"; + Assertions.assertThrows(IllegalArgumentException.class, () -> UUID.fromString(invalidUUID)); + } + + @Test + public void whenUUIDIsValidatedUsingRegex_thenValidationSucceeds() { + Pattern UUID_REGEX = + Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); + + Assertions.assertTrue(UUID_REGEX.matcher("26929514-237c-11ed-861d-0242ac120002").matches()); + + Assertions.assertFalse(UUID_REGEX.matcher("invalid-uuid").matches()); + } +} diff --git a/core-java-modules/core-java-uuid/src/test/resources/log4j.properties b/core-java-modules/core-java-uuid/src/test/resources/log4j.properties new file mode 100644 index 0000000000..621cf01735 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/test/resources/log4j.properties @@ -0,0 +1,6 @@ +log4j.rootLogger=DEBUG, A1 + +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n \ No newline at end of file diff --git a/core-java-modules/core-java-uuid/src/test/resources/log4j2.xml b/core-java-modules/core-java-uuid/src/test/resources/log4j2.xml new file mode 100644 index 0000000000..a824bef9b0 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/test/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-uuid/src/test/resources/log4jstructuraldp.properties b/core-java-modules/core-java-uuid/src/test/resources/log4jstructuraldp.properties new file mode 100644 index 0000000000..5bc2bfe4b9 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/test/resources/log4jstructuraldp.properties @@ -0,0 +1,9 @@ + +# Root logger +log4j.rootLogger=INFO, file, stdout + +# Write to console +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file