Skip to content

Commit ebf0f00

Browse files
authored
BAEL-5650 jar file path (eugenp#12463)
* BAEL-5650 jar file path * move to jar module
1 parent 91878d9 commit ebf0f00

4 files changed

Lines changed: 106 additions & 4 deletions

File tree

core-java-modules/core-java-jar/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
<artifactId>moneta</artifactId>
5555
<version>${javamoney.moneta.version}</version>
5656
</dependency>
57+
<dependency>
58+
<groupId>org.mockito</groupId>
59+
<artifactId>mockito-junit-jupiter</artifactId>
60+
<version>${mockito.version}</version>
61+
<scope>test</scope>
62+
</dependency>
5763
</dependencies>
5864

5965
<build>
@@ -266,6 +272,7 @@
266272
<!-- util -->
267273
<unix4j.version>0.4</unix4j.version>
268274
<grep4j.version>1.8.7</grep4j.version>
275+
<mockito.version>4.6.1</mockito.version>
269276
<!-- maven plugins -->
270277
<javamoney.moneta.version>1.1</javamoney.moneta.version>
271278
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
@@ -279,4 +286,4 @@
279286
<target.version>1.8</target.version>
280287
</properties>
281288

282-
</project>
289+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.jarfile;
2+
3+
import java.net.URISyntaxException;
4+
import java.net.URL;
5+
import java.nio.file.Paths;
6+
7+
public class JarFilePathResolver {
8+
9+
public String getJarFilePath(Class clazz) {
10+
try {
11+
return byGetProtectionDomain(clazz);
12+
} catch (Exception e) {
13+
// cannot get jar file path using byGetProtectionDomain
14+
// Exception handling omitted
15+
}
16+
return byGetResource(clazz);
17+
}
18+
19+
String byGetProtectionDomain(Class clazz) throws URISyntaxException {
20+
URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
21+
return Paths.get(url.toURI()).toString();
22+
}
23+
24+
String byGetResource(Class clazz) {
25+
final URL classResource = clazz.getResource(clazz.getSimpleName() + ".class");
26+
if (classResource == null) {
27+
throw new RuntimeException("class resource is null");
28+
}
29+
30+
final String url = classResource.toString();
31+
if (url.startsWith("jar:file:")) {
32+
// extract 'file:......jarName.jar' part from the url string
33+
String path = url.replaceAll("^jar:(file:.*[.]jar)!/.*", "$1");
34+
try {
35+
return Paths.get(new URL(path).toURI()).toString();
36+
} catch (Exception e) {
37+
throw new RuntimeException("Invalid Jar File URL String");
38+
}
39+
}
40+
throw new RuntimeException("Invalid Jar File URL String");
41+
}
42+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.jarfile;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Mockito.never;
5+
import static org.mockito.Mockito.times;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.when;
8+
9+
import com.google.common.base.Ascii;
10+
import java.io.File;
11+
import java.net.URISyntaxException;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.extension.ExtendWith;
14+
import org.mockito.Spy;
15+
import org.mockito.junit.jupiter.MockitoExtension;
16+
17+
@ExtendWith(MockitoExtension.class)
18+
class JarFilePathResolverUnitTest {
19+
@Spy
20+
JarFilePathResolver jarFilePathResolver;
21+
22+
@Test
23+
void givenClassObjectWhenCallingByGetProtectionDomainShouldGetExpectedPath() throws Exception {
24+
String jarPath = jarFilePathResolver.byGetProtectionDomain(Ascii.class);
25+
assertThat(jarPath).endsWith(".jar").contains("guava");
26+
assertThat(new File(jarPath)).exists();
27+
}
28+
29+
@Test
30+
void givenClassObjectWhenCallingByGetResourceShouldGetExpectedPath() {
31+
String jarPath = jarFilePathResolver.byGetResource(Ascii.class);
32+
assertThat(jarPath).endsWith(".jar").contains("guava");
33+
assertThat(new File(jarPath)).exists();
34+
}
35+
36+
@Test
37+
void givenClassObjectWhenNoSecurityExceptionRaisedShouldGetExpectedPath() throws URISyntaxException {
38+
String jarPath = jarFilePathResolver.getJarFilePath(Ascii.class);
39+
assertThat(jarPath).endsWith(".jar").contains("guava");
40+
assertThat(new File(jarPath)).exists();
41+
verify(jarFilePathResolver, times(1)).byGetProtectionDomain(Ascii.class);
42+
verify(jarFilePathResolver, never()).byGetResource(Ascii.class);
43+
}
44+
45+
@Test
46+
void givenClassObjectWhenSecurityExceptionRaisedShouldGetExpectedPath() throws URISyntaxException {
47+
when(jarFilePathResolver.byGetProtectionDomain(Ascii.class)).thenThrow(new SecurityException("not allowed"));
48+
String jarPath = jarFilePathResolver.getJarFilePath(Ascii.class);
49+
assertThat(jarPath).endsWith(".jar").contains("guava");
50+
assertThat(new File(jarPath)).exists();
51+
verify(jarFilePathResolver, times(1)).byGetProtectionDomain(Ascii.class);
52+
verify(jarFilePathResolver, times(1)).byGetResource(Ascii.class);
53+
}
54+
55+
}

core-java-modules/core-java-lang-5/pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<artifactId>core-java-modules</artifactId>
1414
<version>0.0.1-SNAPSHOT</version>
1515
</parent>
16-
1716
<build>
1817
<finalName>core-java-lang-5</finalName>
1918
<resources>
@@ -23,5 +22,4 @@
2322
</resource>
2423
</resources>
2524
</build>
26-
27-
</project>
25+
</project>

0 commit comments

Comments
 (0)