-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUseThisAsTemplate4YourAppTests.java
123 lines (96 loc) · 5.01 KB
/
UseThisAsTemplate4YourAppTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package net.sharksystem.asap.mockAndTemplates;
import net.sharksystem.asap.ASAPException;
import net.sharksystem.asap.ASAPPeer;
import net.sharksystem.asap.apps.testsupport.ASAPTestPeerFS;
import org.junit.Test;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import static net.sharksystem.asap.mockAndTemplates.TestUtils.*;
/**
* An ASAP app communicates by sending messages. First, you must ensure stability in your application.
* Implement methods that serialized and deserialize messages. Implement a listener. Test your app
* by testing scenarios. This class comprises a scenario in two steps.
*
* One tests uses an asap mock. It simulates a message exchange but does not use ASAP at all.
*
* The asapTestExamples is nearly identical with one important difference: The ASAP engines are used.
*
* Note: The test scenarios do not differ at all. Your application and test logic is written once and is tested
* against a mock and later against ASAP. Same interfaces are also available in Android. YOu can spent some
* times by implementing test scenarios. Makes app coding on your target platform much faster.
*
* Test your app first with the mock and afterwards with the ASAP protocol stack. If anything runs smoothly -
* you will have a stable Android or Java app in no time.
*/
public class UseThisAsTemplate4YourAppTests {
private static final int PORT = 7777;
private static int port = 0;
static int getPortNumber() {
if(UseThisAsTemplate4YourAppTests.port == 0) {
UseThisAsTemplate4YourAppTests.port = PORT;
} else {
UseThisAsTemplate4YourAppTests.port++;
}
return UseThisAsTemplate4YourAppTests.port;
}
@Test
public void asapTestExample() throws IOException, ASAPException, InterruptedException {
///////////////// ALICE //////////////////////////////////////////////////////////////
// setup mocked peer / asap application and activity in android
Collection<CharSequence> formats = new ArrayList<>();
formats.add(YOUR_APP_NAME);
ASAPTestPeerFS aliceSimplePeer = new ASAPTestPeerFS(ALICE, formats);
ASAPTestPeerFS bobSimplePeer = new ASAPTestPeerFS(BOB, formats);
// 1st encounter
this.scenarioPart1(aliceSimplePeer, bobSimplePeer);
aliceSimplePeer.startEncounter(getPortNumber(), bobSimplePeer);
// give your app a moment to process
Thread.sleep(1000);
// stop encounter
bobSimplePeer.stopEncounter(aliceSimplePeer);
// give your app a moment to process
Thread.sleep(1000);
// 2nd encounter
this.scenarioPart2(aliceSimplePeer, bobSimplePeer);
aliceSimplePeer.startEncounter(getPortNumber(), bobSimplePeer);
}
public void scenarioPart1(ASAPPeer alicePeer, ASAPPeer bobPeer)
throws IOException, ASAPException, InterruptedException {
// simulate ASAP first encounter with full ASAP protocol stack and engines
System.out.println("+++++++++++++++++++ 1st encounter starts soon ++++++++++++++++++++");
Thread.sleep(50);
// setup message received listener - this should be replaced with your code - you implement a listener.
ASAPMessageReceivedListenerExample aliceMessageReceivedListenerExample =
new ASAPMessageReceivedListenerExample();
alicePeer.addASAPMessageReceivedListener(YOUR_APP_NAME, aliceMessageReceivedListenerExample);
// example - this should be produced by your application
byte[] serializedData = TestUtils.serializeExample(42, "from alice", true);
alicePeer.sendASAPMessage(YOUR_APP_NAME, YOUR_URI, serializedData);
///////////////// BOB //////////////////////////////////////////////////////////////
// this should be replaced with your code - you must implement a listener.
ASAPMessageReceivedListenerExample asapMessageReceivedListenerExample =
new ASAPMessageReceivedListenerExample();
// register your listener (or that mock) with asap connection mock
bobPeer.addASAPMessageReceivedListener(YOUR_APP_NAME, asapMessageReceivedListenerExample);
// bob writes something
bobPeer.sendASAPMessage(YOUR_APP_NAME, YOUR_URI,
TestUtils.serializeExample(43, "from bob", false));
bobPeer.sendASAPMessage(YOUR_APP_NAME, YOUR_URI,
TestUtils.serializeExample(44, "from bob again", false));
// give your app a moment to process
Thread.sleep(500);
}
public void scenarioPart2(ASAPPeer alicePeer, ASAPPeer bobPeer)
throws IOException, ASAPException, InterruptedException {
// bob writes something
bobPeer.sendASAPMessage(YOUR_APP_NAME, YOUR_URI,
TestUtils.serializeExample(43, "third message from bob", false));
// simulate second encounter
System.out.println("+++++++++++++++++++ 2nd encounter starts soon ++++++++++++++++++++");
Thread.sleep(50);
}
public void testScenarioResults() {
// TODO
}
}