|
| 1 | +package com.iluwatar.doubledispatch; |
| 2 | + |
| 3 | +import org.junit.After; |
| 4 | +import org.junit.Before; |
| 5 | + |
| 6 | +import java.io.PrintStream; |
| 7 | +import java.util.Objects; |
| 8 | + |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.times; |
| 12 | +import static org.mockito.Mockito.verify; |
| 13 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 14 | + |
| 15 | +/** |
| 16 | + * Date: 12/10/15 - 8:37 PM |
| 17 | + * |
| 18 | + * @author Jeroen Meulemeester |
| 19 | + */ |
| 20 | +public abstract class CollisionTest<O extends GameObject> { |
| 21 | + |
| 22 | + /** |
| 23 | + * The mocked standard out {@link PrintStream}, required if some of the actions on the tested |
| 24 | + * object don't have a direct influence on any other accessible objects, except for writing to |
| 25 | + * std-out using {@link System#out} |
| 26 | + */ |
| 27 | + private final PrintStream stdOutMock = mock(PrintStream.class); |
| 28 | + |
| 29 | + /** |
| 30 | + * Keep the original std-out so it can be restored after the test |
| 31 | + */ |
| 32 | + private final PrintStream stdOutOrig = System.out; |
| 33 | + |
| 34 | + /** |
| 35 | + * Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test |
| 36 | + */ |
| 37 | + @Before |
| 38 | + public void setUp() { |
| 39 | + System.setOut(this.stdOutMock); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Removed the mocked std-out {@link PrintStream} again from the {@link System} class |
| 44 | + */ |
| 45 | + @After |
| 46 | + public void tearDown() { |
| 47 | + System.setOut(this.stdOutOrig); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the mocked stdOut {@link PrintStream} |
| 52 | + * |
| 53 | + * @return The stdOut print stream mock, renewed before each test |
| 54 | + */ |
| 55 | + final PrintStream getStdOutMock() { |
| 56 | + return this.stdOutMock; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Get the tested object |
| 61 | + * |
| 62 | + * @return The tested object, should never return 'null' |
| 63 | + */ |
| 64 | + abstract O getTestedObject(); |
| 65 | + |
| 66 | + /** |
| 67 | + * Collide the tested item with the other given item and verify if the damage and fire state is as |
| 68 | + * expected |
| 69 | + * |
| 70 | + * @param other The other object we have to collide with |
| 71 | + * @param otherDamaged Indicates if the other object should be damaged after the collision |
| 72 | + * @param otherOnFire Indicates if the other object should be burning after the collision |
| 73 | + * @param thisDamaged Indicates if the test object should be damaged after the collision |
| 74 | + * @param thisOnFire Indicates if the other object should be burning after the collision |
| 75 | + * @param description The expected description of the collision |
| 76 | + */ |
| 77 | + void testCollision(final GameObject other, final boolean otherDamaged, final boolean otherOnFire, |
| 78 | + final boolean thisDamaged, final boolean thisOnFire, final String description) { |
| 79 | + |
| 80 | + Objects.requireNonNull(other); |
| 81 | + Objects.requireNonNull(getTestedObject()); |
| 82 | + |
| 83 | + final O tested = getTestedObject(); |
| 84 | + |
| 85 | + tested.collision(other); |
| 86 | + |
| 87 | + verify(getStdOutMock(), times(1)).println(description); |
| 88 | + verifyNoMoreInteractions(getStdOutMock()); |
| 89 | + |
| 90 | + testOnFire(other, tested, otherOnFire); |
| 91 | + testDamaged(other, tested, otherDamaged); |
| 92 | + |
| 93 | + testOnFire(tested, other, thisOnFire); |
| 94 | + testDamaged(tested, other, thisDamaged); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Test if the fire state of the target matches the expected state after colliding with the given |
| 100 | + * object |
| 101 | + * |
| 102 | + * @param target The target object |
| 103 | + * @param other The other object |
| 104 | + * @param expectTargetOnFire The expected state of fire on the target object |
| 105 | + */ |
| 106 | + private void testOnFire(final GameObject target, final GameObject other, final boolean expectTargetOnFire) { |
| 107 | + final String targetName = target.getClass().getSimpleName(); |
| 108 | + final String otherName = other.getClass().getSimpleName(); |
| 109 | + |
| 110 | + final String errorMessage = expectTargetOnFire ? |
| 111 | + "Expected [" + targetName + "] to be on fire after colliding with [" + otherName + "] but it was not!" : |
| 112 | + "Expected [" + targetName + "] not to be on fire after colliding with [" + otherName + "] but it was!"; |
| 113 | + |
| 114 | + assertEquals(errorMessage, expectTargetOnFire, target.isOnFire()); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test if the damage state of the target matches the expected state after colliding with the |
| 119 | + * given object |
| 120 | + * |
| 121 | + * @param target The target object |
| 122 | + * @param other The other object |
| 123 | + * @param expectedDamage The expected state of damage on the target object |
| 124 | + */ |
| 125 | + private void testDamaged(final GameObject target, final GameObject other, final boolean expectedDamage) { |
| 126 | + final String targetName = target.getClass().getSimpleName(); |
| 127 | + final String otherName = other.getClass().getSimpleName(); |
| 128 | + |
| 129 | + final String errorMessage = expectedDamage ? |
| 130 | + "Expected [" + targetName + "] to be damaged after colliding with [" + otherName + "] but it was not!" : |
| 131 | + "Expected [" + targetName + "] not to be damaged after colliding with [" + otherName + "] but it was!"; |
| 132 | + |
| 133 | + assertEquals(errorMessage, expectedDamage, target.isDamaged()); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments