commit
0e97e597eb
@ -121,7 +121,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<jna.version>5.7.0</jna.version>
|
<jna.version>5.7.0</jna.version>
|
||||||
<kafka.version>3.6.1</kafka.version>
|
<kafka.version>3.9.0</kafka.version>
|
||||||
<testcontainers-kafka.version>1.19.3</testcontainers-kafka.version>
|
<testcontainers-kafka.version>1.19.3</testcontainers-kafka.version>
|
||||||
<testcontainers-jupiter.version>1.19.3</testcontainers-jupiter.version>
|
<testcontainers-jupiter.version>1.19.3</testcontainers-jupiter.version>
|
||||||
<jackson.databind.version>2.15.2</jackson.databind.version>
|
<jackson.databind.version>2.15.2</jackson.databind.version>
|
||||||
@ -130,4 +130,4 @@
|
|||||||
<awaitility.version>3.0.0</awaitility.version>
|
<awaitility.version>3.0.0</awaitility.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
package com.baeldung.switchpatterns;
|
|
||||||
|
|
||||||
public class ParenthesizedPatterns {
|
|
||||||
|
|
||||||
static double getDoubleValueUsingIf(Object o) {
|
|
||||||
return switch (o) {
|
|
||||||
case String s -> {
|
|
||||||
if (s.length() > 0) {
|
|
||||||
if (s.contains("#") || s.contains("@")) {
|
|
||||||
yield 0d;
|
|
||||||
} else {
|
|
||||||
yield Double.parseDouble(s);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
yield 0d;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default -> 0d;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static double getDoubleValueUsingParenthesizedPatterns(Object o) {
|
|
||||||
return switch (o) {
|
|
||||||
case String s && s.length() > 0 && !(s.contains("#") || s.contains("@")) -> Double.parseDouble(s);
|
|
||||||
default -> 0d;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.switchpatterns;
|
||||||
|
|
||||||
|
public class GuardedPatterns {
|
||||||
|
|
||||||
|
static double getDoubleValueUsingIf(Object o) {
|
||||||
|
return switch (o) {
|
||||||
|
case String s -> {
|
||||||
|
if (s.length() > 0) {
|
||||||
|
yield Double.parseDouble(s);
|
||||||
|
} else {
|
||||||
|
yield 0d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default -> 0d;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static double getDoubleValueUsingGuardedPatterns(Object o) {
|
||||||
|
return switch (o) {
|
||||||
|
case String s when s.length() > 0 -> Double.parseDouble(s);
|
||||||
|
default -> 0d;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.switchpatterns;
|
||||||
|
|
||||||
|
public class HandlingNullValues {
|
||||||
|
|
||||||
|
static double getDoubleUsingSwitchNullCase(Object o) {
|
||||||
|
return switch (o) {
|
||||||
|
case String s -> Double.parseDouble(s);
|
||||||
|
case null -> 0d;
|
||||||
|
default -> 0d;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static double getDoubleUsingSwitchTotalType(Object o) {
|
||||||
|
return switch (o) {
|
||||||
|
case String s -> Double.parseDouble(s);
|
||||||
|
case Object ob -> 0d;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.switchpatterns;
|
||||||
|
|
||||||
|
public class PatternMatching {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Object o = args[0];
|
||||||
|
if (o instanceof String s) {
|
||||||
|
System.out.printf("Object is a string %s", s);
|
||||||
|
} else if(o instanceof Number n) {
|
||||||
|
System.out.printf("Object is a number %n", n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.switchpatterns;
|
||||||
|
|
||||||
|
public class SwitchStatement {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String b = "B";
|
||||||
|
switch (args[0]) {
|
||||||
|
case "A" -> System.out.println("Parameter is A");
|
||||||
|
case b -> System.out.println("Parameter is b");
|
||||||
|
default -> System.out.println("Parameter is unknown");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.switchpatterns;
|
||||||
|
|
||||||
|
public class TypePatterns {
|
||||||
|
|
||||||
|
static double getDoubleUsingIf(Object o) {
|
||||||
|
double result;
|
||||||
|
|
||||||
|
if (o instanceof Integer) {
|
||||||
|
result = ((Integer) o).doubleValue();
|
||||||
|
} else if (o instanceof Float) {
|
||||||
|
result = ((Float) o).doubleValue();
|
||||||
|
} else if (o instanceof String) {
|
||||||
|
result = Double.parseDouble(((String) o));
|
||||||
|
} else {
|
||||||
|
result = 0d;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double getDoubleUsingSwitch(Object o) {
|
||||||
|
return switch (o) {
|
||||||
|
case Integer i -> i.doubleValue();
|
||||||
|
case Float f -> f.doubleValue();
|
||||||
|
case String s -> Double.parseDouble(s);
|
||||||
|
default -> 0d;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,9 +3,9 @@ package com.baeldung.switchpatterns;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static com.baeldung.switchpatterns.ParenthesizedPatterns.*;
|
import static com.baeldung.switchpatterns.GuardedPatterns.*;
|
||||||
|
|
||||||
class ParenthesizedPatternsUnitTest {
|
class GuardedPatternsUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||||
@ -17,24 +17,14 @@ class ParenthesizedPatternsUnitTest {
|
|||||||
assertEquals(10d, getDoubleValueUsingIf("10"));
|
assertEquals(10d, getDoubleValueUsingIf("10"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenIfImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
|
|
||||||
assertEquals(0d, getDoubleValueUsingIf("@10"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||||
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns(""));
|
assertEquals(0d, getDoubleValueUsingGuardedPatterns(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
||||||
assertEquals(10d, getDoubleValueUsingParenthesizedPatterns("10"));
|
assertEquals(10d, getDoubleValueUsingGuardedPatterns("10"));
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenPatternsImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
|
|
||||||
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns("@10"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.switchpatterns;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static com.baeldung.switchpatterns.HandlingNullValues.*;
|
||||||
|
|
||||||
|
class HandlingNullValuesUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNullCaseInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(10d, getDoubleUsingSwitchNullCase("10"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenTotalTypeInSwitch_whenUsingNullArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(0d, getDoubleUsingSwitchNullCase(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenTotalTypeInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(10d, getDoubleUsingSwitchTotalType("10"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.switchpatterns;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static com.baeldung.switchpatterns.TypePatterns.*;
|
||||||
|
|
||||||
|
class TypePatternsUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenIfImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(10d, getDoubleUsingIf(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenIfImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(10d, getDoubleUsingIf(10.0f));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenIfImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(10d, getDoubleUsingIf("10"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenIfImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(0d, getDoubleUsingIf('c'));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenSwitchImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(10d, getDoubleUsingSwitch(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenSwitchImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(10d, getDoubleUsingSwitch(10.0f));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenSwitchImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(10d, getDoubleUsingSwitch("10"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenSwitchImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() {
|
||||||
|
assertEquals(0d, getDoubleUsingSwitch('c'));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
28
core-java-modules/core-java-24/pom.xml
Normal file
28
core-java-modules/core-java-24/pom.xml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>core-java-24</artifactId>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>24</source>
|
||||||
|
<target>24</target>
|
||||||
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.java.javafeatures;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class SwitchPreview {
|
||||||
|
|
||||||
|
void primitiveTypePatternExample() {
|
||||||
|
Random r=new Random();
|
||||||
|
switch (r.nextInt()) {
|
||||||
|
case 1 -> System.out.println("int is 1");
|
||||||
|
case int i when i > 1 && i < 100 -> System.out.println("int is greater than 1 and less than 100");
|
||||||
|
default -> System.out.println("int is greater or equal to 100");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,26 +1,56 @@
|
|||||||
package com.baeldung.logallrequests;
|
package com.baeldung.logallrequests;
|
||||||
|
|
||||||
import java.io.CharArrayWriter;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
import jakarta.servlet.ServletOutputStream;
|
||||||
|
import jakarta.servlet.WriteListener;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import jakarta.servlet.http.HttpServletResponseWrapper;
|
import jakarta.servlet.http.HttpServletResponseWrapper;
|
||||||
|
|
||||||
public class ResponseWrapper extends HttpServletResponseWrapper {
|
public class ResponseWrapper extends HttpServletResponseWrapper {
|
||||||
|
|
||||||
private final CharArrayWriter charArrayWriter = new CharArrayWriter();
|
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
private final PrintWriter writer = new PrintWriter(charArrayWriter);
|
private final PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream));
|
||||||
|
|
||||||
public ResponseWrapper(HttpServletResponse response) {
|
public ResponseWrapper(HttpServletResponse response) {
|
||||||
super(response);
|
super(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletOutputStream getOutputStream() {
|
||||||
|
return new ServletOutputStream() {
|
||||||
|
@Override
|
||||||
|
public boolean isReady() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setWriteListener(WriteListener writeListener) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(int b) {
|
||||||
|
outputStream.write(b);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PrintWriter getWriter() {
|
public PrintWriter getWriter() {
|
||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flushBuffer() throws IOException {
|
||||||
|
super.flushBuffer();
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
|
||||||
public String getBodyAsString() {
|
public String getBodyAsString() {
|
||||||
return charArrayWriter.toString();
|
writer.flush();
|
||||||
|
return outputStream.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ public class LoggingFilterIntegrationTest {
|
|||||||
.andReturn();
|
.andReturn();
|
||||||
|
|
||||||
assertThat(output.getAll()).contains("Incoming Request: [GET] /api/hello");
|
assertThat(output.getAll()).contains("Incoming Request: [GET] /api/hello");
|
||||||
|
assertThat(output.getAll()).contains("Response Body: Hello, World!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -2,7 +2,6 @@ package com.baeldung.configurationproperties;
|
|||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
|
||||||
@ConfigurationProperties(prefix = "database")
|
@ConfigurationProperties(prefix = "database")
|
||||||
public class Database {
|
public class Database {
|
||||||
|
|
@ -9,9 +9,6 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.configurationproperties.Database;
|
|
||||||
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = DatabaseConfigPropertiesApp.class)
|
@SpringBootTest(classes = DatabaseConfigPropertiesApp.class)
|
||||||
@TestPropertySource("classpath:database-test.properties")
|
@TestPropertySource("classpath:database-test.properties")
|
@ -81,13 +81,6 @@
|
|||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<mainClass>com.baeldung.springdoc.SpringdocApplication</mainClass>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
@ -66,45 +66,15 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>pre-integration-test</id>
|
|
||||||
<goals>
|
|
||||||
<goal>start</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
<execution>
|
|
||||||
<id>post-integration-test</id>
|
|
||||||
<goals>
|
|
||||||
<goal>stop</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.springdoc</groupId>
|
|
||||||
<artifactId>springdoc-openapi-maven-plugin</artifactId>
|
|
||||||
<version>${springdoc-openapi-maven-plugin.version}</version>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<phase>integration-test</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>generate</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<apiDocsUrl>http://localhost:8080/api-docs</apiDocsUrl>
|
<mainClass>com.baeldung.springdoc.SpringdocApplication</mainClass>
|
||||||
<outputFileName>openapi.json</outputFileName>
|
|
||||||
<outputDir>${project.build.directory}</outputDir>
|
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.asciidoctor</groupId>
|
<groupId>org.asciidoctor</groupId>
|
||||||
<artifactId>asciidoctor-maven-plugin</artifactId>
|
<artifactId>asciidoctor-maven-plugin</artifactId>
|
||||||
@ -150,7 +120,6 @@
|
|||||||
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
|
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
|
||||||
<springdoc-openapi-maven-plugin.version>1.4</springdoc-openapi-maven-plugin.version>
|
<springdoc-openapi-maven-plugin.version>1.4</springdoc-openapi-maven-plugin.version>
|
||||||
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
|
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
|
||||||
<start-class>com.baeldung.springdoc.SpringdocApplication</start-class>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.applicationcontext;
|
package com.baeldung.springapplicationcontext;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.applicationcontext;
|
package com.baeldung.springapplicationcontext;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.applicationcontext;
|
package com.baeldung.springapplicationcontext;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.applicationcontext;
|
package com.baeldung.springapplicationcontext;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.applicationcontext;
|
package com.baeldung.springapplicationcontext;
|
||||||
|
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.applicationcontext;
|
package com.baeldung.springapplicationcontext;
|
||||||
|
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
@ -1,45 +1,50 @@
|
|||||||
package com.baeldung.filter;
|
package com.baeldung.filter;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.Customizer;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.core.userdetails.User;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
||||||
|
|
||||||
import com.baeldung.security.RestAuthenticationEntryPoint;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class CustomWebSecurityConfigurerAdapter {
|
public class CustomWebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
@Autowired private RestAuthenticationEntryPoint authenticationEntryPoint;
|
@Bean
|
||||||
|
public InMemoryUserDetailsManager userDetailsService() {
|
||||||
@Autowired
|
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
UserDetails user = User.withUsername("user1")
|
||||||
auth
|
.password(encoder.encode("user1Pass"))
|
||||||
.inMemoryAuthentication()
|
.roles("USER")
|
||||||
.withUser("user1")
|
.build();
|
||||||
.password(passwordEncoder().encode("user1Pass"))
|
return new InMemoryUserDetailsManager(user);
|
||||||
.authorities("ROLE_USER");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
http.authorizeHttpRequests(expressionInterceptUrlRegistry ->
|
http.authorizeHttpRequests(authorizationManagerRequestMatcherRegistry ->
|
||||||
expressionInterceptUrlRegistry.requestMatchers("/securityNone").permitAll()
|
authorizationManagerRequestMatcherRegistry.requestMatchers("/securityNone/**").permitAll()
|
||||||
.anyRequest().authenticated())
|
.anyRequest().authenticated()
|
||||||
.httpBasic(httpSecurityHttpBasicConfigurer -> httpSecurityHttpBasicConfigurer.authenticationEntryPoint(authenticationEntryPoint));
|
)
|
||||||
|
.httpBasic(Customizer.withDefaults());
|
||||||
http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
|
http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PasswordEncoder passwordEncoder() {
|
public SecurityFilterChain securedFilterChain(HttpSecurity http) throws Exception {
|
||||||
return new BCryptPasswordEncoder();
|
http.authorizeHttpRequests(authorizationManagerRequestMatcherRegistry ->
|
||||||
|
authorizationManagerRequestMatcherRegistry.requestMatchers("/secured/**").authenticated())
|
||||||
|
.httpBasic(Customizer.withDefaults());
|
||||||
|
http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
|
||||||
|
return http.build();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.filter;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class FilterApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(FilterApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.filter;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class FilterController {
|
||||||
|
|
||||||
|
@GetMapping("/securityNone/test")
|
||||||
|
public String publicEndpoint() {
|
||||||
|
return "This is a public endpoint.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/secured/test")
|
||||||
|
public String securedEndpoint() {
|
||||||
|
return "This is a secured endpoint.";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.filter;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = FilterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
public class FilterControllerUnitTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestRestTemplate template;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRequestOnPublicService_shouldSucceedWith200() throws Exception {
|
||||||
|
ResponseEntity<String> result = template.getForEntity("/securityNone/test", String.class);
|
||||||
|
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
|
||||||
|
ResponseEntity<String> result = template.withBasicAuth("user1", "user1Pass")
|
||||||
|
.getForEntity("/secured/test", String.class);
|
||||||
|
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRequestOnPrivateService_shouldFailWith401() throws Exception {
|
||||||
|
ResponseEntity<String> result = template.getForEntity("/secured/test", String.class);
|
||||||
|
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidAuthRequestOnPrivateService_shouldSucceedWith401() throws Exception {
|
||||||
|
ResponseEntity<String> result = template.withBasicAuth("spring", "wrong")
|
||||||
|
.getForEntity("/secured/test", String.class);
|
||||||
|
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.jacoco;
|
||||||
|
|
||||||
|
public class Palindrome {
|
||||||
|
|
||||||
|
public boolean isPalindrome(String inputString) {
|
||||||
|
if (inputString.length() == 0) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
char firstChar = inputString.charAt(0);
|
||||||
|
char lastChar = inputString.charAt(inputString.length() - 1);
|
||||||
|
String mid = inputString.substring(1, inputString.length() - 1);
|
||||||
|
return (firstChar == lastChar) && isPalindrome(mid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.jacoco;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class PalindromeUnitTest {
|
||||||
|
@Test
|
||||||
|
public void whenEmptyString_thenAccept() {
|
||||||
|
Palindrome palindromeTester = new Palindrome();
|
||||||
|
assertTrue(palindromeTester.isPalindrome(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPalindrom_thenAccept() {
|
||||||
|
Palindrome palindromeTester = new Palindrome();
|
||||||
|
assertTrue(palindromeTester.isPalindrome("noon"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNotPalindrom_thenReject(){
|
||||||
|
Palindrome palindromeTester = new Palindrome();
|
||||||
|
assertFalse(palindromeTester.isPalindrome("box"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNearPalindrom_thenReject(){
|
||||||
|
Palindrome palindromeTester = new Palindrome();
|
||||||
|
assertFalse(palindromeTester.isPalindrome("neon"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user