Added guava 18 test project
This commit is contained in:
parent
3e39bf59c8
commit
2228486d4a
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="owner.project.facets" value="java"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
|
||||
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_BUILDER_ENABLED" value="false"/>
|
||||
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_DISABLED_BUILDER" value="org.eclipse.wst.jsdt.core.javascriptValidator"/>
|
||||
<mapAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS"/>
|
||||
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
|
||||
</launchConfiguration>
|
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>guava</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
|
||||
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beansProjectDescription>
|
||||
<version>1</version>
|
||||
<pluginVersion><![CDATA[3.3.0.201307091516-RELEASE]]></pluginVersion>
|
||||
<configSuffixes>
|
||||
<configSuffix><![CDATA[xml]]></configSuffix>
|
||||
</configSuffixes>
|
||||
<enableImports><![CDATA[false]]></enableImports>
|
||||
<configs>
|
||||
<config>src/main/webapp/WEB-INF/api-servlet.xml</config>
|
||||
</configs>
|
||||
<configSets>
|
||||
</configSets>
|
||||
</beansProjectDescription>
|
|
@ -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)
|
|
@ -0,0 +1,41 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>18.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.3</version>
|
||||
<configuration>
|
||||
<debug>true</debug>
|
||||
<optimize>true</optimize>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
<showDeprecation>true</showDeprecation>
|
||||
<showWarnings>true</showWarnings>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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<User> 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<User> byAge = input -> input.getAge() > ADULT_AGE;
|
||||
|
||||
List<String> 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<User> 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<User> 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<User> usersList = new ArrayList<>();
|
||||
usersList.add(new User(3L, "David", 32));
|
||||
|
||||
FluentIterable<User> 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<User> 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<String, Boolean> 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<String, Boolean> 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<String, Boolean> 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<String, Boolean> threadExecutions = new ConcurrentHashMap<>();
|
||||
|
||||
Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true);
|
||||
|
||||
ListeningExecutorService executor = MoreExecutors.newDirectExecutorService();
|
||||
executor.execute(logThreadRun);
|
||||
|
||||
Assert.assertTrue(threadExecutions.get("main"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue