From bb6ab6215b3e7ea90f4afab30f8c5a0ea7f7b890 Mon Sep 17 00:00:00 2001 From: oborkovskyi Date: Thu, 17 Dec 2015 19:44:32 +0200 Subject: [PATCH 1/3] Added guava 18 test project --- guava18/.classpath | 36 ++++ ...e.wst.jsdt.core.javascriptValidator.launch | 7 + guava18/.gitignore | 13 ++ guava18/.project | 36 ++++ guava18/.springBeans | 14 ++ guava18/README.md | 13 ++ guava18/pom.xml | 41 ++++ .../baeldung/guava/entity/Administrator.java | 18 ++ .../com/baeldung/guava/entity/Player.java | 8 + .../java/com/baeldung/guava/entity/User.java | 36 ++++ .../java/com/baeldung/guava/GuavaTest.java | 177 ++++++++++++++++++ 11 files changed, 399 insertions(+) create mode 100644 guava18/.classpath create mode 100644 guava18/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch create mode 100644 guava18/.gitignore create mode 100644 guava18/.project create mode 100644 guava18/.springBeans create mode 100644 guava18/README.md create mode 100644 guava18/pom.xml create mode 100644 guava18/src/main/java/com/baeldung/guava/entity/Administrator.java create mode 100644 guava18/src/main/java/com/baeldung/guava/entity/Player.java create mode 100644 guava18/src/main/java/com/baeldung/guava/entity/User.java create mode 100644 guava18/src/test/java/com/baeldung/guava/GuavaTest.java diff --git a/guava18/.classpath b/guava18/.classpath new file mode 100644 index 0000000000..8ebf6d9c31 --- /dev/null +++ b/guava18/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/guava18/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/guava18/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch new file mode 100644 index 0000000000..627021fb96 --- /dev/null +++ b/guava18/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch @@ -0,0 +1,7 @@ + + + + + + + diff --git a/guava18/.gitignore b/guava18/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/guava18/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/guava18/.project b/guava18/.project new file mode 100644 index 0000000000..829dc83809 --- /dev/null +++ b/guava18/.project @@ -0,0 +1,36 @@ + + + guava + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/guava18/.springBeans b/guava18/.springBeans new file mode 100644 index 0000000000..a79097f40d --- /dev/null +++ b/guava18/.springBeans @@ -0,0 +1,14 @@ + + + 1 + + + + + + + src/main/webapp/WEB-INF/api-servlet.xml + + + + diff --git a/guava18/README.md b/guava18/README.md new file mode 100644 index 0000000000..8960b4676e --- /dev/null +++ b/guava18/README.md @@ -0,0 +1,13 @@ +========= + +## Guava and Hamcrest Cookbooks and Examples + + +### Relevant Articles: +- [Guava Collections Cookbook](http://www.baeldung.com/guava-collections) +- [Guava Ordering Cookbook](http://www.baeldung.com/guava-order) +- [Guava Functional Cookbook](http://www.baeldung.com/guava-functions-predicates) + +- [Hamcrest Collections Cookbook](http://www.baeldung.com/hamcrest-collections-arrays) + +- [Partition a List in Java](http://www.baeldung.com/java-list-split) diff --git a/guava18/pom.xml b/guava18/pom.xml new file mode 100644 index 0000000000..21f4697a73 --- /dev/null +++ b/guava18/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.baeldung + guava + 1.0-SNAPSHOT + + + + com.google.guava + guava + 18.0 + + + junit + junit + 4.12 + + + + + + maven-compiler-plugin + 3.3 + + true + true + 1.8 + 1.8 + UTF-8 + true + true + + + + + + \ No newline at end of file diff --git a/guava18/src/main/java/com/baeldung/guava/entity/Administrator.java b/guava18/src/main/java/com/baeldung/guava/entity/Administrator.java new file mode 100644 index 0000000000..f9447ea44d --- /dev/null +++ b/guava18/src/main/java/com/baeldung/guava/entity/Administrator.java @@ -0,0 +1,18 @@ +package com.baeldung.guava.entity; + +import com.google.common.base.MoreObjects; + +public class Administrator extends User{ + public Administrator(long id, String name, int age) { + super(id, name, age); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("id", getId()) + .add("name", getName()) + .add("age", getAge()) + .toString(); + } +} \ No newline at end of file diff --git a/guava18/src/main/java/com/baeldung/guava/entity/Player.java b/guava18/src/main/java/com/baeldung/guava/entity/Player.java new file mode 100644 index 0000000000..f460fa9f00 --- /dev/null +++ b/guava18/src/main/java/com/baeldung/guava/entity/Player.java @@ -0,0 +1,8 @@ +package com.baeldung.guava.entity; + +public class Player extends User{ + public Player(long id, String name, int age) { + super(id, name, age); + } + +} diff --git a/guava18/src/main/java/com/baeldung/guava/entity/User.java b/guava18/src/main/java/com/baeldung/guava/entity/User.java new file mode 100644 index 0000000000..be673edb10 --- /dev/null +++ b/guava18/src/main/java/com/baeldung/guava/entity/User.java @@ -0,0 +1,36 @@ +package com.baeldung.guava.entity; + +import com.google.common.base.MoreObjects; + +public class User{ + private long id; + private String name; + private int age; + + public User(long id, String name, int age) { + this.id = id; + this.name = name; + this.age = age; + } + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(User.class) + .add("id", id) + .add("name", name) + .add("age", age) + .toString(); + } +} \ No newline at end of file diff --git a/guava18/src/test/java/com/baeldung/guava/GuavaTest.java b/guava18/src/test/java/com/baeldung/guava/GuavaTest.java new file mode 100644 index 0000000000..5d1d1c074a --- /dev/null +++ b/guava18/src/test/java/com/baeldung/guava/GuavaTest.java @@ -0,0 +1,177 @@ +import com.baeldung.guava.entity.Administrator; +import com.baeldung.guava.entity.Player; +import com.baeldung.guava.entity.User; +import com.google.common.base.Functions; +import com.google.common.base.Joiner; +import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; +import com.google.common.hash.HashCode; +import com.google.common.hash.Hashing; +import com.google.common.net.InetAddresses; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; +import org.junit.Assert; +import org.junit.Test; + +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.*; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class GuavaTest { + + private static final int ADULT_AGE = 18; + + @Test + public void whenToString_shouldIncludeAllFields() throws Exception { + User user = new User(12L, "John Doe", 25); + + Assert.assertThat(user.toString(), equalTo("User{id=12, name=John Doe, age=25}")); + } + + @Test + public void whenPlayerToString_shouldCallParentToString() throws Exception { + User user = new Player(12L, "John Doe", 25); + + Assert.assertThat(user.toString(), equalTo("User{id=12, name=John Doe, age=25}")); + } + + @Test + public void whenAdministratorToString_shouldExecuteAdministratorToString() throws Exception { + User user = new Administrator(12L, "John Doe", 25); + + Assert.assertThat(user.toString(), equalTo("Administrator{id=12, name=John Doe, age=25}")); + } + + @Test + public void whenFilteringByAge_shouldFilterOnlyAdultUsers() throws Exception { + List users = new ArrayList<>(); + users.add(new User(1L, "John", 45)); + users.add(new User(2L, "Michael", 27)); + users.add(new User(3L, "Max", 16)); + users.add(new User(4L, "Bob", 10)); + users.add(new User(5L, "Bill", 65)); + + Predicate byAge = input -> input.getAge() > ADULT_AGE; + + List results = FluentIterable.from(users) + .filter(byAge) + .transform(Functions.toStringFunction()) + .toList(); + + Assert.assertThat(results.size(), equalTo(3)); + } + + @Test + public void whenCreatingFluentIterableFromArray_shouldContainAllUsers() throws Exception { + User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)} ; + FluentIterable users = FluentIterable.of(usersArray); + + Assert.assertThat(users.size(), equalTo(2)); + } + + @Test + public void whenAppendingElementsToFluentIterable_shouldContainAllUsers() throws Exception { + User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; + + FluentIterable users = FluentIterable.of(usersArray).append( + new User(3L, "Bob", 23), + new User(4L, "Bill", 17) + ); + + Assert.assertThat(users.size(), equalTo(4)); + } + + @Test + public void whenAppendingListToFluentIterable_shouldContainAllUsers() throws Exception { + User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; + + List usersList = new ArrayList<>(); + usersList.add(new User(3L, "David", 32)); + + FluentIterable users = FluentIterable.of(usersArray).append(usersList); + + Assert.assertThat(users.size(), equalTo(3)); + } + + @Test + public void whenJoiningFluentIterableElements_shouldOutputAllUsers() throws Exception { + User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; + + FluentIterable users = FluentIterable.of(usersArray); + + Assert.assertThat(users.join(Joiner.on("; ")), + equalTo("User{id=1, name=John, age=45}; User{id=2, name=Max, age=15}")); + } + + @Test + public void whenHashingData_shouldReturnCorrectHashCode() throws Exception { + int receivedData = 123; + + HashCode hashCode = Hashing.crc32c().hashInt(receivedData); + Assert.assertThat(hashCode.toString(), equalTo("495be649")); + } + + @Test + public void whenDecrementingIpAddress_shouldReturnOneLessIpAddress() throws Exception { + InetAddress address = InetAddress.getByName("127.0.0.5"); + InetAddress decrementedAddress = InetAddresses.decrement(address); + + Assert.assertThat(decrementedAddress.toString(), equalTo("/127.0.0.4")); + + } + + @Test + public void whenExecutingRunnableInThread_shouldLogThreadExecution() throws Exception { + ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); + Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true); + + Thread t = new Thread(logThreadRun); + t.run(); + + Assert.assertTrue(threadExecutions.get("main")); + } + + @Test + public void whenExecutingRunnableInThreadPool_shouldLogAllThreadsExecutions() throws Exception { + ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); + + Runnable logThreadRun= () -> threadExecutions.put(Thread.currentThread().getName(), true); + + ExecutorService executorService = Executors.newFixedThreadPool(2); + executorService.submit(logThreadRun); + executorService.submit(logThreadRun); + executorService.shutdown(); + + executorService.awaitTermination(100, TimeUnit.MILLISECONDS); + + Assert.assertTrue(threadExecutions.get("pool-1-thread-1")); + Assert.assertTrue(threadExecutions.get("pool-1-thread-2")); + } + + @Test + public void whenExecutingRunnableInDirectExecutor_shouldLogThreadExecution() throws Exception { + ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); + + Runnable logThreadRun= () -> threadExecutions.put(Thread.currentThread().getName(), true); + + Executor executor = MoreExecutors.directExecutor(); + executor.execute(logThreadRun); + + Assert.assertTrue(threadExecutions.get("main")); + } + + @Test + public void whenExecutingRunnableInListeningExecutor_shouldLogThreadExecution() throws Exception { + ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); + + Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true); + + ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); + executor.execute(logThreadRun); + + Assert.assertTrue(threadExecutions.get("main")); + } +} \ No newline at end of file From b4738d8cd6440a0d2710d2d176c98b7cd5dfe6ad Mon Sep 17 00:00:00 2001 From: oborkovskyi Date: Mon, 21 Dec 2015 13:40:09 +0200 Subject: [PATCH 2/3] Removed extra files --- guava18/.classpath | 36 ------------------- ...e.wst.jsdt.core.javascriptValidator.launch | 7 ---- guava18/.gitignore | 13 ------- guava18/.project | 36 ------------------- guava18/.springBeans | 14 -------- 5 files changed, 106 deletions(-) delete mode 100644 guava18/.classpath delete mode 100644 guava18/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch delete mode 100644 guava18/.gitignore delete mode 100644 guava18/.project delete mode 100644 guava18/.springBeans diff --git a/guava18/.classpath b/guava18/.classpath deleted file mode 100644 index 8ebf6d9c31..0000000000 --- a/guava18/.classpath +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/guava18/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/guava18/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch deleted file mode 100644 index 627021fb96..0000000000 --- a/guava18/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/guava18/.gitignore b/guava18/.gitignore deleted file mode 100644 index 83c05e60c8..0000000000 --- a/guava18/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -*.class - -#folders# -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* - -# Packaged files # -*.jar -*.war -*.ear \ No newline at end of file diff --git a/guava18/.project b/guava18/.project deleted file mode 100644 index 829dc83809..0000000000 --- a/guava18/.project +++ /dev/null @@ -1,36 +0,0 @@ - - - guava - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.wst.common.project.facet.core.builder - - - - - org.eclipse.wst.validation.validationbuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.jem.workbench.JavaEMFNature - org.eclipse.wst.common.modulecore.ModuleCoreNature - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - org.eclipse.wst.common.project.facet.core.nature - - diff --git a/guava18/.springBeans b/guava18/.springBeans deleted file mode 100644 index a79097f40d..0000000000 --- a/guava18/.springBeans +++ /dev/null @@ -1,14 +0,0 @@ - - - 1 - - - - - - - src/main/webapp/WEB-INF/api-servlet.xml - - - - From 6c83190bdc457d5e39e95d1cacf0f8c016778c9f Mon Sep 17 00:00:00 2001 From: oborkovskyi Date: Mon, 21 Dec 2015 13:52:26 +0200 Subject: [PATCH 3/3] Splitted test to grouped by methods used tests. --- .../baeldung/guava/FluentIterableTest.java | 80 ++++++++ .../baeldung/guava/GuavaMiscUtilsTest.java | 41 ++++ .../java/com/baeldung/guava/GuavaTest.java | 177 ------------------ .../com/baeldung/guava/MoreExecutorsTest.java | 51 +++++ .../com/baeldung/guava/MoreObjectsTest.java | 33 ++++ 5 files changed, 205 insertions(+), 177 deletions(-) create mode 100644 guava18/src/test/java/com/baeldung/guava/FluentIterableTest.java create mode 100644 guava18/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java delete mode 100644 guava18/src/test/java/com/baeldung/guava/GuavaTest.java create mode 100644 guava18/src/test/java/com/baeldung/guava/MoreExecutorsTest.java create mode 100644 guava18/src/test/java/com/baeldung/guava/MoreObjectsTest.java diff --git a/guava18/src/test/java/com/baeldung/guava/FluentIterableTest.java b/guava18/src/test/java/com/baeldung/guava/FluentIterableTest.java new file mode 100644 index 0000000000..82552fca8f --- /dev/null +++ b/guava18/src/test/java/com/baeldung/guava/FluentIterableTest.java @@ -0,0 +1,80 @@ +package com.baeldung.guava; + +import com.baeldung.guava.entity.User; +import com.google.common.base.Functions; +import com.google.common.base.Joiner; +import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class FluentIterableTest { + + private static final int ADULT_AGE = 18; + + @Test + public void whenFilteringByAge_shouldFilterOnlyAdultUsers() throws Exception { + List users = new ArrayList<>(); + users.add(new User(1L, "John", 45)); + users.add(new User(2L, "Michael", 27)); + users.add(new User(3L, "Max", 16)); + users.add(new User(4L, "Bob", 10)); + users.add(new User(5L, "Bill", 65)); + + Predicate byAge = input -> input.getAge() > ADULT_AGE; + + List results = FluentIterable.from(users) + .filter(byAge) + .transform(Functions.toStringFunction()) + .toList(); + + Assert.assertThat(results.size(), equalTo(3)); + } + + @Test + public void whenCreatingFluentIterableFromArray_shouldContainAllUsers() throws Exception { + User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; + FluentIterable users = FluentIterable.of(usersArray); + + Assert.assertThat(users.size(), equalTo(2)); + } + + @Test + public void whenAppendingElementsToFluentIterable_shouldContainAllUsers() throws Exception { + User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; + + FluentIterable users = FluentIterable.of(usersArray).append( + new User(3L, "Bob", 23), + new User(4L, "Bill", 17) + ); + + Assert.assertThat(users.size(), equalTo(4)); + } + + @Test + public void whenAppendingListToFluentIterable_shouldContainAllUsers() throws Exception { + User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; + + List usersList = new ArrayList<>(); + usersList.add(new User(3L, "David", 32)); + + FluentIterable users = FluentIterable.of(usersArray).append(usersList); + + Assert.assertThat(users.size(), equalTo(3)); + } + + @Test + public void whenJoiningFluentIterableElements_shouldOutputAllUsers() throws Exception { + User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; + + FluentIterable users = FluentIterable.of(usersArray); + + Assert.assertThat(users.join(Joiner.on("; ")), + equalTo("User{id=1, name=John, age=45}; User{id=2, name=Max, age=15}")); + } +} \ No newline at end of file diff --git a/guava18/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java b/guava18/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java new file mode 100644 index 0000000000..db82ea6da8 --- /dev/null +++ b/guava18/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java @@ -0,0 +1,41 @@ +package com.baeldung.guava; + +import com.google.common.hash.HashCode; +import com.google.common.hash.Hashing; +import com.google.common.net.InetAddresses; +import org.junit.Assert; +import org.junit.Test; + +import java.net.InetAddress; +import java.util.concurrent.ConcurrentHashMap; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class GuavaMiscUtilsTest { + @Test + public void whenHashingData_shouldReturnCorrectHashCode() throws Exception { + int receivedData = 123; + + HashCode hashCode = Hashing.crc32c().hashInt(receivedData); + Assert.assertThat(hashCode.toString(), equalTo("495be649")); + } + + @Test + public void whenDecrementingIpAddress_shouldReturnOneLessIpAddress() throws Exception { + InetAddress address = InetAddress.getByName("127.0.0.5"); + InetAddress decrementedAddress = InetAddresses.decrement(address); + + Assert.assertThat(decrementedAddress.toString(), equalTo("/127.0.0.4")); + } + + @Test + public void whenExecutingRunnableInThread_shouldLogThreadExecution() throws Exception { + ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); + Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true); + + Thread t = new Thread(logThreadRun); + t.run(); + + Assert.assertTrue(threadExecutions.get("main")); + } +} diff --git a/guava18/src/test/java/com/baeldung/guava/GuavaTest.java b/guava18/src/test/java/com/baeldung/guava/GuavaTest.java deleted file mode 100644 index 5d1d1c074a..0000000000 --- a/guava18/src/test/java/com/baeldung/guava/GuavaTest.java +++ /dev/null @@ -1,177 +0,0 @@ -import com.baeldung.guava.entity.Administrator; -import com.baeldung.guava.entity.Player; -import com.baeldung.guava.entity.User; -import com.google.common.base.Functions; -import com.google.common.base.Joiner; -import com.google.common.base.Predicate; -import com.google.common.collect.FluentIterable; -import com.google.common.hash.HashCode; -import com.google.common.hash.Hashing; -import com.google.common.net.InetAddresses; -import com.google.common.util.concurrent.ListeningExecutorService; -import com.google.common.util.concurrent.MoreExecutors; -import org.junit.Assert; -import org.junit.Test; - -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.*; - -import static org.hamcrest.CoreMatchers.equalTo; - -public class GuavaTest { - - private static final int ADULT_AGE = 18; - - @Test - public void whenToString_shouldIncludeAllFields() throws Exception { - User user = new User(12L, "John Doe", 25); - - Assert.assertThat(user.toString(), equalTo("User{id=12, name=John Doe, age=25}")); - } - - @Test - public void whenPlayerToString_shouldCallParentToString() throws Exception { - User user = new Player(12L, "John Doe", 25); - - Assert.assertThat(user.toString(), equalTo("User{id=12, name=John Doe, age=25}")); - } - - @Test - public void whenAdministratorToString_shouldExecuteAdministratorToString() throws Exception { - User user = new Administrator(12L, "John Doe", 25); - - Assert.assertThat(user.toString(), equalTo("Administrator{id=12, name=John Doe, age=25}")); - } - - @Test - public void whenFilteringByAge_shouldFilterOnlyAdultUsers() throws Exception { - List users = new ArrayList<>(); - users.add(new User(1L, "John", 45)); - users.add(new User(2L, "Michael", 27)); - users.add(new User(3L, "Max", 16)); - users.add(new User(4L, "Bob", 10)); - users.add(new User(5L, "Bill", 65)); - - Predicate byAge = input -> input.getAge() > ADULT_AGE; - - List results = FluentIterable.from(users) - .filter(byAge) - .transform(Functions.toStringFunction()) - .toList(); - - Assert.assertThat(results.size(), equalTo(3)); - } - - @Test - public void whenCreatingFluentIterableFromArray_shouldContainAllUsers() throws Exception { - User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)} ; - FluentIterable users = FluentIterable.of(usersArray); - - Assert.assertThat(users.size(), equalTo(2)); - } - - @Test - public void whenAppendingElementsToFluentIterable_shouldContainAllUsers() throws Exception { - User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; - - FluentIterable users = FluentIterable.of(usersArray).append( - new User(3L, "Bob", 23), - new User(4L, "Bill", 17) - ); - - Assert.assertThat(users.size(), equalTo(4)); - } - - @Test - public void whenAppendingListToFluentIterable_shouldContainAllUsers() throws Exception { - User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; - - List usersList = new ArrayList<>(); - usersList.add(new User(3L, "David", 32)); - - FluentIterable users = FluentIterable.of(usersArray).append(usersList); - - Assert.assertThat(users.size(), equalTo(3)); - } - - @Test - public void whenJoiningFluentIterableElements_shouldOutputAllUsers() throws Exception { - User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)}; - - FluentIterable users = FluentIterable.of(usersArray); - - Assert.assertThat(users.join(Joiner.on("; ")), - equalTo("User{id=1, name=John, age=45}; User{id=2, name=Max, age=15}")); - } - - @Test - public void whenHashingData_shouldReturnCorrectHashCode() throws Exception { - int receivedData = 123; - - HashCode hashCode = Hashing.crc32c().hashInt(receivedData); - Assert.assertThat(hashCode.toString(), equalTo("495be649")); - } - - @Test - public void whenDecrementingIpAddress_shouldReturnOneLessIpAddress() throws Exception { - InetAddress address = InetAddress.getByName("127.0.0.5"); - InetAddress decrementedAddress = InetAddresses.decrement(address); - - Assert.assertThat(decrementedAddress.toString(), equalTo("/127.0.0.4")); - - } - - @Test - public void whenExecutingRunnableInThread_shouldLogThreadExecution() throws Exception { - ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); - Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true); - - Thread t = new Thread(logThreadRun); - t.run(); - - Assert.assertTrue(threadExecutions.get("main")); - } - - @Test - public void whenExecutingRunnableInThreadPool_shouldLogAllThreadsExecutions() throws Exception { - ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); - - Runnable logThreadRun= () -> threadExecutions.put(Thread.currentThread().getName(), true); - - ExecutorService executorService = Executors.newFixedThreadPool(2); - executorService.submit(logThreadRun); - executorService.submit(logThreadRun); - executorService.shutdown(); - - executorService.awaitTermination(100, TimeUnit.MILLISECONDS); - - Assert.assertTrue(threadExecutions.get("pool-1-thread-1")); - Assert.assertTrue(threadExecutions.get("pool-1-thread-2")); - } - - @Test - public void whenExecutingRunnableInDirectExecutor_shouldLogThreadExecution() throws Exception { - ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); - - Runnable logThreadRun= () -> threadExecutions.put(Thread.currentThread().getName(), true); - - Executor executor = MoreExecutors.directExecutor(); - executor.execute(logThreadRun); - - Assert.assertTrue(threadExecutions.get("main")); - } - - @Test - public void whenExecutingRunnableInListeningExecutor_shouldLogThreadExecution() throws Exception { - ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); - - Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true); - - ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); - executor.execute(logThreadRun); - - Assert.assertTrue(threadExecutions.get("main")); - } -} \ No newline at end of file diff --git a/guava18/src/test/java/com/baeldung/guava/MoreExecutorsTest.java b/guava18/src/test/java/com/baeldung/guava/MoreExecutorsTest.java new file mode 100644 index 0000000000..ec173499f1 --- /dev/null +++ b/guava18/src/test/java/com/baeldung/guava/MoreExecutorsTest.java @@ -0,0 +1,51 @@ +package com.baeldung.guava; + +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.*; + +public class MoreExecutorsTest { + @Test + public void whenExecutingRunnableInThreadPool_shouldLogAllThreadsExecutions() throws Exception { + ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); + + Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true); + + ExecutorService executorService = Executors.newFixedThreadPool(2); + executorService.submit(logThreadRun); + executorService.submit(logThreadRun); + executorService.shutdown(); + + executorService.awaitTermination(100, TimeUnit.MILLISECONDS); + + Assert.assertTrue(threadExecutions.get("pool-1-thread-1")); + Assert.assertTrue(threadExecutions.get("pool-1-thread-2")); + } + + @Test + public void whenExecutingRunnableInDirectExecutor_shouldLogThreadExecution() throws Exception { + ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); + + Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true); + + Executor executor = MoreExecutors.directExecutor(); + executor.execute(logThreadRun); + + Assert.assertTrue(threadExecutions.get("main")); + } + + @Test + public void whenExecutingRunnableInListeningExecutor_shouldLogThreadExecution() throws Exception { + ConcurrentHashMap threadExecutions = new ConcurrentHashMap<>(); + + Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true); + + ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); + executor.execute(logThreadRun); + + Assert.assertTrue(threadExecutions.get("main")); + } +} diff --git a/guava18/src/test/java/com/baeldung/guava/MoreObjectsTest.java b/guava18/src/test/java/com/baeldung/guava/MoreObjectsTest.java new file mode 100644 index 0000000000..5e46b5aba9 --- /dev/null +++ b/guava18/src/test/java/com/baeldung/guava/MoreObjectsTest.java @@ -0,0 +1,33 @@ +package com.baeldung.guava; + +import com.baeldung.guava.entity.Administrator; +import com.baeldung.guava.entity.Player; +import com.baeldung.guava.entity.User; +import org.junit.Assert; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class MoreObjectsTest { + + @Test + public void whenToString_shouldIncludeAllFields() throws Exception { + User user = new User(12L, "John Doe", 25); + + Assert.assertThat(user.toString(), equalTo("User{id=12, name=John Doe, age=25}")); + } + + @Test + public void whenPlayerToString_shouldCallParentToString() throws Exception { + User user = new Player(12L, "John Doe", 25); + + Assert.assertThat(user.toString(), equalTo("User{id=12, name=John Doe, age=25}")); + } + + @Test + public void whenAdministratorToString_shouldExecuteAdministratorToString() throws Exception { + User user = new Administrator(12L, "John Doe", 25); + + Assert.assertThat(user.toString(), equalTo("Administrator{id=12, name=John Doe, age=25}")); + } +}