Merge branch 'eugenp:master' into master

This commit is contained in:
Ulisses Lima 2022-07-11 16:38:21 -03:00 committed by GitHub
commit 6d5ea19088
34 changed files with 277 additions and 11 deletions

View File

@ -54,6 +54,12 @@
<artifactId>moneta</artifactId>
<version>${javamoney.moneta.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -266,6 +272,7 @@
<!-- util -->
<unix4j.version>0.4</unix4j.version>
<grep4j.version>1.8.7</grep4j.version>
<mockito.version>4.6.1</mockito.version>
<!-- maven plugins -->
<javamoney.moneta.version>1.1</javamoney.moneta.version>
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
@ -279,4 +286,4 @@
<target.version>1.8</target.version>
</properties>
</project>
</project>

View File

@ -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");
}
}

View File

@ -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);
}
}

View File

@ -13,7 +13,6 @@
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<finalName>core-java-lang-5</finalName>
<resources>
@ -23,5 +22,4 @@
</resource>
</resources>
</build>
</project>
</project>

View File

@ -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<String> authors;
}

View File

@ -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<String> authors;
}

View File

@ -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<String> authors;
}

View File

@ -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<Date> datesClosed;
}

View File

@ -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.*;

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -259,7 +259,7 @@
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
<ignore/>
</action>
</pluginExecution>
<pluginExecution>
@ -278,7 +278,7 @@
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
<ignore/>
</action>
</pluginExecution>
</pluginExecutions>
@ -621,7 +621,6 @@
<module>spring-katharsis</module>
<module>spring-mobile</module>
<module>spring-mockito</module>
<module>spring-native</module>
<module>spring-protobuf</module>
<module>spring-quartz</module>
@ -1050,7 +1049,6 @@
<module>spring-katharsis</module>
<module>spring-mobile</module>
<module>spring-mockito</module>
<module>spring-native</module>
<module>spring-protobuf</module>
<module>spring-quartz</module>
@ -1364,7 +1362,7 @@
<!-- logging -->
<org.slf4j.version>1.7.32</org.slf4j.version>
<logback.version>1.2.6</logback.version>
<logback.version>1.2.7</logback.version>
<!-- plugins -->
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>

View File

@ -39,6 +39,7 @@
<module>rest-assured</module>
<module>rest-testing</module>
<module>selenium-junit-testng</module>
<module>spring-mockito</module>
<module>spring-testing-2</module>
<module>spring-testing</module>
<module>test-containers</module>

View File

@ -13,7 +13,7 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>