Browse Source

more TestFramework (reduce dependencies)

JNI-104
heck 5 years ago
parent
commit
f09fcbcaff
  1. 6
      test/java/foundation/pEp/jniadapter/test/Makefile.conf
  2. 4
      test/java/foundation/pEp/jniadapter/test/basic/Makefile
  3. 2
      test/java/foundation/pEp/jniadapter/test/basic/TestMain.java
  4. 5
      test/java/foundation/pEp/jniadapter/test/framework/AbstractTestContext.java
  5. 53
      test/java/foundation/pEp/jniadapter/test/framework/TestContext.java
  6. 53
      test/java/foundation/pEp/jniadapter/test/framework/TestLogger.java
  7. 25
      test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java
  8. 12
      test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java
  9. 10
      test/java/foundation/pEp/jniadapter/test/framework/examples/Makefile.conf
  10. 25
      test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/Makefile
  11. 23
      test/java/foundation/pEp/jniadapter/test/framework/examples/helloworld/TestMain.java
  12. 35
      test/java/foundation/pEp/jniadapter/test/jni100/Makefile
  13. 27
      test/java/foundation/pEp/jniadapter/test/jni100/TestMain.java
  14. 3
      test/java/foundation/pEp/jniadapter/test/jni88/Makefile
  15. 3
      test/java/foundation/pEp/jniadapter/test/jni91/Makefile
  16. 5
      test/java/foundation/pEp/jniadapter/test/jni92/Makefile
  17. 25
      test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java
  18. 3
      test/java/foundation/pEp/jniadapter/test/jni94/Makefile
  19. 31
      test/java/foundation/pEp/jniadapter/test/jni94/TestMain.java
  20. 4
      test/java/foundation/pEp/jniadapter/test/regression/Makefile
  21. 3
      test/java/foundation/pEp/jniadapter/test/regression/TestMain.java
  22. 7
      test/java/foundation/pEp/jniadapter/test/template/Makefile
  23. 4
      test/java/foundation/pEp/jniadapter/test/template/TestMain.java
  24. 105
      test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java
  25. 54
      test/java/foundation/pEp/jniadapter/test/utils/AdapterTestUtils.java
  26. 19
      test/java/foundation/pEp/jniadapter/test/utils/TestCallbacks.java

6
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=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 ifdef ENGINE_LIB_PATH
ifeq ($(PLATFORM),linux) ifeq ($(PLATFORM),linux)

4
test/java/foundation/pEp/jniadapter/test/basic/Makefile

@ -5,8 +5,8 @@ TEST_UNIT_NAME=basic
JAVA_CLASSES = \ JAVA_CLASSES = \
SyncCallbacks.class \ SyncCallbacks.class \
TestMain.class \ TestMain.class
../utils/TestUtils.class
.PHONY: compile run test clean .PHONY: compile run test clean

2
test/java/foundation/pEp/jniadapter/test/basic/TestMain.java

@ -1,6 +1,6 @@
package foundation.pEp.jniadapter.test.basic; package foundation.pEp.jniadapter.test.basic;
import foundation.pEp.jniadapter.*; import foundation.pEp.jniadapter.*;
import foundation.pEp.jniadapter.test.utils.TestUtils;
import java.util.Vector; import java.util.Vector;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;

5
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;
}

53
test/java/foundation/pEp/jniadapter/test/framework/TestContext.java

@ -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<Identity> vID;
public Vector<String> 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<Identity>();
vStr = new Vector<String>();
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;
}
}
}

53
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");
}
}

25
test/java/foundation/pEp/jniadapter/test/framework/TestUnit.java

@ -1,29 +1,32 @@
package foundation.pEp.jniadapter.test.framework; package foundation.pEp.jniadapter.test.framework;
import foundation.pEp.jniadapter.test.utils.TestUtils;
import java.util.function.Consumer; import java.util.function.Consumer;
public class TestUnit<T extends TestContext> implements Runnable { public class TestUnit<T extends AbstractTestContext> implements Runnable {
String testUnitName = "default test unit"; String testUnitName = "default test unit";
T ctx; T ctx;
Consumer<T> lambda; Consumer<T> lambda;
public TestUnit(String name, T c, Consumer<T> consumer) { public TestUnit(String name, T context, Consumer<T> lambda) {
testUnitName = name; this.testUnitName = name;
lambda = consumer; this.lambda = lambda;
ctx = c; this.ctx = context;
} }
public void run() { public void run() {
TestUtils.logH1(testUnitName); TestLogger.logH1(testUnitName);
try { try {
//Init the Context
ctx.init(); ctx.init();
//Run the test against the context
lambda.accept(ctx); lambda.accept(ctx);
} catch (Throwable e) { } catch (Throwable t) {
TestUtils.logH1("TestUnit FAILED: " + e.toString()); //Test fails, upon cought exception, otherwise succeeds
TestLogger.logH1("TestUnit FAILED: " + t.toString());
return; return;
} }
TestUtils.logH2("SUCCESS!");
TestLogger.logH2("SUCCESS!");
} }
} }

12
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");
}
}
}

10
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

25
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)

23
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<HelloWorldTestContext>("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();
}
}

35
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

27
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<AdapterBaseTestContext>("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();
}
}

3
test/java/foundation/pEp/jniadapter/test/jni88/Makefile

@ -4,8 +4,7 @@ include ../Makefile.conf
TEST_UNIT_NAME=jni88 TEST_UNIT_NAME=jni88
JAVA_CLASSES = \ JAVA_CLASSES = \
TestMain.class \ TestMain.class
../utils/TestUtils.class
.PHONY: compile run test clean .PHONY: compile run test clean

3
test/java/foundation/pEp/jniadapter/test/jni91/Makefile

@ -4,8 +4,7 @@ include ../Makefile.conf
TEST_UNIT_NAME=jni91 TEST_UNIT_NAME=jni91
JAVA_CLASSES = \ JAVA_CLASSES = \
TestMain.class \ TestMain.class
../utils/TestUtils.class
.PHONY: compile run test clean .PHONY: compile run test clean

5
test/java/foundation/pEp/jniadapter/test/jni92/Makefile

@ -4,9 +4,10 @@ include ../Makefile.conf
TEST_UNIT_NAME=jni92 TEST_UNIT_NAME=jni92
JAVA_CLASSES = \ JAVA_CLASSES = \
SyncCallbacks.class \
TestMain.class \ TestMain.class \
../utils/TestUtils.class ../utils/AdapterTestUtils.class
JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK)
.PHONY: compile run test clean .PHONY: compile run test clean

25
test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java

@ -1,5 +1,6 @@
package foundation.pEp.jniadapter.test.jni92; 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 foundation.pEp.jniadapter.*;
import java.lang.Thread; import java.lang.Thread;
@ -24,7 +25,7 @@ class TestThread extends Thread {
} }
public void run() { public void run() {
TestUtils.logH1( "Thread Starting"); TestLogger.logH1( "Thread Starting");
TestMain.TestMainRun(nrEngines, useSharedEngines); TestMain.TestMainRun(nrEngines, useSharedEngines);
} }
} }
@ -35,9 +36,9 @@ class TestMain {
public static Engine createNewEngine() throws pEpException { public static Engine createNewEngine() throws pEpException {
Engine e; Engine e;
TestUtils.logH2("Creating new Engine"); TestLogger.logH2("Creating new Engine");
e = 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; return e;
} }
@ -51,7 +52,7 @@ class TestMain {
public static void engineConsumer(Vector<Engine> ev, Consumer<Engine> ec) { public static void engineConsumer(Vector<Engine> ev, Consumer<Engine> ec) {
ev.forEach(e -> { 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); ec.accept(e);
}); });
} }
@ -60,9 +61,9 @@ class TestMain {
Consumer<Engine> c = (e) -> { Consumer<Engine> c = (e) -> {
Vector<Identity> v = e.own_identities_retrieve(); Vector<Identity> v = e.own_identities_retrieve();
TestUtils.log("own idents: " + v.size()); TestLogger.log("own idents: " + v.size());
v.forEach( i -> { v.forEach( i -> {
TestUtils.log(TestUtils.identityToString(i, true)); TestLogger.log(AdapterTestUtils.identityToString(i, true));
}); });
e.getVersion(); e.getVersion();
e.OpenPGP_list_keyinfo(""); e.OpenPGP_list_keyinfo("");
@ -77,8 +78,8 @@ class TestMain {
} }
public static void main(String[] args) { public static void main(String[] args) {
TestUtils.logH1("JNI-92 Starting"); TestLogger.logH1("JNI-92 Starting");
TestUtils.setLoggingEnabled(false); TestLogger.setLoggingEnabled(false);
int nrTestruns = 1000; int nrTestruns = 1000;
boolean multiThreaded = true; boolean multiThreaded = true;
boolean useSharedEngines = true; boolean useSharedEngines = true;
@ -90,7 +91,7 @@ class TestMain {
} }
for (int run = 0; run < nrTestruns; run++ ) { for (int run = 0; run < nrTestruns; run++ ) {
TestUtils.logH1("Testrun Nr: " + run); TestLogger.logH1("Testrun Nr: " + run);
if (!multiThreaded) { if (!multiThreaded) {
// Single Threaded // Single Threaded
TestMainRun(nrEnginesPerThread, useSharedEngines); TestMainRun(nrEnginesPerThread, useSharedEngines);
@ -109,11 +110,11 @@ class TestMain {
try { try {
t.join(); t.join();
} catch (Exception e) { } 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(); System.gc();
// TestUtils.sleep(2000); // TestUtils.sleep(2000);
} }

3
test/java/foundation/pEp/jniadapter/test/jni94/Makefile

@ -5,7 +5,8 @@ TEST_UNIT_NAME=jni94
JAVA_CLASSES = \ JAVA_CLASSES = \
TestMain.class \ TestMain.class \
../utils/TestUtils.class
JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK)
.PHONY: compile run test clean .PHONY: compile run test clean

31
test/java/foundation/pEp/jniadapter/test/jni94/TestMain.java

@ -1,9 +1,7 @@
package foundation.pEp.jniadapter.test.jni94; package foundation.pEp.jniadapter.test.jni94;
import foundation.pEp.jniadapter.test.framework.*;
import foundation.pEp.jniadapter.*; import foundation.pEp.jniadapter.*;
import foundation.pEp.jniadapter.test.utils.TestUtils; import foundation.pEp.jniadapter.test.utils.AdapterBaseTestContext;
import java.lang.Thread;
/* /*
This test tries to use the feature described in in JNI-94 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 { class TestMain {
public static void main(String[] args) { public static void main(String[] args) {
Engine engine; new TestUnit<AdapterBaseTestContext>("JNI-94", new AdapterBaseTestContext(), ctx -> {
try { TestLogger.logH2("Creating new Engine");
TestUtils.logH2("Creating new Engine"); ctx.engine = new Engine();
engine = new Engine();
Sync.DefaultCallback callbacks = new Sync.DefaultCallback(); Sync.DefaultCallback callbacks = new Sync.DefaultCallback();
engine.setMessageToSendCallback(callbacks); ctx.engine.setMessageToSendCallback(callbacks);
TestUtils.logH2("Machine directory: "); TestLogger.logH2("Machine directory: ");
TestUtils.log(engine.getMachineDirectory()); TestLogger.log(ctx.engine.getMachineDirectory());
TestUtils.logH2("User directory:" ); TestLogger.logH2("User directory:" );
TestUtils.log(engine.getUserDirectory()); TestLogger.log(ctx.engine.getUserDirectory());
} }).run();
catch (pEpException ex) {
System.out.println("Cannot load");
return;
}
} }
} }

4
test/java/foundation/pEp/jniadapter/test/regression/Makefile

@ -5,7 +5,9 @@ TEST_UNIT_NAME=regression
JAVA_CLASSES = \ JAVA_CLASSES = \
TestMain.class \ TestMain.class \
../utils/TestUtils.class ../utils/AdapterBaseTestContext.class
JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK)
.PHONY: compile run test clean .PHONY: compile run test clean

3
test/java/foundation/pEp/jniadapter/test/regression/TestMain.java

@ -1,8 +1,9 @@
package foundation.pEp.jniadapter.test.regression; package foundation.pEp.jniadapter.test.regression;
import foundation.pEp.jniadapter.test.framework.*; import foundation.pEp.jniadapter.test.framework.*;
import foundation.pEp.jniadapter.test.utils.*;
import foundation.pEp.jniadapter.*; import foundation.pEp.jniadapter.*;
class RegTestContext extends TestContext { class RegTestContext extends AdapterBaseTestContext {
// enhance the context // enhance the context
@Override @Override

7
test/java/foundation/pEp/jniadapter/test/template/Makefile

@ -4,10 +4,9 @@ include ../Makefile.conf
TEST_UNIT_NAME=template TEST_UNIT_NAME=template
JAVA_CLASSES = \ JAVA_CLASSES = \
TestMain.class \ TestMain.class
../framework/TestUnit.class \
../framework/TestContext.class \ JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK)
../utils/TestUtils.class
.PHONY: compile run test clean .PHONY: compile run test clean

4
test/java/foundation/pEp/jniadapter/test/template/TestMain.java

@ -1,11 +1,11 @@
package foundation.pEp.jniadapter.test.template; package foundation.pEp.jniadapter.test.template;
import foundation.pEp.jniadapter.test.framework.*; import foundation.pEp.jniadapter.test.framework.*;
import foundation.pEp.jniadapter.*; import foundation.pEp.jniadapter.test.utils.AdapterBaseTestContext;
class TestMain { class TestMain {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
new TestUnit<TestContext>("Test Template",new TestContext() , ctx -> { new TestUnit<AdapterBaseTestContext>("Test Template",new AdapterBaseTestContext() , ctx -> {
// do stuff using the context // do stuff using the context
// Test FAILS on unhandled exception, otherwise SUCCESS // Test FAILS on unhandled exception, otherwise SUCCESS

105
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<Identity> vID;
public Vector<String> 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<Identity>();
vStr = new Vector<String>();
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<Identity> vID;
public Vector<String> 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;
}
}
}
*/

54
test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java → test/java/foundation/pEp/jniadapter/test/utils/AdapterTestUtils.java

@ -4,17 +4,7 @@ import foundation.pEp.jniadapter.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Vector; import java.util.Vector;
public class TestUtils { public class AdapterTestUtils {
public static void sleep(int mSec) {
try {
Thread.sleep(mSec);
} catch (InterruptedException ex) {
System.out.println("sleep got interrupted");
}
}
public static String identityToString(Identity i, Boolean full) { public static String identityToString(Identity i, Boolean full) {
String ret = ""; String ret = "";
if(full) { if(full) {
@ -215,46 +205,4 @@ public class TestUtils {
msg.setLongmsg("Hi i am the message longmsg"); msg.setLongmsg("Hi i am the message longmsg");
return msg; 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");
}
} }

19
test/java/foundation/pEp/jniadapter/test/utils/TestCallbacks.java

@ -1,20 +1,21 @@
package foundation.pEp.jniadapter.test.utils; package foundation.pEp.jniadapter.test.utils;
import foundation.pEp.jniadapter.test.framework.*;
import foundation.pEp.jniadapter.*; import foundation.pEp.jniadapter.*;
public class TestCallbacks implements Sync.MessageToSendCallback, Sync.NotifyHandshakeCallback { public class TestCallbacks implements Sync.MessageToSendCallback, Sync.NotifyHandshakeCallback {
public void messageToSend(Message message) { public void messageToSend(Message message) {
TestUtils.logH1("Message to send called"); TestLogger.logH1("Message to send called");
TestUtils.log("From: " + message.getFrom()); TestLogger.log("From: " + message.getFrom());
TestUtils.log("To: " + message.getTo()); TestLogger.log("To: " + message.getTo());
TestUtils.log("Subject: " + message.getShortmsg()); TestLogger.log("Subject: " + message.getShortmsg());
TestUtils.log("Attachement[0]: " + message.getAttachments().get(0).toString()); TestLogger.log("Attachement[0]: " + message.getAttachments().get(0).toString());
} }
public void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal) { public void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal) {
TestUtils.logH1("Notify handshake called"); TestLogger.logH1("Notify handshake called");
TestUtils.log("myself: " + TestUtils.identityToString(myself, false)); TestLogger.log("myself: " + AdapterTestUtils.identityToString(myself, false));
TestUtils.log("Partner: " + TestUtils.identityToString(partner, false)); TestLogger.log("Partner: " + AdapterTestUtils.identityToString(partner, false));
TestUtils.log("Signal: " + signal); TestLogger.log("Signal: " + signal);
} }
} }

Loading…
Cancel
Save