11package net .sharksystem .asap ;
22
3- import net .sharksystem .asap .apps .ASAPJavaApplication ;
4- import net .sharksystem .asap .apps .ASAPJavaApplicationFS ;
5- import net .sharksystem .asap .apps .ASAPMessageReceivedListener ;
6- import net .sharksystem .asap .apps .ASAPMessages ;
3+ import net .sharksystem .asap .apps .*;
4+ import net .sharksystem .asap .util .ASAPEngineThread ;
5+ import net .sharksystem .cmdline .TCPChannel ;
76import org .junit .Test ;
87
98import java .io .IOException ;
@@ -14,23 +13,80 @@ public class ASAPJavaApplicationTests {
1413 public static final String ALICE = "Alice" ;
1514 public static final String BOB = "Bob" ;
1615 public static final String ALICE_ROOT_FOLDER = "tests/Alice" ;
17- private static final CharSequence ALICE_APP_FORMAT = "TEST_FORMAT" ;
16+ public static final String BOB_ROOT_FOLDER = "tests/Bob" ;
17+ private static final CharSequence APP_FORMAT = "TEST_FORMAT" ;
1818 private static final byte [] TESTMESSAGE = "TestMessage" .getBytes ();
19+ private static final int PORT = 7777 ;
1920
2021 @ Test
21- public void usageTest () throws IOException , ASAPException {
22+ public void usageTest () throws IOException , ASAPException , InterruptedException {
2223 Collection <CharSequence > formats = new HashSet <>();
23- formats .add (ALICE_APP_FORMAT );
24+ formats .add (APP_FORMAT );
2425
25- ASAPJavaApplication asapJavaApplication =
26+ ASAPJavaApplication asapJavaApplicationAlice =
2627 ASAPJavaApplicationFS .createASAPJavaApplication (ALICE , ALICE_ROOT_FOLDER , formats );
2728
2829 Collection <CharSequence > recipients = new HashSet <>();
2930 recipients .add (BOB );
3031
31- asapJavaApplication .sendASAPMessage (ALICE_APP_FORMAT , "yourSchema://yourURI" , recipients , TESTMESSAGE );
32+ asapJavaApplicationAlice .sendASAPMessage (APP_FORMAT , "yourSchema://yourURI" , recipients , TESTMESSAGE );
33+ asapJavaApplicationAlice .setASAPMessageReceivedListener (APP_FORMAT , new ListenerExample ());
3234
33- asapJavaApplication .setASAPMessageReceivedListener (ALICE_APP_FORMAT , new ListenerExample ());
35+ ///////////////////////////////////////////////////////////////////////////////////////////////////
36+ // create a tcp connection //
37+ ///////////////////////////////////////////////////////////////////////////////////////////////////
38+
39+ // create bob engine
40+ ASAPJavaApplication asapJavaApplicationBob =
41+ ASAPJavaApplicationFS .createASAPJavaApplication (BOB , BOB_ROOT_FOLDER , formats );
42+
43+ asapJavaApplicationBob .setASAPMessageReceivedListener (APP_FORMAT , new ListenerExample ());
44+
45+ // create connections for both sides
46+ TCPChannel aliceChannel = new TCPChannel (PORT , true , "a2b" );
47+ TCPChannel bobChannel = new TCPChannel (PORT , false , "b2a" );
48+
49+ aliceChannel .start ();
50+ bobChannel .start ();
51+
52+ // wait to connect
53+ aliceChannel .waitForConnection ();
54+ bobChannel .waitForConnection ();
55+
56+ ///////////////////////////////////////////////////////////////////////////////////////////////////
57+ // run asap connection //
58+ ///////////////////////////////////////////////////////////////////////////////////////////////////
59+
60+ // run engine as thread
61+ ASAPHandleConnectionThread aliceEngineThread = new ASAPHandleConnectionThread (asapJavaApplicationAlice ,
62+ aliceChannel .getInputStream (), aliceChannel .getOutputStream ());
63+
64+ aliceEngineThread .start ();
65+
66+ // let's start communication
67+ asapJavaApplicationBob .handleConnection (bobChannel .getInputStream (), bobChannel .getOutputStream ());
68+
69+ // wait until communication probably ends
70+ System .out .flush ();
71+ System .err .flush ();
72+ Thread .sleep (2000 );
73+ System .out .flush ();
74+ System .err .flush ();
75+
76+ // close connections: note ASAPEngine does NOT close any connection!!
77+ aliceChannel .close ();
78+ bobChannel .close ();
79+ System .out .flush ();
80+ System .err .flush ();
81+ Thread .sleep (1000 );
82+ System .out .flush ();
83+ System .err .flush ();
84+
85+ ///////////////////////////////////////////////////////////////////////////////////////////////////
86+ // test results //
87+ ///////////////////////////////////////////////////////////////////////////////////////////////////
88+
89+ // TODO: should test something
3490 }
3591
3692 private class ListenerExample implements ASAPMessageReceivedListener {
0 commit comments