Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-614
This commit is contained in:
+81
@@ -0,0 +1,81 @@
|
||||
package com.baeldung.java9.language.stream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CollectorImprovementTest {
|
||||
private static class Blog {
|
||||
private String authorName;
|
||||
private List<String> comments;
|
||||
|
||||
public Blog(String authorName) {
|
||||
this.authorName = authorName;
|
||||
this.comments = new LinkedList<String>();
|
||||
}
|
||||
|
||||
public String getAuthorName() {
|
||||
return this.authorName;
|
||||
}
|
||||
|
||||
public List<String> getComments() {
|
||||
return new LinkedList<String>(this.comments);
|
||||
}
|
||||
|
||||
public void addComment(String comment) {
|
||||
this.comments.add(comment);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFiltering() {
|
||||
List<Integer> numbers = List.of(1, 2, 3, 5, 5);
|
||||
|
||||
Map<Integer, Long> result = numbers.stream()
|
||||
.filter(val -> val > 3).collect(Collectors.groupingBy(
|
||||
Function.identity(), Collectors.counting()));
|
||||
|
||||
assertEquals(1, result.size());
|
||||
|
||||
result = numbers.stream().collect(Collectors.groupingBy(
|
||||
Function.identity(), Collectors.filtering(val -> val > 3,
|
||||
Collectors.counting())));
|
||||
|
||||
assertEquals(4, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlatMapping() {
|
||||
Blog blog1 = new CollectorImprovementTest.Blog("1");
|
||||
blog1.addComment("Nice");
|
||||
blog1.addComment("Very Nice");
|
||||
Blog blog2 = new CollectorImprovementTest.Blog("2");
|
||||
blog2.addComment("Disappointing");
|
||||
blog2.addComment("Ok");
|
||||
blog2.addComment("Could be better");
|
||||
List<Blog> blogs = List.of(blog1, blog2);
|
||||
|
||||
Map<String, List<List<String>>> authorComments1 =
|
||||
blogs.stream().collect(Collectors.groupingBy(
|
||||
Blog::getAuthorName, Collectors.mapping(
|
||||
Blog::getComments, Collectors.toList())));
|
||||
|
||||
assertEquals(2, authorComments1.size());
|
||||
assertEquals(2, authorComments1.get("1").get(0).size());
|
||||
assertEquals(3, authorComments1.get("2").get(0).size());
|
||||
|
||||
Map<String, List<String>> authorComments2 =
|
||||
blogs.stream().collect(Collectors.groupingBy(
|
||||
Blog::getAuthorName, Collectors.flatMapping(
|
||||
blog -> blog.getComments().stream(),
|
||||
Collectors.toList())));
|
||||
|
||||
assertEquals(2, authorComments2.size());
|
||||
assertEquals(2, authorComments2.get("1").size());
|
||||
assertEquals(3, authorComments2.get("2").size());
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -52,4 +52,5 @@
|
||||
- [URL Encoding and Decoding in Java](http://www.baeldung.com/java-url-encoding-decoding)
|
||||
- [Calculate the Size of a File in Java](http://www.baeldung.com/java-file-size)
|
||||
- [The Basics of Java Generics](http://www.baeldung.com/java-generics)
|
||||
- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
- [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven)
|
||||
|
||||
@@ -64,11 +64,6 @@
|
||||
<version>${grep4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.lmax</groupId>
|
||||
<artifactId>disruptor</artifactId>
|
||||
<version>${disruptor.version}</version>
|
||||
</dependency>
|
||||
<!-- web -->
|
||||
|
||||
<!-- marshalling -->
|
||||
@@ -369,7 +364,6 @@
|
||||
<unix4j.version>0.4</unix4j.version>
|
||||
<grep4j.version>1.8.7</grep4j.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<disruptor.version>3.3.6</disruptor.version>
|
||||
|
||||
<!-- testing -->
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.concurrent.countdownlatch;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class BrokenWorker implements Runnable {
|
||||
private final List<String> outputScraper;
|
||||
private final CountDownLatch countDownLatch;
|
||||
|
||||
public BrokenWorker(final List<String> outputScraper, final CountDownLatch countDownLatch) {
|
||||
this.outputScraper = outputScraper;
|
||||
this.countDownLatch = countDownLatch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (true) {
|
||||
throw new RuntimeException("Oh dear");
|
||||
}
|
||||
countDownLatch.countDown();
|
||||
outputScraper.add("Counted down");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.concurrent.countdownlatch;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class WaitingWorker implements Runnable {
|
||||
|
||||
private final List<String> outputScraper;
|
||||
private final CountDownLatch readyThreadCounter;
|
||||
private final CountDownLatch callingThreadBlocker;
|
||||
private final CountDownLatch completedThreadCounter;
|
||||
|
||||
public WaitingWorker(final List<String> outputScraper,
|
||||
final CountDownLatch readyThreadCounter,
|
||||
final CountDownLatch callingThreadBlocker,
|
||||
CountDownLatch completedThreadCounter) {
|
||||
|
||||
this.outputScraper = outputScraper;
|
||||
this.readyThreadCounter = readyThreadCounter;
|
||||
this.callingThreadBlocker = callingThreadBlocker;
|
||||
this.completedThreadCounter = completedThreadCounter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// Mark this thread as read / started
|
||||
readyThreadCounter.countDown();
|
||||
try {
|
||||
callingThreadBlocker.await();
|
||||
outputScraper.add("Counted down");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
completedThreadCounter.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.concurrent.countdownlatch;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class Worker implements Runnable {
|
||||
private final List<String> outputScraper;
|
||||
private final CountDownLatch countDownLatch;
|
||||
|
||||
public Worker(final List<String> outputScraper, final CountDownLatch countDownLatch) {
|
||||
this.outputScraper = outputScraper;
|
||||
this.countDownLatch = countDownLatch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// Do some work
|
||||
System.out.println("Doing some logic");
|
||||
countDownLatch.countDown();
|
||||
outputScraper.add("Counted down");
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.concurrent.countdownlatch;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CountdownLatchExampleTest {
|
||||
@Test
|
||||
public void whenParallelProcessing_thenMainThreadWillBlockUntilCompletion() throws InterruptedException {
|
||||
// Given
|
||||
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
|
||||
CountDownLatch countDownLatch = new CountDownLatch(5);
|
||||
List<Thread> workers = Stream
|
||||
.generate(() -> new Thread(new Worker(outputScraper, countDownLatch)))
|
||||
.limit(5)
|
||||
.collect(toList());
|
||||
|
||||
// When
|
||||
workers.forEach(Thread::start);
|
||||
countDownLatch.await(); // Block until workers finish
|
||||
outputScraper.add("Latch released");
|
||||
|
||||
// Then
|
||||
outputScraper.forEach(Object::toString);
|
||||
assertThat(outputScraper)
|
||||
.containsExactly(
|
||||
"Counted down",
|
||||
"Counted down",
|
||||
"Counted down",
|
||||
"Counted down",
|
||||
"Counted down",
|
||||
"Latch released"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFailingToParallelProcess_thenMainThreadShouldTimeout() throws InterruptedException {
|
||||
// Given
|
||||
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
|
||||
CountDownLatch countDownLatch = new CountDownLatch(5);
|
||||
List<Thread> workers = Stream
|
||||
.generate(() -> new Thread(new BrokenWorker(outputScraper, countDownLatch)))
|
||||
.limit(5)
|
||||
.collect(toList());
|
||||
|
||||
// When
|
||||
workers.forEach(Thread::start);
|
||||
final boolean result = countDownLatch.await(3L, TimeUnit.SECONDS);
|
||||
|
||||
// Then
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDoingLotsOfThreadsInParallel_thenStartThemAtTheSameTime() throws InterruptedException {
|
||||
// Given
|
||||
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
|
||||
CountDownLatch readyThreadCounter = new CountDownLatch(5);
|
||||
CountDownLatch callingThreadBlocker = new CountDownLatch(1);
|
||||
CountDownLatch completedThreadCounter = new CountDownLatch(5);
|
||||
List<Thread> workers = Stream
|
||||
.generate(() -> new Thread(new WaitingWorker(outputScraper, readyThreadCounter, callingThreadBlocker, completedThreadCounter)))
|
||||
.limit(5)
|
||||
.collect(toList());
|
||||
|
||||
// When
|
||||
workers.forEach(Thread::start);
|
||||
readyThreadCounter.await(); // Block until workers start
|
||||
outputScraper.add("Workers ready");
|
||||
callingThreadBlocker.countDown(); // Start workers
|
||||
completedThreadCounter.await(); // Block until workers finish
|
||||
outputScraper.add("Workers complete");
|
||||
|
||||
// Then
|
||||
outputScraper.forEach(Object::toString);
|
||||
assertThat(outputScraper)
|
||||
.containsExactly(
|
||||
"Workers ready",
|
||||
"Counted down",
|
||||
"Counted down",
|
||||
"Counted down",
|
||||
"Counted down",
|
||||
"Counted down",
|
||||
"Workers complete"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
package com.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.EnumHashBiMap;
|
||||
@@ -13,32 +11,32 @@ import com.google.common.collect.ImmutableBiMap;
|
||||
|
||||
public class GuavaBiMapTest {
|
||||
@Test
|
||||
public void whenQueryByValue_shouldReturnKey() {
|
||||
public void whenQueryByValue_returnsKey() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
capitalCountryBiMap.put("Washingon, D.C.", "USA");
|
||||
capitalCountryBiMap.put("Moscow", "Russia");
|
||||
|
||||
final String countryHeadName = capitalCountryBiMap.inverse().get("India");
|
||||
final String countryCapitalName = capitalCountryBiMap.inverse().get("India");
|
||||
|
||||
assertEquals("New Delhi", countryHeadName);
|
||||
assertEquals("New Delhi", countryCapitalName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateBiMapFromExistingMap_shouldReturnKey() {
|
||||
final Map<String, String> personCountryHeadMap = new HashMap<>();
|
||||
personCountryHeadMap.put("New Delhi", "India");
|
||||
personCountryHeadMap.put("Washingon, D.C.", "USA");
|
||||
personCountryHeadMap.put("Moscow", "Russia");
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create(personCountryHeadMap);
|
||||
public void whenCreateBiMapFromExistingMap_returnsKey() {
|
||||
final Map<String, String> capitalCountryMap = new HashMap<>();
|
||||
capitalCountryMap.put("New Delhi", "India");
|
||||
capitalCountryMap.put("Washingon, D.C.", "USA");
|
||||
capitalCountryMap.put("Moscow", "Russia");
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create(capitalCountryMap);
|
||||
|
||||
final String countryHeadName = capitalCountryBiMap.inverse().get("India");
|
||||
final String countryCapitalName = capitalCountryBiMap.inverse().get("India");
|
||||
|
||||
assertEquals("New Delhi", countryHeadName);
|
||||
assertEquals("New Delhi", countryCapitalName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenQueryByKey_shouldReturnValue() {
|
||||
public void whenQueryByKey_returnsValue() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
@@ -49,7 +47,7 @@ public class GuavaBiMapTest {
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void whenSameValueIsBeingPresent_shouldThrowException() {
|
||||
public void whenSameValueIsPresent_throwsException() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
@@ -59,7 +57,7 @@ public class GuavaBiMapTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameValueIsBeingPresent_whenForcePutIsUsed_shouldCompleteSuccessfully() {
|
||||
public void givenSameValueIsPresent_whenForcePut_completesSuccessfully() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
@@ -72,7 +70,7 @@ public class GuavaBiMapTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSameKeyIsBeingPresent_shouldReplaceAlreadyPresent() {
|
||||
public void whenSameKeyIsPresent_replacesAlreadyPresent() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
@@ -84,21 +82,21 @@ public class GuavaBiMapTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingImmutableBiMap_shouldAllowPutSuccessfully() {
|
||||
public void whenUsingImmutableBiMap_allowsPutSuccessfully() {
|
||||
final BiMap<String, String> capitalCountryBiMap = new ImmutableBiMap.Builder<String, String>().put("New Delhi", "India").put("Washingon, D.C.", "USA").put("Moscow", "Russia").build();
|
||||
|
||||
assertEquals("USA", capitalCountryBiMap.get("Washingon, D.C."));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenUsingImmutableBiMap_shouldNotAllowRemove() {
|
||||
public void whenUsingImmutableBiMap_doesntAllowRemove() {
|
||||
final BiMap<String, String> capitalCountryBiMap = new ImmutableBiMap.Builder<String, String>().put("New Delhi", "India").put("Washingon, D.C.", "USA").put("Moscow", "Russia").build();
|
||||
|
||||
capitalCountryBiMap.remove("New Delhi");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenUsingImmutableBiMap_shouldNotAllowPut() {
|
||||
public void whenUsingImmutableBiMap_doesntAllowPut() {
|
||||
final BiMap<String, String> capitalCountryBiMap = new ImmutableBiMap.Builder<String, String>().put("New Delhi", "India").put("Washingon, D.C.", "USA").put("Moscow", "Russia").build();
|
||||
|
||||
capitalCountryBiMap.put("New York", "USA");
|
||||
@@ -109,7 +107,7 @@ public class GuavaBiMapTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEnumAsKeyInMap_shouldReplaceAlreadyPresent() {
|
||||
public void whenUsingEnumAsKeyInMap_replacesAlreadyPresent() {
|
||||
final BiMap<Operation, String> operationStringBiMap = EnumHashBiMap.create(Operation.class);
|
||||
|
||||
operationStringBiMap.put(Operation.ADD, "Add");
|
||||
@@ -119,4 +117,4 @@ public class GuavaBiMapTest {
|
||||
|
||||
assertEquals("Divide", operationStringBiMap.get(Operation.DIVIDE));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -311,13 +311,7 @@ public class MapTest {
|
||||
|
||||
@Test
|
||||
public void givenTreeMap_whenOrdersEntriesByComparator_thenCorrect() {
|
||||
TreeMap<Integer, String> map = new TreeMap<>(new Comparator<Integer>() {
|
||||
|
||||
@Override
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
return o2 - o1;
|
||||
}
|
||||
});
|
||||
TreeMap<Integer, String> map = new TreeMap<>(Comparator.reverseOrder());
|
||||
map.put(3, "val");
|
||||
map.put(2, "val");
|
||||
map.put(1, "val");
|
||||
|
||||
@@ -17,7 +17,7 @@ public class Java8FindAnyFindFirstTest {
|
||||
@Test
|
||||
public void createStream_whenFindAnyResultIsPresent_thenCorrect() {
|
||||
|
||||
List<String> list = Arrays.asList("A","B","C","D");
|
||||
List<String> list = Arrays.asList("A", "B", "C", "D");
|
||||
|
||||
Optional<String> result = list.stream().findAny();
|
||||
|
||||
@@ -25,14 +25,27 @@ public class Java8FindAnyFindFirstTest {
|
||||
assertThat(result.get(), anyOf(is("A"), is("B"), is("C"), is("D")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createParallelStream_whenFindAnyResultIsPresent_thenCorrect() throws Exception {
|
||||
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
|
||||
Optional<Integer> result = list
|
||||
.stream()
|
||||
.parallel()
|
||||
.filter(num -> num < 4)
|
||||
.findAny();
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertThat(result.get(), anyOf(is(1), is(2), is(3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createStream_whenFindFirstResultIsPresent_thenCorrect() {
|
||||
|
||||
List<String> list = Arrays.asList("A","B","C","D");
|
||||
List<String> list = Arrays.asList("A", "B", "C", "D");
|
||||
|
||||
Optional<String> result = list.stream().findFirst();
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertThat(result.get(),is("A"));
|
||||
assertThat(result.get(), is("A"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,251 @@
|
||||
<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>disruptor</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>disruptor</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.lmax</groupId>
|
||||
<artifactId>disruptor</artifactId>
|
||||
<version>${disruptor.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- logging -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>1.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>disruptor</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<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.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>libs/</classpathPrefix>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>com.jolira</groupId>
|
||||
<artifactId>onejar-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
<attachToBuild>true</attachToBuild>
|
||||
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>one-jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
<logback.version>1.1.7</logback.version>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<disruptor.version>3.3.6</disruptor.version>
|
||||
|
||||
<!-- testing -->
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
<testng.version>6.10</testng.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,128 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
import com.google.common.collect.ImmutableRangeMap;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.RangeMap;
|
||||
import com.google.common.collect.TreeRangeMap;
|
||||
|
||||
public class GuavaRangeMapTest {
|
||||
|
||||
@Test
|
||||
public void givenRangeMap_whenQueryWithinRange_returnsSucessfully() {
|
||||
final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();
|
||||
|
||||
experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
|
||||
experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
|
||||
experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");
|
||||
|
||||
assertEquals("Vice President", experienceRangeDesignationMap.get(6));
|
||||
assertEquals("Executive Director", experienceRangeDesignationMap.get(15));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeMap_whenQueryOutsideRange_returnsNull() {
|
||||
final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();
|
||||
|
||||
experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
|
||||
experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
|
||||
experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");
|
||||
|
||||
assertNull(experienceRangeDesignationMap.get(31));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeMap_whenRemoveRangeIsCalled_removesSucessfully() {
|
||||
final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();
|
||||
|
||||
experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
|
||||
experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
|
||||
experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");
|
||||
experienceRangeDesignationMap.remove(Range.closed(8, 15));
|
||||
experienceRangeDesignationMap.remove(Range.closed(20, 26));
|
||||
|
||||
assertNull(experienceRangeDesignationMap.get(9));
|
||||
assertEquals("Managing Director", experienceRangeDesignationMap.get(16));
|
||||
assertEquals("Managing Director", experienceRangeDesignationMap.get(30));
|
||||
assertNull(experienceRangeDesignationMap.get(25));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeMap_whenSpanIsCalled_returnsSucessfully() {
|
||||
final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();
|
||||
|
||||
experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
|
||||
experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
|
||||
experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");
|
||||
final Range<Integer> experienceSpan = experienceRangeDesignationMap.span();
|
||||
|
||||
assertEquals(0, experienceSpan.lowerEndpoint().intValue());
|
||||
assertEquals(30, experienceSpan.upperEndpoint().intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeMap_whenGetEntryIsCalled_returnsEntrySucessfully() {
|
||||
final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();
|
||||
|
||||
experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
|
||||
experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
|
||||
experienceRangeDesignationMap.put(Range.closed(20, 30), "Managing Director");
|
||||
final Map.Entry<Range<Integer>, String> experiencEntry = experienceRangeDesignationMap.getEntry(10);
|
||||
|
||||
assertEquals(Range.closed(9, 15), experiencEntry.getKey());
|
||||
assertEquals("Executive Director", experiencEntry.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeMap_whenSubRangeMapIsCalled_returnsSubRangeSucessfully() {
|
||||
final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();
|
||||
|
||||
experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
|
||||
experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
|
||||
experienceRangeDesignationMap.put(Range.closed(8, 15), "Executive Director");
|
||||
experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");
|
||||
final RangeMap<Integer, String> experiencedSubRangeDesignationMap = experienceRangeDesignationMap.subRangeMap(Range.closed(4, 14));
|
||||
|
||||
assertNull(experiencedSubRangeDesignationMap.get(3));
|
||||
assertEquals("Executive Director", experiencedSubRangeDesignationMap.get(14));
|
||||
assertEquals("Vice President", experiencedSubRangeDesignationMap.get(7));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImmutableRangeMap_whenQueryWithinRange_returnsSucessfully() {
|
||||
final RangeMap<Integer, String> experienceRangeDesignationMap = ImmutableRangeMap.<Integer, String> builder()
|
||||
.put(Range.closed(0, 2), "Associate")
|
||||
.put(Range.closed(3, 5), "Senior Associate")
|
||||
.put(Range.closed(6, 8), "Vice President")
|
||||
.put(Range.closed(9, 15), "Executive Director")
|
||||
.put(Range.closed(16, 30), "Managing Director").build();
|
||||
|
||||
assertEquals("Vice President", experienceRangeDesignationMap.get(6));
|
||||
assertEquals("Executive Director", experienceRangeDesignationMap.get(15));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenImmutableRangeMap_whenRangeOverlaps_ThrowsException() {
|
||||
ImmutableRangeMap.<Integer, String> builder()
|
||||
.put(Range.closed(0, 2), "Associate")
|
||||
.put(Range.closed(3, 5), "Senior Associate")
|
||||
.put(Range.closed(6, 8), "Vice President")
|
||||
.put(Range.closed(8, 15), "Executive Director")
|
||||
.put(Range.closed(16, 30), "Managing Director").build();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import org.junit.Test;
|
||||
import com.google.common.collect.ImmutableRangeSet;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.RangeSet;
|
||||
import com.google.common.collect.TreeRangeSet;
|
||||
|
||||
public class GuavaRangeSetTest {
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenQueryWithinRange_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
|
||||
assertTrue(numberRangeSet.contains(1));
|
||||
assertFalse(numberRangeSet.contains(9));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenEnclosesWithinRange_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 10));
|
||||
numberRangeSet.add(Range.closed(15, 18));
|
||||
|
||||
assertTrue(numberRangeSet.encloses(Range.closed(4, 5)));
|
||||
assertFalse(numberRangeSet.encloses(Range.closed(4, 11)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenComplementIsCalled_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
final RangeSet<Integer> numberRangeComplementSet = numberRangeSet.complement();
|
||||
|
||||
assertTrue(numberRangeComplementSet.contains(-1000));
|
||||
assertFalse(numberRangeComplementSet.contains(2));
|
||||
assertFalse(numberRangeComplementSet.contains(3));
|
||||
assertTrue(numberRangeComplementSet.contains(1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenIntersectsWithinRange_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 10));
|
||||
numberRangeSet.add(Range.closed(15, 18));
|
||||
|
||||
assertTrue(numberRangeSet.intersects(Range.closed(4, 17)));
|
||||
assertFalse(numberRangeSet.intersects(Range.closed(19, 200)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenRemoveRangeIsCalled_removesSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
numberRangeSet.add(Range.closed(9, 15));
|
||||
numberRangeSet.remove(Range.closed(3, 5));
|
||||
numberRangeSet.remove(Range.closed(7, 10));
|
||||
|
||||
assertTrue(numberRangeSet.contains(1));
|
||||
assertFalse(numberRangeSet.contains(9));
|
||||
assertTrue(numberRangeSet.contains(12));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenSpanIsCalled_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
final Range<Integer> experienceSpan = numberRangeSet.span();
|
||||
|
||||
assertEquals(0, experienceSpan.lowerEndpoint().intValue());
|
||||
assertEquals(8, experienceSpan.upperEndpoint().intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenSubRangeSetIsCalled_returnsSubRangeSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
final RangeSet<Integer> numberSubRangeSet = numberRangeSet.subRangeSet(Range.closed(4, 14));
|
||||
|
||||
assertFalse(numberSubRangeSet.contains(3));
|
||||
assertFalse(numberSubRangeSet.contains(14));
|
||||
assertTrue(numberSubRangeSet.contains(7));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImmutableRangeSet_whenQueryWithinRange_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = ImmutableRangeSet.<Integer> builder()
|
||||
.add(Range.closed(0, 2))
|
||||
.add(Range.closed(3, 5))
|
||||
.add(Range.closed(6, 8)).build();
|
||||
|
||||
assertTrue(numberRangeSet.contains(6));
|
||||
assertFalse(numberRangeSet.contains(15));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenImmutableRangeMap_whenRangeOverlaps_ThrowsException() {
|
||||
ImmutableRangeSet.<Integer> builder()
|
||||
.add(Range.closed(0, 2))
|
||||
.add(Range.closed(3, 5))
|
||||
.add(Range.closed(5, 8)).build();
|
||||
|
||||
}
|
||||
}
|
||||
+88
-34
@@ -1,44 +1,98 @@
|
||||
<?xml version="1.0"?>
|
||||
<project
|
||||
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<project 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"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-mongodb</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-mongodb</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mongo.version>3.4.1</mongo.version>
|
||||
<flapdoodle.version>1.11</flapdoodle.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embedmongo</groupId>
|
||||
<artifactId>de.flapdoodle.embedmongo</artifactId>
|
||||
<version>${flapdoodle.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embedmongo</groupId>
|
||||
<artifactId>de.flapdoodle.embedmongo</artifactId>
|
||||
<version>${flapdoodle.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>${mongo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>${mongo.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
<testFailureIgnore>true</testFailureIgnore>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mongo.version>3.4.1</mongo.version>
|
||||
<flapdoodle.version>1.11</flapdoodle.version>
|
||||
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -7,47 +7,47 @@ import com.mongodb.DBCursor;
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
public class MongoExample {
|
||||
public static void main( String[] args ) {
|
||||
|
||||
MongoClient mongoClient = new MongoClient("localhost", 27017);
|
||||
|
||||
DB database = mongoClient.getDB("myMongoDb");
|
||||
|
||||
// print existing databases
|
||||
mongoClient.getDatabaseNames().forEach(System.out::println);
|
||||
|
||||
database.createCollection("customers", null);
|
||||
|
||||
// print all collections in customers database
|
||||
database.getCollectionNames().forEach(System.out::println);
|
||||
|
||||
// create data
|
||||
DBCollection collection = database.getCollection("customers");
|
||||
BasicDBObject document = new BasicDBObject();
|
||||
document.put("name", "Shubham");
|
||||
document.put("company", "Baeldung");
|
||||
collection.insert(document);
|
||||
|
||||
// update data
|
||||
BasicDBObject query = new BasicDBObject();
|
||||
query.put("name", "Shubham");
|
||||
BasicDBObject newDocument = new BasicDBObject();
|
||||
newDocument.put("name", "John");
|
||||
BasicDBObject updateObject = new BasicDBObject();
|
||||
updateObject.put("$set", newDocument);
|
||||
collection.update(query, updateObject);
|
||||
|
||||
// read data
|
||||
BasicDBObject searchQuery = new BasicDBObject();
|
||||
searchQuery.put("name", "John");
|
||||
DBCursor cursor = collection.find(searchQuery);
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
|
||||
// delete data
|
||||
BasicDBObject deleteQuery = new BasicDBObject();
|
||||
deleteQuery.put("name", "John");
|
||||
collection.remove(deleteQuery);
|
||||
public static void main(String[] args) {
|
||||
|
||||
MongoClient mongoClient = new MongoClient("localhost", 27017);
|
||||
|
||||
DB database = mongoClient.getDB("myMongoDb");
|
||||
|
||||
// print existing databases
|
||||
mongoClient.getDatabaseNames().forEach(System.out::println);
|
||||
|
||||
database.createCollection("customers", null);
|
||||
|
||||
// print all collections in customers database
|
||||
database.getCollectionNames().forEach(System.out::println);
|
||||
|
||||
// create data
|
||||
DBCollection collection = database.getCollection("customers");
|
||||
BasicDBObject document = new BasicDBObject();
|
||||
document.put("name", "Shubham");
|
||||
document.put("company", "Baeldung");
|
||||
collection.insert(document);
|
||||
|
||||
// update data
|
||||
BasicDBObject query = new BasicDBObject();
|
||||
query.put("name", "Shubham");
|
||||
BasicDBObject newDocument = new BasicDBObject();
|
||||
newDocument.put("name", "John");
|
||||
BasicDBObject updateObject = new BasicDBObject();
|
||||
updateObject.put("$set", newDocument);
|
||||
collection.update(query, updateObject);
|
||||
|
||||
// read data
|
||||
BasicDBObject searchQuery = new BasicDBObject();
|
||||
searchQuery.put("name", "John");
|
||||
DBCursor cursor = collection.find(searchQuery);
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
|
||||
// delete data
|
||||
BasicDBObject deleteQuery = new BasicDBObject();
|
||||
deleteQuery.put("name", "John");
|
||||
collection.remove(deleteQuery);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBCursor;
|
||||
import com.mongodb.Mongo;
|
||||
|
||||
import de.flapdoodle.embedmongo.MongoDBRuntime;
|
||||
import de.flapdoodle.embedmongo.MongodExecutable;
|
||||
import de.flapdoodle.embedmongo.MongodProcess;
|
||||
import de.flapdoodle.embedmongo.config.MongodConfig;
|
||||
import de.flapdoodle.embedmongo.distribution.Version;
|
||||
import de.flapdoodle.embedmongo.runtime.Network;
|
||||
|
||||
public class AppIntegrationTest {
|
||||
|
||||
private static final String DB_NAME = "myMongoDb";
|
||||
private MongodExecutable mongodExe;
|
||||
private MongodProcess mongod;
|
||||
private Mongo mongo;
|
||||
private DB db;
|
||||
private DBCollection collection;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
// Creating Mongodbruntime instance
|
||||
MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance();
|
||||
|
||||
// Creating MongodbExecutable
|
||||
mongodExe = runtime.prepare(new MongodConfig(Version.V2_0_1, 12345, Network.localhostIsIPv6()));
|
||||
|
||||
// Starting Mongodb
|
||||
mongod = mongodExe.start();
|
||||
mongo = new Mongo("localhost", 12345);
|
||||
|
||||
// Creating DB
|
||||
db = mongo.getDB(DB_NAME);
|
||||
|
||||
// Creating collection Object and adding values
|
||||
collection = db.getCollection("customers");
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() throws Exception {
|
||||
mongod.stop();
|
||||
mongodExe.cleanup();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddressPersistance() {
|
||||
BasicDBObject contact = new BasicDBObject();
|
||||
contact.put("name", "John");
|
||||
contact.put("company", "Baeldung");
|
||||
|
||||
// Inserting document
|
||||
collection.insert(contact);
|
||||
DBCursor cursorDoc = collection.find();
|
||||
BasicDBObject contact1 = new BasicDBObject();
|
||||
while (cursorDoc.hasNext()) {
|
||||
contact1 = (BasicDBObject) cursorDoc.next();
|
||||
System.out.println(contact1);
|
||||
}
|
||||
assertEquals(contact1.get("name"), "John");
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBCursor;
|
||||
import com.mongodb.Mongo;
|
||||
import de.flapdoodle.embedmongo.MongoDBRuntime;
|
||||
import de.flapdoodle.embedmongo.MongodExecutable;
|
||||
import de.flapdoodle.embedmongo.MongodProcess;
|
||||
import de.flapdoodle.embedmongo.config.MongodConfig;
|
||||
import de.flapdoodle.embedmongo.distribution.Version;
|
||||
import de.flapdoodle.embedmongo.runtime.Network;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
private static final String DB_NAME = "myMongoDb";
|
||||
private MongodExecutable mongodExe;
|
||||
private MongodProcess mongod;
|
||||
private Mongo mongo;
|
||||
private DB db;
|
||||
private DBCollection collection;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
|
||||
// Creating Mongodbruntime instance
|
||||
MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance();
|
||||
|
||||
// Creating MongodbExecutable
|
||||
mongodExe = runtime.prepare(new MongodConfig(Version.V2_0_1, 12345,
|
||||
Network.localhostIsIPv6()));
|
||||
|
||||
// Starting Mongodb
|
||||
mongod = mongodExe.start();
|
||||
mongo = new Mongo("localhost", 12345);
|
||||
|
||||
// Creating DB
|
||||
db = mongo.getDB(DB_NAME);
|
||||
|
||||
// Creating collection Object and adding values
|
||||
collection = db.getCollection("customers");
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() throws Exception {
|
||||
mongod.stop();
|
||||
mongodExe.cleanup();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddressPersistance() {
|
||||
|
||||
BasicDBObject contact = new BasicDBObject();
|
||||
contact.put("name", "John");
|
||||
contact.put("company", "Baeldung");
|
||||
|
||||
// Inserting document
|
||||
collection.insert(contact);
|
||||
DBCursor cursorDoc = collection.find();
|
||||
BasicDBObject contact1 = new BasicDBObject();
|
||||
while (cursorDoc.hasNext()) {
|
||||
contact1 = (BasicDBObject) cursorDoc.next();
|
||||
System.out.println(contact1);
|
||||
}
|
||||
assertEquals(contact1.get("name"), "John");
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -37,4 +37,4 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
</project>
|
||||
@@ -1,16 +1,15 @@
|
||||
package com.baeldung.metrics.core;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import com.codahale.metrics.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.codahale.metrics.*;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class MetricsTest {
|
||||
@Test
|
||||
@@ -139,7 +138,7 @@ public class MetricsTest {
|
||||
|
||||
long elapsed1 = context1.stop();
|
||||
|
||||
assertEquals(5000000000L, elapsed1, 1000000);
|
||||
assertEquals(5000000000L, elapsed1, 10000000);
|
||||
assertThat(timer.getCount(), equalTo(1L));
|
||||
assertEquals(0.2, timer.getMeanRate(), 0.1);
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user