Add simple tests for delegate pattern #324

This commit is contained in:
Joseph McCarthy 2015-12-27 14:10:49 +00:00
parent fb0617e9c5
commit a49dbefb56
5 changed files with 56 additions and 11 deletions

View File

@ -41,5 +41,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.14.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,10 +1,10 @@
package com.iluwatar.delegation.simple;
public abstract class Controller<T extends Printer> {
public abstract class AbstractPrinterController<T extends Printer> implements Printer{
private Printer printer;
public Controller(Printer printer) {
public AbstractPrinterController(Printer printer) {
this.printer = printer;
}

View File

@ -1,6 +1,6 @@
package com.iluwatar.delegation.simple;
public class PrinterController extends Controller implements Printer {
public class PrinterController extends AbstractPrinterController {
public PrinterController(Printer printer) {
super(printer);

View File

@ -1,6 +1,6 @@
package com.iluwatar.delegation.simple.printers;
import com.iluwatar.delegation.simple.Printer;
import com.iluwatar.delegation.simple.AbstractPrinterController;
import com.iluwatar.delegation.simple.PrinterController;
public class App {
@ -8,13 +8,9 @@ public class App {
public static final String MESSAGE_TO_PRINT = "hello world";
public static void main(String[] args) {
Printer hpPrinter = new HPPrinter();
Printer canonPrinter = new CanonPrinter();
Printer epsonPrinter = new EpsonPrinter();
PrinterController hpPrinterController = new PrinterController(hpPrinter);
PrinterController canonPrinterController = new PrinterController(canonPrinter);
PrinterController epsonPrinterController = new PrinterController(epsonPrinter);
AbstractPrinterController hpPrinterController = new PrinterController(new HPPrinter());
AbstractPrinterController canonPrinterController = new PrinterController(new CanonPrinter());
AbstractPrinterController epsonPrinterController = new PrinterController(new EpsonPrinter());
hpPrinterController.print(MESSAGE_TO_PRINT);
canonPrinterController.print(MESSAGE_TO_PRINT);

View File

@ -0,0 +1,43 @@
package com.iluwatar.delegation.simple;
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.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import static org.junit.Assert.assertEquals;
public class DelegateTest {
private static final String MESSAGE = "Test Message Printed";
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
@Test
public void testCanonPrinter() throws Exception {
AbstractPrinterController abstractController = new PrinterController(new CanonPrinter());
abstractController.print(MESSAGE);
assertEquals("Canon Printer : Test Message Printed\n", systemOutRule.getLog());
}
@Test
public void testHPPrinter() throws Exception {
AbstractPrinterController abstractController = new PrinterController(new HPPrinter());
abstractController.print(MESSAGE);
assertEquals("HP Printer : Test Message Printed\n", systemOutRule.getLog());
}
@Test
public void testEpsonPrinter() throws Exception {
AbstractPrinterController abstractController = new PrinterController(new EpsonPrinter());
abstractController.print(MESSAGE);
assertEquals("Epson Printer : Test Message Printed\n", systemOutRule.getLog());
}
}