Merge pull request #515 from dbryla/master

#502 Added logging framework to repository
This commit is contained in:
Ilkka Seppälä
2016-11-18 20:47:17 +02:00
committed by GitHub
215 changed files with 2927 additions and 2450 deletions

View File

@ -22,6 +22,9 @@
*/
package com.iluwatar.decorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* The Decorator pattern is a more flexible alternative to subclassing. The Decorator class
@ -36,6 +39,8 @@ package com.iluwatar.decorator;
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
* Program entry point
*
@ -44,17 +49,17 @@ public class App {
public static void main(String[] args) {
// simple troll
System.out.println("A simple looking troll approaches.");
LOGGER.info("A simple looking troll approaches.");
Hostile troll = new Troll();
troll.attack();
troll.fleeBattle();
System.out.printf("Simple troll power %d.\n", troll.getAttackPower());
LOGGER.info("Simple troll power {}.\n", troll.getAttackPower());
// change the behavior of the simple troll by adding a decorator
System.out.println("\nA smart looking troll surprises you.");
LOGGER.info("A smart looking troll surprises you.");
Hostile smart = new SmartHostile(troll);
smart.attack();
smart.fleeBattle();
System.out.printf("Smart troll power %d.\n", smart.getAttackPower());
LOGGER.info("Smart troll power {}.\n", smart.getAttackPower());
}
}

View File

@ -22,6 +22,9 @@
*/
package com.iluwatar.decorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* SmartHostile is a decorator for {@link Hostile} objects. The calls to the {@link Hostile} interface
* are intercepted and decorated. Finally the calls are delegated to the decorated {@link Hostile}
@ -30,6 +33,8 @@ package com.iluwatar.decorator;
*/
public class SmartHostile implements Hostile {
private static final Logger LOGGER = LoggerFactory.getLogger(SmartHostile.class);
private Hostile decorated;
public SmartHostile(Hostile decorated) {
@ -38,7 +43,7 @@ public class SmartHostile implements Hostile {
@Override
public void attack() {
System.out.println("It throws a rock at you!");
LOGGER.info("It throws a rock at you!");
decorated.attack();
}
@ -50,7 +55,7 @@ public class SmartHostile implements Hostile {
@Override
public void fleeBattle() {
System.out.println("It calls for help!");
LOGGER.info("It calls for help!");
decorated.fleeBattle();
}
}

View File

@ -22,6 +22,9 @@
*/
package com.iluwatar.decorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* Troll implements {@link Hostile} interface directly.
@ -29,9 +32,11 @@ package com.iluwatar.decorator;
*/
public class Troll implements Hostile {
private static final Logger LOGGER = LoggerFactory.getLogger(Troll.class);
@Override
public void attack() {
System.out.println("The troll swings at you with a club!");
LOGGER.info("The troll swings at you with a club!");
}
@Override
@ -41,6 +46,6 @@ public class Troll implements Hostile {
@Override
public void fleeBattle() {
System.out.println("The troll shrieks in horror and runs away!");
LOGGER.info("The troll shrieks in horror and runs away!");
}
}

View File

@ -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
@ -40,31 +42,16 @@ import static org.mockito.internal.verification.VerificationModeFactory.times;
*/
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
@ -73,12 +60,34 @@ public class TrollTest {
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();
}
}
}