From b372864029de888933bb6be4293097699acd32a2 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Mon, 11 Jul 2022 04:09:49 +0200 Subject: [PATCH] BAEL-5650 jar file path (#12463) * BAEL-5650 jar file path * move to jar module --- core-java-modules/core-java-jar/pom.xml | 9 ++- .../baeldung/jarfile/JarFilePathResolver.java | 42 ++++++++++++++ .../jarfile/JarFilePathResolverUnitTest.java | 55 +++++++++++++++++++ core-java-modules/core-java-lang-5/pom.xml | 4 +- 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 core-java-modules/core-java-jar/src/main/java/com/baeldung/jarfile/JarFilePathResolver.java create mode 100644 core-java-modules/core-java-jar/src/test/java/com/baeldung/jarfile/JarFilePathResolverUnitTest.java diff --git a/core-java-modules/core-java-jar/pom.xml b/core-java-modules/core-java-jar/pom.xml index 19da9b8a56..c56fcbd8b5 100644 --- a/core-java-modules/core-java-jar/pom.xml +++ b/core-java-modules/core-java-jar/pom.xml @@ -54,6 +54,12 @@ moneta ${javamoney.moneta.version} + + org.mockito + mockito-junit-jupiter + ${mockito.version} + test + @@ -266,6 +272,7 @@ 0.4 1.8.7 + 4.6.1 1.1 3.0.0-M1 @@ -279,4 +286,4 @@ 1.8 - \ No newline at end of file + diff --git a/core-java-modules/core-java-jar/src/main/java/com/baeldung/jarfile/JarFilePathResolver.java b/core-java-modules/core-java-jar/src/main/java/com/baeldung/jarfile/JarFilePathResolver.java new file mode 100644 index 0000000000..3dab91f31a --- /dev/null +++ b/core-java-modules/core-java-jar/src/main/java/com/baeldung/jarfile/JarFilePathResolver.java @@ -0,0 +1,42 @@ +package com.baeldung.jarfile; + +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Paths; + +public class JarFilePathResolver { + + public String getJarFilePath(Class clazz) { + try { + return byGetProtectionDomain(clazz); + } catch (Exception e) { + // cannot get jar file path using byGetProtectionDomain + // Exception handling omitted + } + return byGetResource(clazz); + } + + String byGetProtectionDomain(Class clazz) throws URISyntaxException { + URL url = clazz.getProtectionDomain().getCodeSource().getLocation(); + return Paths.get(url.toURI()).toString(); + } + + String byGetResource(Class clazz) { + final URL classResource = clazz.getResource(clazz.getSimpleName() + ".class"); + if (classResource == null) { + throw new RuntimeException("class resource is null"); + } + + final String url = classResource.toString(); + if (url.startsWith("jar:file:")) { + // extract 'file:......jarName.jar' part from the url string + String path = url.replaceAll("^jar:(file:.*[.]jar)!/.*", "$1"); + try { + return Paths.get(new URL(path).toURI()).toString(); + } catch (Exception e) { + throw new RuntimeException("Invalid Jar File URL String"); + } + } + throw new RuntimeException("Invalid Jar File URL String"); + } +} diff --git a/core-java-modules/core-java-jar/src/test/java/com/baeldung/jarfile/JarFilePathResolverUnitTest.java b/core-java-modules/core-java-jar/src/test/java/com/baeldung/jarfile/JarFilePathResolverUnitTest.java new file mode 100644 index 0000000000..ad84bd9379 --- /dev/null +++ b/core-java-modules/core-java-jar/src/test/java/com/baeldung/jarfile/JarFilePathResolverUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.jarfile; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.base.Ascii; +import java.io.File; +import java.net.URISyntaxException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class JarFilePathResolverUnitTest { + @Spy + JarFilePathResolver jarFilePathResolver; + + @Test + void givenClassObjectWhenCallingByGetProtectionDomainShouldGetExpectedPath() throws Exception { + String jarPath = jarFilePathResolver.byGetProtectionDomain(Ascii.class); + assertThat(jarPath).endsWith(".jar").contains("guava"); + assertThat(new File(jarPath)).exists(); + } + + @Test + void givenClassObjectWhenCallingByGetResourceShouldGetExpectedPath() { + String jarPath = jarFilePathResolver.byGetResource(Ascii.class); + assertThat(jarPath).endsWith(".jar").contains("guava"); + assertThat(new File(jarPath)).exists(); + } + + @Test + void givenClassObjectWhenNoSecurityExceptionRaisedShouldGetExpectedPath() throws URISyntaxException { + String jarPath = jarFilePathResolver.getJarFilePath(Ascii.class); + assertThat(jarPath).endsWith(".jar").contains("guava"); + assertThat(new File(jarPath)).exists(); + verify(jarFilePathResolver, times(1)).byGetProtectionDomain(Ascii.class); + verify(jarFilePathResolver, never()).byGetResource(Ascii.class); + } + + @Test + void givenClassObjectWhenSecurityExceptionRaisedShouldGetExpectedPath() throws URISyntaxException { + when(jarFilePathResolver.byGetProtectionDomain(Ascii.class)).thenThrow(new SecurityException("not allowed")); + String jarPath = jarFilePathResolver.getJarFilePath(Ascii.class); + assertThat(jarPath).endsWith(".jar").contains("guava"); + assertThat(new File(jarPath)).exists(); + verify(jarFilePathResolver, times(1)).byGetProtectionDomain(Ascii.class); + verify(jarFilePathResolver, times(1)).byGetResource(Ascii.class); + } + +} diff --git a/core-java-modules/core-java-lang-5/pom.xml b/core-java-modules/core-java-lang-5/pom.xml index 6d3771bd31..5255866f03 100644 --- a/core-java-modules/core-java-lang-5/pom.xml +++ b/core-java-modules/core-java-lang-5/pom.xml @@ -13,7 +13,6 @@ core-java-modules 0.0.1-SNAPSHOT - core-java-lang-5 @@ -23,5 +22,4 @@ - - \ No newline at end of file +