#502 Adjusted tests for logger introduction
This commit is contained in:
		@@ -1,76 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * The MIT License
 | 
			
		||||
 * Copyright (c) 2014 Ilkka Seppälä
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
 * in the Software without restriction, including without limitation the rights
 | 
			
		||||
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
 * copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
 * furnished to do so, subject to the following conditions:
 | 
			
		||||
 *
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
			
		||||
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
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
 | 
			
		||||
   */
 | 
			
		||||
  protected final PrintStream getStdOutMock() {
 | 
			
		||||
    return this.stdOutMock;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -22,20 +22,33 @@
 | 
			
		||||
 */
 | 
			
		||||
package com.iluwatar.observer;
 | 
			
		||||
 | 
			
		||||
import com.iluwatar.observer.utils.InMemoryAppender;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
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;
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Date: 12/27/15 - 11:44 AM
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jeroen Meulemeester
 | 
			
		||||
 */
 | 
			
		||||
public abstract class WeatherObserverTest<O extends WeatherObserver> extends StdOutTest {
 | 
			
		||||
public abstract class WeatherObserverTest<O extends WeatherObserver> {
 | 
			
		||||
 | 
			
		||||
  private InMemoryAppender appender;
 | 
			
		||||
 | 
			
		||||
  @Before
 | 
			
		||||
  public void setUp() {
 | 
			
		||||
    appender = new InMemoryAppender();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @After
 | 
			
		||||
  public void tearDown() {
 | 
			
		||||
    appender.stop();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The observer instance factory
 | 
			
		||||
@@ -71,11 +84,11 @@ public abstract class WeatherObserverTest<O extends WeatherObserver> extends Std
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testObserver() {
 | 
			
		||||
    final O observer = this.factory.get();
 | 
			
		||||
    verifyZeroInteractions(getStdOutMock());
 | 
			
		||||
    assertEquals(0, appender.getLogSize());
 | 
			
		||||
 | 
			
		||||
    observer.update(this.weather);
 | 
			
		||||
    verify(getStdOutMock()).println(this.response);
 | 
			
		||||
    verifyNoMoreInteractions(getStdOutMock());
 | 
			
		||||
    assertEquals(response, appender.getLastMessage());
 | 
			
		||||
    assertEquals(1, appender.getLogSize());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -22,9 +22,13 @@
 | 
			
		||||
 */
 | 
			
		||||
package com.iluwatar.observer;
 | 
			
		||||
 | 
			
		||||
import com.iluwatar.observer.utils.InMemoryAppender;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.mockito.InOrder;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
import static org.mockito.Mockito.inOrder;
 | 
			
		||||
import static org.mockito.Mockito.mock;
 | 
			
		||||
import static org.mockito.Mockito.verify;
 | 
			
		||||
@@ -36,7 +40,19 @@ import static org.mockito.Mockito.verifyZeroInteractions;
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jeroen Meulemeester
 | 
			
		||||
 */
 | 
			
		||||
public class WeatherTest extends StdOutTest {
 | 
			
		||||
public class WeatherTest {
 | 
			
		||||
 | 
			
		||||
  private InMemoryAppender appender;
 | 
			
		||||
 | 
			
		||||
  @Before
 | 
			
		||||
  public void setUp() {
 | 
			
		||||
    appender = new InMemoryAppender(Weather.class);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @After
 | 
			
		||||
  public void tearDown() {
 | 
			
		||||
    appender.stop();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Add a {@link WeatherObserver}, verify if it gets notified of a weather change, remove the
 | 
			
		||||
@@ -51,14 +67,15 @@ public class WeatherTest extends StdOutTest {
 | 
			
		||||
    verifyZeroInteractions(observer);
 | 
			
		||||
 | 
			
		||||
    weather.timePasses();
 | 
			
		||||
    verify(getStdOutMock()).println("The weather changed to rainy.");
 | 
			
		||||
    assertEquals("The weather changed to rainy.", appender.getLastMessage());
 | 
			
		||||
    verify(observer).update(WeatherType.RAINY);
 | 
			
		||||
 | 
			
		||||
    weather.removeObserver(observer);
 | 
			
		||||
    weather.timePasses();
 | 
			
		||||
    verify(getStdOutMock()).println("The weather changed to windy.");
 | 
			
		||||
    assertEquals("The weather changed to windy.", appender.getLastMessage());
 | 
			
		||||
 | 
			
		||||
    verifyNoMoreInteractions(observer, getStdOutMock());
 | 
			
		||||
    verifyNoMoreInteractions(observer);
 | 
			
		||||
    assertEquals(2, appender.getLogSize());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
@@ -70,7 +87,7 @@ public class WeatherTest extends StdOutTest {
 | 
			
		||||
    final Weather weather = new Weather();
 | 
			
		||||
    weather.addObserver(observer);
 | 
			
		||||
 | 
			
		||||
    final InOrder inOrder = inOrder(observer, getStdOutMock());
 | 
			
		||||
    final InOrder inOrder = inOrder(observer);
 | 
			
		||||
    final WeatherType[] weatherTypes = WeatherType.values();
 | 
			
		||||
    for (int i = 1; i < 20; i++) {
 | 
			
		||||
      weather.timePasses();
 | 
			
		||||
@@ -80,4 +97,4 @@ public class WeatherTest extends StdOutTest {
 | 
			
		||||
    verifyNoMoreInteractions(observer);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -22,25 +22,35 @@
 | 
			
		||||
 */
 | 
			
		||||
package com.iluwatar.observer.generic;
 | 
			
		||||
 | 
			
		||||
import com.iluwatar.observer.StdOutTest;
 | 
			
		||||
import com.iluwatar.observer.WeatherObserver;
 | 
			
		||||
import com.iluwatar.observer.WeatherType;
 | 
			
		||||
 | 
			
		||||
import com.iluwatar.observer.utils.InMemoryAppender;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
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;
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
import static org.mockito.Mockito.*;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Date: 12/27/15 - 11:08 AM
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jeroen Meulemeester
 | 
			
		||||
 */
 | 
			
		||||
public class GWeatherTest extends StdOutTest {
 | 
			
		||||
public class GWeatherTest {
 | 
			
		||||
 | 
			
		||||
  private InMemoryAppender appender;
 | 
			
		||||
 | 
			
		||||
  @Before
 | 
			
		||||
  public void setUp() {
 | 
			
		||||
    appender = new InMemoryAppender(GWeather.class);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @After
 | 
			
		||||
  public void tearDown() {
 | 
			
		||||
    appender.stop();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Add a {@link WeatherObserver}, verify if it gets notified of a weather change, remove the
 | 
			
		||||
@@ -55,14 +65,15 @@ public class GWeatherTest extends StdOutTest {
 | 
			
		||||
    verifyZeroInteractions(observer);
 | 
			
		||||
 | 
			
		||||
    weather.timePasses();
 | 
			
		||||
    verify(getStdOutMock()).println("The weather changed to rainy.");
 | 
			
		||||
    assertEquals("The weather changed to rainy.", appender.getLastMessage());
 | 
			
		||||
    verify(observer).update(weather, WeatherType.RAINY);
 | 
			
		||||
 | 
			
		||||
    weather.removeObserver(observer);
 | 
			
		||||
    weather.timePasses();
 | 
			
		||||
    verify(getStdOutMock()).println("The weather changed to windy.");
 | 
			
		||||
    assertEquals("The weather changed to windy.", appender.getLastMessage());
 | 
			
		||||
 | 
			
		||||
    verifyNoMoreInteractions(observer, getStdOutMock());
 | 
			
		||||
    verifyNoMoreInteractions(observer);
 | 
			
		||||
    assertEquals(2, appender.getLogSize());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
@@ -74,7 +85,7 @@ public class GWeatherTest extends StdOutTest {
 | 
			
		||||
    final GWeather weather = new GWeather();
 | 
			
		||||
    weather.addObserver(observer);
 | 
			
		||||
 | 
			
		||||
    final InOrder inOrder = inOrder(observer, getStdOutMock());
 | 
			
		||||
    final InOrder inOrder = inOrder(observer);
 | 
			
		||||
    final WeatherType[] weatherTypes = WeatherType.values();
 | 
			
		||||
    for (int i = 1; i < 20; i++) {
 | 
			
		||||
      weather.timePasses();
 | 
			
		||||
@@ -84,4 +95,4 @@ public class GWeatherTest extends StdOutTest {
 | 
			
		||||
    verifyNoMoreInteractions(observer);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -22,23 +22,34 @@
 | 
			
		||||
 */
 | 
			
		||||
package com.iluwatar.observer.generic;
 | 
			
		||||
 | 
			
		||||
import com.iluwatar.observer.StdOutTest;
 | 
			
		||||
import com.iluwatar.observer.WeatherType;
 | 
			
		||||
 | 
			
		||||
import com.iluwatar.observer.utils.InMemoryAppender;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
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;
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Date: 12/27/15 - 11:44 AM
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jeroen Meulemeester
 | 
			
		||||
 */
 | 
			
		||||
public abstract class ObserverTest<O extends Observer> extends StdOutTest {
 | 
			
		||||
public abstract class ObserverTest<O extends Observer> {
 | 
			
		||||
 | 
			
		||||
  private InMemoryAppender appender;
 | 
			
		||||
 | 
			
		||||
  @Before
 | 
			
		||||
  public void setUp() {
 | 
			
		||||
    appender = new InMemoryAppender();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @After
 | 
			
		||||
  public void tearDown() {
 | 
			
		||||
    appender.stop();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The observer instance factory
 | 
			
		||||
@@ -74,11 +85,11 @@ public abstract class ObserverTest<O extends Observer> extends StdOutTest {
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testObserver() {
 | 
			
		||||
    final O observer = this.factory.get();
 | 
			
		||||
    verifyZeroInteractions(getStdOutMock());
 | 
			
		||||
    assertEquals(0, appender.getLogSize());
 | 
			
		||||
 | 
			
		||||
    observer.update(null, this.weather);
 | 
			
		||||
    verify(getStdOutMock()).println(this.response);
 | 
			
		||||
    verifyNoMoreInteractions(getStdOutMock());
 | 
			
		||||
    assertEquals(this.response, appender.getLastMessage());
 | 
			
		||||
    assertEquals(1, appender.getLogSize());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,58 @@
 | 
			
		||||
/**
 | 
			
		||||
 * The MIT License
 | 
			
		||||
 * Copyright (c) 2014 Ilkka Seppälä
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
 * in the Software without restriction, including without limitation the rights
 | 
			
		||||
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
 * copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
 * furnished to do so, subject to the following conditions:
 | 
			
		||||
 *
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
			
		||||
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
package com.iluwatar.observer.utils;
 | 
			
		||||
 | 
			
		||||
import ch.qos.logback.classic.Logger;
 | 
			
		||||
import ch.qos.logback.classic.spi.ILoggingEvent;
 | 
			
		||||
import ch.qos.logback.core.AppenderBase;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
import java.util.LinkedList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public class InMemoryAppender extends AppenderBase<ILoggingEvent> {
 | 
			
		||||
  private List<ILoggingEvent> log = new LinkedList<>();
 | 
			
		||||
 | 
			
		||||
  public InMemoryAppender(Class clazz) {
 | 
			
		||||
    ((Logger) LoggerFactory.getLogger(clazz)).addAppender(this);
 | 
			
		||||
    start();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public InMemoryAppender() {
 | 
			
		||||
    ((Logger) LoggerFactory.getLogger("root")).addAppender(this);
 | 
			
		||||
    start();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  protected void append(ILoggingEvent eventObject) {
 | 
			
		||||
    log.add(eventObject);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public int getLogSize() {
 | 
			
		||||
    return log.size();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getLastMessage() {
 | 
			
		||||
    return log.get(log.size() - 1).getFormattedMessage();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user