aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/test/Test.java
diff options
context:
space:
mode:
authorJ08nY2017-10-15 00:01:42 +0200
committerJ08nY2017-10-15 00:01:42 +0200
commit88f829e238097343a044f437c2d4cfeb8b6cfdff (patch)
treec58ab4b1c9af55693f80d15fc3f4f3334ceb11cf /src/cz/crcs/ectester/reader/test/Test.java
parente78bd5d010bd6ced2b71d83b88748f9cc8d98d5e (diff)
downloadECTester-88f829e238097343a044f437c2d4cfeb8b6cfdff.tar.gz
ECTester-88f829e238097343a044f437c2d4cfeb8b6cfdff.tar.zst
ECTester-88f829e238097343a044f437c2d4cfeb8b6cfdff.zip
Diffstat (limited to 'src/cz/crcs/ectester/reader/test/Test.java')
-rw-r--r--src/cz/crcs/ectester/reader/test/Test.java175
1 files changed, 123 insertions, 52 deletions
diff --git a/src/cz/crcs/ectester/reader/test/Test.java b/src/cz/crcs/ectester/reader/test/Test.java
index e22e0a4..cd0cf49 100644
--- a/src/cz/crcs/ectester/reader/test/Test.java
+++ b/src/cz/crcs/ectester/reader/test/Test.java
@@ -5,35 +5,16 @@ import cz.crcs.ectester.reader.response.Response;
import javax.smartcardio.CardException;
import java.util.function.BiFunction;
+import java.util.function.Function;
/**
+ * An abstract test that can be run and has a Result.
+ *
* @author Jan Jancar johny@neuromancer.sk
*/
-public class Test {
- private boolean hasRun = false;
- private BiFunction<Command, Response, Result> callback;
- private Result result;
- private Result expected;
- private Command command;
- private Response response;
-
- public Test(Command command, Result expected) {
- this.command = command;
- this.expected = expected;
- }
-
- public Test(Command command, Result expected, BiFunction<Command, Response, Result> callback) {
- this(command, expected);
- this.callback = callback;
- }
-
- public Command getCommand() {
- return command;
- }
-
- public Response getResponse() {
- return response;
- }
+public abstract class Test {
+ boolean hasRun = false;
+ Result result;
public Result getResult() {
if (!hasRun) {
@@ -42,44 +23,134 @@ public class Test {
return result;
}
- public Result getExpected() {
- return expected;
+ public boolean hasRun() {
+ return hasRun;
}
- public boolean ok() {
- return result == expected || expected == Result.ANY;
+ public abstract boolean ok();
+
+ public abstract void run() throws CardException;
+
+ /**
+ * A result of a Test.
+ */
+ public enum Result {
+ SUCCESS,
+ FAILURE,
+ ANY
}
- public void run() throws CardException {
- response = command.send();
- if (callback != null) {
- result = callback.apply(command, response);
- } else {
- if (response.successful()) {
- result = Result.SUCCESS;
+ /**
+ * 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 static class Simple extends Test {
+ private BiFunction<Command, Response, Result> callback;
+ private Result expected;
+ private Command command;
+ private Response response;
+
+ public Simple(Command command, Result expected) {
+ this.command = command;
+ this.expected = expected;
+ }
+
+ public Simple(Command command, Result expected, BiFunction<Command, Response, Result> callback) {
+ this(command, expected);
+ this.callback = callback;
+ }
+
+ public Command getCommand() {
+ return command;
+ }
+
+ public Response getResponse() {
+ return response;
+ }
+
+ public Result getExpected() {
+ return expected;
+ }
+
+ @Override
+ public boolean ok() {
+ return result == expected || expected == Result.ANY;
+ }
+
+ @Override
+ public void run() throws CardException {
+ response = command.send();
+ if (callback != null) {
+ result = callback.apply(command, response);
} else {
- result = Result.FAILURE;
+ if (response.successful()) {
+ result = Result.SUCCESS;
+ } else {
+ result = Result.FAILURE;
+ }
}
+ hasRun = true;
}
- hasRun = true;
- }
- public boolean hasRun() {
- return hasRun;
+ @Override
+ public String toString() {
+ if (hasRun) {
+ return (ok() ? "OK " : "NOK") + " " + response.toString();
+ } else {
+ return "";
+ }
+ }
}
- @Override
- public String toString() {
- if (hasRun) {
- return (ok() ? "OK " : "NOK") + " " + response.toString();
- } else {
- return "";
+ /**
+ * A compound test that runs many Tests and has a Result dependent on all/some of their Results.
+ */
+ public static class Compound extends Test {
+ private Function<Test[], Result> callback;
+ private Test[] tests;
+
+ private Compound(Function<Test[], Result> callback, Test... tests) {
+ this.callback = callback;
+ this.tests = tests;
}
- }
- public enum Result {
- SUCCESS,
- FAILURE,
- ANY
+ public Compound function(Function<Test[], Result> callback, Test... tests) {
+ return new Compound(callback, tests);
+ }
+
+ public Compound all(Result what, Test... all) {
+ return new Compound((tests) -> {
+ for (Test test : tests) {
+ if (test.getResult() != what) {
+ return Result.FAILURE;
+ }
+ }
+ return Result.SUCCESS;
+ }, all);
+ }
+
+ public Compound any(Result what, Test... any) {
+ return new Compound((tests) -> {
+ for (Test test : tests) {
+ if (test.getResult() == what) {
+ return Result.SUCCESS;
+ }
+ }
+ return Result.FAILURE;
+ }, any);
+ }
+
+ @Override
+ public boolean ok() {
+ return result == Result.SUCCESS;
+ }
+
+ @Override
+ public void run() throws CardException {
+ for (Test test: tests) {
+ test.run();
+ }
+ result = callback.apply(tests);
+ }
}
}