Added tests for observer pattern
This commit is contained in:
		@@ -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>
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,37 @@
 | 
			
		||||
package com.iluwatar.observer;
 | 
			
		||||
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.junit.runners.Parameterized;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Date: 12/27/15 - 12:07 PM
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jeroen Meulemeester
 | 
			
		||||
 */
 | 
			
		||||
@RunWith(Parameterized.class)
 | 
			
		||||
public class HobbitsTest extends WeatherObserverTest<Hobbits> {
 | 
			
		||||
 | 
			
		||||
  @Parameterized.Parameters
 | 
			
		||||
  public static Collection<Object[]> data() {
 | 
			
		||||
    final ArrayList<Object[]> testData = new ArrayList<>();
 | 
			
		||||
    testData.add(new Object[]{WeatherType.SUNNY, "The happy hobbits bade in the warm sun."});
 | 
			
		||||
    testData.add(new Object[]{WeatherType.RAINY, "The hobbits look for cover from the rain."});
 | 
			
		||||
    testData.add(new Object[]{WeatherType.WINDY, "The hobbits hold their hats tightly in the windy weather."});
 | 
			
		||||
    testData.add(new Object[]{WeatherType.COLD, "The hobbits are shivering in the cold weather."});
 | 
			
		||||
    return testData;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Create a new test with the given weather and expected response
 | 
			
		||||
   *
 | 
			
		||||
   * @param weather  The weather that should be unleashed on the observer
 | 
			
		||||
   * @param response The expected response from the observer
 | 
			
		||||
   */
 | 
			
		||||
  public HobbitsTest(final WeatherType weather, final String response) {
 | 
			
		||||
    super(weather, response, Hobbits::new);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								observer/src/test/java/com/iluwatar/observer/OrcsTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								observer/src/test/java/com/iluwatar/observer/OrcsTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
package com.iluwatar.observer;
 | 
			
		||||
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.junit.runners.Parameterized;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Date: 12/27/15 - 12:07 PM
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jeroen Meulemeester
 | 
			
		||||
 */
 | 
			
		||||
@RunWith(Parameterized.class)
 | 
			
		||||
public class OrcsTest extends WeatherObserverTest<Orcs> {
 | 
			
		||||
 | 
			
		||||
  @Parameterized.Parameters
 | 
			
		||||
  public static Collection<Object[]> data() {
 | 
			
		||||
    final ArrayList<Object[]> testData = new ArrayList<>();
 | 
			
		||||
    testData.add(new Object[]{WeatherType.SUNNY, "The sun hurts the orcs' eyes."});
 | 
			
		||||
    testData.add(new Object[]{WeatherType.RAINY, "The orcs are dripping wet."});
 | 
			
		||||
    testData.add(new Object[]{WeatherType.WINDY, "The orc smell almost vanishes in the wind."});
 | 
			
		||||
    testData.add(new Object[]{WeatherType.COLD, "The orcs are freezing cold."});
 | 
			
		||||
    return testData;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Create a new test with the given weather and expected response
 | 
			
		||||
   *
 | 
			
		||||
   * @param weather  The weather that should be unleashed on the observer
 | 
			
		||||
   * @param response The expected response from the observer
 | 
			
		||||
   */
 | 
			
		||||
  public OrcsTest(final WeatherType weather, final String response) {
 | 
			
		||||
    super(weather, response, Orcs::new);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										54
									
								
								observer/src/test/java/com/iluwatar/observer/StdOutTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								observer/src/test/java/com/iluwatar/observer/StdOutTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
package com.iluwatar.observer;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
 | 
			
		||||
import java.io.PrintStream;
 | 
			
		||||
 | 
			
		||||
import static org.mockito.Mockito.mock;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Date: 12/27/15 - 12:16 PM
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jeroen Meulemeester
 | 
			
		||||
 */
 | 
			
		||||
public abstract class StdOutTest {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The mocked standard out {@link PrintStream}, required since changes in the weather doesn't has
 | 
			
		||||
   * 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;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,59 @@
 | 
			
		||||
package com.iluwatar.observer;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.util.function.Supplier;
 | 
			
		||||
 | 
			
		||||
import static org.mockito.Mockito.verify;
 | 
			
		||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
 | 
			
		||||
import static org.mockito.Mockito.verifyZeroInteractions;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Date: 12/27/15 - 11:44 AM
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jeroen Meulemeester
 | 
			
		||||
 */
 | 
			
		||||
public abstract class WeatherObserverTest<O extends WeatherObserver> extends StdOutTest {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The observer instance factory
 | 
			
		||||
   */
 | 
			
		||||
  private final Supplier<O> factory;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The weather type currently tested
 | 
			
		||||
   */
 | 
			
		||||
  private final WeatherType weather;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The expected response from the observer
 | 
			
		||||
   */
 | 
			
		||||
  private final String response;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Create a new test instance using the given parameters
 | 
			
		||||
   *
 | 
			
		||||
   * @param weather  The weather currently being tested
 | 
			
		||||
   * @param response The expected response from the observer
 | 
			
		||||
   * @param factory  The factory, used to create an instance of the tested observer
 | 
			
		||||
   */
 | 
			
		||||
  WeatherObserverTest(final WeatherType weather, final String response, final Supplier<O> factory) {
 | 
			
		||||
    this.weather = weather;
 | 
			
		||||
    this.response = response;
 | 
			
		||||
    this.factory = factory;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Verify if the weather has the expected influence on the observer
 | 
			
		||||
   */
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testObserver() {
 | 
			
		||||
    final O observer = this.factory.get();
 | 
			
		||||
    verifyZeroInteractions(getStdOutMock());
 | 
			
		||||
 | 
			
		||||
    observer.update(this.weather);
 | 
			
		||||
    verify(getStdOutMock()).println(this.response);
 | 
			
		||||
    verifyNoMoreInteractions(getStdOutMock());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,61 @@
 | 
			
		||||
package com.iluwatar.observer;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.mockito.InOrder;
 | 
			
		||||
 | 
			
		||||
import static org.mockito.Mockito.inOrder;
 | 
			
		||||
import static org.mockito.Mockito.mock;
 | 
			
		||||
import static org.mockito.Mockito.verify;
 | 
			
		||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
 | 
			
		||||
import static org.mockito.Mockito.verifyZeroInteractions;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Date: 12/27/15 - 11:08 AM
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jeroen Meulemeester
 | 
			
		||||
 */
 | 
			
		||||
public class WeatherTest extends StdOutTest {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Add a {@link WeatherObserver}, verify if it gets notified of a weather change, remove the
 | 
			
		||||
   * observer again and verify that there are no more notifications.
 | 
			
		||||
   */
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testAddRemoveObserver() {
 | 
			
		||||
    final WeatherObserver observer = mock(WeatherObserver.class);
 | 
			
		||||
 | 
			
		||||
    final Weather weather = new Weather();
 | 
			
		||||
    weather.addObserver(observer);
 | 
			
		||||
    verifyZeroInteractions(observer);
 | 
			
		||||
 | 
			
		||||
    weather.timePasses();
 | 
			
		||||
    verify(getStdOutMock()).println("The weather changed to rainy.");
 | 
			
		||||
    verify(observer).update(WeatherType.RAINY);
 | 
			
		||||
 | 
			
		||||
    weather.removeObserver(observer);
 | 
			
		||||
    weather.timePasses();
 | 
			
		||||
    verify(getStdOutMock()).println("The weather changed to windy.");
 | 
			
		||||
 | 
			
		||||
    verifyNoMoreInteractions(observer, getStdOutMock());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Verify if the weather passes in the order of the {@link WeatherType}s
 | 
			
		||||
   */
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testTimePasses() {
 | 
			
		||||
    final WeatherObserver observer = mock(WeatherObserver.class);
 | 
			
		||||
    final Weather weather = new Weather();
 | 
			
		||||
    weather.addObserver(observer);
 | 
			
		||||
 | 
			
		||||
    final InOrder inOrder = inOrder(observer, getStdOutMock());
 | 
			
		||||
    final WeatherType[] weatherTypes = WeatherType.values();
 | 
			
		||||
    for (int i = 1; i < 20; i++) {
 | 
			
		||||
      weather.timePasses();
 | 
			
		||||
      inOrder.verify(observer).update(weatherTypes[i % weatherTypes.length]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    verifyNoMoreInteractions(observer);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user