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 + diff --git a/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/specific/Book.java b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/specific/Book.java new file mode 100644 index 0000000000..5877147082 --- /dev/null +++ b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/specific/Book.java @@ -0,0 +1,17 @@ +package com.baeldung.imports.specific; + +import java.util.Date; +import java.util.List; +import java.util.UUID; + +public class Book { + + private UUID id; + + private String name; + + private Date datePublished; + + private List authors; + +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/wildcard/Book.java b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/wildcard/Book.java new file mode 100644 index 0000000000..18754cdd28 --- /dev/null +++ b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/wildcard/Book.java @@ -0,0 +1,15 @@ +package com.baeldung.imports.wildcard; + +import java.util.*; + +public class Book { + + private UUID id; + + private String name; + + private Date datePublished; + + private List authors; + +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/wildcard/BookView.java b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/wildcard/BookView.java new file mode 100644 index 0000000000..e6e21ab03d --- /dev/null +++ b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/wildcard/BookView.java @@ -0,0 +1,17 @@ +package com.baeldung.imports.wildcard; + +import java.awt.*; +import java.util.*; +import java.util.List; + +public class BookView extends Frame { + + private UUID id; + + private String name; + + private Date datePublished; + + private List authors; + +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/wildcard/Library.java b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/wildcard/Library.java new file mode 100644 index 0000000000..8e1d79aca4 --- /dev/null +++ b/core-java-modules/core-java-lang-5/src/main/java/com/baeldung/imports/wildcard/Library.java @@ -0,0 +1,19 @@ +package com.baeldung.imports.wildcard; + +import java.util.*; +import java.sql.*; +import java.sql.Date; + +public class Library { + + private UUID id; + + private String name; + + private Time openingTime; + + private Time closingTime; + + private List datesClosed; + +} \ No newline at end of file diff --git a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/PersonAndEmployeeReflectionUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/PersonAndEmployeeReflectionUnitTest.java index b1a6a1fe57..cb1b5e926a 100644 --- a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/PersonAndEmployeeReflectionUnitTest.java +++ b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/PersonAndEmployeeReflectionUnitTest.java @@ -9,7 +9,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; -import java.util.stream.Stream; import static org.junit.Assert.*; diff --git a/core-java-modules/core-java-string-apis/src/main/java/com/baeldung/clearstringbuilderorstringbuffer/BenchmarkRunner.java b/core-java-modules/core-java-string-apis/src/main/java/com/baeldung/clearstringbuilderorstringbuffer/BenchmarkRunner.java new file mode 100644 index 0000000000..e0cead9c77 --- /dev/null +++ b/core-java-modules/core-java-string-apis/src/main/java/com/baeldung/clearstringbuilderorstringbuffer/BenchmarkRunner.java @@ -0,0 +1,32 @@ +package com.baeldung.clearstringbuilderorstringbuffer; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.infra.Blackhole; + +public class BenchmarkRunner { + + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(args); + } + + @State(Scope.Benchmark) + public static class MyState { + final String HELLO = "Hello World"; + final StringBuilder sb = new StringBuilder().append(HELLO); + } + + @Benchmark + public void evaluateSetLength(Blackhole blackhole, MyState state) { + state.sb.setLength(0); + blackhole.consume(state.sb.toString()); + } + + @Benchmark + public void evaluateDelete(Blackhole blackhole, MyState state) { + state.sb.delete(0, state.sb.length()); + blackhole.consume(state.sb.toString()); + } + +} diff --git a/core-java-modules/core-java-string-apis/src/test/java/com/baeldung/clearstringbuilderorstringbuffer/ClearStringBuilderOrStringBufferUnitTest.java b/core-java-modules/core-java-string-apis/src/test/java/com/baeldung/clearstringbuilderorstringbuffer/ClearStringBuilderOrStringBufferUnitTest.java new file mode 100644 index 0000000000..ad46c23969 --- /dev/null +++ b/core-java-modules/core-java-string-apis/src/test/java/com/baeldung/clearstringbuilderorstringbuffer/ClearStringBuilderOrStringBufferUnitTest.java @@ -0,0 +1,66 @@ +package com.baeldung.clearstringbuilderorstringbuffer; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ClearStringBuilderOrStringBufferUnitTest { + + @Test + void whenSetLengthToZero_ThenStringBuilderIsCleared() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Hello World"); + int initialCapacity = stringBuilder.capacity(); + stringBuilder.setLength(0); + assertEquals("", stringBuilder.toString()); + assertEquals(initialCapacity, stringBuilder.capacity()); + } + + @Test + void whenDeleteAll_ThenStringBuilderIsCleared() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Hello World"); + int initialCapacity = stringBuilder.capacity(); + stringBuilder.delete(0, stringBuilder.length()); + assertEquals("", stringBuilder.toString()); + assertEquals(initialCapacity, stringBuilder.capacity()); + } + + @Test + void whenSetLengthToZero_ThenStringBufferIsCleared() { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("Hello World"); + int initialCapacity = stringBuffer.capacity(); + stringBuffer.setLength(0); + assertEquals("", stringBuffer.toString()); + assertEquals(initialCapacity, stringBuffer.capacity()); + } + + @Test + void whenDeleteAll_ThenStringBufferIsCleared() { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("Hello World"); + int initialCapacity = stringBuffer.capacity(); + stringBuffer.delete(0, stringBuffer.length()); + assertEquals("", stringBuffer.toString()); + assertEquals(initialCapacity, stringBuffer.capacity()); + } + + // Note: It did not make the cut to the article, but here is another way to reset a StringBuilder + @Test + void whenAssignedToNewStringBuilder_ThenStringBuilderIsCleared() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Hello World"); + stringBuilder = new StringBuilder(); + assertEquals("", stringBuilder.toString()); + } + + @Test + void whenAssignedToNewStringBuffer_ThenStringBufferIsCleared() { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("Hello World"); + stringBuffer = new StringBuffer(); + assertEquals("", stringBuffer.toString()); + } + +} diff --git a/pom.xml b/pom.xml index 0e0aa876eb..1fe42bd734 100644 --- a/pom.xml +++ b/pom.xml @@ -259,7 +259,7 @@ - + @@ -278,7 +278,7 @@ - + @@ -621,7 +621,6 @@ spring-katharsis spring-mobile - spring-mockito spring-native spring-protobuf spring-quartz @@ -1050,7 +1049,6 @@ spring-katharsis spring-mobile - spring-mockito spring-native spring-protobuf spring-quartz @@ -1364,7 +1362,7 @@ 1.7.32 - 1.2.6 + 1.2.7 2.22.2 diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 3d5db76827..8213637631 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -39,6 +39,7 @@ rest-assured rest-testing selenium-junit-testng + spring-mockito spring-testing-2 spring-testing test-containers diff --git a/spring-mockito/.gitignore b/testing-modules/spring-mockito/.gitignore similarity index 100% rename from spring-mockito/.gitignore rename to testing-modules/spring-mockito/.gitignore diff --git a/spring-mockito/README.md b/testing-modules/spring-mockito/README.md similarity index 100% rename from spring-mockito/README.md rename to testing-modules/spring-mockito/README.md diff --git a/spring-mockito/pom.xml b/testing-modules/spring-mockito/pom.xml similarity index 94% rename from spring-mockito/pom.xml rename to testing-modules/spring-mockito/pom.xml index e0d7bee6f3..117da9478e 100644 --- a/spring-mockito/pom.xml +++ b/testing-modules/spring-mockito/pom.xml @@ -13,7 +13,7 @@ com.baeldung parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-2 + ../../parent-boot-2 diff --git a/spring-mockito/src/main/java/com/baeldung/MocksApplication.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/MocksApplication.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/MocksApplication.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/MocksApplication.java diff --git a/spring-mockito/src/main/java/com/baeldung/NameService.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/NameService.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/NameService.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/NameService.java diff --git a/spring-mockito/src/main/java/com/baeldung/UserService.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/UserService.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/UserService.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/UserService.java diff --git a/spring-mockito/src/main/java/com/baeldung/app/api/Flower.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/app/api/Flower.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/app/api/Flower.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/app/api/Flower.java diff --git a/spring-mockito/src/main/java/com/baeldung/app/api/MessageApi.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/app/api/MessageApi.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/app/api/MessageApi.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/app/api/MessageApi.java diff --git a/spring-mockito/src/main/java/com/baeldung/app/rest/FlowerController.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/app/rest/FlowerController.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/app/rest/FlowerController.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/app/rest/FlowerController.java diff --git a/spring-mockito/src/main/java/com/baeldung/app/rest/MessageController.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/app/rest/MessageController.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/app/rest/MessageController.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/app/rest/MessageController.java diff --git a/spring-mockito/src/main/java/com/baeldung/domain/model/Message.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/domain/model/Message.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/domain/model/Message.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/domain/model/Message.java diff --git a/spring-mockito/src/main/java/com/baeldung/domain/service/FlowerService.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/domain/service/FlowerService.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/domain/service/FlowerService.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/domain/service/FlowerService.java diff --git a/spring-mockito/src/main/java/com/baeldung/domain/service/MessageService.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/domain/service/MessageService.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/domain/service/MessageService.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/domain/service/MessageService.java diff --git a/spring-mockito/src/main/java/com/baeldung/domain/util/MessageMatcher.java b/testing-modules/spring-mockito/src/main/java/com/baeldung/domain/util/MessageMatcher.java similarity index 100% rename from spring-mockito/src/main/java/com/baeldung/domain/util/MessageMatcher.java rename to testing-modules/spring-mockito/src/main/java/com/baeldung/domain/util/MessageMatcher.java diff --git a/spring-mockito/src/main/resources/application.properties b/testing-modules/spring-mockito/src/main/resources/application.properties similarity index 100% rename from spring-mockito/src/main/resources/application.properties rename to testing-modules/spring-mockito/src/main/resources/application.properties diff --git a/spring-mockito/src/main/resources/logback.xml b/testing-modules/spring-mockito/src/main/resources/logback.xml similarity index 100% rename from spring-mockito/src/main/resources/logback.xml rename to testing-modules/spring-mockito/src/main/resources/logback.xml diff --git a/spring-mockito/src/test/java/com/baeldung/NameServiceTestConfiguration.java b/testing-modules/spring-mockito/src/test/java/com/baeldung/NameServiceTestConfiguration.java similarity index 100% rename from spring-mockito/src/test/java/com/baeldung/NameServiceTestConfiguration.java rename to testing-modules/spring-mockito/src/test/java/com/baeldung/NameServiceTestConfiguration.java diff --git a/spring-mockito/src/test/java/com/baeldung/UserServiceUnitTest.java b/testing-modules/spring-mockito/src/test/java/com/baeldung/UserServiceUnitTest.java similarity index 100% rename from spring-mockito/src/test/java/com/baeldung/UserServiceUnitTest.java rename to testing-modules/spring-mockito/src/test/java/com/baeldung/UserServiceUnitTest.java diff --git a/spring-mockito/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java b/testing-modules/spring-mockito/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java similarity index 100% rename from spring-mockito/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java rename to testing-modules/spring-mockito/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java diff --git a/spring-mockito/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java b/testing-modules/spring-mockito/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java similarity index 100% rename from spring-mockito/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java rename to testing-modules/spring-mockito/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java diff --git a/spring-mockito/src/test/java/org/baeldung/SpringContextTest.java b/testing-modules/spring-mockito/src/test/java/org/baeldung/SpringContextTest.java similarity index 100% rename from spring-mockito/src/test/java/org/baeldung/SpringContextTest.java rename to testing-modules/spring-mockito/src/test/java/org/baeldung/SpringContextTest.java