Skip to content

Commit

Permalink
iluwatar#502 Adjusted tests for logger introduction
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-bryla committed Nov 4, 2016
1 parent 27d6d50 commit e138163
Show file tree
Hide file tree
Showing 56 changed files with 1,446 additions and 1,601 deletions.
61 changes: 35 additions & 26 deletions decorator/src/test/java/com/iluwatar/decorator/TrollTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@
*/
package com.iluwatar.decorator;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import java.io.PrintStream;
import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import static org.mockito.internal.verification.VerificationModeFactory.times;

/**
* Date: 12/7/15 - 7:26 PM
Expand All @@ -40,31 +42,16 @@
*/
public class TrollTest {

/**
* The mocked standard out stream, required since the actions don't have any influence on other
* objects, except for writing to the std-out using {@link System#out}
*/
private final PrintStream stdOutMock = mock(PrintStream.class);
private InMemoryAppender appender;

/**
* 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);
appender = new InMemoryAppender(Troll.class);
}

/**
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
*/
@After
public void tearDown() {
System.setOut(this.stdOutOrig);
appender.stop();
}

@Test
Expand All @@ -73,12 +60,34 @@ public void testTrollActions() throws Exception {
assertEquals(10, troll.getAttackPower());

troll.attack();
verify(this.stdOutMock, times(1)).println(eq("The troll swings at you with a club!"));
assertEquals("The troll swings at you with a club!", appender.getLastMessage());

troll.fleeBattle();
verify(this.stdOutMock, times(1)).println(eq("The troll shrieks in horror and runs away!"));
assertEquals("The troll shrieks in horror and runs away!", appender.getLastMessage());

verifyNoMoreInteractions(this.stdOutMock);
assertEquals(2, appender.getLogSize());
}

}
private class InMemoryAppender extends AppenderBase<ILoggingEvent> {

private List<ILoggingEvent> log = new LinkedList<>();

public InMemoryAppender(Class clazz) {
((Logger) LoggerFactory.getLogger(clazz)).addAppender(this);
start();
}

@Override
protected void append(ILoggingEvent eventObject) {
log.add(eventObject);
}

public String getLastMessage() {
return log.get(log.size() - 1).getMessage();
}

public int getLogSize() {
return log.size();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,83 @@
*/
package com.iluwatar.delegation.simple;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import com.iluwatar.delegation.simple.printers.CanonPrinter;
import com.iluwatar.delegation.simple.printers.EpsonPrinter;
import com.iluwatar.delegation.simple.printers.HpPrinter;
import org.junit.Rule;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.slf4j.LoggerFactory;

import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class DelegateTest {

private static final String MESSAGE = "Test Message Printed";
private InMemoryAppender appender;

@Before
public void setUp() {
appender = new InMemoryAppender();
}

@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
@After
public void tearDown() {
appender.stop();
}

private static final String MESSAGE = "Test Message Printed";

@Test
public void testCanonPrinter() throws Exception {
PrinterController printerController = new PrinterController(new CanonPrinter());
printerController.print(MESSAGE);

assertEquals("Canon Printer : Test Message Printed", systemOutRule.getLog());
assertEquals("Canon Printer : Test Message Printed", appender.getLastMessage());
}

@Test
public void testHpPrinter() throws Exception {
PrinterController printerController = new PrinterController(new HpPrinter());
printerController.print(MESSAGE);

assertEquals("HP Printer : Test Message Printed", systemOutRule.getLog());
assertEquals("HP Printer : Test Message Printed", appender.getLastMessage());
}

@Test
public void testEpsonPrinter() throws Exception {
PrinterController printerController = new PrinterController(new EpsonPrinter());
printerController.print(MESSAGE);

assertEquals("Epson Printer : Test Message Printed", systemOutRule.getLog());
assertEquals("Epson Printer : Test Message Printed", appender.getLastMessage());
}

private class InMemoryAppender extends AppenderBase<ILoggingEvent> {

private List<ILoggingEvent> log = new LinkedList<>();

public InMemoryAppender() {
((Logger) LoggerFactory.getLogger("root")).addAppender(this);
start();
}

@Override
protected void append(ILoggingEvent eventObject) {
log.add(eventObject);
}

public String getLastMessage() {
return log.get(log.size() - 1).getFormattedMessage();
}

public int getLogSize() {
return log.size();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,31 @@
*/
package com.iluwatar.dependency.injection;

import com.iluwatar.dependency.injection.utils.InMemoryAppender;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.junit.Assert.assertEquals;

/**
* Date: 12/10/15 - 8:40 PM
*
* @author Jeroen Meulemeester
*/
public class AdvancedWizardTest extends StdOutTest {
public class AdvancedWizardTest {

private InMemoryAppender appender;

@Before
public void setUp() {
appender = new InMemoryAppender(Tobacco.class);
}

@After
public void tearDown() {
appender.stop();
}

/**
* Test if the {@link AdvancedWizard} smokes whatever instance of {@link Tobacco} is passed to him
Expand All @@ -51,12 +64,13 @@ public void testSmokeEveryThing() throws Exception {
advancedWizard.smoke();

// Verify if the wizard is smoking the correct tobacco ...
verify(getStdOutMock(), times(1)).println("AdvancedWizard smoking " + tobacco.getClass().getSimpleName());
assertEquals("AdvancedWizard smoking " + tobacco.getClass().getSimpleName(), appender.getLastMessage());

// ... and nothing else is happening.
verifyNoMoreInteractions(getStdOutMock());
}

// ... and nothing else is happening.
assertEquals(tobaccos.length, appender.getLogSize());

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,31 @@
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;

import com.iluwatar.dependency.injection.utils.InMemoryAppender;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.junit.Assert.assertEquals;

/**
* Date: 12/10/15 - 8:57 PM
*
* @author Jeroen Meulemeester
*/
public class GuiceWizardTest extends StdOutTest {
public class GuiceWizardTest {

private InMemoryAppender appender;

@Before
public void setUp() {
appender = new InMemoryAppender(Tobacco.class);
}

@After
public void tearDown() {
appender.stop();
}

/**
* Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
Expand All @@ -55,12 +67,11 @@ public void testSmokeEveryThingThroughConstructor() throws Exception {
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());
assertEquals("GuiceWizard smoking " + tobacco.getClass().getSimpleName(), appender.getLastMessage());
}

// ... and nothing else is happening.
assertEquals(tobaccos.length, appender.getLogSize());
}

/**
Expand Down Expand Up @@ -89,12 +100,11 @@ protected void configure() {
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());
assertEquals("GuiceWizard smoking " + tobaccoClass.getSimpleName(), appender.getLastMessage());
}

// ... and nothing else is happening.
assertEquals(tobaccos.length, appender.getLogSize());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,31 @@
*/
package com.iluwatar.dependency.injection;

import com.iluwatar.dependency.injection.utils.InMemoryAppender;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.*;
import static org.junit.Assert.assertEquals;

/**
* Date: 12/10/15 - 8:26 PM
*
* @author Jeroen Meulemeester
*/
public class SimpleWizardTest extends StdOutTest {
public class SimpleWizardTest {

private InMemoryAppender appender;

@Before
public void setUp() {
appender = new InMemoryAppender(Tobacco.class);
}

@After
public void tearDown() {
appender.stop();
}

/**
* Test if the {@link SimpleWizard} does the only thing it can do: Smoke it's {@link
Expand All @@ -41,8 +56,8 @@ public class SimpleWizardTest extends StdOutTest {
public void testSmoke() {
final SimpleWizard simpleWizard = new SimpleWizard();
simpleWizard.smoke();
verify(getStdOutMock(), times(1)).println("SimpleWizard smoking OldTobyTobacco");
verifyNoMoreInteractions(getStdOutMock());
assertEquals("SimpleWizard smoking OldTobyTobacco", appender.getLastMessage());
assertEquals(1, appender.getLogSize());
}

}
}
Loading

0 comments on commit e138163

Please sign in to comment.