00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package sensix.test;
00036
00037 import junit.framework.*;
00038 import sensix.*;
00039 import sensix.discovery.*;
00040 import sensix.sensing.*;
00041
00042
00043 public class ThreeLevelTest extends TestCase
00044 {
00045 private class Runner extends Thread
00046 {
00047 private ThreeLevelTest parent;
00048 private int which;
00049 private int num_levels;
00050 private byte debug_level;
00051 private boolean ready;
00052
00053 public Runner(ThreeLevelTest test, int what, int lvls, byte dl) {
00054 parent = test;
00055 which = what;
00056 num_levels = lvls;
00057 debug_level = dl;
00058 ready = false;
00059 }
00060
00061 public void run() {
00062 try {
00063 switch (which) {
00064 case 0:
00065 parent.sclient = new SenseClient(30, num_levels,
00066 "ClientTest_3." + num_levels,
00067 debug_level);
00068 ready = true;
00069 break;
00070
00071 case 1:
00072 parent.sserver_1 = new SenseServer(20, num_levels - 1,
00073 "ServerTest_3." + (num_levels - 1),
00074 debug_level, "");
00075 ready = true;
00076 parent.sserver_1.run();
00077 break;
00078
00079 case 2:
00080 parent.sserver_2 = new SenseServer(10, 1,
00081 "ServerTest_3.1",
00082 debug_level, "");
00083 ready = true;
00084 parent.sserver_2.run();
00085 break;
00086 }
00087 } catch (DiscoveryException de) {
00088 System.err.println("Discovery exception: " + de.description);
00089 de.printStackTrace(System.err);
00090 fail("Exception starting SENSIX Discovery service");
00091 } catch (Exception e) {
00092 System.err.println("Miscellaneous exception");
00093 e.printStackTrace(System.err);
00094 if (which == 0)
00095 fail("Exception starting SENSIX client: " + e.toString());
00096 else
00097 fail("Exception starting SENSIX server: " + e.toString());
00098 }
00099 }
00100
00101 public void check_ready() {
00102 while (!ready) {
00103 try {
00104 Thread.sleep(10);
00105 } catch (Exception e) {}
00106 }
00107 }
00108 }
00109
00110 public SenseClient sclient;
00111 public SenseServer sserver_1;
00112 public SenseServer sserver_2;
00113 protected Runner runc, runs1, runs2;
00114
00115 public static boolean VERBOSE = false;
00116
00117
00118
00119 protected void setUp() {
00120 byte dl = 0;
00121 if (VERBOSE)
00122 dl = 2;
00123
00124 runc = new Runner(this, 0, 3, dl);
00125 runc.start();
00126 runs1 = new Runner(this, 1, 3, dl);
00127 runs1.start();
00128 runs2 = new Runner(this, 2, 3, dl);
00129 runs2.start();
00130
00131 runc.check_ready();
00132 runs1.check_ready();
00133 runs2.check_ready();
00134 }
00135
00136
00137 protected void tearDown() {
00138 sclient.shutdown();
00139 sserver_1.shutdown();
00140 sserver_2.shutdown();
00141 try {
00142 runc.join();
00143 runs1.join();
00144 runs2.join();
00145 } catch (InterruptedException e) {}
00146 }
00147
00148
00149
00150 public void testStartStop() {
00151 try {
00152 Thread.sleep(100);
00153 } catch (InterruptedException e) {
00154 System.err.println("Interruption");
00155 }
00156 }
00157
00158
00159 public void testThreeLevelTask() {
00160 try {
00161 Capability loc1 = (Capability)(new Location(37.505, -130.0302));
00162 sserver_1.addCapability(Sensix.GPS, loc1);
00163 Capability loc2 = (Capability)(new Location());
00164 sserver_2.addCapability(Sensix.GPS, loc2);
00165
00166 Thread.sleep(100);
00167
00168 String cmd = "recite(spatialseries(sense(sensor=GPS)))";
00169 Sensory f = SenseParser.senseParser(cmd);
00170 if (sclient.devolve(f) < 0)
00171 fail("Unexpected Discovery exception");
00172
00173 Thread.sleep(200);
00174
00175 Data[] array = f.results();
00176 if (array == null || array.length == 0 || array[0] == null)
00177 fail("NULL data");
00178 if (array.length != 8)
00179 fail("Not enough data");
00180 assertEquals(array[0].fresult(), 37.505);
00181 assertEquals(array[1].fresult(), -130.0302);
00182 assertEquals(array[2].fresult(), 0.0);
00183 assertEquals(array[4].fresult(), 35.8880556);
00184 assertEquals(array[5].fresult(), -106.3063889);
00185 assertEquals(array[6].fresult(), 2255.52);
00186 } catch (sensix.sensing.ParseException e) {
00187 System.err.println("Parse Exception in test: ");
00188 e.printStackTrace(System.err);
00189 } catch (java.io.IOException e) {
00190 System.err.println("I/O Exception in test: ");
00191 e.printStackTrace(System.err);
00192 } catch (InterruptedException e) {
00193 System.err.println("Interruption");
00194 }
00195 }
00196
00197
00198 public void testDetask() {
00199 try {
00200 Capability loc1 = (Capability)(new Location(37.505, -130.0302));
00201 sserver_1.addCapability(Sensix.GPS, loc1);
00202 Capability loc2 = (Capability)(new Location());
00203 sserver_2.addCapability(Sensix.GPS, loc2);
00204
00205 Thread.sleep(100);
00206
00207 String cmd = "recite(spatialseries(sense(sensor=GPS)))";
00208 Sensory f = SenseParser.senseParser(cmd);
00209 if (sclient.devolve(f) < 0)
00210 fail("Unexpected Discovery exception");
00211
00212 Thread.sleep(50);
00213 sclient.detask(f);
00214
00215 Thread.sleep(200);
00216 Data[] array = f.results();
00217
00218 if (array != null && array.length != 0 && array.length != 4)
00219 fail("Task cancel failed");
00220 } catch (sensix.sensing.ParseException e) {
00221 System.err.println("Parse Exception in test: ");
00222 e.printStackTrace(System.err);
00223 } catch (java.io.IOException e) {
00224 System.err.println("I/O Exception in test: ");
00225 e.printStackTrace(System.err);
00226 } catch (InterruptedException e) {
00227 System.err.println("Interruption");
00228 }
00229 }
00230
00231
00232 public void testReTasking() {
00233 try {
00234 Capability loc1 = (Capability)(new Location(37.505, -130.0302));
00235 sserver_1.addCapability(Sensix.GPS, loc1);
00236 Capability loc2 = (Capability)(new Location());
00237 sserver_2.addCapability(Sensix.GPS, loc2);
00238
00239 Thread.sleep(100);
00240
00241 String cmd = "recite(spatialseries(sense(sensor=GPS)))";
00242 Sensory f = SenseParser.senseParser(cmd);
00243 if (sclient.devolve(f) < 0)
00244 fail("Unexpected Discovery exception");
00245
00246 Thread.sleep(200);
00247
00248 Data[] array = f.results();
00249 if (array == null || array.length == 0 || array[0] == null)
00250 fail("NULL data");
00251 if (array.length != 8)
00252 fail("Not enough data");
00253 assertEquals(array[0].fresult(), 37.505);
00254 assertEquals(array[1].fresult(), -130.0302);
00255 assertEquals(array[2].fresult(), 0.0);
00256 assertEquals(array[4].fresult(), 35.8880556);
00257 assertEquals(array[5].fresult(), -106.3063889);
00258 assertEquals(array[6].fresult(), 2255.52);
00259
00260 cmd = "recite(spatialseries(sense(sensor=GPS)))";
00261 f = SenseParser.senseParser(cmd);
00262 if (sclient.devolve(f) < 0)
00263 fail("Unexpected Discovery exception");
00264
00265 Thread.sleep(200);
00266
00267 array = f.results();
00268 if (array == null || array.length == 0 || array[0] == null)
00269 fail("NULL data");
00270 if (array.length != 8)
00271 fail("Not enough data");
00272 assertEquals(array[0].fresult(), 37.505);
00273 assertEquals(array[1].fresult(), -130.0302);
00274 assertEquals(array[2].fresult(), 0.0);
00275 assertEquals(array[4].fresult(), 35.8880556);
00276 assertEquals(array[5].fresult(), -106.3063889);
00277 assertEquals(array[6].fresult(), 2255.52);
00278
00279 cmd = "recite(spatialseries(sense(sensor=GPS)))";
00280 f = SenseParser.senseParser(cmd);
00281 if (sclient.devolve(f) < 0)
00282 fail("Unexpected Discovery exception");
00283
00284 Thread.sleep(200);
00285
00286 array = f.results();
00287 if (array == null || array.length == 0 || array[0] == null)
00288 fail("NULL data");
00289 if (array.length != 8)
00290 fail("Not enough data");
00291 assertEquals(array[0].fresult(), 37.505);
00292 assertEquals(array[1].fresult(), -130.0302);
00293 assertEquals(array[2].fresult(), 0.0);
00294 assertEquals(array[4].fresult(), 35.8880556);
00295 assertEquals(array[5].fresult(), -106.3063889);
00296 assertEquals(array[6].fresult(), 2255.52);
00297 } catch (sensix.sensing.ParseException e) {
00298 System.err.println("Parse Exception in test: ");
00299 e.printStackTrace(System.err);
00300 } catch (java.io.IOException e) {
00301 System.err.println("I/O Exception in test: ");
00302 e.printStackTrace(System.err);
00303 } catch (InterruptedException e) {
00304 System.err.println("Interruption");
00305 }
00306 }
00307 }