Skip to content

Commit 6c83190

Browse files
author
oborkovskyi
committed
Splitted test to grouped by methods used tests.
1 parent b4738d8 commit 6c83190

File tree

5 files changed

+205
-177
lines changed

5 files changed

+205
-177
lines changed
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+
}

guava18/src/test/java/com/baeldung/guava/GuavaTest.java

Lines changed: 0 additions & 177 deletions
This file was deleted.
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)