From f09fcbcaff9f195c98335dfa5b02003ed2057de7 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 14 May 2020 23:51:58 +0200 Subject: [PATCH] more TestFramework (reduce dependencies) --- .../pEp/jniadapter/test/Makefile.conf | 6 + .../pEp/jniadapter/test/basic/Makefile | 4 +- .../pEp/jniadapter/test/basic/TestMain.java | 2 +- .../test/framework/AbstractTestContext.java | 5 + .../test/framework/TestContext.java | 53 --------- .../jniadapter/test/framework/TestLogger.java | 53 +++++++++ .../jniadapter/test/framework/TestUnit.java | 25 +++-- .../jniadapter/test/framework/TestUtils.java | 12 ++ .../test/framework/examples/Makefile.conf | 10 ++ .../framework/examples/helloworld/Makefile | 25 +++++ .../examples/helloworld/TestMain.java | 23 ++++ .../pEp/jniadapter/test/jni100/Makefile | 35 ++++++ .../pEp/jniadapter/test/jni100/TestMain.java | 27 +++++ .../pEp/jniadapter/test/jni88/Makefile | 3 +- .../pEp/jniadapter/test/jni91/Makefile | 3 +- .../pEp/jniadapter/test/jni92/Makefile | 5 +- .../pEp/jniadapter/test/jni92/TestMain.java | 25 +++-- .../pEp/jniadapter/test/jni94/Makefile | 3 +- .../pEp/jniadapter/test/jni94/TestMain.java | 31 ++---- .../pEp/jniadapter/test/regression/Makefile | 4 +- .../jniadapter/test/regression/TestMain.java | 3 +- .../pEp/jniadapter/test/template/Makefile | 7 +- .../jniadapter/test/template/TestMain.java | 4 +- .../test/utils/AdapterBaseTestContext.java | 105 ++++++++++++++++++ .../{TestUtils.java => AdapterTestUtils.java} | 54 +-------- .../jniadapter/test/utils/TestCallbacks.java | 19 ++-- 26 files changed, 371 insertions(+), 175 deletions(-) create mode 100644 test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java delete mode 100644 test/java/foundation/pEp/jniadapter/test/framework/TestContext.java create mode 100644 test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java create mode 100644 test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java create mode 100644 test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf create mode 100644 test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/Makefile create mode 100644 test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java create mode 100644 test/java/foundation/pEp/jniadapter/test/jni100/Makefile create mode 100644 test/java/foundation/pEp/jniadapter/test/jni100/TestMain.java create mode 100644 test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java rename test/java/foundation/pEp/jniadapter/test/utils/{TestUtils.java => AdapterTestUtils.java} (80%) diff --git a/test/java/foundation/pEp/jniadapter/test/Makefile.conf b/test/java/foundation/pEp/jniadapter/test/Makefile.conf index 0504c6a..c275826 100644 --- a/test/java/foundation/pEp/jniadapter/test/Makefile.conf +++ b/test/java/foundation/pEp/jniadapter/test/Makefile.conf @@ -9,6 +9,12 @@ CLASSPATH=.:$(REPOROOT)/src JAVA=java -Xcheck:jni -cp $(CLASSPATH) -Djava.library.path=$(CLASSPATH) +JAVA_CLASSES_FRAMEWORK= \ + ../framework/TestUnit.class \ + ../framework/AbstractTestContext.class \ + ../framework/TestLogger.class \ + ../framework/TestUtils.class + ifdef ENGINE_LIB_PATH ifeq ($(PLATFORM),linux) diff --git a/test/java/foundation/pEp/jniadapter/test/basic/Makefile b/test/java/foundation/pEp/jniadapter/test/basic/Makefile index 4d557f0..1383458 100644 --- a/test/java/foundation/pEp/jniadapter/test/basic/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/basic/Makefile @@ -5,8 +5,8 @@ TEST_UNIT_NAME=basic JAVA_CLASSES = \ SyncCallbacks.class \ - TestMain.class \ - ../utils/TestUtils.class + TestMain.class + .PHONY: compile run test clean diff --git a/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java b/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java index 1926ca9..d326246 100644 --- a/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java @@ -1,6 +1,6 @@ package foundation.pEp.jniadapter.test.basic; import foundation.pEp.jniadapter.*; -import foundation.pEp.jniadapter.test.utils.TestUtils; + import java.util.Vector; import java.net.URL; import java.net.URLClassLoader; diff --git a/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java b/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java new file mode 100644 index 0000000..92cb36d --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java @@ -0,0 +1,5 @@ +package foundation.pEp.jniadapter.test.framework; + +public interface AbstractTestContext { + void init() throws Throwable; +} \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestContext.java b/test/java/foundation/pEp/jniadapter/test/framework/TestContext.java deleted file mode 100644 index 63a9e37..0000000 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestContext.java +++ /dev/null @@ -1,53 +0,0 @@ -package foundation.pEp.jniadapter.test.framework; -import foundation.pEp.jniadapter.test.utils.TestUtils; -import foundation.pEp.jniadapter.*; - -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Vector; - -public class TestContext { - public Sync.DefaultCallback cb = new Sync.DefaultCallback(); - public Identity alice; - public Identity bob; - public Message msgToSelf; - public Message msgToBob; - public Vector vID; - public Vector vStr; - public byte[] key; - private String fileName = "../resources/test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"; - public Engine engine; - - public TestContext() { } - - public void init() throws Exception { - vID = new Vector(); - vStr = new Vector(); - - engine = new Engine(); - - alice = new Identity(); - alice.user_id = "23"; - alice.address = "alice@peptest.org"; - alice.me = true; - - bob = new Identity(); - bob.user_id = "42"; - bob.address = "bob@peptest.org"; - - msgToSelf = TestUtils.makeNewTestMessage(alice, alice, Message.Direction.Outgoing); - msgToBob = TestUtils.makeNewTestMessage(alice, bob, Message.Direction.Outgoing); - - vID.add(bob); - vStr.add("StringItem"); - - try { - Path path = Paths.get(fileName); - key = Files.readAllBytes(path); - } catch (Exception e) { - TestUtils.log("Could not open key file:" + fileName); - throw e; - } - } -} \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java b/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java new file mode 100644 index 0000000..d3c594f --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java @@ -0,0 +1,53 @@ +// +// Source code recreated from a .class file by IntelliJ IDEA +// (powered by Fernflower decompiler) +// + +package foundation.pEp.jniadapter.test.framework; + +public class TestLogger { + private static boolean logEnabled = true; + + public TestLogger() { + } + + public static void setLoggingEnabled(boolean enabled) { + logEnabled = enabled; + } + + public static boolean isLoggingEnabled() { + return logEnabled; + } + + public static void log(String msg) { + if (logEnabled) { + String threadStr = String.format("%-10s", Thread.currentThread().getName()); + String logStr = threadStr + ": " + msg; + System.out.println(logStr); + } + + } + + public static void logH1(String msg) { + log(getDecoratedString(msg, "=")); + } + + public static void logH2(String msg) { + log(getDecoratedString(msg, "-")); + } + + private static String getDecoratedString(String msg, String decoration) { + byte var2 = 80; + String ret = ""; + + for(int i = 0; (double)i < Math.ceil((double)((var2 - msg.length() + 2) / 2)); ++i) { + ret = ret + decoration; + } + + return ret + " " + msg + " " + ret; + } + + public static void logSectEnd(String msg) { + log(msg + "\n"); + } +} diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java index 607ff51..4df9ba2 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java @@ -1,29 +1,32 @@ package foundation.pEp.jniadapter.test.framework; -import foundation.pEp.jniadapter.test.utils.TestUtils; import java.util.function.Consumer; -public class TestUnit implements Runnable { +public class TestUnit implements Runnable { String testUnitName = "default test unit"; T ctx; Consumer lambda; - public TestUnit(String name, T c, Consumer consumer) { - testUnitName = name; - lambda = consumer; - ctx = c; + public TestUnit(String name, T context, Consumer lambda) { + this.testUnitName = name; + this.lambda = lambda; + this.ctx = context; } public void run() { - TestUtils.logH1(testUnitName); + TestLogger.logH1(testUnitName); + try { + //Init the Context ctx.init(); + //Run the test against the context lambda.accept(ctx); - } catch (Throwable e) { - TestUtils.logH1("TestUnit FAILED: " + e.toString()); + } catch (Throwable t) { + //Test fails, upon cought exception, otherwise succeeds + TestLogger.logH1("TestUnit FAILED: " + t.toString()); return; } - TestUtils.logH2("SUCCESS!"); + + TestLogger.logH2("SUCCESS!"); } } - diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java b/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java new file mode 100644 index 0000000..2748782 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java @@ -0,0 +1,12 @@ +package foundation.pEp.jniadapter.test.framework; + +public class TestUtils { + + public static void sleep(int mSec) { + try { + Thread.sleep(mSec); + } catch (InterruptedException ex) { + System.out.println("sleep got interrupted"); + } + } +} \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf b/test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf new file mode 100644 index 0000000..8c12ccd --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf @@ -0,0 +1,10 @@ +JAVA_PKG_BASENAME=foundation.pEp.jniadapter.test.framework.examples +JAVA_PKG_BASEPATH=foundation/pEp/jniadapter/test/framework/examples +JAVA_CWD=../../../../../../../ +JAVA=java + +JAVA_CLASSES_FRAMEWORK= \ + ../../TestUnit.class \ + ../../AbstractTestContext.class \ + ../../TestLogger.class \ + ../../TestUtils.class diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/Makefile b/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/Makefile new file mode 100644 index 0000000..de97c29 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/Makefile @@ -0,0 +1,25 @@ +include ../Makefile.conf + +TEST_UNIT_NAME=helloworld + +JAVA_CLASSES = \ + TestMain.class \ + +# Use the test framework +JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK) + +.PHONY: compile run test clean + +all: compile + $(MAKE) run + +run: compile + cd $(JAVA_CWD);$(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestMain + +compile: $(JAVA_CLASSES) + +%.class: %.java + cd $(JAVA_CWD);pwd;javac $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$< + +clean: + rm -f $(JAVA_CLASSES) diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java b/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java new file mode 100644 index 0000000..60f96b4 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java @@ -0,0 +1,23 @@ +package foundation.pEp.jniadapter.test.framework.examples.helloworld; +import foundation.pEp.jniadapter.test.framework.*; + +class HelloWorldTestContext implements AbstractTestContext { + String name; + + @Override + public void init() throws Throwable { + name = "UnitTestFrameWorkWithoutAName"; + } +} + +class TestMain { + public static void main(String[] args) throws Exception { + new TestUnit("Hello World",new HelloWorldTestContext() , ctx -> { + // do stuff using the context + // Test FAILS on unhandled exception, otherwise SUCCESS + TestLogger.log("Hello World from: " + ctx.name); + }).run(); + } +} + + diff --git a/test/java/foundation/pEp/jniadapter/test/jni100/Makefile b/test/java/foundation/pEp/jniadapter/test/jni100/Makefile new file mode 100644 index 0000000..1b41e07 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni100/Makefile @@ -0,0 +1,35 @@ +include ../../../../../../../Makefile.conf +include ../Makefile.conf + +TEST_UNIT_NAME=jni100 + +JAVA_CLASSES = \ + TestMain.class \ + ../utils/AdapterTestUtils.class \ + ../utils/TestCallbacks.class + +JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK) + +.PHONY: compile run test clean + +all: compile + $(MAKE) run + +run: compile clean-pep-home + cd $(JAVA_CWD);pwd;HOME=$(JAVA_PEP_HOME_DIR) $(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestMain + +compile: $(JAVA_CLASSES) + +%.class: %.java + cd $(JAVA_CWD);javac -cp $(CLASSPATH) $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$< + +clean: + rm -f $(JAVA_CLASSES) + rm -f *.class + rm -f *.log + rm -Rf .gnupg + rm -Rf .lldb + +clean-pep-home: + rm -rf $(PEP_HOME_DIR)/* + rm -rf $(PEP_HOME_DIR)/.pEp \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/jni100/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni100/TestMain.java new file mode 100644 index 0000000..347f18b --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni100/TestMain.java @@ -0,0 +1,27 @@ +package foundation.pEp.jniadapter.test.jni100; +import foundation.pEp.jniadapter.test.framework.*; +import foundation.pEp.jniadapter.*; +import foundation.pEp.jniadapter.test.utils.*; + + +class TestMain { + public static void main(String[] args) throws Exception { + new TestUnit("JNI-100",new AdapterBaseTestContext() , ctx -> { + TestCallbacks cb = new TestCallbacks(); + + ctx.engine.setMessageToSendCallback(cb); + ctx.engine.setNotifyHandshakeCallback(cb); + + ctx.alice = ctx.engine.myself(ctx.alice); + TestLogger.log(AdapterTestUtils.identityToString(ctx.alice, true)); + + Message msg1 = ctx.engine.encrypt_message(ctx.msgToBob, ctx.vStr, Message.EncFormat.PEP); + + ctx.engine.key_reset_all_own_keys(); + + TestLogger.log(AdapterTestUtils.identityToString(ctx.alice, true)); + }).run(); + } +} + + diff --git a/test/java/foundation/pEp/jniadapter/test/jni88/Makefile b/test/java/foundation/pEp/jniadapter/test/jni88/Makefile index 407b9f5..e8aa7aa 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni88/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/jni88/Makefile @@ -4,8 +4,7 @@ include ../Makefile.conf TEST_UNIT_NAME=jni88 JAVA_CLASSES = \ - TestMain.class \ - ../utils/TestUtils.class + TestMain.class .PHONY: compile run test clean diff --git a/test/java/foundation/pEp/jniadapter/test/jni91/Makefile b/test/java/foundation/pEp/jniadapter/test/jni91/Makefile index 0035de8..71fb5b6 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni91/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/jni91/Makefile @@ -4,8 +4,7 @@ include ../Makefile.conf TEST_UNIT_NAME=jni91 JAVA_CLASSES = \ - TestMain.class \ - ../utils/TestUtils.class + TestMain.class .PHONY: compile run test clean diff --git a/test/java/foundation/pEp/jniadapter/test/jni92/Makefile b/test/java/foundation/pEp/jniadapter/test/jni92/Makefile index fac4197..b1a5f4a 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni92/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/jni92/Makefile @@ -4,9 +4,10 @@ include ../Makefile.conf TEST_UNIT_NAME=jni92 JAVA_CLASSES = \ - SyncCallbacks.class \ TestMain.class \ - ../utils/TestUtils.class + ../utils/AdapterTestUtils.class + +JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK) .PHONY: compile run test clean diff --git a/test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java index 8caee4f..04eab30 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java @@ -1,5 +1,6 @@ package foundation.pEp.jniadapter.test.jni92; -import foundation.pEp.jniadapter.test.utils.TestUtils; +import foundation.pEp.jniadapter.test.utils.*; +import foundation.pEp.jniadapter.test.framework.*; import foundation.pEp.jniadapter.*; import java.lang.Thread; @@ -24,7 +25,7 @@ class TestThread extends Thread { } public void run() { - TestUtils.logH1( "Thread Starting"); + TestLogger.logH1( "Thread Starting"); TestMain.TestMainRun(nrEngines, useSharedEngines); } } @@ -35,9 +36,9 @@ class TestMain { public static Engine createNewEngine() throws pEpException { Engine e; - TestUtils.logH2("Creating new Engine"); + TestLogger.logH2("Creating new Engine"); e = new Engine(); - TestUtils.log("Engine created with java object ID: " + e.getId()); + TestLogger.log("Engine created with java object ID: " + e.getId()); return e; } @@ -51,7 +52,7 @@ class TestMain { public static void engineConsumer(Vector ev, Consumer ec) { ev.forEach(e -> { - TestUtils.logH2("engineConsumer: on engine java object ID: " + e.getId()); + TestLogger.logH2("engineConsumer: on engine java object ID: " + e.getId()); ec.accept(e); }); } @@ -60,9 +61,9 @@ class TestMain { Consumer c = (e) -> { Vector v = e.own_identities_retrieve(); - TestUtils.log("own idents: " + v.size()); + TestLogger.log("own idents: " + v.size()); v.forEach( i -> { - TestUtils.log(TestUtils.identityToString(i, true)); + TestLogger.log(AdapterTestUtils.identityToString(i, true)); }); e.getVersion(); e.OpenPGP_list_keyinfo(""); @@ -77,8 +78,8 @@ class TestMain { } public static void main(String[] args) { - TestUtils.logH1("JNI-92 Starting"); - TestUtils.setLoggingEnabled(false); + TestLogger.logH1("JNI-92 Starting"); + TestLogger.setLoggingEnabled(false); int nrTestruns = 1000; boolean multiThreaded = true; boolean useSharedEngines = true; @@ -90,7 +91,7 @@ class TestMain { } for (int run = 0; run < nrTestruns; run++ ) { - TestUtils.logH1("Testrun Nr: " + run); + TestLogger.logH1("Testrun Nr: " + run); if (!multiThreaded) { // Single Threaded TestMainRun(nrEnginesPerThread, useSharedEngines); @@ -109,11 +110,11 @@ class TestMain { try { t.join(); } catch (Exception e) { - TestUtils.log("Exception joining thread" + e.toString()); + TestLogger.log("Exception joining thread" + e.toString()); } }); } - TestUtils.logH1("Testrun DONE" ); + TestLogger.logH1("Testrun DONE" ); System.gc(); // TestUtils.sleep(2000); } diff --git a/test/java/foundation/pEp/jniadapter/test/jni94/Makefile b/test/java/foundation/pEp/jniadapter/test/jni94/Makefile index d4cc4b4..5c221d6 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni94/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/jni94/Makefile @@ -5,7 +5,8 @@ TEST_UNIT_NAME=jni94 JAVA_CLASSES = \ TestMain.class \ - ../utils/TestUtils.class + +JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK) .PHONY: compile run test clean diff --git a/test/java/foundation/pEp/jniadapter/test/jni94/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni94/TestMain.java index a29e7a9..bae1469 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni94/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/jni94/TestMain.java @@ -1,9 +1,7 @@ package foundation.pEp.jniadapter.test.jni94; +import foundation.pEp.jniadapter.test.framework.*; import foundation.pEp.jniadapter.*; -import foundation.pEp.jniadapter.test.utils.TestUtils; - -import java.lang.Thread; - +import foundation.pEp.jniadapter.test.utils.AdapterBaseTestContext; /* This test tries to use the feature described in in JNI-94 @@ -15,21 +13,16 @@ https://pep.foundation/jira/browse/JNI-94 class TestMain { public static void main(String[] args) { - Engine engine; - try { - TestUtils.logH2("Creating new Engine"); - engine = new Engine(); + new TestUnit("JNI-94", new AdapterBaseTestContext(), ctx -> { + TestLogger.logH2("Creating new Engine"); + ctx.engine = new Engine(); Sync.DefaultCallback callbacks = new Sync.DefaultCallback(); - engine.setMessageToSendCallback(callbacks); - TestUtils.logH2("Machine directory: "); - TestUtils.log(engine.getMachineDirectory()); - - TestUtils.logH2("User directory:" ); - TestUtils.log(engine.getUserDirectory()); - } - catch (pEpException ex) { - System.out.println("Cannot load"); - return; - } + ctx.engine.setMessageToSendCallback(callbacks); + TestLogger.logH2("Machine directory: "); + TestLogger.log(ctx.engine.getMachineDirectory()); + + TestLogger.logH2("User directory:" ); + TestLogger.log(ctx.engine.getUserDirectory()); + }).run(); } } diff --git a/test/java/foundation/pEp/jniadapter/test/regression/Makefile b/test/java/foundation/pEp/jniadapter/test/regression/Makefile index e02cf3a..b92525c 100644 --- a/test/java/foundation/pEp/jniadapter/test/regression/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/regression/Makefile @@ -5,7 +5,9 @@ TEST_UNIT_NAME=regression JAVA_CLASSES = \ TestMain.class \ - ../utils/TestUtils.class + ../utils/AdapterBaseTestContext.class + +JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK) .PHONY: compile run test clean diff --git a/test/java/foundation/pEp/jniadapter/test/regression/TestMain.java b/test/java/foundation/pEp/jniadapter/test/regression/TestMain.java index d9ec2cd..5133bd7 100644 --- a/test/java/foundation/pEp/jniadapter/test/regression/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/regression/TestMain.java @@ -1,8 +1,9 @@ package foundation.pEp.jniadapter.test.regression; import foundation.pEp.jniadapter.test.framework.*; +import foundation.pEp.jniadapter.test.utils.*; import foundation.pEp.jniadapter.*; -class RegTestContext extends TestContext { +class RegTestContext extends AdapterBaseTestContext { // enhance the context @Override diff --git a/test/java/foundation/pEp/jniadapter/test/template/Makefile b/test/java/foundation/pEp/jniadapter/test/template/Makefile index 5b3ad94..81b12b9 100644 --- a/test/java/foundation/pEp/jniadapter/test/template/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/template/Makefile @@ -4,10 +4,9 @@ include ../Makefile.conf TEST_UNIT_NAME=template JAVA_CLASSES = \ - TestMain.class \ - ../framework/TestUnit.class \ - ../framework/TestContext.class \ - ../utils/TestUtils.class + TestMain.class + +JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK) .PHONY: compile run test clean diff --git a/test/java/foundation/pEp/jniadapter/test/template/TestMain.java b/test/java/foundation/pEp/jniadapter/test/template/TestMain.java index e3b9348..79a29a8 100644 --- a/test/java/foundation/pEp/jniadapter/test/template/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/template/TestMain.java @@ -1,11 +1,11 @@ package foundation.pEp.jniadapter.test.template; import foundation.pEp.jniadapter.test.framework.*; -import foundation.pEp.jniadapter.*; +import foundation.pEp.jniadapter.test.utils.AdapterBaseTestContext; class TestMain { public static void main(String[] args) throws Exception { - new TestUnit("Test Template",new TestContext() , ctx -> { + new TestUnit("Test Template",new AdapterBaseTestContext() , ctx -> { // do stuff using the context // Test FAILS on unhandled exception, otherwise SUCCESS diff --git a/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java b/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java new file mode 100644 index 0000000..e8d5100 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java @@ -0,0 +1,105 @@ +package foundation.pEp.jniadapter.test.utils; +import foundation.pEp.jniadapter.test.framework.*; +import foundation.pEp.jniadapter.*; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Vector; + +public class AdapterBaseTestContext implements AbstractTestContext { + public Sync.DefaultCallback cb = new Sync.DefaultCallback(); + public Identity alice; + public Identity bob; + public Message msgToSelf; + public Message msgToBob; + public Vector vID; + public Vector vStr; + public byte[] key; + private String fileName = "../resources/test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"; + public Engine engine; + + public AdapterBaseTestContext() { } + + public void init() throws Exception { + vID = new Vector(); + vStr = new Vector(); + + engine = new Engine(); + + alice = new Identity(); + alice.user_id = "23"; + alice.address = "alice@peptest.org"; + alice.me = true; + + bob = new Identity(); + bob.user_id = "42"; + bob.address = "bob@peptest.org"; + + msgToSelf = AdapterTestUtils.makeNewTestMessage(alice, alice, Message.Direction.Outgoing); + msgToBob = AdapterTestUtils.makeNewTestMessage(alice, bob, Message.Direction.Outgoing); + + vID.add(bob); + vStr.add("StringItem"); + + try { + Path path = Paths.get(fileName); + key = Files.readAllBytes(path); + } catch (Exception e) { + TestLogger.log("Could not open key file:" + fileName); + throw e; + } + } +} + + +/*package foundation.pEp.jniadapter.test.framework; +import foundation.pEp.jniadapter.test.utils.*; +import foundation.pEp.jniadapter.*; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Vector; + +public class TestContext { + public TestCallbacks cb = new TestCallbacks(); + public Identity alice; + public Identity bob; + public Message msgToSelf; + public Message msgToBob; + public Vector vID; + public Vector vStr; + public byte[] key; + private String fileName = "../resources/test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"; + public Engine engine; + + public TestContext() { + } + + public void init() throws Exception { + this.vID = new Vector(); + this.vStr = new Vector(); + this.engine = new Engine(); + this.alice = new Identity(); + this.alice.user_id = "23"; + this.alice.address = "alice@peptest.org"; + this.alice.me = true; + this.bob = new Identity(); + this.bob.user_id = "42"; + this.bob.address = "bob@peptest.org"; + this.msgToSelf = AdapterTestUtils.makeNewTestMessage(this.alice, this.alice, Message.Direction.Outgoing); + this.msgToBob = AdapterTestUtils.makeNewTestMessage(this.alice, this.bob, Message.Direction.Outgoing); + this.vID.add(this.bob); + this.vStr.add("StringItem"); + + try { + Path var1 = Paths.get(this.fileName); + this.key = Files.readAllBytes(var1); + } catch (Exception var2) { + TestLogger.log("Could not open key file:" + this.fileName); + throw var2; + } + } +} +*/ \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java b/test/java/foundation/pEp/jniadapter/test/utils/AdapterTestUtils.java similarity index 80% rename from test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java rename to test/java/foundation/pEp/jniadapter/test/utils/AdapterTestUtils.java index 332914c..2445599 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/AdapterTestUtils.java @@ -4,17 +4,7 @@ import foundation.pEp.jniadapter.*; import java.util.ArrayList; import java.util.Vector; -public class TestUtils { - - - public static void sleep(int mSec) { - try { - Thread.sleep(mSec); - } catch (InterruptedException ex) { - System.out.println("sleep got interrupted"); - } - } - +public class AdapterTestUtils { public static String identityToString(Identity i, Boolean full) { String ret = ""; if(full) { @@ -215,46 +205,4 @@ public class TestUtils { msg.setLongmsg("Hi i am the message longmsg"); return msg; } - - // ------------------------ Logging ------------------------ - - private static boolean logEnabled = true; - - public static void setLoggingEnabled(boolean enabled) { - logEnabled = enabled; - } - - public static boolean isLoggingEnabled() { - return logEnabled; - } - - public static void log(String msg) { - if(logEnabled) { - String threadNameFmt = String.format("%-10s", Thread.currentThread().getName()); - String msgOut = threadNameFmt + ": " + msg; - System.out.println(msgOut); - } - } - - public static void logH1(String msg) { - log( getDecoratedString(msg, "=")); - } - - public static void logH2(String msg) { - log( getDecoratedString(msg, "-")); - } - - private static String getDecoratedString(String msg, String s) { - int lineWidth = 80; - String decorationChar = s; - String decorationStr = ""; - for (int i = 0; i < Math.ceil((lineWidth - msg.length() + 2) / 2); i++) { - decorationStr += decorationChar; - } - return decorationStr + " " + msg + " " + decorationStr; - } - - public static void logSectEnd(String msg) { - log(msg + "\n"); - } } \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/utils/TestCallbacks.java b/test/java/foundation/pEp/jniadapter/test/utils/TestCallbacks.java index 96bd7da..15ab223 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/TestCallbacks.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/TestCallbacks.java @@ -1,20 +1,21 @@ package foundation.pEp.jniadapter.test.utils; +import foundation.pEp.jniadapter.test.framework.*; import foundation.pEp.jniadapter.*; public class TestCallbacks implements Sync.MessageToSendCallback, Sync.NotifyHandshakeCallback { public void messageToSend(Message message) { - TestUtils.logH1("Message to send called"); - TestUtils.log("From: " + message.getFrom()); - TestUtils.log("To: " + message.getTo()); - TestUtils.log("Subject: " + message.getShortmsg()); - TestUtils.log("Attachement[0]: " + message.getAttachments().get(0).toString()); + TestLogger.logH1("Message to send called"); + TestLogger.log("From: " + message.getFrom()); + TestLogger.log("To: " + message.getTo()); + TestLogger.log("Subject: " + message.getShortmsg()); + TestLogger.log("Attachement[0]: " + message.getAttachments().get(0).toString()); } public void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal) { - TestUtils.logH1("Notify handshake called"); - TestUtils.log("myself: " + TestUtils.identityToString(myself, false)); - TestUtils.log("Partner: " + TestUtils.identityToString(partner, false)); - TestUtils.log("Signal: " + signal); + TestLogger.logH1("Notify handshake called"); + TestLogger.log("myself: " + AdapterTestUtils.identityToString(myself, false)); + TestLogger.log("Partner: " + AdapterTestUtils.identityToString(partner, false)); + TestLogger.log("Signal: " + signal); } }