Skip to content

Commit 41910d7

Browse files
committed
Merge pull request eugenp#311 from aborkov/master
Added guava 18 test project
2 parents 5c27cc6 + 6c83190 commit 41910d7

9 files changed

Lines changed: 321 additions & 0 deletions

File tree

‎guava18/README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=========
2+
3+
## Guava and Hamcrest Cookbooks and Examples
4+
5+
6+
### Relevant Articles:
7+
- [Guava Collections Cookbook](http://www.baeldung.com/guava-collections)
8+
- [Guava Ordering Cookbook](http://www.baeldung.com/guava-order)
9+
- [Guava Functional Cookbook](http://www.baeldung.com/guava-functions-predicates)
10+
11+
- [Hamcrest Collections Cookbook](http://www.baeldung.com/hamcrest-collections-arrays)
12+
13+
- [Partition a List in Java](http://www.baeldung.com/java-list-split)

‎guava18/pom.xml‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>guava</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.google.guava</groupId>
14+
<artifactId>guava</artifactId>
15+
<version>18.0</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<artifactId>maven-compiler-plugin</artifactId>
27+
<version>3.3</version>
28+
<configuration>
29+
<debug>true</debug>
30+
<optimize>true</optimize>
31+
<source>1.8</source>
32+
<target>1.8</target>
33+
<encoding>UTF-8</encoding>
34+
<showDeprecation>true</showDeprecation>
35+
<showWarnings>true</showWarnings>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
41+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.guava.entity;
2+
3+
import com.google.common.base.MoreObjects;
4+
5+
public class Administrator extends User{
6+
public Administrator(long id, String name, int age) {
7+
super(id, name, age);
8+
}
9+
10+
@Override
11+
public String toString() {
12+
return MoreObjects.toStringHelper(this)
13+
.add("id", getId())
14+
.add("name", getName())
15+
.add("age", getAge())
16+
.toString();
17+
}
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.guava.entity;
2+
3+
public class Player extends User{
4+
public Player(long id, String name, int age) {
5+
super(id, name, age);
6+
}
7+
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.guava.entity;
2+
3+
import com.google.common.base.MoreObjects;
4+
5+
public class User{
6+
private long id;
7+
private String name;
8+
private int age;
9+
10+
public User(long id, String name, int age) {
11+
this.id = id;
12+
this.name = name;
13+
this.age = age;
14+
}
15+
16+
public long getId() {
17+
return id;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public int getAge() {
25+
return age;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return MoreObjects.toStringHelper(User.class)
31+
.add("id", id)
32+
.add("name", name)
33+
.add("age", age)
34+
.toString();
35+
}
36+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.guava;
2+
3+
import com.baeldung.guava.entity.User;
4+
import com.google.common.base.Functions;
5+
import com.google.common.base.Joiner;
6+
import com.google.common.base.Predicate;
7+
import com.google.common.collect.FluentIterable;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
import static org.hamcrest.CoreMatchers.equalTo;
15+
16+
public class FluentIterableTest {
17+
18+
private static final int ADULT_AGE = 18;
19+
20+
@Test
21+
public void whenFilteringByAge_shouldFilterOnlyAdultUsers() throws Exception {
22+
List<User> users = new ArrayList<>();
23+
users.add(new User(1L, "John", 45));
24+
users.add(new User(2L, "Michael", 27));
25+
users.add(new User(3L, "Max", 16));
26+
users.add(new User(4L, "Bob", 10));
27+
users.add(new User(5L, "Bill", 65));
28+
29+
Predicate<User> byAge = input -> input.getAge() > ADULT_AGE;
30+
31+
List<String> results = FluentIterable.from(users)
32+
.filter(byAge)
33+
.transform(Functions.toStringFunction())
34+
.toList();
35+
36+
Assert.assertThat(results.size(), equalTo(3));
37+
}
38+
39+
@Test
40+
public void whenCreatingFluentIterableFromArray_shouldContainAllUsers() throws Exception {
41+
User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)};
42+
FluentIterable<User> users = FluentIterable.of(usersArray);
43+
44+
Assert.assertThat(users.size(), equalTo(2));
45+
}
46+
47+
@Test
48+
public void whenAppendingElementsToFluentIterable_shouldContainAllUsers() throws Exception {
49+
User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)};
50+
51+
FluentIterable<User> users = FluentIterable.of(usersArray).append(
52+
new User(3L, "Bob", 23),
53+
new User(4L, "Bill", 17)
54+
);
55+
56+
Assert.assertThat(users.size(), equalTo(4));
57+
}
58+
59+
@Test
60+
public void whenAppendingListToFluentIterable_shouldContainAllUsers() throws Exception {
61+
User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)};
62+
63+
List<User> usersList = new ArrayList<>();
64+
usersList.add(new User(3L, "David", 32));
65+
66+
FluentIterable<User> users = FluentIterable.of(usersArray).append(usersList);
67+
68+
Assert.assertThat(users.size(), equalTo(3));
69+
}
70+
71+
@Test
72+
public void whenJoiningFluentIterableElements_shouldOutputAllUsers() throws Exception {
73+
User[] usersArray = {new User(1L, "John", 45), new User(2L, "Max", 15)};
74+
75+
FluentIterable<User> users = FluentIterable.of(usersArray);
76+
77+
Assert.assertThat(users.join(Joiner.on("; ")),
78+
equalTo("User{id=1, name=John, age=45}; User{id=2, name=Max, age=15}"));
79+
}
80+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.guava;
2+
3+
import com.google.common.hash.HashCode;
4+
import com.google.common.hash.Hashing;
5+
import com.google.common.net.InetAddresses;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.net.InetAddress;
10+
import java.util.concurrent.ConcurrentHashMap;
11+
12+
import static org.hamcrest.CoreMatchers.equalTo;
13+
14+
public class GuavaMiscUtilsTest {
15+
@Test
16+
public void whenHashingData_shouldReturnCorrectHashCode() throws Exception {
17+
int receivedData = 123;
18+
19+
HashCode hashCode = Hashing.crc32c().hashInt(receivedData);
20+
Assert.assertThat(hashCode.toString(), equalTo("495be649"));
21+
}
22+
23+
@Test
24+
public void whenDecrementingIpAddress_shouldReturnOneLessIpAddress() throws Exception {
25+
InetAddress address = InetAddress.getByName("127.0.0.5");
26+
InetAddress decrementedAddress = InetAddresses.decrement(address);
27+
28+
Assert.assertThat(decrementedAddress.toString(), equalTo("/127.0.0.4"));
29+
}
30+
31+
@Test
32+
public void whenExecutingRunnableInThread_shouldLogThreadExecution() throws Exception {
33+
ConcurrentHashMap<String, Boolean> threadExecutions = new ConcurrentHashMap<>();
34+
Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true);
35+
36+
Thread t = new Thread(logThreadRun);
37+
t.run();
38+
39+
Assert.assertTrue(threadExecutions.get("main"));
40+
}
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.guava;
2+
3+
import com.google.common.util.concurrent.ListeningExecutorService;
4+
import com.google.common.util.concurrent.MoreExecutors;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.concurrent.*;
9+
10+
public class MoreExecutorsTest {
11+
@Test
12+
public void whenExecutingRunnableInThreadPool_shouldLogAllThreadsExecutions() throws Exception {
13+
ConcurrentHashMap<String, Boolean> threadExecutions = new ConcurrentHashMap<>();
14+
15+
Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true);
16+
17+
ExecutorService executorService = Executors.newFixedThreadPool(2);
18+
executorService.submit(logThreadRun);
19+
executorService.submit(logThreadRun);
20+
executorService.shutdown();
21+
22+
executorService.awaitTermination(100, TimeUnit.MILLISECONDS);
23+
24+
Assert.assertTrue(threadExecutions.get("pool-1-thread-1"));
25+
Assert.assertTrue(threadExecutions.get("pool-1-thread-2"));
26+
}
27+
28+
@Test
29+
public void whenExecutingRunnableInDirectExecutor_shouldLogThreadExecution() throws Exception {
30+
ConcurrentHashMap<String, Boolean> threadExecutions = new ConcurrentHashMap<>();
31+
32+
Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true);
33+
34+
Executor executor = MoreExecutors.directExecutor();
35+
executor.execute(logThreadRun);
36+
37+
Assert.assertTrue(threadExecutions.get("main"));
38+
}
39+
40+
@Test
41+
public void whenExecutingRunnableInListeningExecutor_shouldLogThreadExecution() throws Exception {
42+
ConcurrentHashMap<String, Boolean> threadExecutions = new ConcurrentHashMap<>();
43+
44+
Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true);
45+
46+
ListeningExecutorService executor = MoreExecutors.newDirectExecutorService();
47+
executor.execute(logThreadRun);
48+
49+
Assert.assertTrue(threadExecutions.get("main"));
50+
}
51+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.guava;
2+
3+
import com.baeldung.guava.entity.Administrator;
4+
import com.baeldung.guava.entity.Player;
5+
import com.baeldung.guava.entity.User;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import static org.hamcrest.CoreMatchers.equalTo;
10+
11+
public class MoreObjectsTest {
12+
13+
@Test
14+
public void whenToString_shouldIncludeAllFields() throws Exception {
15+
User user = new User(12L, "John Doe", 25);
16+
17+
Assert.assertThat(user.toString(), equalTo("User{id=12, name=John Doe, age=25}"));
18+
}
19+
20+
@Test
21+
public void whenPlayerToString_shouldCallParentToString() throws Exception {
22+
User user = new Player(12L, "John Doe", 25);
23+
24+
Assert.assertThat(user.toString(), equalTo("User{id=12, name=John Doe, age=25}"));
25+
}
26+
27+
@Test
28+
public void whenAdministratorToString_shouldExecuteAdministratorToString() throws Exception {
29+
User user = new Administrator(12L, "John Doe", 25);
30+
31+
Assert.assertThat(user.toString(), equalTo("Administrator{id=12, name=John Doe, age=25}"));
32+
}
33+
}

0 commit comments

Comments
 (0)