Skip to content

Commit 299d612

Browse files
committed
Added tests for proxy pattern
1 parent 9d4c315 commit 299d612

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

proxy/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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.iluwatar.proxy;
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 some actions don't have any
19+
* influence on accessible objects, except for writing to std-out using {@link System#out}
20+
*/
21+
private final PrintStream stdOutMock = mock(PrintStream.class);
22+
23+
/**
24+
* Keep the original std-out so it can be restored after the test
25+
*/
26+
private final PrintStream stdOutOrig = System.out;
27+
28+
/**
29+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
30+
*/
31+
@Before
32+
public void setUp() {
33+
System.setOut(this.stdOutMock);
34+
}
35+
36+
/**
37+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
38+
*/
39+
@After
40+
public void tearDown() {
41+
System.setOut(this.stdOutOrig);
42+
}
43+
44+
/**
45+
* Get the mocked stdOut {@link PrintStream}
46+
*
47+
* @return The stdOut print stream mock, renewed before each test
48+
*/
49+
final PrintStream getStdOutMock() {
50+
return this.stdOutMock;
51+
}
52+
53+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwatar.proxy;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Date: 12/28/15 - 9:02 PM
9+
*
10+
* @author Jeroen Meulemeester
11+
*/
12+
public class WizardTest {
13+
14+
@Test
15+
public void testToString() throws Exception {
16+
final String[] wizardNames = {"Gandalf", "Dumbledore", "Oz", "Merlin"};
17+
for (final String name : wizardNames) {
18+
assertEquals(name, new Wizard(name).toString());
19+
}
20+
}
21+
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar.proxy;
2+
3+
import org.junit.Test;
4+
import org.mockito.InOrder;
5+
6+
import static org.mockito.Mockito.inOrder;
7+
8+
/**
9+
* Date: 12/28/15 - 9:18 PM
10+
*
11+
* @author Jeroen Meulemeester
12+
*/
13+
public class WizardTowerProxyTest extends StdOutTest {
14+
15+
@Test
16+
public void testEnter() throws Exception {
17+
final Wizard[] wizards = new Wizard[]{
18+
new Wizard("Gandalf"),
19+
new Wizard("Dumbledore"),
20+
new Wizard("Oz"),
21+
new Wizard("Merlin")
22+
};
23+
24+
final WizardTowerProxy tower = new WizardTowerProxy();
25+
for (final Wizard wizard : wizards) {
26+
tower.enter(wizard);
27+
}
28+
29+
final InOrder inOrder = inOrder(getStdOutMock());
30+
inOrder.verify(getStdOutMock()).println("Gandalf enters the tower.");
31+
inOrder.verify(getStdOutMock()).println("Dumbledore enters the tower.");
32+
inOrder.verify(getStdOutMock()).println("Oz enters the tower.");
33+
inOrder.verify(getStdOutMock()).println("Merlin is not allowed to enter!");
34+
inOrder.verifyNoMoreInteractions();
35+
36+
}
37+
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar.proxy;
2+
3+
import org.junit.Test;
4+
import org.mockito.InOrder;
5+
6+
import static org.mockito.Mockito.inOrder;
7+
8+
/**
9+
* Date: 12/28/15 - 9:18 PM
10+
*
11+
* @author Jeroen Meulemeester
12+
*/
13+
public class WizardTowerTest extends StdOutTest {
14+
15+
@Test
16+
public void testEnter() throws Exception {
17+
final Wizard[] wizards = new Wizard[]{
18+
new Wizard("Gandalf"),
19+
new Wizard("Dumbledore"),
20+
new Wizard("Oz"),
21+
new Wizard("Merlin")
22+
};
23+
24+
final WizardTower tower = new WizardTower();
25+
for (final Wizard wizard : wizards) {
26+
tower.enter(wizard);
27+
}
28+
29+
final InOrder inOrder = inOrder(getStdOutMock());
30+
inOrder.verify(getStdOutMock()).println("Gandalf enters the tower.");
31+
inOrder.verify(getStdOutMock()).println("Dumbledore enters the tower.");
32+
inOrder.verify(getStdOutMock()).println("Oz enters the tower.");
33+
inOrder.verify(getStdOutMock()).println("Merlin enters the tower.");
34+
inOrder.verifyNoMoreInteractions();
35+
36+
}
37+
38+
}

0 commit comments

Comments
 (0)