Merge pull request #316 from fluxw42/master

Added more JUnit tests
This commit is contained in:
Ilkka Seppälä 2015-12-12 09:44:41 +02:00
commit 1b74e0ff67
20 changed files with 1059 additions and 9 deletions

View File

@ -17,6 +17,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,90 @@
package com.iluwatar.composite;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Date: 12/11/15 - 8:12 PM
*
* @author Jeroen Meulemeester
*/
public class MessengerTest {
/**
* The buffer used to capture every write to {@link System#out}
*/
private ByteArrayOutputStream stdOutBuffer = new ByteArrayOutputStream();
/**
* Keep the original std-out so it can be restored after the test
*/
private final PrintStream realStdOut = System.out;
/**
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
*/
@Before
public void setUp() {
this.stdOutBuffer = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOutBuffer));
}
/**
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
*/
@After
public void tearDown() {
System.setOut(realStdOut);
}
/**
* Test the message from the orcs
*/
@Test
public void testMessageFromOrcs() {
final Messenger messenger = new Messenger();
testMessage(
messenger.messageFromOrcs(),
"Where there is a whip there is a way."
);
}
/**
* Test the message from the elves
*/
@Test
public void testMessageFromElves() {
final Messenger messenger = new Messenger();
testMessage(
messenger.messageFromElves(),
"Much wind pours from your mouth."
);
}
/**
* Test if the given composed message matches the expected message
*
* @param composedMessage The composed message, received from the messenger
* @param message The expected message
*/
private void testMessage(final LetterComposite composedMessage, final String message) {
// Test is the composed message has the correct number of words
final String[] words = message.split(" ");
assertNotNull(composedMessage);
assertEquals(words.length, composedMessage.count());
// Print the message to the mocked stdOut ...
composedMessage.print();
// ... and verify if the message matches with the expected one
assertEquals(message, new String(this.stdOutBuffer.toByteArray()).trim());
}
}

View File

@ -17,6 +17,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -13,6 +13,11 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>

View File

@ -0,0 +1,40 @@
package com.iluwatar.dependency.injection;
import org.junit.Test;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Date: 12/10/15 - 8:40 PM
*
* @author Jeroen Meulemeester
*/
public class AdvancedWizardTest extends StdOutTest {
/**
* Test if the {@link AdvancedWizard} smokes whatever instance of {@link Tobacco} is passed to him
* through the constructor parameter
*/
@Test
public void testSmokeEveryThing() throws Exception {
final Tobacco[] tobaccos = {
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
};
for (final Tobacco tobacco : tobaccos) {
final AdvancedWizard advancedWizard = new AdvancedWizard(tobacco);
advancedWizard.smoke();
// Verify if the wizard is smoking the correct tobacco ...
verify(getStdOutMock(), times(1)).println("AdvancedWizard smoking " + tobacco.getClass().getSimpleName());
// ... and nothing else is happening.
verifyNoMoreInteractions(getStdOutMock());
}
}
}

View File

@ -0,0 +1,78 @@
package com.iluwatar.dependency.injection;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.Test;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Date: 12/10/15 - 8:57 PM
*
* @author Jeroen Meulemeester
*/
public class GuiceWizardTest extends StdOutTest {
/**
* Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
* through the constructor parameter
*/
@Test
public void testSmokeEveryThingThroughConstructor() throws Exception {
final Tobacco[] tobaccos = {
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
};
for (final Tobacco tobacco : tobaccos) {
final GuiceWizard guiceWizard = new GuiceWizard(tobacco);
guiceWizard.smoke();
// Verify if the wizard is smoking the correct tobacco ...
verify(getStdOutMock(), times(1)).println("GuiceWizard smoking " + tobacco.getClass().getSimpleName());
// ... and nothing else is happening.
verifyNoMoreInteractions(getStdOutMock());
}
}
/**
* Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
* through the Guice google inject framework
*/
@Test
public void testSmokeEveryThingThroughInjectionFramework() throws Exception {
@SuppressWarnings("unchecked")
final Class<? extends Tobacco>[] tobaccos = new Class[]{
OldTobyTobacco.class, RivendellTobacco.class, SecondBreakfastTobacco.class
};
for (final Class<? extends Tobacco> tobaccoClass : tobaccos) {
// Configure the tobacco in the injection framework ...
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Tobacco.class).to(tobaccoClass);
}
});
// ... and create a new wizard with it
final GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
guiceWizard.smoke();
// Verify if the wizard is smoking the correct tobacco ...
verify(getStdOutMock(), times(1)).println("GuiceWizard smoking " + tobaccoClass.getSimpleName());
// ... and nothing else is happening.
verifyNoMoreInteractions(getStdOutMock());
}
}
}

View File

@ -0,0 +1,33 @@
package com.iluwatar.dependency.injection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.PrintStream;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Date: 12/10/15 - 8:26 PM
*
* @author Jeroen Meulemeester
*/
public class SimpleWizardTest extends StdOutTest {
/**
* Test if the {@link SimpleWizard} does the only thing it can do: Smoke it's {@link
* OldTobyTobacco}
*/
@Test
public void testSmoke() {
final SimpleWizard simpleWizard = new SimpleWizard();
simpleWizard.smoke();
verify(getStdOutMock(), times(1)).println("SimpleWizard smoking OldTobyTobacco");
verifyNoMoreInteractions(getStdOutMock());
}
}

View File

@ -0,0 +1,53 @@
package com.iluwatar.dependency.injection;
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 the actions of the wizard don't
* have any influence on any other accessible objects, 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;
}
}

View File

@ -12,5 +12,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,6 +1,7 @@
package com.iluwatar.doublechecked.locking;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@ -38,4 +39,14 @@ public class Inventory {
}
return false;
}
/**
* Get all the items in the inventory
*
* @return All the items of the inventory, as an unmodifiable list
*/
public final List<Item> getItems() {
return Collections.unmodifiableList(items);
}
}

View File

@ -0,0 +1,110 @@
package com.iluwatar.doublechecked.locking;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.io.PrintStream;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Date: 12/10/15 - 9:34 PM
*
* @author Jeroen Meulemeester
*/
public class InventoryTest {
/**
* The mocked standard out {@link PrintStream}, used to verify a steady increasing size of the
* {@link Inventory} while adding items from multiple threads concurrently
*/
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);
}
/**
* The number of threads used to stress test the locking of the {@link Inventory#addItem(Item)}
* method
*/
private static final int THREAD_COUNT = 8;
/**
* The maximum number of {@link Item}s allowed in the {@link Inventory}
*/
private static final int INVENTORY_SIZE = 1000;
/**
* Concurrently add multiple items to the inventory, and check if the items were added in order by
* checking the stdOut for continuous growth of the inventory. When 'items.size()=xx' shows up out
* of order, it means that the locking is not ok, increasing the risk of going over the inventory
* item limit.
*/
@Test(timeout = 10000)
public void testAddItem() throws Exception {
// Create a new inventory with a limit of 1000 items and put some load on the add method
final Inventory inventory = new Inventory(INVENTORY_SIZE);
final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);
for (int i = 0; i < THREAD_COUNT; i++) {
executorService.execute(() -> {
while (inventory.addItem(new Item())) ;
});
}
// Wait until all threads have finished
executorService.shutdown();
executorService.awaitTermination(5, TimeUnit.SECONDS);
// Check the number of items in the inventory. It should not have exceeded the allowed maximum
final List<Item> items = inventory.getItems();
assertNotNull(items);
assertEquals(INVENTORY_SIZE, items.size());
// Capture all stdOut messages ...
final ArgumentCaptor<String> stdOutCaptor = ArgumentCaptor.forClass(String.class);
verify(this.stdOutMock, times(INVENTORY_SIZE)).println(stdOutCaptor.capture());
// ... verify if we got all 1000
final List<String> values = stdOutCaptor.getAllValues();
assertEquals(INVENTORY_SIZE, values.size());
// ... and check if the inventory size is increasing continuously
for (int i = 0; i < values.size(); i++) {
assertNotNull(values.get(i));
assertTrue(values.get(i).contains("items.size()=" + (i + 1)));
}
verifyNoMoreInteractions(this.stdOutMock);
}
}

View File

@ -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>

View File

@ -0,0 +1,136 @@
package com.iluwatar.doubledispatch;
import org.junit.After;
import org.junit.Before;
import java.io.PrintStream;
import java.util.Objects;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Date: 12/10/15 - 8:37 PM
*
* @author Jeroen Meulemeester
*/
public abstract class CollisionTest<O extends GameObject> {
/**
* The mocked standard out {@link PrintStream}, required if some of the actions on the tested
* object don't have a direct influence on any other accessible objects, 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;
}
/**
* Get the tested object
*
* @return The tested object, should never return 'null'
*/
abstract O getTestedObject();
/**
* Collide the tested item with the other given item and verify if the damage and fire state is as
* expected
*
* @param other The other object we have to collide with
* @param otherDamaged Indicates if the other object should be damaged after the collision
* @param otherOnFire Indicates if the other object should be burning after the collision
* @param thisDamaged Indicates if the test object should be damaged after the collision
* @param thisOnFire Indicates if the other object should be burning after the collision
* @param description The expected description of the collision
*/
void testCollision(final GameObject other, final boolean otherDamaged, final boolean otherOnFire,
final boolean thisDamaged, final boolean thisOnFire, final String description) {
Objects.requireNonNull(other);
Objects.requireNonNull(getTestedObject());
final O tested = getTestedObject();
tested.collision(other);
verify(getStdOutMock(), times(1)).println(description);
verifyNoMoreInteractions(getStdOutMock());
testOnFire(other, tested, otherOnFire);
testDamaged(other, tested, otherDamaged);
testOnFire(tested, other, thisOnFire);
testDamaged(tested, other, thisDamaged);
}
/**
* Test if the fire state of the target matches the expected state after colliding with the given
* object
*
* @param target The target object
* @param other The other object
* @param expectTargetOnFire The expected state of fire on the target object
*/
private void testOnFire(final GameObject target, final GameObject other, final boolean expectTargetOnFire) {
final String targetName = target.getClass().getSimpleName();
final String otherName = other.getClass().getSimpleName();
final String errorMessage = expectTargetOnFire ?
"Expected [" + targetName + "] to be on fire after colliding with [" + otherName + "] but it was not!" :
"Expected [" + targetName + "] not to be on fire after colliding with [" + otherName + "] but it was!";
assertEquals(errorMessage, expectTargetOnFire, target.isOnFire());
}
/**
* Test if the damage state of the target matches the expected state after colliding with the
* given object
*
* @param target The target object
* @param other The other object
* @param expectedDamage The expected state of damage on the target object
*/
private void testDamaged(final GameObject target, final GameObject other, final boolean expectedDamage) {
final String targetName = target.getClass().getSimpleName();
final String otherName = other.getClass().getSimpleName();
final String errorMessage = expectedDamage ?
"Expected [" + targetName + "] to be damaged after colliding with [" + otherName + "] but it was not!" :
"Expected [" + targetName + "] not to be damaged after colliding with [" + otherName + "] but it was!";
assertEquals(errorMessage, expectedDamage, target.isDamaged());
}
}

View File

@ -0,0 +1,88 @@
package com.iluwatar.doubledispatch;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Date: 12/10/15 - 11:31 PM
*
* @author Jeroen Meulemeester
*/
public class FlamingAsteroidTest extends CollisionTest<FlamingAsteroid> {
@Override
final FlamingAsteroid getTestedObject() {
return new FlamingAsteroid(1, 2, 3, 4);
}
/**
* Test the constructor parameters
*/
@Test
public void testConstructor() {
final FlamingAsteroid asteroid = new FlamingAsteroid(1, 2, 3, 4);
assertEquals(1, asteroid.getLeft());
assertEquals(2, asteroid.getTop());
assertEquals(3, asteroid.getRight());
assertEquals(4, asteroid.getBottom());
assertTrue(asteroid.isOnFire());
assertFalse(asteroid.isDamaged());
assertEquals("FlamingAsteroid at [1,2,3,4] damaged=false onFire=true", asteroid.toString());
}
/**
* Test what happens we collide with an asteroid
*/
@Test
public void testCollideFlamingAsteroid() {
testCollision(
new FlamingAsteroid(1, 2, 3, 4),
false, true,
false, true,
"FlamingAsteroid hits FlamingAsteroid."
);
}
/**
* Test what happens we collide with an meteoroid
*/
@Test
public void testCollideMeteoroid() {
testCollision(
new Meteoroid(1, 1, 3, 4),
false, false,
false, true,
"FlamingAsteroid hits Meteoroid."
);
}
/**
* Test what happens we collide with ISS
*/
@Test
public void testCollideSpaceStationIss() {
testCollision(
new SpaceStationIss(1, 1, 3, 4),
true, true,
false, true,
"FlamingAsteroid hits SpaceStationIss. SpaceStationIss is damaged! SpaceStationIss is set on fire!"
);
}
/**
* Test what happens we collide with MIR
*/
@Test
public void testCollideSpaceStationMir() {
testCollision(
new SpaceStationMir(1, 1, 3, 4),
true, true,
false, true,
"FlamingAsteroid hits SpaceStationMir. SpaceStationMir is damaged! SpaceStationMir is set on fire!"
);
}
}

View File

@ -0,0 +1,87 @@
package com.iluwatar.doubledispatch;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
/**
* Date: 12/10/15 - 11:31 PM
*
* @author Jeroen Meulemeester
*/
public class MeteoroidTest extends CollisionTest<Meteoroid> {
@Override
final Meteoroid getTestedObject() {
return new Meteoroid(1, 2, 3, 4);
}
/**
* Test the constructor parameters
*/
@Test
public void testConstructor() {
final Meteoroid meteoroid = new Meteoroid(1, 2, 3, 4);
assertEquals(1, meteoroid.getLeft());
assertEquals(2, meteoroid.getTop());
assertEquals(3, meteoroid.getRight());
assertEquals(4, meteoroid.getBottom());
assertFalse(meteoroid.isOnFire());
assertFalse(meteoroid.isDamaged());
assertEquals("Meteoroid at [1,2,3,4] damaged=false onFire=false", meteoroid.toString());
}
/**
* Test what happens we collide with an asteroid
*/
@Test
public void testCollideFlamingAsteroid() {
testCollision(
new FlamingAsteroid(1, 1, 3, 4),
false, true,
false, false,
"Meteoroid hits FlamingAsteroid."
);
}
/**
* Test what happens we collide with an meteoroid
*/
@Test
public void testCollideMeteoroid() {
testCollision(
new Meteoroid(1, 1, 3, 4),
false, false,
false, false,
"Meteoroid hits Meteoroid."
);
}
/**
* Test what happens we collide with ISS
*/
@Test
public void testCollideSpaceStationIss() {
testCollision(
new SpaceStationIss(1, 1, 3, 4),
true, false,
false, false,
"Meteoroid hits SpaceStationIss. SpaceStationIss is damaged!"
);
}
/**
* Test what happens we collide with MIR
*/
@Test
public void testCollideSpaceStationMir() {
testCollision(
new SpaceStationMir(1, 1, 3, 4),
true, false,
false, false,
"Meteoroid hits SpaceStationMir. SpaceStationMir is damaged!"
);
}
}

View File

@ -1,22 +1,47 @@
package com.iluwatar.doubledispatch;
import org.junit.Assert;
import org.junit.Test;
import com.iluwatar.doubledispatch.Rectangle;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
*
* Unit test for Rectangle
*
*/
public class RectangleTest {
/**
* Test if the values passed through the constructor matches the values fetched from the getters
*/
@Test
public void test() {
Assert.assertTrue(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(0, 0, 1, 1)));
Assert.assertTrue(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(-1, -5, 7, 8)));
Assert.assertFalse(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(2, 2, 3, 3)));
Assert.assertFalse(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(-2, -2, -1, -1)));
public void testConstructor() {
final Rectangle rectangle = new Rectangle(1, 2, 3, 4);
assertEquals(1, rectangle.getLeft());
assertEquals(2, rectangle.getTop());
assertEquals(3, rectangle.getRight());
assertEquals(4, rectangle.getBottom());
}
/**
* Test if the values passed through the constructor matches the values in the {@link
* #toString()}
*/
@Test
public void testToString() throws Exception {
final Rectangle rectangle = new Rectangle(1, 2, 3, 4);
assertEquals("[1,2,3,4]", rectangle.toString());
}
/**
* Test if the {@link Rectangle} class can detect if it intersects with another rectangle.
*/
@Test
public void testIntersection() {
assertTrue(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(0, 0, 1, 1)));
assertTrue(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(-1, -5, 7, 8)));
assertFalse(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(2, 2, 3, 3)));
assertFalse(new Rectangle(0, 0, 1, 1).intersectsWith(new Rectangle(-2, -2, -1, -1)));
}
}

View File

@ -0,0 +1,87 @@
package com.iluwatar.doubledispatch;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
/**
* Date: 12/10/15 - 11:31 PM
*
* @author Jeroen Meulemeester
*/
public class SpaceStationIssTest extends CollisionTest<SpaceStationIss> {
@Override
final SpaceStationIss getTestedObject() {
return new SpaceStationIss(1, 2, 3, 4);
}
/**
* Test the constructor parameters
*/
@Test
public void testConstructor() {
final SpaceStationIss iss = new SpaceStationIss(1, 2, 3, 4);
assertEquals(1, iss.getLeft());
assertEquals(2, iss.getTop());
assertEquals(3, iss.getRight());
assertEquals(4, iss.getBottom());
assertFalse(iss.isOnFire());
assertFalse(iss.isDamaged());
assertEquals("SpaceStationIss at [1,2,3,4] damaged=false onFire=false", iss.toString());
}
/**
* Test what happens we collide with an asteroid
*/
@Test
public void testCollideFlamingAsteroid() {
testCollision(
new FlamingAsteroid(1, 1, 3, 4),
false, true,
false, false,
"SpaceStationIss hits FlamingAsteroid."
);
}
/**
* Test what happens we collide with an meteoroid
*/
@Test
public void testCollideMeteoroid() {
testCollision(
new Meteoroid(1, 1, 3, 4),
false, false,
false, false,
"SpaceStationIss hits Meteoroid."
);
}
/**
* Test what happens we collide with ISS
*/
@Test
public void testCollideSpaceStationIss() {
testCollision(
new SpaceStationIss(1, 1, 3, 4),
true, false,
false, false,
"SpaceStationIss hits SpaceStationIss. SpaceStationIss is damaged!"
);
}
/**
* Test what happens we collide with MIR
*/
@Test
public void testCollideSpaceStationMir() {
testCollision(
new SpaceStationMir(1, 1, 3, 4),
true, false,
false, false,
"SpaceStationIss hits SpaceStationMir. SpaceStationMir is damaged!"
);
}
}

View File

@ -0,0 +1,87 @@
package com.iluwatar.doubledispatch;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
/**
* Date: 12/10/15 - 11:31 PM
*
* @author Jeroen Meulemeester
*/
public class SpaceStationMirTest extends CollisionTest<SpaceStationMir> {
@Override
final SpaceStationMir getTestedObject() {
return new SpaceStationMir(1, 2, 3, 4);
}
/**
* Test the constructor parameters
*/
@Test
public void testConstructor() {
final SpaceStationMir mir = new SpaceStationMir(1, 2, 3, 4);
assertEquals(1, mir.getLeft());
assertEquals(2, mir.getTop());
assertEquals(3, mir.getRight());
assertEquals(4, mir.getBottom());
assertFalse(mir.isOnFire());
assertFalse(mir.isDamaged());
assertEquals("SpaceStationMir at [1,2,3,4] damaged=false onFire=false", mir.toString());
}
/**
* Test what happens we collide with an asteroid
*/
@Test
public void testCollideFlamingAsteroid() {
testCollision(
new FlamingAsteroid(1, 1, 3, 4),
false, true,
false, false,
"SpaceStationMir hits FlamingAsteroid."
);
}
/**
* Test what happens we collide with an meteoroid
*/
@Test
public void testCollideMeteoroid() {
testCollision(
new Meteoroid(1, 1, 3, 4),
false, false,
false, false,
"SpaceStationMir hits Meteoroid."
);
}
/**
* Test what happens we collide with ISS
*/
@Test
public void testCollideSpaceStationIss() {
testCollision(
new SpaceStationIss(1, 1, 3, 4),
true, false,
false, false,
"SpaceStationMir hits SpaceStationIss. SpaceStationIss is damaged!"
);
}
/**
* Test what happens we collide with MIR
*/
@Test
public void testCollideSpaceStationMir() {
testCollision(
new SpaceStationMir(1, 1, 3, 4),
true, false,
false, false,
"SpaceStationMir hits SpaceStationMir. SpaceStationMir is damaged!"
);
}
}

View File

@ -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>

View File

@ -0,0 +1,103 @@
package com.iluwatar.facade;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.PrintStream;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.internal.verification.VerificationModeFactory.times;
/**
* Date: 12/9/15 - 9:40 PM
*
* @author Jeroen Meulemeester
*/
public class DwarvenGoldmineFacadeTest {
/**
* The mocked standard out {@link PrintStream}, required since the actions on the gold mine facade
* don't have any influence on any other accessible objects, 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);
}
/**
* Test a complete day cycle in the gold mine by executing all three different steps: {@link
* DwarvenGoldmineFacade#startNewDay()}, {@link DwarvenGoldmineFacade#digOutGold()} and {@link
* DwarvenGoldmineFacade#endDay()}.
*
* See if the workers are doing what's expected from them on each step.
*/
@Test
public void testFullWorkDay() {
final DwarvenGoldmineFacade goldMine = new DwarvenGoldmineFacade();
goldMine.startNewDay();
// On the start of a day, all workers should wake up ...
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger wakes up."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator wakes up."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger wakes up."));
// ... and go to the mine
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger goes to the mine."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator goes to the mine."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger goes to the mine."));
// No other actions were invoked, so the workers shouldn't have done (printed) anything else
verifyNoMoreInteractions(this.stdOutMock);
// Now do some actual work, start digging gold!
goldMine.digOutGold();
// Since we gave the dig command, every worker should be doing it's job ...
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger digs for gold."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator moves gold chunks out of the mine."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger creates another promising tunnel."));
// Again, they shouldn't be doing anything else.
verifyNoMoreInteractions(this.stdOutMock);
// Enough gold, lets end the day.
goldMine.endDay();
// Check if the workers go home ...
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger goes home."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator goes home."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger goes home."));
// ... and go to sleep. We need well rested workers the next day :)
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger goes to sleep."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator goes to sleep."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger goes to sleep."));
// Every worker should be sleeping now, no other actions allowed
verifyNoMoreInteractions(this.stdOutMock);
}
}