Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
89e987aa64
@ -0,0 +1,3 @@
|
|||||||
|
void main() {
|
||||||
|
System.out.println("Hello, World!");
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.unnamedclasses;
|
||||||
|
|
||||||
|
public class HelloWorldChild extends HelloWorldSuper {
|
||||||
|
void main() {
|
||||||
|
System.out.println("Hello, World!");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.unnamedclasses;
|
||||||
|
|
||||||
|
public class HelloWorldSuper {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Hello from the superclass");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
private String getMessage() {
|
||||||
|
return "Hello, World!";
|
||||||
|
}
|
||||||
|
void main() {
|
||||||
|
System.out.println(getMessage());
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
## Core Java Collections ArrayList
|
||||||
|
|
||||||
|
This module contains articles about the Java ArrayList collection
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Create an ArrayList with Multiple Object Types](https://www.baeldung.com/arraylist-with-multiple-object-types)
|
32
core-java-modules/core-java-collections-array-list-2/pom.xml
Normal file
32
core-java-modules/core-java-collections-array-list-2/pom.xml
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<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>
|
||||||
|
<artifactId>core-java-collections-array-list-2</artifactId>
|
||||||
|
<name>core-java-collections-array-list-2</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<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>${maven-compiler-plugin.source}</source>
|
||||||
|
<target>${maven-compiler-plugin.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven-compiler-plugin.source>17</maven-compiler-plugin.source>
|
||||||
|
<maven-compiler-plugin.target>17</maven-compiler-plugin.target>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.baeldung.list.multipleobjecttypes;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
public class AlternativeMultipeTypeList {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// List of Parent Class
|
||||||
|
ArrayList<Number> myList = new ArrayList<>();
|
||||||
|
myList.add(1.2);
|
||||||
|
myList.add(2);
|
||||||
|
myList.add(-3.5);
|
||||||
|
|
||||||
|
// List of Interface type
|
||||||
|
ArrayList<Map> diffMapList = new ArrayList<>();
|
||||||
|
diffMapList.add(new HashMap<>());
|
||||||
|
diffMapList.add(new TreeMap<>());
|
||||||
|
diffMapList.add(new LinkedHashMap<>());
|
||||||
|
|
||||||
|
// List of Custom Object
|
||||||
|
ArrayList<CustomObject> objList = new ArrayList<>();
|
||||||
|
objList.add(new CustomObject("String"));
|
||||||
|
objList.add(new CustomObject(2));
|
||||||
|
|
||||||
|
// List via Functional Interface
|
||||||
|
List<Object> dataList = new ArrayList<>();
|
||||||
|
|
||||||
|
Predicate<Object> myPredicate = inputData -> (inputData instanceof String || inputData instanceof Integer);
|
||||||
|
|
||||||
|
UserFunctionalInterface myInterface = (listObj, data) -> {
|
||||||
|
if (myPredicate.test(data))
|
||||||
|
listObj.add(data);
|
||||||
|
else
|
||||||
|
System.out.println("Skipping input as data not allowed for class: " + data.getClass()
|
||||||
|
.getSimpleName());
|
||||||
|
return listObj;
|
||||||
|
};
|
||||||
|
|
||||||
|
myInterface.addToList(dataList, Integer.valueOf(2));
|
||||||
|
myInterface.addToList(dataList, Double.valueOf(3.33));
|
||||||
|
myInterface.addToList(dataList, "String Value");
|
||||||
|
myInterface.printList(dataList);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.list.multipleobjecttypes;
|
||||||
|
|
||||||
|
public class CustomObject {
|
||||||
|
String classData;
|
||||||
|
Integer intData;
|
||||||
|
|
||||||
|
CustomObject(String classData) {
|
||||||
|
this.classData = classData;
|
||||||
|
}
|
||||||
|
|
||||||
|
CustomObject(Integer intData) {
|
||||||
|
this.intData = intData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClassData() {
|
||||||
|
return this.classData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getIntData() {
|
||||||
|
return this.intData;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.list.multipleobjecttypes;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MultipleObjectTypeArrayList {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
ArrayList<Object> multiTypeList = new ArrayList<>();
|
||||||
|
|
||||||
|
multiTypeList.add(Integer.valueOf(10));
|
||||||
|
multiTypeList.add(Double.valueOf(11.5));
|
||||||
|
multiTypeList.add("String Data");
|
||||||
|
multiTypeList.add(Arrays.asList(1, 2, 3));
|
||||||
|
multiTypeList.add(new CustomObject("Class Data"));
|
||||||
|
multiTypeList.add(BigInteger.valueOf(123456789));
|
||||||
|
multiTypeList.add(LocalDate.of(2023, 9, 19));
|
||||||
|
|
||||||
|
for (Object dataObj : multiTypeList) {
|
||||||
|
if (dataObj instanceof Integer intData)
|
||||||
|
System.out.println("Integer Data : " + intData);
|
||||||
|
else if (dataObj instanceof Double doubleData)
|
||||||
|
System.out.println("Double Data : " + doubleData);
|
||||||
|
else if (dataObj instanceof String stringData)
|
||||||
|
System.out.println("String Data : " + stringData);
|
||||||
|
else if (dataObj instanceof List<?> intList)
|
||||||
|
System.out.println("List Data : " + intList);
|
||||||
|
else if (dataObj instanceof CustomObject customObj)
|
||||||
|
System.out.println("CustomObject Data : " + customObj.getClassData());
|
||||||
|
else if (dataObj instanceof BigInteger bigIntData)
|
||||||
|
System.out.println("BigInteger Data : " + bigIntData);
|
||||||
|
else if (dataObj instanceof LocalDate localDate)
|
||||||
|
System.out.println("LocalDate Data : " + localDate.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.list.multipleobjecttypes;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface UserFunctionalInterface {
|
||||||
|
|
||||||
|
List<Object> addToList(List<Object> list, Object data);
|
||||||
|
|
||||||
|
default void printList(List<Object> dataList) {
|
||||||
|
for (Object data : dataList) {
|
||||||
|
if (data instanceof String stringData)
|
||||||
|
System.out.println("String Data: " + stringData);
|
||||||
|
if (data instanceof Integer intData)
|
||||||
|
System.out.println("Integer Data: " + intData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -28,13 +28,14 @@
|
|||||||
<!--<module>core-java-streams-2</module> -->
|
<!--<module>core-java-streams-2</module> -->
|
||||||
<!--<module>core-java-sun</module> -->
|
<!--<module>core-java-sun</module> -->
|
||||||
<module>core-java-9-improvements</module>
|
<module>core-java-9-improvements</module>
|
||||||
<module>core-java-collections-array-list</module>
|
|
||||||
<module>core-java-9-streams</module>
|
<module>core-java-9-streams</module>
|
||||||
<module>core-java-9</module>
|
<module>core-java-9</module>
|
||||||
<module>core-java-10</module>
|
<module>core-java-10</module>
|
||||||
<module>core-java-11</module>
|
<module>core-java-11</module>
|
||||||
<module>core-java-11-2</module>
|
<module>core-java-11-2</module>
|
||||||
<module>core-java-11-3</module>
|
<module>core-java-11-3</module>
|
||||||
|
<module>core-java-collections-array-list</module>
|
||||||
|
<module>core-java-collections-array-list-2</module>
|
||||||
<module>core-java-collections-list-4</module>
|
<module>core-java-collections-list-4</module>
|
||||||
<module>core-java-collections-list-5</module>
|
<module>core-java-collections-list-5</module>
|
||||||
<module>core-java-collections-maps-4</module>
|
<module>core-java-collections-maps-4</module>
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
<groupId>org.jenkins-ci.plugins</groupId>
|
<groupId>org.jenkins-ci.plugins</groupId>
|
||||||
<artifactId>plugin</artifactId>
|
<artifactId>plugin</artifactId>
|
||||||
<version>2.33</version>
|
<version>2.33</version>
|
||||||
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -23,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
|
|
||||||
@Testcontainers
|
@Testcontainers
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class ProductRepositoryIntegrationTest {
|
class ProductRepositoryNestedLiveTest {
|
||||||
|
|
||||||
private static final String KEYSPACE_NAME = "mynamespace";
|
private static final String KEYSPACE_NAME = "mynamespace";
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
package org.baeldung.objectmapper;
|
package org.baeldung.objectmapper;
|
||||||
|
|
||||||
import com.datastax.oss.driver.api.core.CqlIdentifier;
|
import java.net.InetSocketAddress;
|
||||||
import com.datastax.oss.driver.api.core.CqlSession;
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.objectmapper.dao.CounterDao;
|
import org.baeldung.objectmapper.dao.CounterDao;
|
||||||
import org.baeldung.objectmapper.dao.UserDao;
|
import org.baeldung.objectmapper.dao.UserDao;
|
||||||
import org.baeldung.objectmapper.entity.Counter;
|
import org.baeldung.objectmapper.entity.Counter;
|
||||||
@ -14,7 +15,8 @@ import org.testcontainers.containers.CassandraContainer;
|
|||||||
import org.testcontainers.junit.jupiter.Container;
|
import org.testcontainers.junit.jupiter.Container;
|
||||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||||
|
|
||||||
import java.util.List;
|
import com.datastax.oss.driver.api.core.CqlIdentifier;
|
||||||
|
import com.datastax.oss.driver.api.core.CqlSession;
|
||||||
|
|
||||||
@Testcontainers
|
@Testcontainers
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@ -23,35 +25,30 @@ public class MapperLiveTest {
|
|||||||
private static final String KEYSPACE_NAME = "baeldung";
|
private static final String KEYSPACE_NAME = "baeldung";
|
||||||
|
|
||||||
@Container
|
@Container
|
||||||
private static final CassandraContainer cassandra = (CassandraContainer) new CassandraContainer("cassandra:3.11.2")
|
private static final CassandraContainer cassandra = (CassandraContainer) new CassandraContainer("cassandra:3.11.2").withExposedPorts(9042);
|
||||||
.withExposedPorts(9042);
|
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
static void setupCassandraConnectionProperties() {
|
static void setupCassandraConnectionProperties() {
|
||||||
System.setProperty("spring.data.cassandra.keyspace-name", KEYSPACE_NAME);
|
System.setProperty("spring.data.cassandra.keyspace-name", KEYSPACE_NAME);
|
||||||
System.setProperty("spring.data.cassandra.contact-points", cassandra.getContainerIpAddress());
|
System.setProperty("spring.data.cassandra.contact-points", cassandra.getHost());
|
||||||
System.setProperty("spring.data.cassandra.port", String.valueOf(cassandra.getMappedPort(9042)));
|
System.setProperty("spring.data.cassandra.port", String.valueOf(cassandra.getMappedPort(9042)));
|
||||||
|
setupCassandra(new InetSocketAddress(cassandra.getHost(), cassandra.getMappedPort(9042)), cassandra.getLocalDatacenter());
|
||||||
}
|
}
|
||||||
|
|
||||||
static UserDao userDao;
|
static UserDao userDao;
|
||||||
static CounterDao counterDao;
|
static CounterDao counterDao;
|
||||||
|
|
||||||
@BeforeAll
|
static void setupCassandra(InetSocketAddress cassandraEndpoint, String localDataCenter) {
|
||||||
static void setup() {
|
CqlSession session = CqlSession.builder()
|
||||||
setupCassandraConnectionProperties();
|
.withLocalDatacenter(localDataCenter)
|
||||||
CqlSession session = CqlSession.builder().build();
|
.addContactPoint(cassandraEndpoint)
|
||||||
|
.build();
|
||||||
|
|
||||||
String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS baeldung " +
|
String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS baeldung " + "WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};";
|
||||||
"WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};";
|
|
||||||
String useKeyspace = "USE baeldung;";
|
String useKeyspace = "USE baeldung;";
|
||||||
String createUserTable = "CREATE TABLE IF NOT EXISTS user_profile " +
|
String createUserTable = "CREATE TABLE IF NOT EXISTS user_profile " + "(id int, username text, user_age int, writetime bigint, PRIMARY KEY (id, user_age)) " + "WITH CLUSTERING ORDER BY (user_age DESC);";
|
||||||
"(id int, username text, user_age int, writetime bigint, PRIMARY KEY (id, user_age)) " +
|
String createAdminTable = "CREATE TABLE IF NOT EXISTS admin_profile " + "(id int, username text, user_age int, role text, writetime bigint, department text, " + "PRIMARY KEY (id, user_age)) " + "WITH CLUSTERING ORDER BY (user_age DESC);";
|
||||||
"WITH CLUSTERING ORDER BY (user_age DESC);";
|
String createCounter = "CREATE TABLE IF NOT EXISTS counter " + "(id text, count counter, PRIMARY KEY (id));";
|
||||||
String createAdminTable = "CREATE TABLE IF NOT EXISTS admin_profile " +
|
|
||||||
"(id int, username text, user_age int, role text, writetime bigint, department text, " +
|
|
||||||
"PRIMARY KEY (id, user_age)) " +
|
|
||||||
"WITH CLUSTERING ORDER BY (user_age DESC);";
|
|
||||||
String createCounter = "CREATE TABLE IF NOT EXISTS counter " +
|
|
||||||
"(id text, count counter, PRIMARY KEY (id));";
|
|
||||||
|
|
||||||
session.execute(createKeyspace);
|
session.execute(createKeyspace);
|
||||||
session.execute(useKeyspace);
|
session.execute(useKeyspace);
|
||||||
@ -87,10 +84,13 @@ public class MapperLiveTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenUser_whenGetUsersOlderThan_thenRetrieved() {
|
void givenUser_whenGetUsersOlderThan_thenRetrieved() {
|
||||||
User user = new User(2, "JaneDoe", 20);
|
User user = new User(2, "JaneDoe", 32);
|
||||||
|
User userTwo = new User(3, "JohnDoe", 20);
|
||||||
userDao.insertUser(user);
|
userDao.insertUser(user);
|
||||||
List<User> retrievedUsers = userDao.getUsersOlderThanAge(30).all();
|
userDao.insertUser(userTwo);
|
||||||
Assertions.assertEquals(retrievedUsers.size(), 1);
|
List<User> retrievedUsers = userDao.getUsersOlderThanAge(30)
|
||||||
|
.all();
|
||||||
|
Assertions.assertEquals(1, retrievedUsers.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -104,6 +104,7 @@
|
|||||||
<module>spring-boot-springdoc-2</module>
|
<module>spring-boot-springdoc-2</module>
|
||||||
<module>spring-boot-documentation</module>
|
<module>spring-boot-documentation</module>
|
||||||
<module>spring-boot-3-url-matching</module>
|
<module>spring-boot-3-url-matching</module>
|
||||||
|
<module>spring-boot-graalvm-docker</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
@ -41,6 +41,27 @@
|
|||||||
<artifactId>mockserver-netty</artifactId>
|
<artifactId>mockserver-netty</artifactId>
|
||||||
<version>${mockserver.version}</version>
|
<version>${mockserver.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>${jupiter.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>${jupiter.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>${jupiter.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-params</artifactId>
|
||||||
|
<version>${jupiter.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mock-server</groupId>
|
<groupId>org.mock-server</groupId>
|
||||||
<artifactId>mockserver-client-java</artifactId>
|
<artifactId>mockserver-client-java</artifactId>
|
||||||
@ -187,8 +208,46 @@
|
|||||||
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
|
||||||
<start-class>com.baeldung.sample.TodoApplication</start-class>
|
<start-class>com.baeldung.sample.TodoApplication</start-class>
|
||||||
<mockserver.version>5.14.0</mockserver.version>
|
<mockserver.version>5.14.0</mockserver.version>
|
||||||
<spring-boot.version>3.1.0</spring-boot.version>
|
<spring-boot.version>3.2.0-SNAPSHOT</spring-boot.version>
|
||||||
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
|
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
|
||||||
|
<jupiter.version>5.10.0</jupiter.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<releases>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</releases>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</pluginRepository>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<releases>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</releases>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.restclient;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Article {
|
||||||
|
Integer id;
|
||||||
|
String title;
|
||||||
|
|
||||||
|
public Article(Integer id, String title) {
|
||||||
|
this.id = id;
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Article article = (Article) o;
|
||||||
|
return Objects.equals(id, article.id) && Objects.equals(title, article.title);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, title);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.baeldung.restclient;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/articles")
|
||||||
|
public class ArticleController {
|
||||||
|
Map<Integer, Article> database = new HashMap<>();
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public Collection<Article> getArticles() {
|
||||||
|
return database.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Article getArticle(@PathVariable Integer id) {
|
||||||
|
return database.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public void createArticle(@RequestBody Article article) {
|
||||||
|
database.put(article.getId(), article);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public void updateArticle(@PathVariable Integer id, @RequestBody Article article) {
|
||||||
|
assert Objects.equals(id, article.getId());
|
||||||
|
database.remove(id);
|
||||||
|
database.put(id, article);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void deleteArticle(@PathVariable Integer id) {
|
||||||
|
database.remove(id);
|
||||||
|
}
|
||||||
|
@DeleteMapping()
|
||||||
|
public void deleteArticles() {
|
||||||
|
database.clear();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.restclient;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class RestClientApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(RestClientApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,114 @@
|
|||||||
|
package com.baeldung.restclient;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
public class RestClientIntegrationTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
private String uriBase;
|
||||||
|
RestClient restClient = RestClient.create();
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public void setup() {
|
||||||
|
uriBase = "http://localhost:" + port;
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void teardown() {
|
||||||
|
restClient.delete()
|
||||||
|
.uri(uriBase + "/articles")
|
||||||
|
.retrieve()
|
||||||
|
.toBodilessEntity();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldGetArticlesAndReturnString() {
|
||||||
|
String articlesAsString = restClient.get()
|
||||||
|
.uri(uriBase + "/articles")
|
||||||
|
.retrieve()
|
||||||
|
.body(String.class);
|
||||||
|
|
||||||
|
assertThat(articlesAsString).isEqualTo("[]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldPostAndGetArticles() {
|
||||||
|
Article article = new Article(1, "How to use RestClient");
|
||||||
|
restClient.post()
|
||||||
|
.uri(uriBase + "/articles")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.body(article)
|
||||||
|
.retrieve()
|
||||||
|
.toBodilessEntity();
|
||||||
|
|
||||||
|
List<Article> articles = restClient.get()
|
||||||
|
.uri(uriBase + "/articles")
|
||||||
|
.retrieve()
|
||||||
|
.body(new ParameterizedTypeReference<>() {});
|
||||||
|
|
||||||
|
assertThat(articles).isEqualTo(List.of(article));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldPostAndPutAndGetArticles() {
|
||||||
|
Article article = new Article(1, "How to use RestClient");
|
||||||
|
restClient.post()
|
||||||
|
.uri(uriBase + "/articles")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.body(article)
|
||||||
|
.retrieve()
|
||||||
|
.toBodilessEntity();
|
||||||
|
|
||||||
|
Article articleChanged = new Article(1, "How to use RestClient even better");
|
||||||
|
restClient.put()
|
||||||
|
.uri(uriBase + "/articles/1")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.body(articleChanged)
|
||||||
|
.retrieve()
|
||||||
|
.toBodilessEntity();
|
||||||
|
|
||||||
|
List<Article> articles = restClient.get()
|
||||||
|
.uri(uriBase + "/articles")
|
||||||
|
.retrieve()
|
||||||
|
.body(new ParameterizedTypeReference<>() {});
|
||||||
|
|
||||||
|
assertThat(articles).isEqualTo(List.of(articleChanged));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldPostAndDeleteArticles() {
|
||||||
|
Article article = new Article(1, "How to use RestClient");
|
||||||
|
restClient.post()
|
||||||
|
.uri(uriBase + "/articles")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.body(article)
|
||||||
|
.retrieve()
|
||||||
|
.toBodilessEntity();
|
||||||
|
|
||||||
|
restClient.delete()
|
||||||
|
.uri(uriBase + "/articles")
|
||||||
|
.retrieve()
|
||||||
|
.toBodilessEntity();
|
||||||
|
|
||||||
|
List<Article> articles = restClient.get()
|
||||||
|
.uri(uriBase + "/articles")
|
||||||
|
.retrieve()
|
||||||
|
.body(new ParameterizedTypeReference<>() {});
|
||||||
|
|
||||||
|
assertThat(articles).isEqualTo(List.of());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
FROM ubuntu:jammy
|
||||||
|
COPY target/springboot-graalvm-docker /springboot-graalvm-docker
|
||||||
|
CMD ["/springboot-graalvm-docker"]
|
41
spring-boot-modules/spring-boot-graalvm-docker/pom.xml
Normal file
41
spring-boot-modules/spring-boot-graalvm-docker/pom.xml
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?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>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-3</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-3</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>spring-boot-graalvm-docker</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<name>spring-boot-graalvm-docker</name>
|
||||||
|
<description>Spring Boot GrralVM with Docker</description>
|
||||||
|
<dependencies>
|
||||||
|
<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.graalvm.buildtools</groupId>
|
||||||
|
<artifactId>native-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.graalvmdockerimage;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class GraalvmDockerImageApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(GraalvmDockerImageApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
class HelloController {
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String hello() {
|
||||||
|
return "Hello GraalVM";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
|
@ -11,10 +11,9 @@
|
|||||||
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>spring-boot-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<relativePath>../../parent-boot-2</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -11,10 +11,9 @@
|
|||||||
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>spring-boot-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<relativePath>../../parent-boot-2</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -11,10 +11,9 @@
|
|||||||
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>spring-boot-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<relativePath>../../parent-boot-2</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -9,10 +9,9 @@
|
|||||||
<description>Demo project for Spring Boot Logging with Log4J2</description>
|
<description>Demo project for Spring Boot Logging with Log4J2</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>spring-boot-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<relativePath>../../parent-boot-2</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -10,10 +10,9 @@
|
|||||||
<description>Module For Spring Boot Integration with BIRT</description>
|
<description>Module For Spring Boot Integration with BIRT</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>spring-boot-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<relativePath>../../parent-boot-2</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -4,10 +4,10 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>parent-boot-3</artifactId>
|
||||||
<version>3.1.3</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath/>
|
<relativePath>../../parent-boot-3</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>springbootsslbundles</artifactId>
|
<artifactId>springbootsslbundles</artifactId>
|
||||||
<name>spring-boot-ssl-bundles</name>
|
<name>spring-boot-ssl-bundles</name>
|
||||||
|
@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class SSLBundleApplicationTests {
|
class SpringContextTest {
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
|
|
@ -22,9 +22,9 @@ public class PathPatternController {
|
|||||||
return "/spring5/*id";
|
return "/spring5/*id";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("//**")
|
@GetMapping("/resources/**")
|
||||||
public String wildcardTakingZeroOrMorePathSegments() {
|
public String wildcardTakingZeroOrMorePathSegments() {
|
||||||
return "//**";
|
return "/resources/**";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{baeldung:[a-z]+}")
|
@GetMapping("/{baeldung:[a-z]+}")
|
||||||
|
@ -29,7 +29,7 @@ public class ExploreSpring5URLPatternUsingRouterFunctions {
|
|||||||
.andRoute(GET("/{var1}_{var2}"), serverRequest -> ok().body(fromValue(serverRequest.pathVariable("var1") + " , " + serverRequest.pathVariable("var2"))))
|
.andRoute(GET("/{var1}_{var2}"), serverRequest -> ok().body(fromValue(serverRequest.pathVariable("var1") + " , " + serverRequest.pathVariable("var2"))))
|
||||||
.andRoute(GET("/{baeldung:[a-z]+}"), serverRequest -> ok().body(fromValue("/{baeldung:[a-z]+} was accessed and baeldung=" + serverRequest.pathVariable("baeldung"))))
|
.andRoute(GET("/{baeldung:[a-z]+}"), serverRequest -> ok().body(fromValue("/{baeldung:[a-z]+} was accessed and baeldung=" + serverRequest.pathVariable("baeldung"))))
|
||||||
.and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/")))
|
.and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/")))
|
||||||
.and(RouterFunctions.resources("//**", new ClassPathResource("/")));
|
.and(RouterFunctions.resources("/resources/**", new ClassPathResource("resources/")));
|
||||||
}
|
}
|
||||||
|
|
||||||
WebServer start() throws Exception {
|
WebServer start() throws Exception {
|
||||||
|
@ -108,9 +108,9 @@ public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRouter_whenAccess_thenGot() throws Exception {
|
public void givenRouter_whenAccess_thenGot() {
|
||||||
client.get()
|
client.get()
|
||||||
.uri("//test/test.txt")
|
.uri("/resources/test/test.txt")
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk()
|
.isOk()
|
||||||
|
@ -72,12 +72,12 @@ public class PathPatternsUsingHandlerMethodIntegrationTest {
|
|||||||
public void givenHandlerMethod_whenURLWithWildcardTakingZeroOrMorePathSegments_then200() {
|
public void givenHandlerMethod_whenURLWithWildcardTakingZeroOrMorePathSegments_then200() {
|
||||||
|
|
||||||
client.get()
|
client.get()
|
||||||
.uri("//baeldung")
|
.uri("/resources/baeldung")
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.is2xxSuccessful()
|
.is2xxSuccessful()
|
||||||
.expectBody()
|
.expectBody()
|
||||||
.equals("//**");
|
.equals("/resources/**");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
## Relevant Articles
|
## Relevant Articles
|
||||||
- [How to Resolve Spring Webflux DataBufferLimitException](https://www.baeldung.com/spring-webflux-databufferlimitexception)
|
- [How to Resolve Spring Webflux DataBufferLimitException](https://www.baeldung.com/spring-webflux-databufferlimitexception)
|
||||||
- [Custom WebFlux Exceptions in Spring Boot 3](https://www.baeldung.com/spring-boot-custom-webflux-exceptions)
|
- [Custom WebFlux Exceptions in Spring Boot 3](https://www.baeldung.com/spring-boot-custom-webflux-exceptions)
|
||||||
- [Handling Errors in Spring WebFlux](https://www.baeldung.com/spring-webflux-errors)
|
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
This module contains articles about Spring Reactive that are also part of an Ebook.
|
This module contains articles about Spring Reactive that **are also part of an Ebook.**
|
||||||
|
|
||||||
## Spring Reactive
|
## Spring Reactive
|
||||||
|
|
||||||
@ -14,9 +14,10 @@ This module contains articles describing reactive processing in Spring.
|
|||||||
- [Spring 5 WebClient](https://www.baeldung.com/spring-5-webclient)
|
- [Spring 5 WebClient](https://www.baeldung.com/spring-5-webclient)
|
||||||
- [Spring WebClient vs. RestTemplate](https://www.baeldung.com/spring-webclient-resttemplate)
|
- [Spring WebClient vs. RestTemplate](https://www.baeldung.com/spring-webclient-resttemplate)
|
||||||
- [Spring WebClient Requests with Parameters](https://www.baeldung.com/webflux-webclient-parameters)
|
- [Spring WebClient Requests with Parameters](https://www.baeldung.com/webflux-webclient-parameters)
|
||||||
|
- [Handling Errors in Spring WebFlux](https://www.baeldung.com/spring-webflux-errors)
|
||||||
- [Spring Security 5 for Reactive Applications](https://www.baeldung.com/spring-security-5-reactive)
|
- [Spring Security 5 for Reactive Applications](https://www.baeldung.com/spring-security-5-reactive)
|
||||||
- [Concurrency in Spring WebFlux](https://www.baeldung.com/spring-webflux-concurrency)
|
- [Concurrency in Spring WebFlux](https://www.baeldung.com/spring-webflux-concurrency)
|
||||||
|
|
||||||
### NOTE:
|
### NOTE:
|
||||||
|
|
||||||
Since this is a module tied to an e-book, it should **not** be moved or used to store the code for any further article.
|
## Since this is a module tied to an e-book, it should **not** be moved or used to store the code for any further article.
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-boot-3</artifactId>
|
<artifactId>parent-boot-3</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../../parent-boot-3/pom.xml</relativePath>
|
<relativePath>../../parent-boot-3</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.spring.reactive.errorhandling;
|
package com.baeldung.reactive.errorhandling;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.spring.reactive.errorhandling;
|
package com.baeldung.reactive.errorhandling;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.spring.reactive.errorhandling;
|
package com.baeldung.reactive.errorhandling;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.spring.reactive.errorhandling;
|
package com.baeldung.reactive.errorhandling;
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.spring.reactive.errorhandling;
|
package com.baeldung.reactive.errorhandling;
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.server.ResponseStatusException;
|
import org.springframework.web.server.ResponseStatusException;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.spring.reactive.errorhandling;
|
package com.baeldung.reactive.errorhandling;
|
||||||
|
|
||||||
import static org.springframework.http.MediaType.TEXT_PLAIN;
|
import static org.springframework.http.MediaType.TEXT_PLAIN;
|
||||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.spring.reactive.errorhandling;
|
package com.baeldung.reactive.errorhandling;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.interfaces.unittest;
|
||||||
|
|
||||||
|
public class Circle implements Shape {
|
||||||
|
|
||||||
|
private double radius;
|
||||||
|
|
||||||
|
Circle(double radius) {
|
||||||
|
this.radius = radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double area() {
|
||||||
|
return 3.14 * radius * radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double circumference() {
|
||||||
|
return 2 * 3.14 * radius;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.interfaces.unittest;
|
||||||
|
|
||||||
|
public class Rectangle implements Shape {
|
||||||
|
|
||||||
|
private double length;
|
||||||
|
private double breadth;
|
||||||
|
|
||||||
|
public Rectangle(double length, double breadth) {
|
||||||
|
this.length = length;
|
||||||
|
this.breadth = breadth;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double area() {
|
||||||
|
return length * breadth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double perimeter() {
|
||||||
|
return 2 * (length + breadth);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.baeldung.interfaces.unittest;
|
||||||
|
|
||||||
|
public interface Shape {
|
||||||
|
|
||||||
|
double area();
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.interfaces.unittest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class CircleExtendsBaseUnitTest extends ShapeUnitTest {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> instantiateShapeWithExpectedArea() {
|
||||||
|
Map<String, Object> shapeAreaMap = new HashMap<>();
|
||||||
|
shapeAreaMap.put("shape", new Circle(5));
|
||||||
|
shapeAreaMap.put("area", 78.5);
|
||||||
|
return shapeAreaMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenCircumferenceIsCalculated_thenSuccessful() {
|
||||||
|
Circle circle = new Circle(2);
|
||||||
|
double circumference = circle.circumference();
|
||||||
|
assertEquals(12.56, circumference);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.interfaces.unittest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class CircleUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenAreaIsCalculated_thenSuccessful() {
|
||||||
|
Shape circle = new Circle(5);
|
||||||
|
double area = circle.area();
|
||||||
|
assertEquals(78.5, area);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenCircumferenceIsCalculated_thenSuccessful() {
|
||||||
|
Circle circle = new Circle(2);
|
||||||
|
double circumference = circle.circumference();
|
||||||
|
assertEquals(12.56, circumference);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.interfaces.unittest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
public class ParameterizedUnitTest {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("data")
|
||||||
|
void givenShapeInstance_whenAreaIsCalculated_thenSuccessful(Shape shapeInstance, double expectedArea) {
|
||||||
|
double area = shapeInstance.area();
|
||||||
|
assertEquals(expectedArea, area);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Collection<Object[]> data() {
|
||||||
|
return Arrays.asList(new Object[][] {
|
||||||
|
{ new Circle(5), 78.5 },
|
||||||
|
{ new Rectangle(4, 5), 20 }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.interfaces.unittest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class RectangleExtendsBaseUnitTest extends ShapeUnitTest {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> instantiateShapeWithExpectedArea() {
|
||||||
|
Map<String, Object> shapeAreaMap = new HashMap<>();
|
||||||
|
shapeAreaMap.put("shape", new Rectangle(5, 4));
|
||||||
|
shapeAreaMap.put("area", 20.0);
|
||||||
|
return shapeAreaMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenPerimeterIsCalculated_thenSuccessful() {
|
||||||
|
Rectangle rectangle = new Rectangle(5, 4);
|
||||||
|
double perimeter = rectangle.perimeter();
|
||||||
|
assertEquals(18, perimeter);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.interfaces.unittest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class RectangleUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenAreaIsCalculated_thenSuccessful() {
|
||||||
|
Shape rectangle = new Rectangle(5, 4);
|
||||||
|
double area = rectangle.area();
|
||||||
|
assertEquals(20, area);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenPerimeterIsCalculated_thenSuccessful() {
|
||||||
|
Rectangle rectangle = new Rectangle(5, 4);
|
||||||
|
double perimeter = rectangle.perimeter();
|
||||||
|
assertEquals(18, perimeter);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.interfaces.unittest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public abstract class ShapeUnitTest {
|
||||||
|
|
||||||
|
public abstract Map<String, Object> instantiateShapeWithExpectedArea();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenShapeInstance_whenAreaIsCalculated_thenSuccessful() {
|
||||||
|
Map<String, Object> shapeAreaMap = instantiateShapeWithExpectedArea();
|
||||||
|
Shape shape = (Shape) shapeAreaMap.get("shape");
|
||||||
|
double expectedArea = (double) shapeAreaMap.get("area");
|
||||||
|
double area = shape.area();
|
||||||
|
assertEquals(expectedArea, area);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user