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 gov.lanl.isr.sensix.test;
00036
00037 import junit.framework.*;
00038 import java.io.*;
00039 import gov.lanl.isr.sensix.*;
00040 import gov.lanl.isr.sensix.discovery.*;
00041 import gov.lanl.isr.sensix.sensing.*;
00042
00043
00044 public class CorbaTwoLevelTest extends TestCase
00045 {
00046 private class Runner extends Thread
00047 {
00048 private CorbaTwoLevelTest parent;
00049 private int which;
00050 private int num_levels;
00051 private byte debug_level;
00052 private boolean ready;
00053
00054 public Runner(CorbaTwoLevelTest test, int what, int lvls, byte dl) {
00055 parent = test;
00056 which = what;
00057 num_levels = lvls;
00058 debug_level = dl;
00059 ready = false;
00060 }
00061
00062 public void run() {
00063 String[] args = null;
00064 java.util.Properties prop = null;
00065
00066 try {
00067 switch (which) {
00068 case 0:
00069 parent.sclient = new SenseClient(30, num_levels,
00070 args, prop,
00071 "ClientTest_2." + num_levels,
00072 debug_level);
00073 ready = true;
00074 break;
00075
00076 case 1:
00077 parent.sserver = new SenseServer(20, num_levels - 1,
00078 args, prop,
00079 "ServerTest_2." + (num_levels - 1),
00080 debug_level, null);
00081 ready = true;
00082 parent.sserver.run();
00083 break;
00084 }
00085 } catch (DiscoveryException de) {
00086 System.err.println("Discovery exception: " + de.description);
00087 de.printStackTrace(System.err);
00088 fail("Exception starting SENSIX Discovery service");
00089 } catch (Exception e) {
00090 System.err.println("Miscellaneous exception");
00091 e.printStackTrace(System.err);
00092 if (which == 0)
00093 fail("Exception starting SENSIX client: " + e.toString());
00094 else
00095 fail("Exception starting SENSIX server: " + e.toString());
00096 }
00097 }
00098
00099 public void check_ready() {
00100 while (!ready) {
00101 try {
00102 Thread.sleep(10);
00103 } catch (Exception e) {}
00104 }
00105 }
00106 }
00107
00108 public SenseClient sclient;
00109 public SenseServer sserver;
00110 protected Runner runc, runs;
00111
00112 public static final boolean VERBOSE = true;
00113 public static final int INITIALIZATION = 500;
00114 public static final boolean MESSAGE_DUMP = false;
00115
00116
00117 protected void setUp() {
00118 byte dl = 0;
00119 if (VERBOSE)
00120 dl = 2;
00121 System.err.println("START TEST");
00122
00123 runc = new Runner(this, 0, 2, dl);
00124 runc.start();
00125 runs = new Runner(this, 1, 2, dl);
00126 runs.start();
00127
00128 runc.check_ready();
00129 runs.check_ready();
00130 }
00131
00132
00133 protected void tearDown() {
00134 sclient.shutdown();
00135 sserver.shutdown();
00136 try {
00137 runc.join();
00138 runs.join();
00139 } catch (InterruptedException e) {}
00140 System.err.println("STOP TEST");
00141 String id = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
00142 try {
00143 FileWriter os = new FileWriter("jcorba." + id + ".msgct", true);
00144 os.write("--------------------\n");
00145 os.close();
00146 } catch (IOException e) {}
00147 if (MESSAGE_DUMP) {
00148 try {
00149 FileWriter os = new FileWriter("jcorba." + id + ".msgs", true);
00150 os.write("----------------------------------------\n");
00151 os.close();
00152 } catch (IOException e) {}
00153 }
00154 }
00155
00156
00157
00158 public void testStartStop() {
00159 try {
00160 Thread.sleep(INITIALIZATION);
00161 } catch (InterruptedException e) {
00162 System.err.println("Interruption");
00163 }
00164 }
00165
00166
00167 public void testAddCapability() {
00168 try {
00169 Capability loc = (Capability)(new LocationImpl());
00170 sserver.addCapability(SenseCorba.GPS, loc);
00171
00172 Thread.sleep(INITIALIZATION);
00173 } catch (InterruptedException e) {
00174 System.err.println("Interruption");
00175 }
00176 }
00177
00178
00179 public void testTwoLevelTask() {
00180 try {
00181 Capability loc = (Capability)(new LocationImpl());
00182 sserver.addCapability(SenseCorba.GPS, loc);
00183
00184 Thread.sleep(INITIALIZATION);
00185 System.err.println("DISCOVERY END");
00186
00187 String cmd = "recite(spatialseries(sense(sensor=GPS)))";
00188 Sensory f = SenseParser.senseParser(sclient.getPoa(), cmd);
00189 if (sclient.devolve(f) < 0)
00190 fail("Unexpected Discovery exception");
00191 System.err.println("COMMAND SENT");
00192
00193 Thread.sleep(100);
00194
00195 System.err.println("DATA RECVD");
00196 Data[] array = f.results();
00197 if (array == null || array.length == 0 || array[0] == null)
00198 fail("NULL data");
00199 assertEquals(array[0].fresult(), 35.8880556);
00200 assertEquals(array[1].fresult(), -106.3063889);
00201 assertEquals(array[2].fresult(), 2255.52);
00202 } catch (ParseException e) {
00203 System.err.println("Parse Exception in test: ");
00204 e.printStackTrace(System.err);
00205 } catch (org.omg.CORBA.UserException e) {
00206 System.err.println("Servant not active in test: ");
00207 e.printStackTrace(System.err);
00208 } catch (java.io.IOException e) {
00209 System.err.println("I/O Exception in test: ");
00210 e.printStackTrace(System.err);
00211 } catch (InterruptedException e) {
00212 System.err.println("Interruption");
00213 }
00214 }
00215 }