Skip to content

Commit 30e09a8

Browse files
committed
Merge pull request iluwatar#331 from fluxw42/master
Add unit tests for remaining patterns
2 parents f987c72 + 542a832 commit 30e09a8

File tree

111 files changed

+4413
-134
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+4413
-134
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.iluwatar.multiton;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertNotNull;
7+
import static org.junit.Assert.assertSame;
8+
9+
/**
10+
* Date: 12/22/15 - 22:28 AM
11+
*
12+
* @author Jeroen Meulemeester
13+
*/
14+
public class NazgulTest {
15+
16+
/**
17+
* Verify if {@link Nazgul#getInstance(NazgulName)} returns the correct Nazgul multiton instance
18+
*/
19+
@Test
20+
public void testGetInstance() {
21+
for (final NazgulName name : NazgulName.values()) {
22+
final Nazgul nazgul = Nazgul.getInstance(name);
23+
assertNotNull(nazgul);
24+
assertSame(nazgul, Nazgul.getInstance(name));
25+
assertEquals(name, nazgul.getName());
26+
}
27+
}
28+
29+
}

null-object/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
21+
</dependency>
1722
</dependencies>
1823
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.iluwatar.nullobject;
2+
3+
import org.junit.Test;
4+
import org.mockito.Mockito;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNotNull;
8+
import static org.junit.Assert.assertNull;
9+
import static org.junit.Assert.assertSame;
10+
11+
/**
12+
* Date: 12/26/15 - 11:47 PM
13+
*
14+
* @author Jeroen Meulemeester
15+
*/
16+
public class NullNodeTest extends StdOutTest {
17+
18+
/**
19+
* Verify if {@link NullNode#getInstance()} actually returns the same object instance
20+
*/
21+
@Test
22+
public void testGetInstance() {
23+
final NullNode instance = NullNode.getInstance();
24+
assertNotNull(instance);
25+
assertSame(instance, NullNode.getInstance());
26+
}
27+
28+
@Test
29+
public void testFields() {
30+
final NullNode node = NullNode.getInstance();
31+
assertEquals(0, node.getTreeSize());
32+
assertNull(node.getName());
33+
assertNull(node.getLeft());
34+
assertNull(node.getRight());
35+
}
36+
37+
@Test
38+
public void testWalk() throws Exception {
39+
NullNode.getInstance().walk();
40+
Mockito.verifyZeroInteractions(getStdOutMock());
41+
}
42+
43+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.iluwatar.nullobject;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
6+
import java.io.PrintStream;
7+
8+
import static org.mockito.Mockito.mock;
9+
10+
/**
11+
* Date: 12/10/15 - 8:37 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public abstract class StdOutTest {
16+
17+
/**
18+
* The mocked standard out {@link PrintStream}, required since walking through the tree has no
19+
* influence on any other accessible object, except for writing to std-out using {@link
20+
* System#out}
21+
*/
22+
private final PrintStream stdOutMock = mock(PrintStream.class);
23+
24+
/**
25+
* Keep the original std-out so it can be restored after the test
26+
*/
27+
private final PrintStream stdOutOrig = System.out;
28+
29+
/**
30+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
31+
*/
32+
@Before
33+
public void setUp() {
34+
System.setOut(this.stdOutMock);
35+
}
36+
37+
/**
38+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
39+
*/
40+
@After
41+
public void tearDown() {
42+
System.setOut(this.stdOutOrig);
43+
}
44+
45+
/**
46+
* Get the mocked stdOut {@link PrintStream}
47+
*
48+
* @return The stdOut print stream mock, renewed before each test
49+
*/
50+
final PrintStream getStdOutMock() {
51+
return this.stdOutMock;
52+
}
53+
54+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.iluwatar.nullobject;
2+
3+
import org.junit.Test;
4+
import org.mockito.InOrder;
5+
import org.mockito.Mockito;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNotNull;
9+
import static org.junit.Assert.assertSame;
10+
11+
/**
12+
* Date: 12/26/15 - 11:44 PM
13+
*
14+
* @author Jeroen Meulemeester
15+
*/
16+
public class TreeTest extends StdOutTest {
17+
18+
/**
19+
* During the tests, the same tree structure will be used, shown below. End points will be
20+
* terminated with the {@link NullNode} instance.
21+
*
22+
* <pre>
23+
* root
24+
* ├── level1_a
25+
* │   ├── level2_a
26+
* │   │   ├── level3_a
27+
* │   │   └── level3_b
28+
* │   └── level2_b
29+
* └── level1_b
30+
* </pre>
31+
*/
32+
private static final Node TREE_ROOT;
33+
34+
static {
35+
final NodeImpl level1B = new NodeImpl("level1_b", NullNode.getInstance(), NullNode.getInstance());
36+
final NodeImpl level2B = new NodeImpl("level2_b", NullNode.getInstance(), NullNode.getInstance());
37+
final NodeImpl level3A = new NodeImpl("level3_a", NullNode.getInstance(), NullNode.getInstance());
38+
final NodeImpl level3B = new NodeImpl("level3_b", NullNode.getInstance(), NullNode.getInstance());
39+
final NodeImpl level2A = new NodeImpl("level2_a", level3A, level3B);
40+
final NodeImpl level1A = new NodeImpl("level1_a", level2A, level2B);
41+
TREE_ROOT = new NodeImpl("root", level1A, level1B);
42+
}
43+
44+
/**
45+
* Verify the number of items in the tree. The root has 6 children so we expect a {@link
46+
* Node#getTreeSize()} of 7 {@link Node}s in total.
47+
*/
48+
@Test
49+
public void testTreeSize() {
50+
assertEquals(7, TREE_ROOT.getTreeSize());
51+
}
52+
53+
/**
54+
* Walk through the tree and verify if every item is handled
55+
*/
56+
@Test
57+
public void testWalk() {
58+
TREE_ROOT.walk();
59+
60+
final InOrder inOrder = Mockito.inOrder(getStdOutMock());
61+
inOrder.verify(getStdOutMock()).println("root");
62+
inOrder.verify(getStdOutMock()).println("level1_a");
63+
inOrder.verify(getStdOutMock()).println("level2_a");
64+
inOrder.verify(getStdOutMock()).println("level3_a");
65+
inOrder.verify(getStdOutMock()).println("level3_b");
66+
inOrder.verify(getStdOutMock()).println("level2_b");
67+
inOrder.verify(getStdOutMock()).println("level1_b");
68+
inOrder.verifyNoMoreInteractions();
69+
}
70+
71+
@Test
72+
public void testGetLeft() throws Exception {
73+
final Node level1 = TREE_ROOT.getLeft();
74+
assertNotNull(level1);
75+
assertEquals("level1_a", level1.getName());
76+
assertEquals(5, level1.getTreeSize());
77+
78+
final Node level2 = level1.getLeft();
79+
assertNotNull(level2);
80+
assertEquals("level2_a", level2.getName());
81+
assertEquals(3, level2.getTreeSize());
82+
83+
final Node level3 = level2.getLeft();
84+
assertNotNull(level3);
85+
assertEquals("level3_a", level3.getName());
86+
assertEquals(1, level3.getTreeSize());
87+
assertSame(NullNode.getInstance(), level3.getRight());
88+
assertSame(NullNode.getInstance(), level3.getLeft());
89+
}
90+
91+
@Test
92+
public void testGetRight() throws Exception {
93+
final Node level1 = TREE_ROOT.getRight();
94+
assertNotNull(level1);
95+
assertEquals("level1_b", level1.getName());
96+
assertEquals(1, level1.getTreeSize());
97+
assertSame(NullNode.getInstance(), level1.getRight());
98+
assertSame(NullNode.getInstance(), level1.getLeft());
99+
}
100+
101+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.iluwatar.object.pool;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNotSame;
10+
import static org.junit.Assert.assertSame;
11+
import static org.junit.Assert.assertTrue;
12+
13+
/**
14+
* Date: 12/27/15 - 1:05 AM
15+
*
16+
* @author Jeroen Meulemeester
17+
*/
18+
public class OliphauntPoolTest {
19+
20+
/**
21+
* Use the same object 100 times subsequently. This should not take much time since the heavy
22+
* object instantiation is done only once. Verify if we get the same object each time.
23+
*/
24+
@Test(timeout = 5000)
25+
public void testSubsequentCheckinCheckout() {
26+
final OliphauntPool pool = new OliphauntPool();
27+
assertEquals(pool.toString(), "Pool available=0 inUse=0");
28+
29+
final Oliphaunt expectedOliphaunt = pool.checkOut();
30+
assertEquals(pool.toString(), "Pool available=0 inUse=1");
31+
32+
pool.checkIn(expectedOliphaunt);
33+
assertEquals(pool.toString(), "Pool available=1 inUse=0");
34+
35+
for (int i = 0; i < 100; i++) {
36+
final Oliphaunt oliphaunt = pool.checkOut();
37+
assertEquals(pool.toString(), "Pool available=0 inUse=1");
38+
assertSame(expectedOliphaunt, oliphaunt);
39+
assertEquals(expectedOliphaunt.getId(), oliphaunt.getId());
40+
assertEquals(expectedOliphaunt.toString(), oliphaunt.toString());
41+
42+
pool.checkIn(oliphaunt);
43+
assertEquals(pool.toString(), "Pool available=1 inUse=0");
44+
}
45+
46+
}
47+
48+
/**
49+
* Use the same object 100 times subsequently. This should not take much time since the heavy
50+
* object instantiation is done only once. Verify if we get the same object each time.
51+
*/
52+
@Test(timeout = 5000)
53+
public void testConcurrentCheckinCheckout() {
54+
final OliphauntPool pool = new OliphauntPool();
55+
assertEquals(pool.toString(), "Pool available=0 inUse=0");
56+
57+
final Oliphaunt firstOliphaunt = pool.checkOut();
58+
assertEquals(pool.toString(), "Pool available=0 inUse=1");
59+
60+
final Oliphaunt secondOliphaunt = pool.checkOut();
61+
assertEquals(pool.toString(), "Pool available=0 inUse=2");
62+
63+
assertNotSame(firstOliphaunt, secondOliphaunt);
64+
assertEquals(firstOliphaunt.getId() + 1, secondOliphaunt.getId());
65+
66+
// After checking in the second, we should get the same when checking out a new oliphaunt ...
67+
pool.checkIn(secondOliphaunt);
68+
assertEquals(pool.toString(), "Pool available=1 inUse=1");
69+
70+
final Oliphaunt oliphaunt3 = pool.checkOut();
71+
assertEquals(pool.toString(), "Pool available=0 inUse=2");
72+
assertSame(secondOliphaunt, oliphaunt3);
73+
74+
// ... and the same applies for the first one
75+
pool.checkIn(firstOliphaunt);
76+
assertEquals(pool.toString(), "Pool available=1 inUse=1");
77+
78+
final Oliphaunt oliphaunt4 = pool.checkOut();
79+
assertEquals(pool.toString(), "Pool available=0 inUse=2");
80+
assertSame(firstOliphaunt, oliphaunt4);
81+
82+
// When both oliphaunt return to the pool, we should still get the same instances
83+
pool.checkIn(firstOliphaunt);
84+
assertEquals(pool.toString(), "Pool available=1 inUse=1");
85+
86+
pool.checkIn(secondOliphaunt);
87+
assertEquals(pool.toString(), "Pool available=2 inUse=0");
88+
89+
// The order of the returned instances is not determined, so just put them in a list
90+
// and verify if both expected instances are in there.
91+
final List<Oliphaunt> oliphaunts = Arrays.asList(pool.checkOut(), pool.checkOut());
92+
assertEquals(pool.toString(), "Pool available=0 inUse=2");
93+
assertTrue(oliphaunts.contains(firstOliphaunt));
94+
assertTrue(oliphaunts.contains(secondOliphaunt));
95+
96+
}
97+
98+
99+
}

observer/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
21+
</dependency>
1722
</dependencies>
1823
</project>

observer/src/main/java/com/iluwatar/observer/generic/Observable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public void addObserver(O observer) {
2222
this.observers.add(observer);
2323
}
2424

25+
public void removeObserver(O observer) {
26+
this.observers.remove(observer);
27+
}
28+
2529
/**
2630
* Notify observers
2731
*/

0 commit comments

Comments
 (0)