Merge branch 'master' into BAEL-1344

This commit is contained in:
abialas 2017-11-16 22:59:05 +01:00
commit 6cd67b0db6
39 changed files with 830 additions and 144 deletions

View File

@ -114,4 +114,5 @@
- [StringBuilder and StringBuffer in Java](http://www.baeldung.com/java-string-builder-string-buffer)
- [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int)
- [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns)
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)

View File

@ -1,4 +1,3 @@
### Relevant Articles:
- [Introduction to Drools](http://www.baeldung.com/drools)
- [Drools Using Rules from Excel Files](http://www.baeldung.com/drools-excel)
- [Drools Using Rules from Excel Files](http://www.baeldung.com/drools-excel)

View File

@ -1,68 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>drools</artifactId>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>drools</artifactId>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<http-component-version>4.4.6</http-component-version>
<drools-version>7.1.0.Beta2</drools-version>
<drools-version>7.4.1.Final</drools-version>
<apache-poi-version>3.13</apache-poi-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${http-component-version}</version>
</dependency>
<!-- ... -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-ci</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>${drools-version}</version>
</dependency>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${http-component-version}</version>
</dependency>
<!-- ... -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-ci</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
</dependencies>
</dependencies>
</project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

3
guava-modules/README.md Normal file
View File

@ -0,0 +1,3 @@
## Guava Modules

View File

@ -0,0 +1,3 @@
## Logging Modules

37
lucene/pom.xml Normal file
View File

@ -0,0 +1,37 @@
<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>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>lucene</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lucene</name>
<description>An Apache Lucene demo application</description>
<dependencies>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>7.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,78 @@
package com.baeldung.lucene;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
public class InMemoryLuceneIndex {
private Directory memoryIndex;
private StandardAnalyzer analyzer;
public InMemoryLuceneIndex(Directory memoryIndex, StandardAnalyzer analyzer) {
super();
this.memoryIndex = memoryIndex;
this.analyzer = analyzer;
}
/**
*
* @param title
* @param body
*/
public void indexDocument(String title, String body) {
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
try {
IndexWriter writter = new IndexWriter(memoryIndex, indexWriterConfig);
Document document = new Document();
document.add(new TextField("title", title, Field.Store.YES));
document.add(new TextField("body", body, Field.Store.YES));
writter.addDocument(document);
writter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public List<Document> searchIndex(String inField, String queryString) {
try {
Query query = new QueryParser(inField, analyzer).parse(queryString);
IndexReader indexReader = DirectoryReader.open(memoryIndex);
IndexSearcher searcher = new IndexSearcher(indexReader);
TopDocs topDocs = searcher.search(query, 10);
List<Document> documents = new ArrayList<>();
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
documents.add(searcher.doc(scoreDoc.doc));
}
return documents;
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.lucene;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.store.RAMDirectory;
import org.junit.Assert;
import org.junit.Test;
public class LuceneInMemorySearchTest {
@Test
public void givenSearchQueryWhenFetchedDocumentThenCorrect() {
InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer());
inMemoryLuceneIndex.indexDocument("Hello world", "Some hello world ");
List<Document> documents = inMemoryLuceneIndex.searchIndex("body", "world");
Assert.assertEquals("Hello world", documents.get(0).get("title"));
}
}

View File

@ -1,11 +1,11 @@
package com.baeldung.templatemethodpattern.application;
import com.baeldung.templatemethodpattern.model.Computer;
import com.baeldung.templatemethodpattern.model.StandardComputer;
import com.baeldung.templatemethodpattern.model.HighEndComputer;
import com.baeldung.templatemethodpattern.model.ComputerBuilder;
import com.baeldung.templatemethodpattern.model.HighEndComputerBuilder;
import com.baeldung.templatemethodpattern.model.StandardComputerBuilder;
import com.baeldung.templatemethodpattern.model.HighEndComputer;
import com.baeldung.templatemethodpattern.model.StandardComputer;
public class Application {

View File

@ -5,33 +5,15 @@ import java.util.Map;
import java.util.ArrayList;
import java.util.List;
public abstract class ComputerBuilder {
public class Computer {
protected Map<String, String> computerParts = new HashMap<>();
protected List<String> moterboardSetupStatus = new ArrayList<>();
private Map<String, String> computerParts = new HashMap<>();
public final Computer buildComputer() {
addMotherboard();
setupMotherboard();
addProcessor();
return getComputer();
public Computer(Map<String, String> computerParts) {
this.computerParts = computerParts;
}
public abstract void addMotherboard();
public abstract void setupMotherboard();
public abstract void addProcessor();
public List<String> getMotherboardSetupStatus() {
return moterboardSetupStatus;
}
public Map<String, String> getComputerParts() {
return computerParts;
}
private Computer getComputer() {
return new Computer(computerParts);
}
}

View File

@ -8,7 +8,7 @@ import java.util.Map;
public abstract class ComputerBuilder {
protected Map<String, String> computerParts = new HashMap<>();
protected List<String> moterboardSetupStatus = new ArrayList<>();
protected List<String> motherboardSetupStatus = new ArrayList<>();
public final Computer buildComputer() {
addMotherboard();
@ -20,13 +20,13 @@ public abstract class ComputerBuilder {
public abstract void addMotherboard();
public abstract void setupMotherboard();
public abstract void addProcessor();
public List<String> getMotherboardSetupStatus() {
return moterboardSetupStatus;
return motherboardSetupStatus;
}
public Map<String, String> getComputerParts() {
return computerParts;
}

View File

@ -1,26 +1,10 @@
package com.baeldung.templatemethodpattern.model;
public class HighEndComputer extends Computer {
import java.util.Map;
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "High-end Motherboard");
}
public class HighEndComputer extends Computer {
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the high-end motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "High-end Processor");
}
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "High End Motherboard");
}
public HighEndComputer(Map<String, String> computerParts) {
super(computerParts);
}
}

View File

@ -9,13 +9,13 @@ public class HighEndComputerBuilder extends ComputerBuilder {
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the high-end motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
motherboardSetupStatus.add("Screwing the high-end motherboard to the case.");
motherboardSetupStatus.add("Pluging in the power supply connectors.");
motherboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "High-end Processor");
computerParts.put("Processor", "High-end Processor");
}
}

View File

@ -1,25 +1,10 @@
package com.baeldung.templatemethodpattern.model;
import java.util.Map;
public class StandardComputer extends Computer {
public void addMotherboard() {
computerParts.put("Motherboard", "Standard Motherboard");
}
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "Standard Processor");
}
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "Standard Motherboard");
}
public StandardComputer(Map<String, String> computerParts) {
super(computerParts);
}
}

View File

@ -9,9 +9,9 @@ public class StandardComputerBuilder extends ComputerBuilder {
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
motherboardSetupStatus.add("Screwing the standard motherboard to the case.");
motherboardSetupStatus.add("Pluging in the power supply connectors.");
motherboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override

View File

@ -29,12 +29,12 @@ public class TemplateMethodPatternTest {
@Test
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
standardComputer.addMotherboard();
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard"));
standardComputerBuilder.addMotherboard();
assertEquals("Standard Motherboard", standardComputerBuilder.getComputerParts().get("Motherboard"));
}
@Test
public void givenStandardMotheroboard_whenSetup_thenTwoEqualAssertions() {
public void givenStandardMotherboard_whenSetup_thenTwoEqualAssertions() {
standardComputerBuilder.setupMotherboard();
assertEquals("Screwing the standard motherboard to the case.", standardComputerBuilder.getMotherboardSetupStatus().get(0));
assertEquals("Pluging in the power supply connectors.", standardComputerBuilder.getMotherboardSetupStatus().get(1));

View File

@ -0,0 +1,3 @@
## Persistence Modules

View File

@ -261,6 +261,7 @@
<module>saas</module>
<module>deeplearning4j</module>
<module>spring-boot-admin</module>
<module>lucene</module>
</modules>
<dependencies>

View File

@ -0,0 +1,18 @@
package com.baeldung.jupiter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.test.context.junit.jupiter.EnabledIf;
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@EnabledIf(
expression = "#{systemProperties['java.version'].startsWith('1.8')}",
reason = "Enabled on Java 8"
)
public @interface EnabledOnJava8 {
}

View File

@ -0,0 +1,50 @@
package com.baeldung.jupiter;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.DisabledIf;
import org.springframework.test.context.junit.jupiter.EnabledIf;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig(Spring5EnabledAnnotationTest.Config.class)
@TestPropertySource(properties = { "tests.enabled=true" })
public class Spring5EnabledAnnotationTest {
@Configuration
static class Config {
}
@EnabledIf("true")
@Test
void givenEnabledIfLiteral_WhenTrue_ThenTestExecuted() {
assertTrue(true);
}
@EnabledIf(expression = "${tests.enabled}", loadContext = true)
@Test
void givenEnabledIfExpression_WhenTrue_ThenTestExecuted() {
assertTrue(true);
}
@EnabledIf("#{systemProperties['java.version'].startsWith('1.8')}")
@Test
void givenEnabledIfSpel_WhenTrue_ThenTestExecuted() {
assertTrue(true);
}
@EnabledOnJava8
@Test
void givenEnabledOnJava8_WhenTrue_ThenTestExecuted() {
assertTrue(true);
}
@DisabledIf("#{systemProperties['java.version'].startsWith('1.7')}")
@Test
void givenDisabledIf_WhenTrue_ThenTestNotExecuted() {
assertTrue(true);
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
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.spring.cloud</groupId>
@ -15,8 +15,9 @@
<module>spring-cloud-ribbon-client</module>
<module>spring-cloud-rest</module>
<module>spring-cloud-zookeeper</module>
<module>spring-cloud-gateway</module>
<module>spring-cloud-connectors-heroku</module>
<module>spring-cloud-gateway</module>
<module>spring-cloud-stream</module>
<module>spring-cloud-connectors-heroku</module>
</modules>
<packaging>pom</packaging>
@ -38,6 +39,7 @@
<spring-cloud-starter-eureka.version>1.2.3.RELEASE</spring-cloud-starter-eureka.version>
<spring-cloud-starter-feign.version>1.2.3.RELEASE</spring-cloud-starter-feign.version>
<spring-cloud-starter-hystrix.version>1.2.3.RELEASE</spring-cloud-starter-hystrix.version>
<spring-cloud-stream.version>1.3.0.RELEASE</spring-cloud-stream.version>
<spring-boot-starter-web.version>1.4.2.RELEASE</spring-boot-starter-web.version>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<spring-boot-maven-plugin.version>1.4.2.RELEASE</spring-boot-maven-plugin.version>

View File

@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>org.baeldung</groupId>
<artifactId>spring-cloud-stream</artifactId>
<packaging>pom</packaging>
<name>spring-cloud-stream</name>
<parent>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modules>
<module>spring-cloud-stream-rabbit</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
<version>${spring-cloud-stream.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>${spring-cloud-stream.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<version>${spring-cloud-stream.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-stream-rabbit</artifactId>
<packaging>jar</packaging>
<name>spring-cloud-stream-rabbit</name>
<description>Simple Spring Cloud Stream</description>
<parent>
<groupId>org.baeldung</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,38 @@
package com.baeldung.spring.cloud.stream.rabbit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import com.baeldung.spring.cloud.stream.rabbit.processor.MyProcessor;
@SpringBootApplication
@EnableBinding(MyProcessor.class)
public class MultipleOutputsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MultipleOutputsServiceApplication.class, args);
}
@Autowired
private MyProcessor processor;
@StreamListener(MyProcessor.INPUT)
public void routeValues(Integer val) {
if (val < 10) {
processor.anOutput()
.send(message(val));
} else {
processor.anotherOutput()
.send(message(val));
}
}
private static final <T> Message<T> message(T val) {
return MessageBuilder.withPayload(val)
.build();
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.spring.cloud.stream.rabbit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import com.baeldung.spring.cloud.stream.rabbit.processor.MyProcessor;
@SpringBootApplication
@EnableBinding(MyProcessor.class)
public class MultipleOutputsWithConditionsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MultipleOutputsWithConditionsServiceApplication.class, args);
}
@Autowired
private MyProcessor processor;
@StreamListener(target = MyProcessor.INPUT, condition = "payload < 10")
public void routeValuesToAnOutput(Integer val) {
processor.anOutput()
.send(message(val));
}
@StreamListener(target = MyProcessor.INPUT, condition = "payload >= 10")
public void routeValuesToAnotherOutput(Integer val) {
processor.anotherOutput()
.send(message(val));
}
private static final <T> Message<T> message(T val) {
return MessageBuilder.withPayload(val)
.build();
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.spring.cloud.stream.rabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.annotation.SendTo;
import com.baeldung.spring.cloud.stream.rabbit.messages.TextPlainMessageConverter;
import com.baeldung.spring.cloud.stream.rabbit.model.LogMessage;
@SpringBootApplication
@EnableBinding(Processor.class)
public class MyLoggerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyLoggerServiceApplication.class, args);
}
@Bean
public MessageConverter providesTextPlainMessageConverter() {
return new TextPlainMessageConverter();
}
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public LogMessage enrichLogMessage(LogMessage log) {
return new LogMessage(String.format("[1]: %s", log.getMessage()));
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.spring.cloud.stream.rabbit.messages;
import org.springframework.messaging.Message;
import org.springframework.messaging.converter.AbstractMessageConverter;
import org.springframework.util.MimeType;
import com.baeldung.spring.cloud.stream.rabbit.model.LogMessage;
public class TextPlainMessageConverter extends AbstractMessageConverter {
public TextPlainMessageConverter() {
super(new MimeType("text", "plain"));
}
@Override
protected boolean supports(Class<?> clazz) {
return (LogMessage.class == clazz);
}
@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
Object payload = message.getPayload();
String text = payload instanceof String ? (String) payload : new String((byte[]) payload);
return new LogMessage(text);
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.spring.cloud.stream.rabbit.model;
import java.io.Serializable;
public class LogMessage implements Serializable {
private static final long serialVersionUID = -5857383701708275796L;
private String message;
public LogMessage() {
}
public LogMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return message;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.spring.cloud.stream.rabbit.processor;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
public interface MyProcessor {
String INPUT = "myInput";
@Input
SubscribableChannel myInput();
@Output("myOutput")
MessageChannel anOutput();
@Output
MessageChannel anotherOutput();
}

View File

@ -0,0 +1,28 @@
spring:
cloud:
stream:
bindings:
input:
destination: queue.log.messages
binder: local_rabbit
group: logMessageConsumers
output:
destination: queue.pretty.log.messages
binder: local_rabbit
binders:
local_rabbit:
type: rabbit
environment:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
server:
port: 0
management:
health:
binders:
enabled: true

View File

@ -0,0 +1,52 @@
package com.baeldung.spring.cloud.stream.rabbit;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.cloud.stream.rabbit.processor.MyProcessor;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MultipleOutputsServiceApplication.class)
@DirtiesContext
public class MultipleOutputsServiceApplicationIntegrationTest {
@Autowired
private MyProcessor pipe;
@Autowired
private MessageCollector messageCollector;
@Test
public void whenSendMessage_thenResponseIsInAOutput() {
whenSendMessage(1);
thenPayloadInChannelIs(pipe.anOutput(), 1);
}
@Test
public void whenSendMessage_thenResponseIsInAnotherOutput() {
whenSendMessage(11);
thenPayloadInChannelIs(pipe.anotherOutput(), 11);
}
private void whenSendMessage(Integer val) {
pipe.myInput()
.send(MessageBuilder.withPayload(val)
.build());
}
private void thenPayloadInChannelIs(MessageChannel channel, Integer expectedValue) {
Object payload = messageCollector.forChannel(channel)
.poll()
.getPayload();
assertEquals(expectedValue, payload);
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.spring.cloud.stream.rabbit;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.cloud.stream.rabbit.processor.MyProcessor;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MultipleOutputsWithConditionsServiceApplication.class)
@DirtiesContext
public class MultipleOutputsWithConditionsServiceIntegrationTest {
@Autowired
private MyProcessor pipe;
@Autowired
private MessageCollector messageCollector;
@Test
public void whenSendMessage_thenResponseIsInAOutput() {
whenSendMessage(1);
thenPayloadInChannelIs(pipe.anOutput(), 1);
}
@Test
public void whenSendMessage_thenResponseIsInAnotherOutput() {
whenSendMessage(11);
thenPayloadInChannelIs(pipe.anotherOutput(), 11);
}
private void whenSendMessage(Integer val) {
pipe.myInput()
.send(MessageBuilder.withPayload(val)
.build());
}
private void thenPayloadInChannelIs(MessageChannel channel, Integer expectedValue) {
Object payload = messageCollector.forChannel(channel)
.poll()
.getPayload();
assertEquals(expectedValue, payload);
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.spring.cloud.stream.rabbit;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.cloud.stream.rabbit.model.LogMessage;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyLoggerServiceApplication.class)
@DirtiesContext
public class MyLoggerApplicationIntegrationTest {
@Autowired
private Processor pipe;
@Autowired
private MessageCollector messageCollector;
@Test
public void whenSendMessage_thenResponseShouldUpdateText() {
pipe.input()
.send(MessageBuilder.withPayload(new LogMessage("This is my message"))
.build());
Object payload = messageCollector.forChannel(pipe.output())
.poll()
.getPayload();
assertEquals("[1]: This is my message", payload.toString());
}
}

View File

@ -2,4 +2,4 @@
### Relevant Articles
- [Spring REST Shell](http://www.baeldung.com/<ARTICLE_URI>)
- [Spring REST Shell](http://www.baeldung.com/spring-rest-shell)

View File

@ -0,0 +1,3 @@
## Testing Modules

View File

@ -20,7 +20,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.jupiter.version>5.0.1</junit.jupiter.version>
<junit.jupiter.version>5.0.2</junit.jupiter.version>
<junit.platform.version>1.0.1</junit.platform.version>
<junit.vintage.version>4.12.1</junit.vintage.version>
<log4j2.version>2.8.2</log4j2.version>
@ -127,4 +127,4 @@
</dependencies>
</project>
</project>

View File

@ -0,0 +1,34 @@
package com.baeldung.mockito.java8;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.util.List;
import org.junit.Test;
import org.mockito.exceptions.base.MockitoAssertionError;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.VerificationCollector;
public class LazyVerificationTest {
@Test
public void whenLazilyVerified_thenReportsMultipleFailures() {
VerificationCollector collector = MockitoJUnit.collector()
.assertLazily();
List mockList = mock(List.class);
verify(mockList).add("one");
verify(mockList).clear();
try {
collector.collectAndReport();
} catch (MockitoAssertionError error) {
assertTrue(error.getMessage()
.contains("1. Wanted but not invoked:"));
assertTrue(error.getMessage()
.contains("2. Wanted but not invoked:"));
}
}
}

View File

@ -1,6 +1,5 @@
## Relevant articles:
- [Introduction to MockServer](http://www.baeldung.com/mockserver)
- [JMockit Advanced Usage](http://www.baeldung.com/jmockit-advanced-usage)
- [A Guide to JMockit Expectations](http://www.baeldung.com/jmockit-expectations)
- [JMockit 101](http://www.baeldung.com/jmockit-101)

View File

@ -10,3 +10,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Introduction to WireMock](http://www.baeldung.com/introduction-to-wiremock)
- [REST API Testing with Cucumber](http://www.baeldung.com/cucumber-rest-api-testing)
- [Testing a REST API with JBehave](http://www.baeldung.com/jbehave-rest-testing)
- [REST API Testing with Karate](http://www.baeldung.com/karate-rest-api-testing)