Skip to content

Commit

Permalink
Merge pull request #331 from fluxw42/master
Browse files Browse the repository at this point in the history
Add unit tests for remaining patterns
  • Loading branch information
iluwatar committed Jan 1, 2016
2 parents f987c72 + 542a832 commit 30e09a8
Show file tree
Hide file tree
Showing 111 changed files with 4,413 additions and 134 deletions.
29 changes: 29 additions & 0 deletions multiton/src/test/java/com/iluwatar/multiton/NazgulTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.iluwatar.multiton;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;

/**
* Date: 12/22/15 - 22:28 AM
*
* @author Jeroen Meulemeester
*/
public class NazgulTest {

/**
* Verify if {@link Nazgul#getInstance(NazgulName)} returns the correct Nazgul multiton instance
*/
@Test
public void testGetInstance() {
for (final NazgulName name : NazgulName.values()) {
final Nazgul nazgul = Nazgul.getInstance(name);
assertNotNull(nazgul);
assertSame(nazgul, Nazgul.getInstance(name));
assertEquals(name, nazgul.getName());
}
}

}
5 changes: 5 additions & 0 deletions null-object/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.iluwatar.nullobject;

import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

/**
* Date: 12/26/15 - 11:47 PM
*
* @author Jeroen Meulemeester
*/
public class NullNodeTest extends StdOutTest {

/**
* Verify if {@link NullNode#getInstance()} actually returns the same object instance
*/
@Test
public void testGetInstance() {
final NullNode instance = NullNode.getInstance();
assertNotNull(instance);
assertSame(instance, NullNode.getInstance());
}

@Test
public void testFields() {
final NullNode node = NullNode.getInstance();
assertEquals(0, node.getTreeSize());
assertNull(node.getName());
assertNull(node.getLeft());
assertNull(node.getRight());
}

@Test
public void testWalk() throws Exception {
NullNode.getInstance().walk();
Mockito.verifyZeroInteractions(getStdOutMock());
}

}
54 changes: 54 additions & 0 deletions null-object/src/test/java/com/iluwatar/nullobject/StdOutTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.iluwatar.nullobject;

import org.junit.After;
import org.junit.Before;

import java.io.PrintStream;

import static org.mockito.Mockito.mock;

/**
* Date: 12/10/15 - 8:37 PM
*
* @author Jeroen Meulemeester
*/
public abstract class StdOutTest {

/**
* The mocked standard out {@link PrintStream}, required since walking through the tree has no
* influence on any other accessible object, except for writing to std-out using {@link
* System#out}
*/
private final PrintStream stdOutMock = mock(PrintStream.class);

/**
* Keep the original std-out so it can be restored after the test
*/
private final PrintStream stdOutOrig = System.out;

/**
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
*/
@Before
public void setUp() {
System.setOut(this.stdOutMock);
}

/**
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
*/
@After
public void tearDown() {
System.setOut(this.stdOutOrig);
}

/**
* Get the mocked stdOut {@link PrintStream}
*
* @return The stdOut print stream mock, renewed before each test
*/
final PrintStream getStdOutMock() {
return this.stdOutMock;
}

}
101 changes: 101 additions & 0 deletions null-object/src/test/java/com/iluwatar/nullobject/TreeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.iluwatar.nullobject;

import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;

/**
* Date: 12/26/15 - 11:44 PM
*
* @author Jeroen Meulemeester
*/
public class TreeTest extends StdOutTest {

/**
* During the tests, the same tree structure will be used, shown below. End points will be
* terminated with the {@link NullNode} instance.
*
* <pre>
* root
* ├── level1_a
* │   ├── level2_a
* │   │   ├── level3_a
* │   │   └── level3_b
* │   └── level2_b
* └── level1_b
* </pre>
*/
private static final Node TREE_ROOT;

static {
final NodeImpl level1B = new NodeImpl("level1_b", NullNode.getInstance(), NullNode.getInstance());
final NodeImpl level2B = new NodeImpl("level2_b", NullNode.getInstance(), NullNode.getInstance());
final NodeImpl level3A = new NodeImpl("level3_a", NullNode.getInstance(), NullNode.getInstance());
final NodeImpl level3B = new NodeImpl("level3_b", NullNode.getInstance(), NullNode.getInstance());
final NodeImpl level2A = new NodeImpl("level2_a", level3A, level3B);
final NodeImpl level1A = new NodeImpl("level1_a", level2A, level2B);
TREE_ROOT = new NodeImpl("root", level1A, level1B);
}

/**
* Verify the number of items in the tree. The root has 6 children so we expect a {@link
* Node#getTreeSize()} of 7 {@link Node}s in total.
*/
@Test
public void testTreeSize() {
assertEquals(7, TREE_ROOT.getTreeSize());
}

/**
* Walk through the tree and verify if every item is handled
*/
@Test
public void testWalk() {
TREE_ROOT.walk();

final InOrder inOrder = Mockito.inOrder(getStdOutMock());
inOrder.verify(getStdOutMock()).println("root");
inOrder.verify(getStdOutMock()).println("level1_a");
inOrder.verify(getStdOutMock()).println("level2_a");
inOrder.verify(getStdOutMock()).println("level3_a");
inOrder.verify(getStdOutMock()).println("level3_b");
inOrder.verify(getStdOutMock()).println("level2_b");
inOrder.verify(getStdOutMock()).println("level1_b");
inOrder.verifyNoMoreInteractions();
}

@Test
public void testGetLeft() throws Exception {
final Node level1 = TREE_ROOT.getLeft();
assertNotNull(level1);
assertEquals("level1_a", level1.getName());
assertEquals(5, level1.getTreeSize());

final Node level2 = level1.getLeft();
assertNotNull(level2);
assertEquals("level2_a", level2.getName());
assertEquals(3, level2.getTreeSize());

final Node level3 = level2.getLeft();
assertNotNull(level3);
assertEquals("level3_a", level3.getName());
assertEquals(1, level3.getTreeSize());
assertSame(NullNode.getInstance(), level3.getRight());
assertSame(NullNode.getInstance(), level3.getLeft());
}

@Test
public void testGetRight() throws Exception {
final Node level1 = TREE_ROOT.getRight();
assertNotNull(level1);
assertEquals("level1_b", level1.getName());
assertEquals(1, level1.getTreeSize());
assertSame(NullNode.getInstance(), level1.getRight());
assertSame(NullNode.getInstance(), level1.getLeft());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.iluwatar.object.pool;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

/**
* Date: 12/27/15 - 1:05 AM
*
* @author Jeroen Meulemeester
*/
public class OliphauntPoolTest {

/**
* Use the same object 100 times subsequently. This should not take much time since the heavy
* object instantiation is done only once. Verify if we get the same object each time.
*/
@Test(timeout = 5000)
public void testSubsequentCheckinCheckout() {
final OliphauntPool pool = new OliphauntPool();
assertEquals(pool.toString(), "Pool available=0 inUse=0");

final Oliphaunt expectedOliphaunt = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=1");

pool.checkIn(expectedOliphaunt);
assertEquals(pool.toString(), "Pool available=1 inUse=0");

for (int i = 0; i < 100; i++) {
final Oliphaunt oliphaunt = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=1");
assertSame(expectedOliphaunt, oliphaunt);
assertEquals(expectedOliphaunt.getId(), oliphaunt.getId());
assertEquals(expectedOliphaunt.toString(), oliphaunt.toString());

pool.checkIn(oliphaunt);
assertEquals(pool.toString(), "Pool available=1 inUse=0");
}

}

/**
* Use the same object 100 times subsequently. This should not take much time since the heavy
* object instantiation is done only once. Verify if we get the same object each time.
*/
@Test(timeout = 5000)
public void testConcurrentCheckinCheckout() {
final OliphauntPool pool = new OliphauntPool();
assertEquals(pool.toString(), "Pool available=0 inUse=0");

final Oliphaunt firstOliphaunt = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=1");

final Oliphaunt secondOliphaunt = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=2");

assertNotSame(firstOliphaunt, secondOliphaunt);
assertEquals(firstOliphaunt.getId() + 1, secondOliphaunt.getId());

// After checking in the second, we should get the same when checking out a new oliphaunt ...
pool.checkIn(secondOliphaunt);
assertEquals(pool.toString(), "Pool available=1 inUse=1");

final Oliphaunt oliphaunt3 = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=2");
assertSame(secondOliphaunt, oliphaunt3);

// ... and the same applies for the first one
pool.checkIn(firstOliphaunt);
assertEquals(pool.toString(), "Pool available=1 inUse=1");

final Oliphaunt oliphaunt4 = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=2");
assertSame(firstOliphaunt, oliphaunt4);

// When both oliphaunt return to the pool, we should still get the same instances
pool.checkIn(firstOliphaunt);
assertEquals(pool.toString(), "Pool available=1 inUse=1");

pool.checkIn(secondOliphaunt);
assertEquals(pool.toString(), "Pool available=2 inUse=0");

// The order of the returned instances is not determined, so just put them in a list
// and verify if both expected instances are in there.
final List<Oliphaunt> oliphaunts = Arrays.asList(pool.checkOut(), pool.checkOut());
assertEquals(pool.toString(), "Pool available=0 inUse=2");
assertTrue(oliphaunts.contains(firstOliphaunt));
assertTrue(oliphaunts.contains(secondOliphaunt));

}


}
5 changes: 5 additions & 0 deletions observer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public void addObserver(O observer) {
this.observers.add(observer);
}

public void removeObserver(O observer) {
this.observers.remove(observer);
}

/**
* Notify observers
*/
Expand Down
Loading

0 comments on commit 30e09a8

Please sign in to comment.