diff --git a/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java b/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java index 827d18f..ae86f7d 100644 --- a/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java +++ b/test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java @@ -1,8 +1,18 @@ package foundation.pEp.jniadapter.test.framework; +import java.io.File; +import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Collectors; import static foundation.pEp.jniadapter.test.framework.TestLogger.log; @@ -51,6 +61,7 @@ public class TestUtils { } + /* Time Utils */ @@ -62,6 +73,8 @@ public class TestUtils { } } + + /* String Utils */ @@ -192,5 +205,99 @@ public class TestUtils { return text; } } + + + + /* + FSUtils + */ + + // possibly returns an empty list + public static List filterbyFilename(List files, String regex) { + List ret = null; + Predicate dotMsg = file -> file.getName().matches(regex); + ret = files.stream().filter(dotMsg).collect(Collectors.toList()); + return ret; + } + + // Possibly returns an empty ArrayList + public static List listFilesByMtime(File dir) { + List ret = new ArrayList<>(); + File[] listOfFiles = dir.listFiles(); + if (listOfFiles != null) { + Collections.addAll(ret, listOfFiles); + ret = sortFilesByMtime(ret); + } + return ret; + } + + // null in null out + private static List sortFilesByMtime(List files) { + List ret = null; + if (files != null) { + ret = new ArrayList(files); + Collections.sort(ret, (o1, o2) -> { + long ret1 = 0; + ret1 = o1.lastModified() - o2.lastModified(); + return (int) clip(ret1, -1, 1); + }); + } + return ret; + } + + public static String readFile(Path path, Charset decoding) throws IOException { + String ret = null; + byte[] encoded = Files.readAllBytes(path); + ret = new String(encoded, decoding); + if (ret == null) { + throw new IOException("Error reading file: " + path); + } + return ret; + } + + public static void writeFile(Path path, String msg, Charset encoding) throws IOException { + Files.write(path, msg.getBytes(encoding)); + } + + public static boolean deleteRecursively(File dir) { + deleteContentsRecursively(dir); + log("deleting: " + dir.getAbsolutePath()); + return dir.delete(); + } + + public static boolean deleteContentsRecursively(File dir) { + boolean ret = false; + File[] allContents = dir.listFiles(); + if (allContents != null) { + for (File file : allContents) { + ret = deleteRecursively(file); + } + } + return ret; + } + + public static List getAvailableCharsetNames() { + List ret = new ArrayList<>(); + for (String key : Charset.availableCharsets().keySet()) { + Charset val = Charset.forName(key); + ret.add(val.name()); + } + return ret; + } + + + + /* + Math Utils + */ + + public static int clip(int val, int min, int max) { + return Math.max(min, Math.min(max, val)); + } + + public static long clip(long val, long min, long max) { + return Math.max(min, Math.min(max, val)); + } + } diff --git a/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java b/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java index d0ecae8..de9648a 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java @@ -1,14 +1,33 @@ package foundation.pEp.jniadapter.test.utils; import static foundation.pEp.jniadapter.test.framework.TestLogger.*; + import foundation.pEp.jniadapter.test.framework.*; import foundation.pEp.jniadapter.*; +import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Map; -import java.util.Vector; +import java.util.*; + + +//public class ABAliceTestContext extends AdapterBaseTestContext { +// FsMQManager transport; +// +// @Override +// public void init() throws Throwable { +// super.init(); +// transport = new FsMQManager(alice.address, "../resources/per-user-dir/alice/inbox"); +// transport.clearOwnQueue(); +// transport.addPeer(bob.address, "../resources/per-user-dir/bob/inbox"); +// +// transport.broadcastSigOnline(); +// transport.waitForPeerOnline(bob.address); +// } +// +//} + public class AdapterBaseTestContext extends AbstractTestContext { // Basic diff --git a/test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/Makefile.conf b/test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/Makefile.conf deleted file mode 100644 index 626ffdd..0000000 --- a/test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/Makefile.conf +++ /dev/null @@ -1,7 +0,0 @@ -JAVA_PKG_BASENAME=foundation.pEp.jniadapter.test.utils.fsmsgqueue.test -JAVA_PKG_BASEPATH=foundation/pEp/jniadapter/test/utils/fsmsgqueue/test -JAVA_CWD=../../../../../../../../ -JAVA=java -enableassertions - -JAVA_CLASSES_FSMSGQUEUE= \ - ../../FsMsgQueue.class diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java new file mode 100644 index 0000000..b0676e3 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java @@ -0,0 +1,76 @@ +package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager; +import foundation.pEp.jniadapter.test.framework.TestUtils; +import foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.FsMsgQueue; + +import java.util.HashMap; +import java.util.Map; + +import static foundation.pEp.jniadapter.test.framework.TestLogger.log; + +public class FsMQManager { + private String ownAddress; + private FsMsgQueue ownQueue; + private Map peerQueues = new HashMap<>(); + + private static String SIGNALONLINEMSG = "SIGONLINE"; + + public FsMQManager(String ownAddr, String ownQueueDir) { + ownAddress = ownAddr; + ownQueue = new FsMsgQueue(ownQueueDir); + } + + public void addPeer(String address, String queueDir) { + FsMsgQueue q = new FsMsgQueue(queueDir); + peerQueues.put(address, q); + } + + public void sendMsgToPeer(String address, String msg) throws UnknownPeerException { + getQueueForPeer(address).add(msg); + } + + public void waitForPeerOnline(String address) { + String msg = ""; + while (msg != "startup from " + address) { + log("Waiting for " + address); + msg = waitForMsg(); + } + } + + public void clearOwnQueue() { + ownQueue.clear(); + } + + public String waitForMsg() { + while (ownQueue.isEmpty()) { + TestUtils.sleep(100); + } + return ownQueue.remove(); + } + + public void sendSigOnlineToPeer(String address) { + String msg = SIGNALONLINEMSG + " " + ownAddress; + log("Sending SIGONLINE to: " + address); + sendMsgToPeer(address, msg); + } + + public void broadcastSigOnline() { + for (String k : peerQueues.keySet()) { + sendSigOnlineToPeer(k); + } + } + + private FsMsgQueue getQueueForPeer(String address) throws UnknownPeerException { + FsMsgQueue ret = peerQueues.get(address); + if (ret == null) { + throw new UnknownPeerException("No peer with address:" + address); + } + return ret; + } +} + + +class UnknownPeerException extends RuntimeException { + UnknownPeerException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/Makefile.conf b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/Makefile.conf new file mode 100644 index 0000000..45cbd0e --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/Makefile.conf @@ -0,0 +1,8 @@ +JAVA_PKG_BASENAME=foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test +JAVA_PKG_BASEPATH=foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test +JAVA_CWD=../../../../../../../../../ +JAVA=java -enableassertions + +JAVA_CLASSES_FSMSGQUEUE= \ + ../../FsMQManager.class \ + ../../../fsmsgqueue/FsMsgQueue.class diff --git a/test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/readwrite/Makefile b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/Makefile similarity index 93% rename from test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/readwrite/Makefile rename to test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/Makefile index 69948da..f4c22bb 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/readwrite/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/Makefile @@ -1,6 +1,6 @@ include ../Makefile.conf -TEST_UNIT_NAME=readwrite +TEST_UNIT_NAME=regression JAVA_CLASSES = \ TestMain.class \ diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java new file mode 100644 index 0000000..98e8f1f --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java @@ -0,0 +1,67 @@ +package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.regression; + +import static foundation.pEp.jniadapter.test.framework.TestLogger.*; +import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.*; +import foundation.pEp.jniadapter.test.framework.*; + +import java.util.HashMap; +import java.util.Map; + + +class FsMsgQueueTestContext extends AbstractTestContext { + Map peers; + String ownAddress = "Alice"; + String ownQDir = "../resources/fsmsgqueue-test/alice"; + String addressBob = "Bob"; + String addressCarol = "Carol"; + + FsMQManager qm; + + @Override + public void init() throws Throwable { + peers = new HashMap<>(); + peers.put(addressBob, "../resources/fsmsgqueue-test/bob"); + peers.put(addressCarol, "../resources/fsmsgqueue-test/carol"); + } +} + +class TestMain { + public static void main(String[] args) throws Exception { + TestSuite.setVerbose(true); + FsMsgQueueTestContext testCtx = new FsMsgQueueTestContext(); + + new TestUnit("Constructor", testCtx, ctx -> { + log("Creating QM for: " + ctx.ownAddress); + ctx.qm = new FsMQManager(ctx.ownAddress, ctx.ownQDir); + }).add(); + + new TestUnit("Clear own queue", testCtx, ctx -> { + ctx.qm.clearOwnQueue(); + }).add(); + + new TestUnit("Add peer bob", testCtx, ctx -> { + for(String k : ctx.peers.keySet()) { + log("Adding peer: " + k); + ctx.qm.addPeer(k, ctx.peers.get(k)); + } + }).add(); + + new TestUnit("Broadcast online", testCtx, ctx -> { + ctx.qm.broadcastSigOnline(); + }).add(); + + new TestUnit("Wait for bob", testCtx, ctx -> { + log("Waiting for Bob to signal online"); + ctx.qm.waitForPeerOnline(ctx.addressBob); + log("Bob is online"); + }).add(); + + + + + TestSuite.run(); + } +} + + + diff --git a/test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/FsMsgQueue.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/FsMsgQueue.java similarity index 70% rename from test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/FsMsgQueue.java rename to test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/FsMsgQueue.java index e4f1de9..91a1086 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/FsMsgQueue.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/FsMsgQueue.java @@ -1,24 +1,13 @@ -package foundation.pEp.jniadapter.test.utils.fsmsgqueue; +package foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue; import static foundation.pEp.jniadapter.test.framework.TestLogger.*; - -import foundation.pEp.jniadapter.test.framework.*; -import foundation.pEp.jniadapter.test.framework.TestUtils.*; -import foundation.pEp.jniadapter.test.utils.*; - +import static foundation.pEp.jniadapter.test.framework.TestUtils.*; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; -import java.lang.reflect.Parameter; -import java.math.BigInteger; import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; -import java.util.function.Predicate; -import java.util.stream.Collectors; import javafx.util.Pair; @@ -85,21 +74,6 @@ public class FsMsgQueue implements Queue { return ret; } - - private String getNewFilename() { - String ret = ""; - List msgFiles = dotMsgFilesSorted(qDir, true); - int newNumber = 0; - if(msgFiles.size() > 0) { - File latest = msgFiles.get(msgFiles.size() - 1); - newNumber = Integer.parseInt(latest.getName().replace(".msg", "")) + 1; - } - ret = TestUtils.padOrClipString(String.valueOf(newNumber),"0",12,Alignment.Right,""); - ret += ".msg"; - - return ret; - } - @Override public boolean offer(String msg) { boolean ret = true; @@ -111,30 +85,6 @@ public class FsMsgQueue implements Queue { return ret; } - private File getOldestMsgFilename() { - File ret = null; - List msgFiles = dotMsgFilesSorted(qDir, false); - if(msgFiles.size() > 0) { - File oldest = msgFiles.get(msgFiles.size() - 1); - ret = oldest; - } - return ret; - } - - private Pair get() throws Exception { - Pair ret = null; - File oldestFile = getOldestMsgFilename(); - log("reading file:" + oldestFile.getName()); - if (oldestFile == null) { - throw new NoSuchElementException("MNo .msg file in dir: " + qDir); - } else { - String fContent = null; - fContent = readFile(oldestFile.toPath(), fileEncoding); - ret = new Pair<>(oldestFile, fContent); - } - return ret; - } - @Override public String remove() throws NoSuchElementException { String ret = null; @@ -199,7 +149,6 @@ public class FsMsgQueue implements Queue { return ret; } - @Override public void clear() { deleteContentsRecursively(qDir); @@ -260,20 +209,47 @@ public class FsMsgQueue implements Queue { } - //Math Utils - public static int clip(int val, int min, int max) { - return Math.max(min, Math.min(max, val)); - } - public static long clip(long val, long min, long max) { - return Math.max(min, Math.min(max, val)); + private String getNewFilename() { + String ret = ""; + List msgFiles = dotMsgFilesSorted(qDir, true); + int newNumber = 0; + if(msgFiles.size() > 0) { + File latest = msgFiles.get(msgFiles.size() - 1); + newNumber = Integer.parseInt(latest.getName().replace(".msg", "")) + 1; + } + ret = padOrClipString(String.valueOf(newNumber),"0",12,Alignment.Right,""); + ret += ".msg"; + + return ret; } + private File getOldestMsgFilename() { + File ret = null; + List msgFiles = dotMsgFilesSorted(qDir, false); + if(msgFiles.size() > 0) { + File oldest = msgFiles.get(msgFiles.size() - 1); + ret = oldest; + } + return ret; + } - // File Utils + private Pair get() throws Exception { + Pair ret = null; + File oldestFile = getOldestMsgFilename(); + log("reading file:" + oldestFile.getName()); + if (oldestFile == null) { + throw new NoSuchElementException("MNo .msg file in dir: " + qDir); + } else { + String fContent = null; + fContent = readFile(oldestFile.toPath(), fileEncoding); + ret = new Pair<>(oldestFile, fContent); + } + return ret; + } // Possibly returns an empty List - private static List dotMsgFilesSorted(File dir, boolean ascending) { + private List dotMsgFilesSorted(File dir, boolean ascending) { List ret = new ArrayList<>(); File[] listOfFiles = dir.listFiles(); if (listOfFiles != null) { @@ -295,78 +271,7 @@ public class FsMsgQueue implements Queue { return ret; } - // possibly returns an empty list - private static List filterbyFilename(List files, String regex) { - List ret = null; - Predicate dotMsg = file -> file.getName().matches(regex); - ret = files.stream().filter(dotMsg).collect(Collectors.toList()); - return ret; - } - // Possibly returns an empty ArrayList - private static List listFilesByMtime(File dir) { - List ret = new ArrayList<>(); - File[] listOfFiles = dir.listFiles(); - if (listOfFiles != null) { - Collections.addAll(ret, listOfFiles); - ret = sortFilesByMtime(ret); - } - return ret; - } - - // null in null out - private static List sortFilesByMtime(List files) { - List ret = null; - if (files != null) { - ret = new ArrayList(files); - Collections.sort(ret, (o1, o2) -> { - long ret1 = 0; - ret1 = o1.lastModified() - o2.lastModified(); - return (int) clip(ret1, -1, 1); - }); - } - return ret; - } - - public static String readFile(Path path, Charset decoding) throws IOException { - String ret = null; - byte[] encoded = Files.readAllBytes(path); - ret = new String(encoded, decoding); - if (ret == null) { - throw new IOException("Error reading file: " + path); - } - return ret; - } - - public static void writeFile(Path path, String msg, Charset encoding) throws IOException { - Files.write(path, msg.getBytes(encoding)); - } - - public static boolean deleteRecursively(File dir) { - deleteContentsRecursively(dir); - log("deleting: " + dir.getAbsolutePath()); - return dir.delete(); - } - - public static boolean deleteContentsRecursively(File dir) { - boolean ret = false; - File[] allContents = dir.listFiles(); - if (allContents != null) { - for (File file : allContents) { - ret = deleteRecursively(file); - } - } - return ret; - } - - private static List getAvailableCharsetNames() { - List ret = new ArrayList<>(); - for (String key : Charset.availableCharsets().keySet()) { - Charset val = Charset.forName(key); - ret.add(val.name()); - } - return ret; - } } diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/Makefile.conf b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/Makefile.conf new file mode 100644 index 0000000..5524ae4 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/Makefile.conf @@ -0,0 +1,7 @@ +JAVA_PKG_BASENAME=foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.test +JAVA_PKG_BASEPATH=foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test +JAVA_CWD=../../../../../../../../../ +JAVA=java -enableassertions + +JAVA_CLASSES_FSMSGQUEUE= \ + ../../FsMsgQueue.class diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/Makefile b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/Makefile new file mode 100644 index 0000000..f4c22bb --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/Makefile @@ -0,0 +1,24 @@ +include ../Makefile.conf + +TEST_UNIT_NAME=regression + +JAVA_CLASSES = \ + TestMain.class \ + +JAVA_CLASSES += $(JAVA_CLASSES_FSMSGQUEUE) + +.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/utils/fsmsgqueue/test/readwrite/TestMain.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/TestMain.java similarity index 95% rename from test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/readwrite/TestMain.java rename to test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/TestMain.java index 73d30bb..8b2b619 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/readwrite/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/TestMain.java @@ -1,11 +1,11 @@ -package foundation.pEp.jniadapter.test.utils.fsmsgqueue.test.readwrite; +package foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.test.regression; import static foundation.pEp.jniadapter.test.framework.TestLogger.*; -import static foundation.pEp.jniadapter.test.utils.fsmsgqueue.FsMsgQueue.*; +import static foundation.pEp.jniadapter.test.framework.TestUtils.*; + +import foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.*; import foundation.pEp.jniadapter.test.framework.*; -import foundation.pEp.jniadapter.test.utils.*; -import foundation.pEp.jniadapter.test.utils.fsmsgqueue.*; import java.io.File; import java.util.ArrayList;