Skip to content

Commit 1b74e0f

Browse files
committed
Merge pull request iluwatar#316 from fluxw42/master
Added more JUnit tests
2 parents a3372fe + 94f80d1 commit 1b74e0f

20 files changed

Lines changed: 1059 additions & 9 deletions

File tree

‎bridge/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<dependency>
1818
<groupId>org.mockito</groupId>
1919
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
2021
</dependency>
2122
</dependencies>
2223
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.iluwatar.composite;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
13+
/**
14+
* Date: 12/11/15 - 8:12 PM
15+
*
16+
* @author Jeroen Meulemeester
17+
*/
18+
public class MessengerTest {
19+
20+
/**
21+
* The buffer used to capture every write to {@link System#out}
22+
*/
23+
private ByteArrayOutputStream stdOutBuffer = new ByteArrayOutputStream();
24+
25+
/**
26+
* Keep the original std-out so it can be restored after the test
27+
*/
28+
private final PrintStream realStdOut = System.out;
29+
30+
/**
31+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
32+
*/
33+
@Before
34+
public void setUp() {
35+
this.stdOutBuffer = new ByteArrayOutputStream();
36+
System.setOut(new PrintStream(stdOutBuffer));
37+
}
38+
39+
/**
40+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
41+
*/
42+
@After
43+
public void tearDown() {
44+
System.setOut(realStdOut);
45+
}
46+
47+
/**
48+
* Test the message from the orcs
49+
*/
50+
@Test
51+
public void testMessageFromOrcs() {
52+
final Messenger messenger = new Messenger();
53+
testMessage(
54+
messenger.messageFromOrcs(),
55+
"Where there is a whip there is a way."
56+
);
57+
}
58+
59+
/**
60+
* Test the message from the elves
61+
*/
62+
@Test
63+
public void testMessageFromElves() {
64+
final Messenger messenger = new Messenger();
65+
testMessage(
66+
messenger.messageFromElves(),
67+
"Much wind pours from your mouth."
68+
);
69+
}
70+
71+
/**
72+
* Test if the given composed message matches the expected message
73+
*
74+
* @param composedMessage The composed message, received from the messenger
75+
* @param message The expected message
76+
*/
77+
private void testMessage(final LetterComposite composedMessage, final String message) {
78+
// Test is the composed message has the correct number of words
79+
final String[] words = message.split(" ");
80+
assertNotNull(composedMessage);
81+
assertEquals(words.length, composedMessage.count());
82+
83+
// Print the message to the mocked stdOut ...
84+
composedMessage.print();
85+
86+
// ... and verify if the message matches with the expected one
87+
assertEquals(message, new String(this.stdOutBuffer.toByteArray()).trim());
88+
}
89+
90+
}

‎decorator/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<dependency>
1818
<groupId>org.mockito</groupId>
1919
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
2021
</dependency>
2122
</dependencies>
2223
</project>

‎dependency-injection/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<groupId>junit</groupId>
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
1621
</dependency>
1722
<dependency>
1823
<groupId>com.google.inject</groupId>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.iluwatar.dependency.injection;
2+
3+
import org.junit.Test;
4+
5+
import static org.mockito.Mockito.times;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyNoMoreInteractions;
8+
9+
/**
10+
* Date: 12/10/15 - 8:40 PM
11+
*
12+
* @author Jeroen Meulemeester
13+
*/
14+
public class AdvancedWizardTest extends StdOutTest {
15+
16+
/**
17+
* Test if the {@link AdvancedWizard} smokes whatever instance of {@link Tobacco} is passed to him
18+
* through the constructor parameter
19+
*/
20+
@Test
21+
public void testSmokeEveryThing() throws Exception {
22+
23+
final Tobacco[] tobaccos = {
24+
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
25+
};
26+
27+
for (final Tobacco tobacco : tobaccos) {
28+
final AdvancedWizard advancedWizard = new AdvancedWizard(tobacco);
29+
advancedWizard.smoke();
30+
31+
// Verify if the wizard is smoking the correct tobacco ...
32+
verify(getStdOutMock(), times(1)).println("AdvancedWizard smoking " + tobacco.getClass().getSimpleName());
33+
34+
// ... and nothing else is happening.
35+
verifyNoMoreInteractions(getStdOutMock());
36+
}
37+
38+
}
39+
40+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.iluwatar.dependency.injection;
2+
3+
import com.google.inject.AbstractModule;
4+
import com.google.inject.Guice;
5+
import com.google.inject.Injector;
6+
7+
import org.junit.Test;
8+
9+
import static org.mockito.Mockito.times;
10+
import static org.mockito.Mockito.verify;
11+
import static org.mockito.Mockito.verifyNoMoreInteractions;
12+
13+
/**
14+
* Date: 12/10/15 - 8:57 PM
15+
*
16+
* @author Jeroen Meulemeester
17+
*/
18+
public class GuiceWizardTest extends StdOutTest {
19+
20+
/**
21+
* Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
22+
* through the constructor parameter
23+
*/
24+
@Test
25+
public void testSmokeEveryThingThroughConstructor() throws Exception {
26+
27+
final Tobacco[] tobaccos = {
28+
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
29+
};
30+
31+
for (final Tobacco tobacco : tobaccos) {
32+
final GuiceWizard guiceWizard = new GuiceWizard(tobacco);
33+
guiceWizard.smoke();
34+
35+
// Verify if the wizard is smoking the correct tobacco ...
36+
verify(getStdOutMock(), times(1)).println("GuiceWizard smoking " + tobacco.getClass().getSimpleName());
37+
38+
// ... and nothing else is happening.
39+
verifyNoMoreInteractions(getStdOutMock());
40+
}
41+
42+
}
43+
44+
/**
45+
* Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
46+
* through the Guice google inject framework
47+
*/
48+
@Test
49+
public void testSmokeEveryThingThroughInjectionFramework() throws Exception {
50+
51+
@SuppressWarnings("unchecked")
52+
final Class<? extends Tobacco>[] tobaccos = new Class[]{
53+
OldTobyTobacco.class, RivendellTobacco.class, SecondBreakfastTobacco.class
54+
};
55+
56+
for (final Class<? extends Tobacco> tobaccoClass : tobaccos) {
57+
// Configure the tobacco in the injection framework ...
58+
final Injector injector = Guice.createInjector(new AbstractModule() {
59+
@Override
60+
protected void configure() {
61+
bind(Tobacco.class).to(tobaccoClass);
62+
}
63+
});
64+
65+
// ... and create a new wizard with it
66+
final GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
67+
guiceWizard.smoke();
68+
69+
// Verify if the wizard is smoking the correct tobacco ...
70+
verify(getStdOutMock(), times(1)).println("GuiceWizard smoking " + tobaccoClass.getSimpleName());
71+
72+
// ... and nothing else is happening.
73+
verifyNoMoreInteractions(getStdOutMock());
74+
}
75+
76+
}
77+
78+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.dependency.injection;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.PrintStream;
8+
9+
import static org.mockito.Mockito.mock;
10+
import static org.mockito.Mockito.times;
11+
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.verifyNoMoreInteractions;
13+
14+
/**
15+
* Date: 12/10/15 - 8:26 PM
16+
*
17+
* @author Jeroen Meulemeester
18+
*/
19+
public class SimpleWizardTest extends StdOutTest {
20+
21+
/**
22+
* Test if the {@link SimpleWizard} does the only thing it can do: Smoke it's {@link
23+
* OldTobyTobacco}
24+
*/
25+
@Test
26+
public void testSmoke() {
27+
final SimpleWizard simpleWizard = new SimpleWizard();
28+
simpleWizard.smoke();
29+
verify(getStdOutMock(), times(1)).println("SimpleWizard smoking OldTobyTobacco");
30+
verifyNoMoreInteractions(getStdOutMock());
31+
}
32+
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.iluwatar.dependency.injection;
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 the actions of the wizard don't
19+
* have any influence on any other accessible objects, 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+
}

‎double-checked-locking/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
<artifactId>junit</artifactId>
1313
<scope>test</scope>
1414
</dependency>
15+
<dependency>
16+
<groupId>org.mockito</groupId>
17+
<artifactId>mockito-core</artifactId>
18+
<scope>test</scope>
19+
</dependency>
1520
</dependencies>
1621
</project>

‎double-checked-locking/src/main/java/com/iluwatar/doublechecked/locking/Inventory.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.iluwatar.doublechecked.locking;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
45
import java.util.List;
56
import java.util.concurrent.locks.Lock;
67
import java.util.concurrent.locks.ReentrantLock;
@@ -38,4 +39,14 @@ public boolean addItem(Item item) {
3839
}
3940
return false;
4041
}
42+
43+
/**
44+
* Get all the items in the inventory
45+
*
46+
* @return All the items of the inventory, as an unmodifiable list
47+
*/
48+
public final List<Item> getItems() {
49+
return Collections.unmodifiableList(items);
50+
}
51+
4152
}

0 commit comments

Comments
 (0)