1
1
package net .sharksystem .asap ;
2
2
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 ;
7
6
import org .junit .Test ;
8
7
9
8
import java .io .IOException ;
@@ -14,23 +13,80 @@ public class ASAPJavaApplicationTests {
14
13
public static final String ALICE = "Alice" ;
15
14
public static final String BOB = "Bob" ;
16
15
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" ;
18
18
private static final byte [] TESTMESSAGE = "TestMessage" .getBytes ();
19
+ private static final int PORT = 7777 ;
19
20
20
21
@ Test
21
- public void usageTest () throws IOException , ASAPException {
22
+ public void usageTest () throws IOException , ASAPException , InterruptedException {
22
23
Collection <CharSequence > formats = new HashSet <>();
23
- formats .add (ALICE_APP_FORMAT );
24
+ formats .add (APP_FORMAT );
24
25
25
- ASAPJavaApplication asapJavaApplication =
26
+ ASAPJavaApplication asapJavaApplicationAlice =
26
27
ASAPJavaApplicationFS .createASAPJavaApplication (ALICE , ALICE_ROOT_FOLDER , formats );
27
28
28
29
Collection <CharSequence > recipients = new HashSet <>();
29
30
recipients .add (BOB );
30
31
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 ());
32
34
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
34
90
}
35
91
36
92
private class ListenerExample implements ASAPMessageReceivedListener {
0 commit comments