Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
094b9804e5
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.category;
|
||||||
|
|
||||||
|
class BaeldungCategory {
|
||||||
|
|
||||||
|
public static String capitalize(String self) {
|
||||||
|
String capitalizedStr = self;
|
||||||
|
if (self.size() > 0) {
|
||||||
|
capitalizedStr = self.substring(0, 1).toUpperCase() + self.substring(1);
|
||||||
|
}
|
||||||
|
return capitalizedStr
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double toThePower(Number self, Number exponent) {
|
||||||
|
return Math.pow(self, exponent);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.category;
|
||||||
|
|
||||||
|
import groovy.lang.Category
|
||||||
|
|
||||||
|
@Category(Number)
|
||||||
|
class NumberCategory {
|
||||||
|
|
||||||
|
public Number cube() {
|
||||||
|
return this*this*this
|
||||||
|
}
|
||||||
|
|
||||||
|
public int divideWithRoundUp(BigDecimal divisor, boolean isRoundUp) {
|
||||||
|
def mathRound = isRoundUp ? BigDecimal.ROUND_UP : BigDecimal.ROUND_DOWN
|
||||||
|
return (int)new BigDecimal(this).divide(divisor, 0, mathRound)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,101 @@
|
|||||||
|
package com.baeldung.category
|
||||||
|
|
||||||
|
import groovy.time.*
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import groovy.xml.*
|
||||||
|
import groovy.xml.dom.*
|
||||||
|
import com.baeldung.category.BaeldungCategory
|
||||||
|
import com.baeldung.category.NumberCategory
|
||||||
|
|
||||||
|
class CategoryUnitTest extends GroovyTestCase {
|
||||||
|
|
||||||
|
void test_whenUsingTimeCategory_thenOperationOnDate() {
|
||||||
|
def jan_1_2019 = new Date("01/01/2019")
|
||||||
|
use (TimeCategory) {
|
||||||
|
assert jan_1_2019 + 10.seconds == new Date("01/01/2019 00:00:10")
|
||||||
|
|
||||||
|
assert jan_1_2019 + 20.minutes == new Date("01/01/2019 00:20:00")
|
||||||
|
|
||||||
|
assert jan_1_2019 + 2.hours == new Date("01/01/2019 02:00:00")
|
||||||
|
|
||||||
|
assert jan_1_2019 - 1.day == new Date("12/31/2018")
|
||||||
|
|
||||||
|
assert jan_1_2019 + 2.weeks == new Date("01/15/2019")
|
||||||
|
|
||||||
|
assert jan_1_2019 - 2.months == new Date("11/01/2018")
|
||||||
|
|
||||||
|
assert jan_1_2019 + 3.years == new Date("01/01/2022")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenUsingTimeCategory_thenOperationOnNumber() {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy")
|
||||||
|
use (TimeCategory) {
|
||||||
|
assert sdf.format(5.days.from.now) == sdf.format(new Date() + 5.days)
|
||||||
|
|
||||||
|
sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss")
|
||||||
|
assert sdf.format(10.minutes.from.now) == sdf.format(new Date() + 10.minutes)
|
||||||
|
assert sdf.format(2.hours.ago) == sdf.format(new Date() - 2.hours)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenUsingDOMCategory_thenOperationOnXML() {
|
||||||
|
|
||||||
|
def baeldungArticlesText = """
|
||||||
|
<articles>
|
||||||
|
<article core-java="true">
|
||||||
|
<title>An Intro to the Java Debug Interface (JDI)</title>
|
||||||
|
<desc>A quick and practical overview of Java Debug Interface.</desc>
|
||||||
|
</article>
|
||||||
|
<article core-java="false">
|
||||||
|
<title>A Quick Guide to Working with Web Services in Groovy</title>
|
||||||
|
<desc>Learn how to work with Web Services in Groovy.</desc>
|
||||||
|
</article>
|
||||||
|
</articles>
|
||||||
|
"""
|
||||||
|
def baeldungArticlesDom = DOMBuilder.newInstance().parseText(baeldungArticlesText)
|
||||||
|
|
||||||
|
def root = baeldungArticlesDom.documentElement
|
||||||
|
|
||||||
|
use (DOMCategory) {
|
||||||
|
assert root.article.size() == 2
|
||||||
|
|
||||||
|
def articles = root.article
|
||||||
|
|
||||||
|
assert articles[0].title.text() == "An Intro to the Java Debug Interface (JDI)"
|
||||||
|
assert articles[1].desc.text() == "Learn how to work with Web Services in Groovy."
|
||||||
|
|
||||||
|
def articleNode3 = root.appendNode(new QName("article"), ["core-java": "false"])
|
||||||
|
|
||||||
|
articleNode3.appendNode("title", "Metaprogramming in Groovy")
|
||||||
|
articleNode3.appendNode("desc", "Explore the concept of runtime and compile-time metaprogramming in Groovy")
|
||||||
|
|
||||||
|
assert root.article.size() == 3
|
||||||
|
|
||||||
|
assert root.article[2].title.text() == "Metaprogramming in Groovy"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenUsingBaeldungCategory_thenCapitalizeString() {
|
||||||
|
use (BaeldungCategory) {
|
||||||
|
assert "norman".capitalize() == "Norman"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenUsingBaeldungCategory_thenOperationsOnNumber() {
|
||||||
|
use (BaeldungCategory) {
|
||||||
|
assert 50.toThePower(2) == 2500
|
||||||
|
assert 2.4.toThePower(4) == 33.1776
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenUsingNumberCategory_thenOperationsOnNumber() {
|
||||||
|
use (NumberCategory) {
|
||||||
|
assert 3.cube() == 27
|
||||||
|
assert 25.divideWithRoundUp(6, true) == 5
|
||||||
|
assert 120.23.divideWithRoundUp(6.1, true) == 20
|
||||||
|
assert 150.9.divideWithRoundUp(12.1, false) == 12
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
core-java-modules/core-java-concurrency-basic-2/README.md
Normal file
10
core-java-modules/core-java-concurrency-basic-2/README.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
=========
|
||||||
|
|
||||||
|
## Core Java Concurrency Basic 2 Examples
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
|
||||||
|
- [wait and notify() Methods in Java](https://www.baeldung.com/java-wait-notify)
|
||||||
|
- [Difference Between Wait and Sleep in Java](https://www.baeldung.com/java-wait-and-sleep)
|
||||||
|
- [Guide to the Synchronized Keyword in Java](https://www.baeldung.com/java-synchronized)
|
||||||
|
- [Life Cycle of a Thread in Java](https://www.baeldung.com/java-thread-lifecycle)
|
33
core-java-modules/core-java-concurrency-basic-2/pom.xml
Normal file
33
core-java-modules/core-java-concurrency-basic-2/pom.xml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<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>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>core-java-concurrency-basic-2</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-concurrency-basic-2</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-concurrency-basic-2</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="org.springframework" level="WARN" />
|
||||||
|
<logger name="org.springframework.transaction" level="WARN" />
|
||||||
|
|
||||||
|
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||||
|
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -1,15 +1,15 @@
|
|||||||
package com.baeldung.concurrent.waitandnotify;
|
package com.baeldung.concurrent.waitandnotify;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
|
||||||
import org.junit.After;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class NetworkIntegrationTest {
|
public class NetworkIntegrationTest {
|
||||||
|
|
@ -3,18 +3,13 @@
|
|||||||
## Core Java Concurrency Basic Examples
|
## Core Java Concurrency Basic Examples
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Guide To CompletableFuture](http://www.baeldung.com/java-completablefuture)
|
- [Guide To CompletableFuture](https://www.baeldung.com/java-completablefuture)
|
||||||
- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial)
|
- [A Guide to the Java ExecutorService](https://www.baeldung.com/java-executor-service-tutorial)
|
||||||
- [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future)
|
- [Guide to java.util.concurrent.Future](https://www.baeldung.com/java-future)
|
||||||
- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep)
|
- [Overview of the java.util.concurrent](https://www.baeldung.com/java-util-concurrent)
|
||||||
- [Guide to the Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized)
|
- [Implementing a Runnable vs Extending a Thread](https://www.baeldung.com/java-runnable-vs-extending-thread)
|
||||||
- [Overview of the java.util.concurrent](http://www.baeldung.com/java-util-concurrent)
|
- [How to Kill a Java Thread](https://www.baeldung.com/java-thread-stop)
|
||||||
- [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread)
|
- [ExecutorService – Waiting for Threads to Finish](https://www.baeldung.com/java-executor-wait-for-threads)
|
||||||
- [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop)
|
- [Runnable vs. Callable in Java](https://www.baeldung.com/java-runnable-callable)
|
||||||
- [ExecutorService – Waiting for Threads to Finish](http://www.baeldung.com/java-executor-wait-for-threads)
|
|
||||||
- [wait and notify() Methods in Java](http://www.baeldung.com/java-wait-notify)
|
|
||||||
- [Life Cycle of a Thread in Java](http://www.baeldung.com/java-thread-lifecycle)
|
|
||||||
- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable)
|
|
||||||
- [What is Thread-Safety and How to Achieve it?](https://www.baeldung.com/java-thread-safety)
|
- [What is Thread-Safety and How to Achieve it?](https://www.baeldung.com/java-thread-safety)
|
||||||
- [How to Start a Thread in Java](https://www.baeldung.com/java-start-thread)
|
- [How to Start a Thread in Java](https://www.baeldung.com/java-start-thread)
|
||||||
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
|
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
package com.baeldung.concurrent.threadsafety.tests;
|
package com.baeldung.concurrent.threadsafety;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import com.baeldung.concurrent.threadsafety.callables.CounterCallable;
|
import com.baeldung.concurrent.threadsafety.callables.CounterCallable;
|
||||||
import com.baeldung.concurrent.threadsafety.services.Counter;
|
import com.baeldung.concurrent.threadsafety.services.Counter;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
public class CounterTest {
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class CounterUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
|
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
|
@ -1,14 +1,16 @@
|
|||||||
package com.baeldung.concurrent.threadsafety.tests;
|
package com.baeldung.concurrent.threadsafety;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import com.baeldung.concurrent.threadsafety.callables.ExtrinsicLockCounterCallable;
|
import com.baeldung.concurrent.threadsafety.callables.ExtrinsicLockCounterCallable;
|
||||||
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
|
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
public class ExtrinsicLockCounterTest {
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class ExtrinsicLockCounterUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
|
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
|
@ -1,13 +1,16 @@
|
|||||||
package com.baeldung.concurrent.threadsafety.tests;
|
package com.baeldung.concurrent.threadsafety;
|
||||||
|
|
||||||
import com.baeldung.concurrent.threadsafety.mathutils.MathUtils;
|
import com.baeldung.concurrent.threadsafety.mathutils.MathUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class MathUtilsTest {
|
public class MathUtilsUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalledFactorialMethod_thenCorrect() {
|
public void whenCalledFactorialMethod_thenCorrect() {
|
||||||
assertThat(MathUtils.factorial(2)).isEqualTo(2);
|
assertThat(MathUtils.factorial(2)).isEqualTo(new BigInteger("2"));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,14 +1,16 @@
|
|||||||
package com.baeldung.concurrent.threadsafety.tests;
|
package com.baeldung.concurrent.threadsafety;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import com.baeldung.concurrent.threadsafety.callables.MessageServiceCallable;
|
import com.baeldung.concurrent.threadsafety.callables.MessageServiceCallable;
|
||||||
import com.baeldung.concurrent.threadsafety.services.MessageService;
|
import com.baeldung.concurrent.threadsafety.services.MessageService;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
public class MessageServiceTest {
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class MessageServiceUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalledgetMessage_thenCorrect() throws Exception {
|
public void whenCalledgetMessage_thenCorrect() throws Exception {
|
@ -1,14 +1,16 @@
|
|||||||
package com.baeldung.concurrent.threadsafety.tests;
|
package com.baeldung.concurrent.threadsafety;
|
||||||
|
|
||||||
import com.baeldung.concurrent.threadsafety.callables.ReentrantLockCounterCallable;
|
import com.baeldung.concurrent.threadsafety.callables.ReentrantLockCounterCallable;
|
||||||
import com.baeldung.concurrent.threadsafety.services.ReentrantLockCounter;
|
import com.baeldung.concurrent.threadsafety.services.ReentrantLockCounter;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class ReentrantLockCounterTest {
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class ReentrantLockCounterUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
|
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
|
@ -1,14 +1,16 @@
|
|||||||
package com.baeldung.concurrent.threadsafety.tests;
|
package com.baeldung.concurrent.threadsafety;
|
||||||
|
|
||||||
import com.baeldung.concurrent.threadsafety.callables.ReentranReadWriteLockCounterCallable;
|
import com.baeldung.concurrent.threadsafety.callables.ReentranReadWriteLockCounterCallable;
|
||||||
import com.baeldung.concurrent.threadsafety.services.ReentrantReadWriteLockCounter;
|
import com.baeldung.concurrent.threadsafety.services.ReentrantReadWriteLockCounter;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class ReentrantReadWriteLockCounterTest {
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class ReentrantReadWriteLockCounterUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
|
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
|
||||||
@ -16,9 +18,9 @@ public class ReentrantReadWriteLockCounterTest {
|
|||||||
ReentrantReadWriteLockCounter counter = new ReentrantReadWriteLockCounter();
|
ReentrantReadWriteLockCounter counter = new ReentrantReadWriteLockCounter();
|
||||||
Future<Integer> future1 = (Future<Integer>) executorService.submit(new ReentranReadWriteLockCounterCallable(counter));
|
Future<Integer> future1 = (Future<Integer>) executorService.submit(new ReentranReadWriteLockCounterCallable(counter));
|
||||||
Future<Integer> future2 = (Future<Integer>) executorService.submit(new ReentranReadWriteLockCounterCallable(counter));
|
Future<Integer> future2 = (Future<Integer>) executorService.submit(new ReentranReadWriteLockCounterCallable(counter));
|
||||||
|
|
||||||
assertThat(future1.get()).isEqualTo(1);
|
|
||||||
assertThat(future2.get()).isEqualTo(2);
|
assertThat(future2.get()).isEqualTo(2);
|
||||||
|
assertThat(future1.get()).isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,3 +1,11 @@
|
|||||||
## Relevant Articles:
|
## Relevant Articles:
|
||||||
- [Map of Primitives in Java](https://www.baeldung.com/java-map-primitives)
|
- [Map of Primitives in Java](https://www.baeldung.com/java-map-primitives)
|
||||||
- [Copying a HashMap in Java](https://www.baeldung.com/java-copy-hashmap)
|
- [Copying a HashMap in Java](https://www.baeldung.com/java-copy-hashmap)
|
||||||
|
- [Guide to WeakHashMap in Java](https://www.baeldung.com/java-weakhashmap)
|
||||||
|
- [Map to String Conversion in Java](https://www.baeldung.com/java-map-to-string-conversion)
|
||||||
|
- [Iterate over a Map in Java](https://www.baeldung.com/java-iterate-map)
|
||||||
|
- [Merging Two Maps with Java 8](https://www.baeldung.com/java-merge-maps)
|
||||||
|
- [Sort a HashMap in Java](https://www.baeldung.com/java-hashmap-sort)
|
||||||
|
- [Finding the Highest Value in a Java Map](https://www.baeldung.com/java-find-map-max)
|
||||||
|
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||||
|
- [Java TreeMap vs HashMap](https://www.baeldung.com/java-treemap-vs-hashmap)
|
||||||
|
@ -41,6 +41,22 @@
|
|||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-collections4</artifactId>
|
||||||
|
<version>${commons-collections4.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>one.util</groupId>
|
||||||
|
<artifactId>streamex</artifactId>
|
||||||
|
<version>${streamex.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.jayway.awaitility</groupId>
|
||||||
|
<artifactId>awaitility</artifactId>
|
||||||
|
<version>${avaitility.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
@ -50,6 +66,9 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
<streamex.version>0.6.5</streamex.version>
|
||||||
|
<commons-collections4.version>4.1</commons-collections4.version>
|
||||||
|
<avaitility.version>1.7.0</avaitility.version>
|
||||||
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
||||||
<trove4j.version>3.0.2</trove4j.version>
|
<trove4j.version>3.0.2</trove4j.version>
|
||||||
<fastutil.version>8.1.0</fastutil.version>
|
<fastutil.version>8.1.0</fastutil.version>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.convert;
|
package com.baeldung.map.convert;
|
||||||
|
|
||||||
import com.google.common.base.Joiner;
|
import com.google.common.base.Joiner;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.convert;
|
package com.baeldung.map.convert;
|
||||||
|
|
||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
|
|
@ -1,4 +1,6 @@
|
|||||||
package com.baeldung.map;
|
package com.baeldung.map.copyhashmap;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.SerializationUtils;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -6,8 +8,6 @@ import java.util.Map.Entry;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.commons.lang3.SerializationUtils;
|
|
||||||
|
|
||||||
public class CopyHashMap {
|
public class CopyHashMap {
|
||||||
|
|
||||||
public static <String, Employee> HashMap<String, Employee> copyUsingConstructor(HashMap<String, Employee> originalMap) {
|
public static <String, Employee> HashMap<String, Employee> copyUsingConstructor(HashMap<String, Employee> originalMap) {
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.map.initialize;
|
package com.baeldung.map.initialize;
|
||||||
|
|
||||||
import java.util.AbstractMap;
|
import java.util.AbstractMap;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
@ -1,11 +1,7 @@
|
|||||||
package com.baeldung.map.util;
|
package com.baeldung.map.mapmax;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.*;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class MapMax {
|
public class MapMax {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.sort;
|
package com.baeldung.map.mergemaps;
|
||||||
|
|
||||||
public class Employee implements Comparable<Employee> {
|
public class Employee implements Comparable<Employee> {
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
package com.baeldung.map.java_8;
|
package com.baeldung.map.mergemaps;
|
||||||
|
|
||||||
import com.baeldung.sort.Employee;
|
|
||||||
import one.util.streamex.EntryStream;
|
import one.util.streamex.EntryStream;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.map;
|
package com.baeldung.map.primitives;
|
||||||
|
|
||||||
import cern.colt.map.AbstractIntDoubleMap;
|
import cern.colt.map.AbstractIntDoubleMap;
|
||||||
import cern.colt.map.OpenIntDoubleHashMap;
|
import cern.colt.map.OpenIntDoubleHashMap;
|
@ -1,8 +1,8 @@
|
|||||||
package com.baeldung.sort;
|
package com.baeldung.map.sort;
|
||||||
|
|
||||||
|
import com.baeldung.map.mergemaps.Employee;
|
||||||
import com.google.common.base.Functions;
|
import com.google.common.base.Functions;
|
||||||
import com.google.common.collect.ImmutableSortedMap;
|
import com.google.common.collect.ImmutableSortedMap;
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.common.collect.Ordering;
|
import com.google.common.collect.Ordering;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.convert;
|
package com.baeldung.map.convert;
|
||||||
|
|
||||||
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.collections4.MapUtils;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.convert;
|
package com.baeldung.map.convert;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
@ -1,13 +1,12 @@
|
|||||||
package com.baeldung.map;
|
package com.baeldung.map.copyhashmap;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
|
|
||||||
public class CopyHashMapUnitTest {
|
public class CopyHashMapUnitTest {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.map;
|
package com.baeldung.map.copyhashmap;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
@ -1,10 +1,10 @@
|
|||||||
package com.baeldung.java.map.initialize;
|
package com.baeldung.map.initialize;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class MapInitializerUnitTest {
|
public class MapInitializerUnitTest {
|
||||||
|
|
@ -1,14 +1,13 @@
|
|||||||
package com.baeldung.map.util;
|
package com.baeldung.map.mapmax;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Before;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class MapMaxUnitTest {
|
public class MapMaxUnitTest {
|
||||||
|
|
@ -1,16 +1,14 @@
|
|||||||
package com.baeldung.collection;
|
package com.baeldung.map.treemaphashmap;
|
||||||
|
|
||||||
import java.util.ConcurrentModificationException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
|
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class WhenComparingTreeMapVsHashMap {
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
public class TreeMapVsHashMapUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenInsertObjectsTreeMap_thenNaturalOrder() {
|
public void whenInsertObjectsTreeMap_thenNaturalOrder() {
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.weakhashmap;
|
package com.baeldung.map.weakhashmap;
|
||||||
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
@ -3,21 +3,13 @@
|
|||||||
## Java Collections Cookbooks and Examples
|
## Java Collections Cookbooks and Examples
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Guide to WeakHashMap in Java](http://www.baeldung.com/java-weakhashmap)
|
- [Guide to the Guava BiMap](https://www.baeldung.com/guava-bimap)
|
||||||
- [Guide to the Guava BiMap](http://www.baeldung.com/guava-bimap)
|
- [The Java HashMap Under the Hood](https://www.baeldung.com/java-hashmap)
|
||||||
- [The Java HashMap Under the Hood](http://www.baeldung.com/java-hashmap)
|
- [A Guide to LinkedHashMap in Java](https://www.baeldung.com/java-linked-hashmap)
|
||||||
- [A Guide to LinkedHashMap in Java](http://www.baeldung.com/java-linked-hashmap)
|
- [A Guide to TreeMap in Java](https://www.baeldung.com/java-treemap)
|
||||||
- [A Guide to TreeMap in Java](http://www.baeldung.com/java-treemap)
|
- [How to Store Duplicate Keys in a Map in Java?](https://www.baeldung.com/java-map-duplicate-keys)
|
||||||
- [Iterate over a Map in Java](http://www.baeldung.com/java-iterate-map)
|
|
||||||
- [Java TreeMap vs HashMap](http://www.baeldung.com/java-treemap-vs-hashmap)
|
|
||||||
- [How to Store Duplicate Keys in a Map in Java?](http://www.baeldung.com/java-map-duplicate-keys)
|
|
||||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
|
||||||
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
|
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
|
||||||
- [Sort a HashMap in Java](https://www.baeldung.com/java-hashmap-sort)
|
|
||||||
- [Finding the Highest Value in a Java Map](https://www.baeldung.com/java-find-map-max)
|
|
||||||
- [Merging Two Maps with Java 8](https://www.baeldung.com/java-merge-maps)
|
|
||||||
- [How to Check If a Key Exists in a Map](https://www.baeldung.com/java-map-key-exists)
|
- [How to Check If a Key Exists in a Map](https://www.baeldung.com/java-map-key-exists)
|
||||||
- [Comparing Two HashMaps in Java](https://www.baeldung.com/java-compare-hashmaps)
|
- [Comparing Two HashMaps in Java](https://www.baeldung.com/java-compare-hashmaps)
|
||||||
- [Immutable Map Implementations in Java](https://www.baeldung.com/java-immutable-maps)
|
- [Immutable Map Implementations in Java](https://www.baeldung.com/java-immutable-maps)
|
||||||
- [Map to String Conversion in Java](https://www.baeldung.com/java-map-to-string-conversion)
|
|
||||||
- [Guide to Apache Commons MultiValuedMap](https://www.baeldung.com/apache-commons-multi-valued-map)
|
- [Guide to Apache Commons MultiValuedMap](https://www.baeldung.com/apache-commons-multi-valued-map)
|
||||||
|
@ -36,11 +36,6 @@
|
|||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>one.util</groupId>
|
|
||||||
<artifactId>streamex</artifactId>
|
|
||||||
<version>${streamex.version}</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@ -49,6 +44,5 @@
|
|||||||
<avaitility.version>1.7.0</avaitility.version>
|
<avaitility.version>1.7.0</avaitility.version>
|
||||||
<assertj.version>3.6.1</assertj.version>
|
<assertj.version>3.6.1</assertj.version>
|
||||||
<eclipse.collections.version>7.1.0</eclipse.collections.version>
|
<eclipse.collections.version>7.1.0</eclipse.collections.version>
|
||||||
<streamex.version>0.6.5</streamex.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.java.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
@ -1,16 +1,13 @@
|
|||||||
package com.baeldung.java.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
|
|
||||||
|
|
||||||
public class ImmutableMapUnitTest {
|
public class ImmutableMapUnitTest {
|
@ -1,13 +1,11 @@
|
|||||||
package com.baeldung.java.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class KeyCheckUnitTest {
|
public class KeyCheckUnitTest {
|
||||||
|
|
@ -1,13 +1,9 @@
|
|||||||
package com.baeldung.java.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
import com.google.common.collect.ArrayListMultimap;
|
||||||
|
import com.google.common.collect.LinkedHashMultimap;
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
|
import com.google.common.collect.TreeMultimap;
|
||||||
import org.apache.commons.collections4.MultiMap;
|
import org.apache.commons.collections4.MultiMap;
|
||||||
import org.apache.commons.collections4.MultiMapUtils;
|
import org.apache.commons.collections4.MultiMapUtils;
|
||||||
import org.apache.commons.collections4.MultiValuedMap;
|
import org.apache.commons.collections4.MultiValuedMap;
|
||||||
@ -18,10 +14,9 @@ import org.junit.Test;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.google.common.collect.ArrayListMultimap;
|
import java.util.*;
|
||||||
import com.google.common.collect.LinkedHashMultimap;
|
|
||||||
import com.google.common.collect.Multimap;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import com.google.common.collect.TreeMultimap;
|
|
||||||
|
|
||||||
public class MapMultipleValuesUnitTest {
|
public class MapMultipleValuesUnitTest {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(MapMultipleValuesUnitTest.class);
|
private static final Logger LOG = LoggerFactory.getLogger(MapMultipleValuesUnitTest.class);
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
@ -1,9 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.java.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.google.common.collect.HashBiMap;
|
||||||
|
import org.apache.commons.collections4.BidiMap;
|
||||||
|
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -11,11 +14,7 @@ import java.util.HashSet;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.commons.collections4.BidiMap;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.google.common.collect.HashBiMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author swpraman
|
* @author swpraman
|
@ -1,15 +1,4 @@
|
|||||||
package com.baeldung.java.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.apache.commons.collections4.MultiMapUtils;
|
import org.apache.commons.collections4.MultiMapUtils;
|
||||||
import org.apache.commons.collections4.MultiSet;
|
import org.apache.commons.collections4.MultiSet;
|
||||||
@ -18,6 +7,17 @@ import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
|
|||||||
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
|
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
public class MultiValuedMapUnitTest {
|
public class MultiValuedMapUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
@ -1,24 +1,21 @@
|
|||||||
package com.baeldung.java.map.compare;
|
package com.baeldung.map.compare;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import com.google.common.base.Equivalence;
|
||||||
import static org.hamcrest.collection.IsMapContaining.hasEntry;
|
import com.google.common.collect.MapDifference;
|
||||||
import static org.hamcrest.collection.IsMapContaining.hasKey;
|
import com.google.common.collect.MapDifference.ValueDifference;
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.google.common.collect.Maps;
|
||||||
import static org.junit.Assert.assertFalse;
|
import org.junit.Before;
|
||||||
import static org.junit.Assert.assertTrue;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.junit.Before;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import org.junit.Test;
|
import static org.hamcrest.collection.IsMapContaining.hasEntry;
|
||||||
|
import static org.hamcrest.collection.IsMapContaining.hasKey;
|
||||||
import com.google.common.base.Equivalence;
|
import static org.junit.Assert.*;
|
||||||
import com.google.common.collect.MapDifference;
|
|
||||||
import com.google.common.collect.MapDifference.ValueDifference;
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
|
|
||||||
public class HashMapComparisonUnitTest {
|
public class HashMapComparisonUnitTest {
|
||||||
|
|
@ -17,11 +17,50 @@
|
|||||||
<artifactId>flogger</artifactId>
|
<artifactId>flogger</artifactId>
|
||||||
<version>0.4</version>
|
<version>0.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.flogger</groupId>
|
<groupId>com.google.flogger</groupId>
|
||||||
<artifactId>flogger-system-backend</artifactId>
|
<artifactId>flogger-system-backend</artifactId>
|
||||||
<version>0.4</version>
|
<version>0.4</version>
|
||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.flogger</groupId>
|
||||||
|
<artifactId>flogger-slf4j-backend</artifactId>
|
||||||
|
<version>0.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.flogger</groupId>
|
||||||
|
<artifactId>flogger-log4j-backend</artifactId>
|
||||||
|
<version>0.4</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>com.sun.jmx</groupId>
|
||||||
|
<artifactId>jmxri</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>com.sun.jdmk</groupId>
|
||||||
|
<artifactId>jmxtools</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>javax.jms</groupId>
|
||||||
|
<artifactId>jms</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>log4j</groupId>
|
||||||
|
<artifactId>log4j</artifactId>
|
||||||
|
<version>1.2.17</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>log4j</groupId>
|
||||||
|
<artifactId>apache-log4j-extras</artifactId>
|
||||||
|
<version>1.2.17</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
@ -12,6 +12,10 @@ import java.util.stream.IntStream;
|
|||||||
import static com.google.common.flogger.LazyArgs.lazy;
|
import static com.google.common.flogger.LazyArgs.lazy;
|
||||||
|
|
||||||
public class FloggerIntegrationTest {
|
public class FloggerIntegrationTest {
|
||||||
|
static {
|
||||||
|
// System.setProperty("flogger.backend_factory", "com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance");
|
||||||
|
System.setProperty("flogger.backend_factory", "com.google.common.flogger.backend.slf4j.Slf4jBackendFactory#getInstance");
|
||||||
|
}
|
||||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -28,6 +32,16 @@ public class FloggerIntegrationTest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnObject_shouldLogTheObject() {
|
||||||
|
User user = new User();
|
||||||
|
logger.atInfo().log("The user is: %s", user); //correct
|
||||||
|
|
||||||
|
//The following ways of logging are not recommended
|
||||||
|
logger.atInfo().log("The user is: %s", user.toString());
|
||||||
|
logger.atInfo().log("The user is: %s" + user);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenASimpleOperation_shouldLogTheResult() {
|
public void givenASimpleOperation_shouldLogTheResult() {
|
||||||
int result = 45 / 3;
|
int result = 45 / 3;
|
||||||
@ -70,4 +84,13 @@ public class FloggerIntegrationTest {
|
|||||||
int s = 30;
|
int s = 30;
|
||||||
return String.format("%d seconds elapsed so far. %d items pending processing", s, items);
|
return String.format("%d seconds elapsed so far. %d items pending processing", s, items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class User {
|
||||||
|
String name = "Test";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
2
pom.xml
2
pom.xml
@ -407,6 +407,7 @@
|
|||||||
<module>core-java-modules/core-java-collections-array-list</module>
|
<module>core-java-modules/core-java-collections-array-list</module>
|
||||||
<module>core-java-modules/core-java-collections-set</module>
|
<module>core-java-modules/core-java-collections-set</module>
|
||||||
<module>core-java-modules/core-java-concurrency-basic</module>
|
<module>core-java-modules/core-java-concurrency-basic</module>
|
||||||
|
<module>core-java-modules/core-java-concurrency-basic-2</module>
|
||||||
<module>core-java-modules/core-java-concurrency-collections</module>
|
<module>core-java-modules/core-java-concurrency-collections</module>
|
||||||
<module>core-java-modules/core-java-io</module>
|
<module>core-java-modules/core-java-io</module>
|
||||||
<module>core-java-modules/core-java-io-files</module>
|
<module>core-java-modules/core-java-io-files</module>
|
||||||
@ -1146,6 +1147,7 @@
|
|||||||
<module>core-java-modules/core-java-collections-array-list</module>
|
<module>core-java-modules/core-java-collections-array-list</module>
|
||||||
<module>core-java-modules/core-java-collections-set</module>
|
<module>core-java-modules/core-java-collections-set</module>
|
||||||
<module>core-java-modules/core-java-concurrency-basic</module>
|
<module>core-java-modules/core-java-concurrency-basic</module>
|
||||||
|
<module>core-java-modules/core-java-concurrency-basic-2</module>
|
||||||
<module>core-java-modules/core-java-concurrency-collections</module>
|
<module>core-java-modules/core-java-concurrency-collections</module>
|
||||||
<module>core-java-modules/core-java-io</module>
|
<module>core-java-modules/core-java-io</module>
|
||||||
<module>core-java-modules/core-java-io-files</module>
|
<module>core-java-modules/core-java-io-files</module>
|
||||||
|
@ -3,3 +3,4 @@
|
|||||||
This module contains articles about reactive Spring 5
|
This module contains articles about reactive Spring 5
|
||||||
|
|
||||||
- [Spring WebClient vs. RestTemplate](https://www.baeldung.com/spring-webclient-resttemplate)
|
- [Spring WebClient vs. RestTemplate](https://www.baeldung.com/spring-webclient-resttemplate)
|
||||||
|
- More articles: [[<-- prev]](/spring-5-reactive)
|
||||||
|
@ -54,6 +54,18 @@
|
|||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- okhttp -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>4.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>mockwebserver</artifactId>
|
||||||
|
<version>4.0.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- runtime and test scoped -->
|
<!-- runtime and test scoped -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -88,14 +100,25 @@
|
|||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
<!-- WebClient logging with Jetty -->
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-junit-jupiter</artifactId>
|
||||||
|
<version>2.23.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-test</artifactId>
|
||||||
|
<version>3.2.10.RELEASE</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
<artifactId>jetty-reactive-httpclient</artifactId>
|
<artifactId>jetty-reactive-httpclient</artifactId>
|
||||||
<version>${jetty-reactive-httpclient.version}</version>
|
<version>${jetty-reactive-httpclient.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -108,6 +131,29 @@
|
|||||||
<layout>JAR</layout>
|
<layout>JAR</layout>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.22.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.19.1</version>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||||
|
<version>1.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.reactive.enums;
|
||||||
|
|
||||||
|
public enum Role {
|
||||||
|
ENGINEER, SENIOR_ENGINEER, LEAD_ENGINEER
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.baeldung.reactive.model;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baeldung.reactive.enums.Role;
|
||||||
|
|
||||||
|
public class Employee {
|
||||||
|
private Integer employeeId;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private Integer age;
|
||||||
|
private Role role;
|
||||||
|
|
||||||
|
public Employee() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(Integer employeeId, String firstName, String lastName, Integer age, Role role) {
|
||||||
|
this.employeeId = employeeId;
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
this.age = age;
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getEmployeeId() {
|
||||||
|
return employeeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmployeeId(Integer employeeId) {
|
||||||
|
this.employeeId = employeeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(Integer age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Role getRole() {
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRole(Role role) {
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.baeldung.reactive.service;
|
||||||
|
import com.baeldung.reactive.model.Employee;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
public class EmployeeService {
|
||||||
|
|
||||||
|
private WebClient webClient;
|
||||||
|
public static String PATH_PARAM_BY_ID = "/employee/{id}";
|
||||||
|
public static String ADD_EMPLOYEE = "/employee";
|
||||||
|
|
||||||
|
public EmployeeService(WebClient webClient) {
|
||||||
|
this.webClient = webClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeService(String baseUrl) {
|
||||||
|
this.webClient = WebClient.create(baseUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<Employee> getEmployeeById(Integer employeeId) {
|
||||||
|
return webClient
|
||||||
|
.get()
|
||||||
|
.uri(PATH_PARAM_BY_ID, employeeId)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(Employee.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<Employee> addNewEmployee(Employee newEmployee) {
|
||||||
|
|
||||||
|
return webClient
|
||||||
|
.post()
|
||||||
|
.uri(ADD_EMPLOYEE)
|
||||||
|
.syncBody(newEmployee)
|
||||||
|
.retrieve().
|
||||||
|
bodyToMono(Employee.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<Employee> updateEmployee(Integer employeeId, Employee updateEmployee) {
|
||||||
|
|
||||||
|
return webClient
|
||||||
|
.put()
|
||||||
|
.uri(PATH_PARAM_BY_ID,employeeId)
|
||||||
|
.syncBody(updateEmployee)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(Employee.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<String> deleteEmployeeById(Integer employeeId) {
|
||||||
|
return webClient
|
||||||
|
.delete()
|
||||||
|
.uri(PATH_PARAM_BY_ID,employeeId)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,121 @@
|
|||||||
|
package com.baeldung.reactive.service;
|
||||||
|
|
||||||
|
import com.baeldung.reactive.model.Employee;
|
||||||
|
import com.baeldung.reactive.enums.Role;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import okhttp3.mockwebserver.MockResponse;
|
||||||
|
import okhttp3.mockwebserver.MockWebServer;
|
||||||
|
import okhttp3.mockwebserver.RecordedRequest;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class EmployeeServiceIntegrationTest {
|
||||||
|
|
||||||
|
public static MockWebServer mockBackEnd;
|
||||||
|
private EmployeeService employeeService;
|
||||||
|
private ObjectMapper MAPPER = new ObjectMapper();
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void setUp() throws IOException {
|
||||||
|
mockBackEnd = new MockWebServer();
|
||||||
|
mockBackEnd.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void tearDown() throws IOException {
|
||||||
|
mockBackEnd.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void initialize() {
|
||||||
|
|
||||||
|
String baseUrl = String.format("http://localhost:%s", mockBackEnd.getPort());
|
||||||
|
employeeService = new EmployeeService(baseUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getEmployeeById() throws Exception {
|
||||||
|
|
||||||
|
Employee mockEmployee = new Employee(100, "Adam", "Sandler", 32, Role.LEAD_ENGINEER);
|
||||||
|
mockBackEnd.enqueue(new MockResponse().setBody(MAPPER.writeValueAsString(mockEmployee))
|
||||||
|
.addHeader("Content-Type", "application/json"));
|
||||||
|
|
||||||
|
Mono<Employee> employeeMono = employeeService.getEmployeeById(100);
|
||||||
|
|
||||||
|
StepVerifier.create(employeeMono)
|
||||||
|
.expectNextMatches(employee -> employee.getRole().equals(Role.LEAD_ENGINEER))
|
||||||
|
.verifyComplete();
|
||||||
|
|
||||||
|
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
|
||||||
|
assertEquals("GET", recordedRequest.getMethod());
|
||||||
|
assertEquals("/employee/100", recordedRequest.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addNewEmployee() throws Exception {
|
||||||
|
|
||||||
|
Employee newEmployee = new Employee(null, "Adam", "Sandler", 32, Role.LEAD_ENGINEER);
|
||||||
|
Employee webClientResponse = new Employee(100, "Adam", "Sandler", 32, Role.LEAD_ENGINEER);
|
||||||
|
mockBackEnd.enqueue(new MockResponse().setBody(MAPPER.writeValueAsString(webClientResponse))
|
||||||
|
.addHeader("Content-Type", "application/json"));
|
||||||
|
|
||||||
|
Mono<Employee> employeeMono = employeeService.addNewEmployee(newEmployee);
|
||||||
|
|
||||||
|
StepVerifier.create(employeeMono)
|
||||||
|
.expectNextMatches(employee -> employee.getEmployeeId().equals(100))
|
||||||
|
.verifyComplete();
|
||||||
|
|
||||||
|
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
|
||||||
|
assertEquals("POST", recordedRequest.getMethod());
|
||||||
|
assertEquals("/employee", recordedRequest.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void updateEmployee() throws Exception {
|
||||||
|
|
||||||
|
Integer newAge = 33;
|
||||||
|
String newLastName = "Sandler New";
|
||||||
|
Employee updateEmployee = new Employee(100, "Adam", newLastName, newAge, Role.LEAD_ENGINEER);
|
||||||
|
mockBackEnd.enqueue(new MockResponse().setBody(MAPPER.writeValueAsString(updateEmployee))
|
||||||
|
.addHeader("Content-Type", "application/json"));
|
||||||
|
|
||||||
|
Mono<Employee> updatedEmploye = employeeService.updateEmployee(100, updateEmployee);
|
||||||
|
|
||||||
|
StepVerifier.create(updatedEmploye)
|
||||||
|
.expectNextMatches(employee -> employee.getLastName().equals(newLastName) && employee.getAge() == newAge)
|
||||||
|
.verifyComplete();
|
||||||
|
|
||||||
|
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
|
||||||
|
assertEquals("PUT", recordedRequest.getMethod());
|
||||||
|
assertEquals("/employee/100", recordedRequest.getPath());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deleteEmployee() throws Exception {
|
||||||
|
|
||||||
|
String responseMessage = "Employee Deleted SuccessFully";
|
||||||
|
Integer employeeId = 100;
|
||||||
|
mockBackEnd.enqueue(new MockResponse().setBody(MAPPER.writeValueAsString(responseMessage))
|
||||||
|
.addHeader("Content-Type", "application/json"));
|
||||||
|
|
||||||
|
Mono<String> deletedEmployee = employeeService.deleteEmployeeById(employeeId);
|
||||||
|
|
||||||
|
StepVerifier.create(deletedEmployee)
|
||||||
|
.expectNext("\"Employee Deleted SuccessFully\"")
|
||||||
|
.verifyComplete();
|
||||||
|
|
||||||
|
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
|
||||||
|
assertEquals("DELETE", recordedRequest.getMethod());
|
||||||
|
assertEquals("/employee/100", recordedRequest.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,114 @@
|
|||||||
|
package com.baeldung.reactive.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baeldung.reactive.model.Employee;
|
||||||
|
import com.baeldung.reactive.enums.Role;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.exceptions.base.MockitoException;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class EmployeeServiceUnitTest {
|
||||||
|
|
||||||
|
EmployeeService employeeService;
|
||||||
|
@Mock
|
||||||
|
private WebClient webClientMock;
|
||||||
|
@Mock
|
||||||
|
private WebClient.RequestHeadersSpec requestHeadersMock;
|
||||||
|
@Mock
|
||||||
|
private WebClient.RequestHeadersUriSpec requestHeadersUriMock;
|
||||||
|
@Mock
|
||||||
|
private WebClient.RequestBodySpec requestBodyMock;
|
||||||
|
@Mock
|
||||||
|
private WebClient.RequestBodyUriSpec requestBodyUriMock;
|
||||||
|
@Mock
|
||||||
|
private WebClient.ResponseSpec responseMock;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
employeeService = new EmployeeService(webClientMock);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenEmployeeId_whenGetEmployeeById_thenReturnEmployee() {
|
||||||
|
|
||||||
|
Integer employeeId = 100;
|
||||||
|
Employee mockEmployee = new Employee(100, "Adam", "Sandler", 32, Role.LEAD_ENGINEER);
|
||||||
|
when(webClientMock.get()).thenReturn(requestHeadersUriMock);
|
||||||
|
when(requestHeadersUriMock.uri("/employee/{id}", employeeId)).thenReturn(requestHeadersMock);
|
||||||
|
when(requestHeadersMock.retrieve()).thenReturn(responseMock);
|
||||||
|
when(responseMock.bodyToMono(Employee.class)).thenReturn(Mono.just(mockEmployee));
|
||||||
|
|
||||||
|
Mono<Employee> employeeMono = employeeService.getEmployeeById(employeeId);
|
||||||
|
|
||||||
|
StepVerifier.create(employeeMono)
|
||||||
|
.expectNextMatches(employee -> employee.getRole().equals(Role.LEAD_ENGINEER))
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenEmployee_whenAddEmployee_thenAddNewEmployee() {
|
||||||
|
|
||||||
|
Employee newEmployee = new Employee(null, "Adam", "Sandler", 32, Role.LEAD_ENGINEER);
|
||||||
|
Employee webClientResponse = new Employee(100, "Adam", "Sandler", 32, Role.LEAD_ENGINEER);
|
||||||
|
when(webClientMock.post()).thenReturn(requestBodyUriMock);
|
||||||
|
when(requestBodyUriMock.uri(EmployeeService.ADD_EMPLOYEE)).thenReturn(requestBodyMock);
|
||||||
|
when(requestBodyMock.syncBody(newEmployee)).thenReturn(requestHeadersMock);
|
||||||
|
when(requestHeadersMock.retrieve()).thenReturn(responseMock);
|
||||||
|
when(responseMock.bodyToMono(Employee.class)).thenReturn(Mono.just(webClientResponse));
|
||||||
|
|
||||||
|
Mono<Employee> employeeMono = employeeService.addNewEmployee(newEmployee);
|
||||||
|
|
||||||
|
StepVerifier.create(employeeMono)
|
||||||
|
.expectNextMatches(employee -> employee.getEmployeeId().equals(100))
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenEmployee_whenupdateEmployee_thenUpdatedEmployee() {
|
||||||
|
|
||||||
|
Integer newAge = 33;
|
||||||
|
String newLastName = "Sandler New";
|
||||||
|
Employee updateEmployee = new Employee(100, "Adam", newLastName, newAge, Role.LEAD_ENGINEER);
|
||||||
|
when(webClientMock.put()).thenReturn(requestBodyUriMock);
|
||||||
|
when(requestBodyUriMock.uri(EmployeeService.PATH_PARAM_BY_ID, 100)).thenReturn(requestBodyMock);
|
||||||
|
when(requestBodyMock.syncBody(updateEmployee)).thenReturn(requestHeadersMock);
|
||||||
|
when(requestHeadersMock.retrieve()).thenReturn(responseMock);
|
||||||
|
when(responseMock.bodyToMono(Employee.class)).thenReturn(Mono.just(updateEmployee));
|
||||||
|
|
||||||
|
Mono<Employee> updatedEmployee = employeeService.updateEmployee(100, updateEmployee);
|
||||||
|
|
||||||
|
StepVerifier.create(updatedEmployee)
|
||||||
|
.expectNextMatches(employee -> employee.getLastName().equals(newLastName) && employee.getAge() == newAge)
|
||||||
|
.verifyComplete();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenEmployee_whenDeleteEmployeeById_thenDeleteSuccessful() {
|
||||||
|
|
||||||
|
String responseMessage = "Employee Deleted SuccessFully";
|
||||||
|
when(webClientMock.delete()).thenReturn(requestHeadersUriMock);
|
||||||
|
when(requestHeadersUriMock.uri(EmployeeService.PATH_PARAM_BY_ID, 100)).thenReturn(requestHeadersMock);
|
||||||
|
when(requestHeadersMock.retrieve()).thenReturn(responseMock);
|
||||||
|
when(responseMock.bodyToMono(String.class)).thenReturn(Mono.just(responseMessage));
|
||||||
|
|
||||||
|
Mono<String> deletedEmployee = employeeService.deleteEmployeeById(100);
|
||||||
|
|
||||||
|
StepVerifier.create(deletedEmployee)
|
||||||
|
.expectNext(responseMessage)
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
}
|
@ -22,4 +22,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||||||
- [Testing Reactive Streams Using StepVerifier and TestPublisher](https://www.baeldung.com/reactive-streams-step-verifier-test-publisher)
|
- [Testing Reactive Streams Using StepVerifier and TestPublisher](https://www.baeldung.com/reactive-streams-step-verifier-test-publisher)
|
||||||
- [Debugging Reactive Streams in Spring 5](https://www.baeldung.com/spring-debugging-reactive-streams)
|
- [Debugging Reactive Streams in Spring 5](https://www.baeldung.com/spring-debugging-reactive-streams)
|
||||||
- [Static Content in Spring WebFlux](https://www.baeldung.com/spring-webflux-static-content)
|
- [Static Content in Spring WebFlux](https://www.baeldung.com/spring-webflux-static-content)
|
||||||
- [more...](/spring-5-reactive-2)
|
- More articles: [[next -->]](/spring-5-reactive-2)
|
@ -15,7 +15,7 @@ import static org.junit.Assert.assertEquals;
|
|||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||||
@ -36,20 +36,20 @@ public class SpringBootAdminClientApplicationIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenEnvironmentAvailable_ThenAdminServerPropertiesExist() {
|
public void whenEnvironmentAvailable_ThenAdminServerPropertiesExist() {
|
||||||
assertEquals(environment.getProperty("spring.boot.admin.url"), "http://localhost:8080");
|
assertEquals(environment.getProperty("spring.boot.admin.client.url"), "http://localhost:8080");
|
||||||
assertEquals(environment.getProperty("spring.boot.admin.username"), "admin");
|
assertEquals(environment.getProperty("spring.boot.admin.client.username"), "admin");
|
||||||
assertEquals(environment.getProperty("spring.boot.admin.password"), "admin");
|
assertEquals(environment.getProperty("spring.boot.admin.client.password"), "admin");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenHttpBasicAttempted_ThenSuccess() throws Exception {
|
public void whenHttpBasicAttempted_ThenSuccess() throws Exception {
|
||||||
mockMvc.perform(get("/env").with(httpBasic("client", "client")));
|
mockMvc.perform(get("/actuator/env").with(httpBasic("client", "client")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenInvalidHttpBasicAttempted_ThenUnauthorized() throws Exception {
|
public void whenInvalidHttpBasicAttempted_ThenUnauthorized() throws Exception {
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(get("/env").with(httpBasic("client", "invalid")))
|
.perform(get("/actuator/env").with(httpBasic("client", "invalid")))
|
||||||
.andExpect(status().isUnauthorized());
|
.andExpect(unauthenticated());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,10 +51,12 @@
|
|||||||
<version>${spring-boot-admin-starter-client.version}</version>
|
<version>${spring-boot-admin-starter-client.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--mail notifications -->
|
<!--mail notifications -->
|
||||||
<!--<dependency> -->
|
|
||||||
<!--<groupId>org.springframework.boot</groupId> -->
|
<dependency>
|
||||||
<!--<artifactId>spring-boot-starter-mail</artifactId> -->
|
<groupId>org.springframework.boot</groupId>
|
||||||
<!--</dependency> -->
|
<artifactId>spring-boot-starter-mail</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
@ -1,32 +1,40 @@
|
|||||||
package com.baeldung.springbootadminserver.configs;
|
package com.baeldung.springbootadminserver.configs;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
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.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
|
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
|
||||||
|
import de.codecentric.boot.admin.server.notify.CompositeNotifier;
|
||||||
import de.codecentric.boot.admin.server.notify.LoggingNotifier;
|
import de.codecentric.boot.admin.server.notify.LoggingNotifier;
|
||||||
|
import de.codecentric.boot.admin.server.notify.Notifier;
|
||||||
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
|
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
|
||||||
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
|
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class NotifierConfiguration {
|
public class NotifierConfiguration {
|
||||||
@Autowired
|
private final InstanceRepository repository;
|
||||||
private InstanceRepository repository;
|
private final ObjectProvider<List<Notifier>> otherNotifiers;
|
||||||
|
|
||||||
// @Autowired private Notifier notifier;
|
public NotifierConfiguration(InstanceRepository repository, ObjectProvider<List<Notifier>> otherNotifiers) {
|
||||||
|
this.repository = repository;
|
||||||
@Bean
|
this.otherNotifiers = otherNotifiers;
|
||||||
public LoggingNotifier notifier() {
|
|
||||||
return new LoggingNotifier(repository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public FilteringNotifier filteringNotifier() {
|
public FilteringNotifier filteringNotifier() {
|
||||||
return new FilteringNotifier(notifier(), repository);
|
CompositeNotifier delegate = new CompositeNotifier(this.otherNotifiers.getIfAvailable(Collections::emptyList));
|
||||||
|
return new FilteringNotifier(delegate, this.repository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LoggingNotifier notifier() {
|
||||||
|
return new LoggingNotifier(repository);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Primary
|
@Primary
|
||||||
|
@ -5,6 +5,7 @@ import java.util.UUID;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
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.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
||||||
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
|
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
|
||||||
@ -13,6 +14,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|||||||
import de.codecentric.boot.admin.server.config.AdminServerProperties;
|
import de.codecentric.boot.admin.server.config.AdminServerProperties;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@EnableWebSecurity
|
||||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
private final AdminServerProperties adminServer;
|
private final AdminServerProperties adminServer;
|
||||||
|
|
||||||
|
@ -16,14 +16,14 @@ management.endpoints.web.exposure.include=*
|
|||||||
management.endpoint.health.show-details=always
|
management.endpoint.health.show-details=always
|
||||||
|
|
||||||
#mail notifications
|
#mail notifications
|
||||||
#spring.mail.host=smtp.gmail.com
|
spring.mail.host=
|
||||||
#spring.mail.username=test@gmail.com
|
spring.mail.username=
|
||||||
#spring.mail.password=password
|
spring.mail.password=
|
||||||
#spring.mail.port=587
|
spring.mail.port=
|
||||||
#spring.mail.properties.mail.smtp.auth=true
|
spring.mail.properties.mail.smtp.auth=
|
||||||
#spring.mail.properties.mail.smtp.starttls.enable=true
|
spring.mail.properties.mail.smtp.starttls.enable=
|
||||||
|
|
||||||
#spring.boot.admin.notify.mail.to=test@gmail.com
|
spring.boot.admin.notify.mail.to=
|
||||||
|
|
||||||
#hipchat notifications
|
#hipchat notifications
|
||||||
#spring.boot.admin.notify.hipchat.auth-token=<generated_token>
|
#spring.boot.admin.notify.hipchat.auth-token=<generated_token>
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
package com.baeldung.springbootadminserver;
|
package com.baeldung.springbootadminserver;
|
||||||
|
|
||||||
import com.baeldung.springbootadminserver.configs.NotifierConfiguration;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||||
import de.codecentric.boot.admin.server.notify.Notifier;
|
|
||||||
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
|
|
||||||
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -13,11 +10,14 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import com.baeldung.springbootadminserver.configs.NotifierConfiguration;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
|
||||||
|
import de.codecentric.boot.admin.server.notify.Notifier;
|
||||||
|
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
|
||||||
|
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = { NotifierConfiguration.class }, webEnvironment = NONE)
|
@SpringBootTest(classes = { NotifierConfiguration.class, SpringBootAdminServerApplication.class }, webEnvironment = NONE)
|
||||||
public class NotifierConfigurationIntegrationTest {
|
public class NotifierConfigurationIntegrationTest {
|
||||||
|
|
||||||
@Autowired private ApplicationContext applicationContext;
|
@Autowired private ApplicationContext applicationContext;
|
||||||
|
@ -14,6 +14,7 @@ import static org.springframework.security.test.web.servlet.request.SecurityMock
|
|||||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
@ -51,21 +52,21 @@ public class WebSecurityConfigIntegrationTest {
|
|||||||
.password("admin"));
|
.password("admin"));
|
||||||
|
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(get("/api/applications/"))
|
.perform(get("/applications/"))
|
||||||
.andExpect(status().is2xxSuccessful());
|
.andExpect(status().is2xxSuccessful());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenHttpBasicAttempted_ThenSuccess() throws Exception {
|
public void whenHttpBasicAttempted_ThenSuccess() throws Exception {
|
||||||
mockMvc.perform(get("/env").with(httpBasic("admin", "admin")));
|
mockMvc.perform(get("/actuator/env").with(httpBasic("admin", "admin")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenInvalidHttpBasicAttempted_ThenUnauthorized() throws Exception {
|
public void whenInvalidHttpBasicAttempted_ThenUnauthorized() throws Exception {
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(get("/env").with(httpBasic("admin", "invalid")))
|
.perform(get("/actuator/env").with(httpBasic("admin", "invalid")))
|
||||||
.andExpect(status().isUnauthorized());
|
.andExpect(unauthenticated());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
package org.baeldung;
|
|
||||||
|
|
||||||
import org.baeldung.spring.cloud.DataFlowShellApplication;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = DataFlowShellApplication.class)
|
|
||||||
public class SpringContextIntegrationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -6,6 +6,15 @@ import org.junit.runner.RunWith;
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This live test requires:
|
||||||
|
* complete data-flow server and shell setup running
|
||||||
|
*
|
||||||
|
* <br>
|
||||||
|
* For more info:
|
||||||
|
* https://www.baeldung.com/spring-cloud-data-flow-stream-processing
|
||||||
|
*
|
||||||
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = DataFlowShellApplication.class)
|
@SpringBootTest(classes = DataFlowShellApplication.class)
|
||||||
public class SpringContextLiveTest {
|
public class SpringContextLiveTest {
|
||||||
|
@ -5,6 +5,15 @@ import org.junit.runner.RunWith;
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This live test requires:
|
||||||
|
* Eureka server and Gateway application up and running
|
||||||
|
*
|
||||||
|
* <br>
|
||||||
|
* For more info:
|
||||||
|
* https://www.baeldung.com/spring-cloud-netflix-eureka
|
||||||
|
* https://www.baeldung.com/spring-cloud-gateway-pattern
|
||||||
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class SpringContextLiveTest {
|
public class SpringContextLiveTest {
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
package org.baeldung.spring.cloud.vaultsample;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = VaultSampleApplication.class)
|
|
||||||
public class SpringContextIntegrationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
|
||||||
}
|
|
||||||
}
|
|
@ -5,6 +5,15 @@ import org.junit.runner.RunWith;
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This live test requires:
|
||||||
|
* vault server up and running on the environment
|
||||||
|
*
|
||||||
|
* <br>
|
||||||
|
* For more info on setting up the vault server:
|
||||||
|
* https://www.baeldung.com/vault
|
||||||
|
*
|
||||||
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = VaultSampleApplication.class)
|
@SpringBootTest(classes = VaultSampleApplication.class)
|
||||||
public class SpringContextLiveTest {
|
public class SpringContextLiveTest {
|
||||||
|
@ -4,7 +4,7 @@ import org.springframework.context.annotation.ComponentScan;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan("org.baeldung.sample")
|
@ComponentScan("com.baeldung.sample")
|
||||||
public class AppConfig {
|
public class AppConfig {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
#To use a randomly allocated free port during tests to avoid port conflict across tests
|
||||||
|
spring.data.mongodb.port=0
|
@ -6,7 +6,7 @@ import org.springframework.test.context.ContextConfiguration;
|
|||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:EmbeddedActiveMQ.xml"})
|
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
|
||||||
public class SpringContextIntegrationTest {
|
public class SpringContextIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -6,7 +6,7 @@ import org.springframework.test.context.ContextConfiguration;
|
|||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:EmbeddedActiveMQ.xml"})
|
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
|
||||||
public class SpringContextTest {
|
public class SpringContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
2
spring-jooq/src/test/resources/application.properties
Normal file
2
spring-jooq/src/test/resources/application.properties
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
spring.datasource.url=jdbc:h2:tcp:~/jooq;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.interpolation;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import javax.validation.MessageInterpolator;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class MyMessageInterpolator implements MessageInterpolator {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(MyMessageInterpolator.class);
|
||||||
|
|
||||||
|
private final MessageInterpolator defaultInterpolator;
|
||||||
|
|
||||||
|
public MyMessageInterpolator(MessageInterpolator interpolator) {
|
||||||
|
this.defaultInterpolator = interpolator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String interpolate(String messageTemplate, Context context) {
|
||||||
|
messageTemplate = messageTemplate.toUpperCase();
|
||||||
|
return defaultInterpolator.interpolate(messageTemplate, context, Locale.getDefault());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String interpolate(String messageTemplate, Context context, Locale locale) {
|
||||||
|
messageTemplate = messageTemplate.toUpperCase();
|
||||||
|
return defaultInterpolator.interpolate(messageTemplate, context, locale);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.interpolation;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class NotNullRequest {
|
||||||
|
|
||||||
|
@NotNull(message = "stringValue has to be present")
|
||||||
|
private String stringValue;
|
||||||
|
|
||||||
|
public String getStringValue() {
|
||||||
|
return stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStringValue(String stringValue) {
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.interpolation;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class ValidationController {
|
||||||
|
|
||||||
|
@PostMapping("/test-not-null")
|
||||||
|
public void testNotNull(@Valid @RequestBody NotNullRequest request) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.interpolation;
|
||||||
|
|
||||||
|
import java.util.Formatter;
|
||||||
|
import javax.validation.constraints.Size;
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
|
import javax.validation.constraints.DecimalMin;
|
||||||
|
|
||||||
|
public class ValidationExamples {
|
||||||
|
|
||||||
|
private static final Formatter formatter = new Formatter();
|
||||||
|
|
||||||
|
@Size(
|
||||||
|
min = 5,
|
||||||
|
max = 14,
|
||||||
|
message = "The author email '${validatedValue}' must be between {min} and {max} characters long"
|
||||||
|
)
|
||||||
|
private String authorEmail;
|
||||||
|
|
||||||
|
@Min(
|
||||||
|
value = 1,
|
||||||
|
message = "There must be at least {value} test{value > 1 ? 's' : ''} in the test case"
|
||||||
|
)
|
||||||
|
private int testCount;
|
||||||
|
|
||||||
|
@DecimalMin(
|
||||||
|
value = "50",
|
||||||
|
message = "The code coverage ${formatter.format('%1$.2f', validatedValue)} must be higher than {value}%"
|
||||||
|
)
|
||||||
|
private double codeCoverage;
|
||||||
|
|
||||||
|
}
|
@ -1,2 +1,6 @@
|
|||||||
|
## Spring Reactive Kotlin
|
||||||
|
|
||||||
|
This module contains articles about reactive Kotlin
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Spring Webflux with Kotlin](http://www.baeldung.com/spring-webflux-kotlin)
|
- [Spring Webflux with Kotlin](http://www.baeldung.com/spring-webflux-kotlin)
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user