Merge branch 'eugenp:master' into master

This commit is contained in:
sebx59
2022-05-19 09:01:26 +02:00
committed by GitHub
82 changed files with 2117 additions and 6 deletions
@@ -0,0 +1,36 @@
package com.baeldung.java9.delimiters;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
public class DelimiterDemo {
public static List<String> scannerWithDelimiter(String input, String delimiter) {
try (Scanner scan = new Scanner(input)) {
scan.useDelimiter(delimiter);
List<String> result = new ArrayList<String>();
scan.forEachRemaining(result::add);
return result;
}
}
public static List<String> scannerWithDelimiterUsingPattern(String input, Pattern delimiter) {
try (Scanner scan = new Scanner(input)) {
scan.useDelimiter(delimiter);
List<String> result = new ArrayList<String>();
scan.forEachRemaining(result::add);
return result;
}
}
public static List<String> baseScanner(String input) {
try (Scanner scan = new Scanner(input)) {
List<String> result = new ArrayList<String>();
scan.forEachRemaining(result::add);
return result;
}
}
}
@@ -0,0 +1,67 @@
package com.baeldung.java9.delimiters;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
class DelimiterDemoUnitTest {
@Test
void givenSimpleCharacterDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiter, "Welcome to Baeldung", "\\s", Arrays.asList("Welcome", "to", "Baeldung"));
}
@Test
void givenStringDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiter, "HelloBaeldungHelloWorld", "Hello", Arrays.asList("Baeldung", "World"));
}
@Test
void givenVariousPossibleDelimiters_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiter, "Welcome to Baeldung.\nThank you for reading.\nThe team", "\n|\\s", Arrays.asList("Welcome", "to", "Baeldung.", "Thank", "you", "for", "reading.", "The", "team"));
}
@Test
void givenWildcardRegexDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiter, "1aaaaaaa2aa3aaa4", "a+", Arrays.asList("1", "2", "3", "4"));
}
@Test
void givenSimpleCharacterDelimiter_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "Welcome to Baeldung", Pattern.compile("\\s"), Arrays.asList("Welcome", "to", "Baeldung"));
}
@Test
void givenStringDelimiter_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "HelloBaeldungHelloWorld", Pattern.compile("Hello"), Arrays.asList("Baeldung", "World"));
}
@Test
void givenVariousPossibleDelimiters_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "Welcome to Baeldung.\nThank you for reading.\nThe team", Pattern.compile("\n|\\s"), Arrays.asList("Welcome", "to", "Baeldung.", "Thank", "you", "for", "reading.", "The", "team"));
}
@Test
void givenWildcardRegexDelimiters_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "1aaaaaaa2aa3aaa4", Pattern.compile("a*"), Arrays.asList("1", "2", "3", "4"));
}
void checkOutput(BiFunction<String, String, List<String>> function, String input, String delimiter, List<String> expectedOutput) {
assertEquals(expectedOutput, function.apply(input, delimiter));
}
void checkOutput(BiFunction<String, Pattern, List<String>> function, String input, Pattern delimiter, List<String> expectedOutput) {
assertEquals(expectedOutput, function.apply(input, delimiter));
}
@Test
void whenBaseScanner_ThenWhitespacesAreUsedAsDelimiters() {
assertEquals(List.of("Welcome", "at", "Baeldung"), DelimiterDemo.baseScanner("Welcome at Baeldung"));
}
}
@@ -3,4 +3,4 @@
This module contains articles about Java HttpClient
### Relevant articles
- TODO
- [Posting with Java HttpClient](https://www.baeldung.com/java-httpclient-post)
@@ -0,0 +1,12 @@
package com.baeldung.http;
import java.net.http.HttpClient;
import java.time.Duration;
public class JavaHttpClientTimeout {
static HttpClient getHttpClientWithTimeout(int seconds) {
return HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(seconds))
.build();
}
}
@@ -0,0 +1,54 @@
package com.baeldung.http;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpConnectTimeoutException;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static com.baeldung.http.JavaHttpClientTimeout.getHttpClientWithTimeout;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class JavaHttpClientTimeoutIntegrationTest {
private HttpClient httpClient;
private HttpRequest httpRequest;
@BeforeEach
public void setUp() {
httpClient = getHttpClientWithTimeout(3);
httpClient.connectTimeout().map(Duration::toSeconds)
.ifPresent(sec -> System.out.println("Timeout in seconds: " + sec));
httpRequest = HttpRequest.newBuilder().uri(URI.create("http://10.255.255.1")).GET().build();
}
@Test
void shouldThrowExceptionWhenMakingSyncCall() {
HttpConnectTimeoutException thrown = assertThrows(
HttpConnectTimeoutException.class,
() -> httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()),
"Expected doThing() to throw, but it didn't"
);
assertTrue(thrown.getMessage().contains("timed out"));
}
@Test
void shouldThrowExceptionWhenMakingASyncCall() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<String> completableFuture =
httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.exceptionally(Throwable::getMessage);
String response = completableFuture.get(5, TimeUnit.SECONDS);
assertTrue(response.contains("timed out"));
}
}
@@ -0,0 +1,37 @@
package com.baeldung.searchfilesbywildcards;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
public class SearchFileByWildcard {
public static List<String> matchesList = new ArrayList<String>();
public List<String> searchWithWc(Path rootDir, String pattern) throws IOException {
matchesList.clear();
FileVisitor<Path> matcherVisitor = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attribs) throws IOException {
FileSystem fs = FileSystems.getDefault();
PathMatcher matcher = fs.getPathMatcher(pattern);
Path name = file.getFileName();
if (matcher.matches(name)) {
matchesList.add(name.toString());
}
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(rootDir, matcherVisitor);
return matchesList;
}
}
@@ -0,0 +1,30 @@
package com.baeldung.searchfilesbywildcards;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.junit.jupiter.api.Test;
public class SearchFileByWildcardUnitTest {
@Test
public void whenFourFilenameMatch_thenListOfFour() throws IOException {
SearchFileByWildcard sfbw = new SearchFileByWildcard();
List<String> actual = sfbw.searchWithWc(Paths.get("src/test/resources/sfbw"), "glob:*.{txt,docx}");
assertEquals(new HashSet<>(Arrays.asList("six.txt", "three.txt", "two.docx", "one.txt")), new HashSet<>(actual));
}
@Test
public void whenOneFilenameMatch_thenListOfOne() throws IOException {
SearchFileByWildcard sfbw = new SearchFileByWildcard();
List<String> actual = sfbw.searchWithWc(Paths.get("src/test/resources/sfbw"), "glob:????.{csv}");
assertEquals(new HashSet<>(Arrays.asList("five.csv")), new HashSet<>(actual));
}
}
@@ -0,0 +1,7 @@
package com.baeldung.reflection.proxy;
public interface AdvancedOperation {
int multiply(int a, int b);
int divide(int a, int b);
}
@@ -0,0 +1,7 @@
package com.baeldung.reflection.proxy;
public interface BasicOperation {
int add(int a, int b);
int subtract(int a, int b);
}
@@ -0,0 +1,35 @@
package com.baeldung.reflection.proxy;
import org.junit.Test;
import java.lang.reflect.Proxy;
import java.util.function.Consumer;
import static org.junit.Assert.assertTrue;
public class DollarProxyUnitTest {
@Test
public void givenProxy_whenInvokingGetProxyClass_thenGeneratingProxyClass() {
// Java 8: -Dsun.misc.ProxyGenerator.saveGeneratedFiles=true
// Java 9 or later: -Djdk.proxy.ProxyGenerator.saveGeneratedFiles=true
// Note: System.setProperty() doesn't work here
// because ProxyGenerator.saveGeneratedFiles read its property only once.
// The @Test annotation in this method will generate a $Proxy class.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class<?>[] interfaces = {BasicOperation.class, AdvancedOperation.class};
Class<?> proxyClass = Proxy.getProxyClass(classLoader, interfaces);
boolean isProxyClass = Proxy.isProxyClass(proxyClass);
assertTrue(isProxyClass);
}
@Test
public void givenReflection_whenReadingAnnotation_thenGeneratingProxyClass() {
FunctionalInterface instance = Consumer.class.getDeclaredAnnotation(FunctionalInterface.class);
Class<?> clazz = instance.getClass();
boolean isProxyClass = Proxy.isProxyClass(clazz);
assertTrue(isProxyClass);
}
}
@@ -30,6 +30,12 @@
<artifactId>commons-lang3</artifactId>
<version>${apache-commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -0,0 +1,38 @@
package com.baeldung.checkvowels;
import java.util.regex.Pattern;
public class CheckVowels {
private static final String VOWELS = "aeiouAEIOU";
private static final Pattern VOWELS_PATTERN = Pattern.compile("[aeiou]", Pattern.CASE_INSENSITIVE);
public static boolean isInVowelsString(char c) {
return VOWELS.indexOf(c) != -1;
}
public static boolean isInVowelsString(String c) {
return VOWELS.contains(c);
}
public static boolean isVowelBySwitch(char c) {
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
return true;
default:
return false;
}
}
public static boolean isVowelByRegex(String c) {
return VOWELS_PATTERN.matcher(c).matches();
}
}
@@ -0,0 +1,51 @@
package com.baeldung.checkvowels;
import org.junit.jupiter.api.Test;
import static com.baeldung.checkvowels.CheckVowels.*;
import static org.assertj.core.api.Assertions.*;
class CheckVowelsUnitTest {
@Test
void givenAVowelCharacter_thenInVowelString() {
assertThat(isInVowelsString('e')).isTrue();
}
@Test
void givenAConsonantCharacter_thenNotInVowelString() {
assertThat(isInVowelsString('z')).isFalse();
}
@Test
void givenAVowelString_thenInVowelString() {
assertThat(isInVowelsString("e")).isTrue();
}
@Test
void givenAConsonantString_thenNotInVowelString() {
assertThat(isInVowelsString("z")).isFalse();
}
@Test
void givenAVowelCharacter_thenInVowelSwitch() {
assertThat(isVowelBySwitch('e')).isTrue();
}
@Test
void givenAConsonantCharacter_thenNotInVowelSwitch() {
assertThat(isVowelBySwitch('z')).isFalse();
}
@Test
void givenAVowelString_thenInVowelPattern() {
assertThat(isVowelByRegex("e")).isTrue();
assertThat(isVowelByRegex("E")).isTrue();
}
@Test
void givenAVowelCharacter_thenInVowelPattern() {
assertThat(isVowelByRegex(Character.toString('e'))).isTrue();
assertThat(isVowelByRegex("E")).isTrue();
}
}
+1
View File
@@ -8,3 +8,4 @@
- [Convert Byte Size Into a Human-Readable Format in Java](https://www.baeldung.com/java-human-readable-byte-size)
- [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int)
- [Generate a Random Value From an Enum](https://www.baeldung.com/java-enum-random-value)
- [Reverse a Number in Java](https://www.baeldung.com/java-reverse-number)
+29
View File
@@ -0,0 +1,29 @@
# Lightrun Example Application - Tasks Management
This application exists as an example for the Lightrun series of articles.
## Building
This application requires [Apache Maven](https://maven.apache.org/) and [Java 17+](https://www.oracle.com/java/technologies/downloads/).
Building the code is done by executing:
```
$ mvn install
```
from the top level.
## Running
The application consists of three services:
* Tasks
* Users
* API
These are all Spring Boot applications.
The Tasks and Users services exist as microservices for managing one facet of data. Each uses a database, and utilise a JMS queue between them as well. For convenience this infrastructure is all embedded in the applications.
This does mean that the startup order is important. The JMS queue exists within the Tasks service and is connected to from the Users service. As such, the Tasks service must be started before the others.
Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for example:
```
$ java -jar ./target/tasks-service-0.0.1-SNAPSHOT.jar
```
+33
View File
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
+45
View File
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>api-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>api-service</name>
<description>Aggregator Service for LightRun Article</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,13 @@
package com.baeldung.apiservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApiServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ApiServiceApplication.class, args);
}
}
@@ -0,0 +1,33 @@
package com.baeldung.apiservice;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
@Component
public class RequestIdGenerator implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestId = UUID.randomUUID().toString();
MDC.put(RequestIdGenerator.class.getCanonicalName(), requestId);
response.addHeader("X-Request-Id", requestId);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
MDC.remove(RequestIdGenerator.class.getCanonicalName());
}
public static String getRequestId() {
return MDC.get(RequestIdGenerator.class.getCanonicalName());
}
}
@@ -0,0 +1,20 @@
package com.baeldung.apiservice;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.additionalInterceptors((request, body, execution) -> {
request.getHeaders().add("X-Request-Id", RequestIdGenerator.getRequestId());
return execution.execute(request, body);
})
.build();
}
}
@@ -0,0 +1,17 @@
package com.baeldung.apiservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private RequestIdGenerator requestIdGenerator;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestIdGenerator);
}
}
@@ -0,0 +1,11 @@
package com.baeldung.apiservice.adapters.http;
import java.time.Instant;
public record TaskResponse(String id,
String title,
Instant created,
UserResponse createdBy,
UserResponse assignedTo,
String status) {
}
@@ -0,0 +1,54 @@
package com.baeldung.apiservice.adapters.http;
import com.baeldung.apiservice.adapters.tasks.Task;
import com.baeldung.apiservice.adapters.tasks.TaskRepository;
import com.baeldung.apiservice.adapters.users.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/")
@RestController
public class TasksController {
@Autowired
private TaskRepository taskRepository;
@Autowired
private UserRepository userRepository;
@GetMapping("/{id}")
public TaskResponse getTaskById(@PathVariable("id") String id) {
Task task = taskRepository.getTaskById(id);
if (task == null) {
throw new UnknownTaskException();
}
return buildResponse(task);
}
private TaskResponse buildResponse(Task task) {
return new TaskResponse(task.id(),
task.title(),
task.created(),
getUser(task.createdBy()),
getUser(task.assignedTo()),
task.status());
}
private UserResponse getUser(String userId) {
if (userId == null) {
return null;
}
var user = userRepository.getUserById(userId);
if (user == null) {
return null;
}
return new UserResponse(user.id(), user.name());
}
@ExceptionHandler(UnknownTaskException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public void handleUnknownTask() {
}
}
@@ -0,0 +1,4 @@
package com.baeldung.apiservice.adapters.http;
public class UnknownTaskException extends RuntimeException {
}

Some files were not shown because too many files have changed in this diff Show More