From be9c68b2ec522f6e7efda9fad325ab88bd0e8a93 Mon Sep 17 00:00:00 2001 From: J08nY Date: Sun, 3 Dec 2017 21:34:02 +0100 Subject: Introduce SimpleTest, implement other tests usign it. --- src/cz/crcs/ectester/reader/test/CommandTest.java | 55 +++++++++-------------- 1 file changed, 20 insertions(+), 35 deletions(-) (limited to 'src/cz/crcs/ectester/reader') diff --git a/src/cz/crcs/ectester/reader/test/CommandTest.java b/src/cz/crcs/ectester/reader/test/CommandTest.java index b7728b6..5f3dcac 100644 --- a/src/cz/crcs/ectester/reader/test/CommandTest.java +++ b/src/cz/crcs/ectester/reader/test/CommandTest.java @@ -1,45 +1,42 @@ package cz.crcs.ectester.reader.test; import cz.crcs.ectester.common.test.Result; -import cz.crcs.ectester.common.test.Test; +import cz.crcs.ectester.common.test.SimpleTest; +import cz.crcs.ectester.common.test.TestCallback; import cz.crcs.ectester.common.test.TestException; import cz.crcs.ectester.reader.command.Command; import cz.crcs.ectester.reader.response.Response; -import javax.smartcardio.CardException; -import java.util.function.BiFunction; - /** * A simple test that runs one Command to get and evaluate one Response * to get a Result and compare it with the expected one. */ -public class CommandTest extends Test { - private BiFunction callback; - private Command command; - private Response response; - - public CommandTest(Command command, BiFunction callback) { - this.command = command; - this.callback = callback; +public class CommandTest extends SimpleTest { + private CommandTest(CommandTestable command, TestCallback callback) { + super(command, callback); } - public CommandTest(Command command, Result.ExpectedValue expected, String ok, String nok) { - this(command, (cmd, resp) -> { - Result.Value resultValue = Result.Value.fromExpected(expected, resp.successful(), resp.error()); - return new Result(resultValue, resultValue.ok() ? ok : nok); + public CommandTest expect(CommandTestable command, Result.ExpectedValue expected, String ok, String nok) { + return new CommandTest(command, new TestCallback() { + @Override + public Result apply(CommandTestable commandTestable) { + Response resp = commandTestable.getResponse(); + Result.Value resultValue = Result.Value.fromExpected(expected, resp.successful(), resp.error()); + return new Result(resultValue, resultValue.ok() ? ok : nok); + } }); } - public CommandTest(Command command, Result.ExpectedValue expected) { - this(command, expected, null, null); + public CommandTest expect(CommandTestable command, Result.ExpectedValue expected) { + return expect(command, expected, null, null); } public Command getCommand() { - return command; + return testable.getCommand(); } public Response getResponse() { - return response; + return testable.getResponse(); } @Override @@ -47,25 +44,13 @@ public class CommandTest extends Test { if (hasRun) return; - try { - response = command.send(); - } catch (CardException e) { - throw new TestException(e); - } - if (callback != null) { - result = callback.apply(command, response); - } else { - if (response.successful()) { - result = new Result(Result.Value.SUCCESS); - } else { - result = new Result(Result.Value.FAILURE); - } - } + testable.run(); + result = callback.apply(testable); hasRun = true; } @Override public String getDescription() { - return response.getDescription(); + return null; } } -- cgit v1.2.3-70-g09d2