PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/org.mwc.asset.legacy/src/ASSET/Util/SupportTesting.java

https://bitbucket.org/ianmayo/debrief
Java | 498 lines | 312 code | 70 blank | 116 comment | 39 complexity | 79e5dfe493266aae4780cc5f4124e414 MD5 | raw file
  1. package ASSET.Util;
  2. import ASSET.GUI.Core.CoreGUISwing;
  3. import ASSET.Models.Decision.TargetType;
  4. import ASSET.Participants.CoreParticipant;
  5. import ASSET.Participants.ParticipantMovedListener;
  6. import ASSET.Participants.Status;
  7. import ASSET.Scenario.Observers.Recording.CSVTrackObserver;
  8. import ASSET.Scenario.Observers.Recording.DebriefReplayObserver;
  9. import ASSET.Scenario.Observers.TrackPlotObserver;
  10. import ASSET.ParticipantType;
  11. import ASSET.ScenarioType;
  12. import MWC.GUI.Editable;
  13. import MWC.GenericData.WorldArea;
  14. import MWC.GenericData.WorldDistance;
  15. import MWC.GenericData.WorldLocation;
  16. import MWC.GenericData.WorldPath;
  17. import org.w3c.dom.Document;
  18. import javax.xml.transform.TransformerException;
  19. import javax.xml.transform.TransformerFactory;
  20. import javax.xml.transform.dom.DOMSource;
  21. import javax.xml.transform.stream.StreamResult;
  22. import java.beans.PropertyEditor;
  23. import java.beans.PropertyEditorManager;
  24. import java.io.ByteArrayOutputStream;
  25. import java.io.FileWriter;
  26. import java.io.IOException;
  27. import java.lang.reflect.InvocationTargetException;
  28. import java.lang.reflect.Method;
  29. import java.util.Collection;
  30. import java.util.HashMap;
  31. import java.util.Iterator;
  32. /**
  33. * *******************************************************************
  34. * utility class providing testing
  35. * *******************************************************************
  36. */
  37. public class SupportTesting extends junit.framework.TestCase
  38. {
  39. static final String TEST_DIR = "./test_reports/";
  40. /**
  41. * our track plot observer, if we have one
  42. */
  43. protected TrackPlotObserver _tpo;
  44. /**
  45. * the list of participants we're listening to
  46. */
  47. protected HashMap<CoreParticipant, ParticipantMovedListener> _listeningList;
  48. /**
  49. * our debrief plot observer, if we have one
  50. */
  51. protected DebriefReplayObserver _dro;
  52. /**
  53. * our csv track observer, if we have one
  54. */
  55. private CSVTrackObserver _cvo;
  56. /**
  57. * constructor - takes the name of this set of tests
  58. *
  59. * @param s
  60. */
  61. public SupportTesting(String s)
  62. {
  63. super(s);
  64. }
  65. protected void outputThisDocument(Document theDoc, String title)
  66. {
  67. outputThis(theDoc, title);
  68. }
  69. /** dummy test to make Eclipse's automated test finder work
  70. * satisfactorily
  71. */
  72. public void testDummy()
  73. {
  74. assertTrue("I'm not really in right now", true);
  75. }
  76. public static void outputThis(Document theDoc, String title)
  77. {
  78. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  79. javax.xml.transform.TransformerFactory factory
  80. = TransformerFactory.newInstance();
  81. try
  82. {
  83. javax.xml.transform.Transformer transformer
  84. = factory.newTransformer();
  85. StreamResult sr = new StreamResult(bos);
  86. transformer.transform(new DOMSource(theDoc), sr);
  87. bos.close();
  88. }
  89. catch (TransformerException e)
  90. {
  91. e.printStackTrace(); //To change body of catch statement use Options | File Templates.
  92. }
  93. catch (IOException e)
  94. {
  95. e.printStackTrace(); //To change body of catch statement use Options | File Templates.
  96. }
  97. System.out.println("=======" + title + "================");
  98. System.out.println(bos.toString());
  99. System.out.println("=====================");
  100. }
  101. /**
  102. * set up to start recording
  103. *
  104. * @param name the name to prefix any files
  105. * @param doPlot whether to produce a plot
  106. * @param doREP whether to produce a replay file
  107. * @param theScenario
  108. */
  109. protected void startRecording(String name,
  110. boolean doPlot,
  111. boolean doREP, boolean doCSV, ScenarioType theScenario)
  112. {
  113. if (doPlot)
  114. {
  115. _tpo = new TrackPlotObserver(TEST_DIR, 600, 600, name + ".png", null, true, true, false, "track plot", true);
  116. _tpo.setup(theScenario);
  117. }
  118. if (doREP)
  119. {
  120. _dro = new DebriefReplayObserver(TEST_DIR, name + ".rep", false, false, true, null, "debrief plot", true);
  121. _dro.setup(theScenario);
  122. }
  123. if (doCSV)
  124. {
  125. _cvo = new CSVTrackObserver(TEST_DIR, name + ".csv", false, false, true, null, "CSV track", true);
  126. _cvo.setup(theScenario);
  127. }
  128. }
  129. /**
  130. * record this status snapshot
  131. *
  132. * @param stat current status
  133. * @param part participant we're looking at
  134. */
  135. protected void recordThis(Status stat,
  136. ParticipantType part, long newTime)
  137. {
  138. if (_tpo != null)
  139. _tpo.processTheseDetails(stat.getLocation(), stat, part);
  140. if (_dro != null)
  141. {
  142. _dro.writeThesePositionDetails(stat.getLocation(), stat, part, newTime);
  143. }
  144. if (_cvo != null)
  145. {
  146. _cvo.writeThesePositionDetails(stat.getLocation(), stat, part, newTime);
  147. }
  148. }
  149. /**
  150. * start listening to this particular participant
  151. */
  152. protected void startListeningTo(final CoreParticipant cp, String name,
  153. boolean doPlot, boolean doRep, boolean doCSV,
  154. ScenarioType theScenario)
  155. {
  156. // are we up and running?
  157. if ((_dro == null) && (_tpo == null) && (_cvo == null))
  158. {
  159. // no, start recording
  160. this.startRecording(name, doPlot, doRep, doCSV, theScenario);
  161. }
  162. ParticipantMovedListener pml = new ParticipantMovedListener()
  163. {
  164. public void moved(Status newStatus)
  165. {
  166. recordThis(newStatus, cp, newStatus.getTime());
  167. }
  168. public void restart(ScenarioType scenario)
  169. {
  170. }
  171. };
  172. // now listen to it
  173. cp.addParticipantMovedListener(pml);
  174. // and remember it
  175. if (_listeningList == null)
  176. _listeningList = new HashMap<CoreParticipant, ParticipantMovedListener>();
  177. _listeningList.put(cp, pml);
  178. }
  179. /**
  180. * tidy things up, close files
  181. *
  182. * @param theScenario
  183. */
  184. protected void endRecording(ScenarioType theScenario)
  185. {
  186. // stop listening to the participants
  187. if (_listeningList != null)
  188. {
  189. for (Iterator<CoreParticipant> iterator = _listeningList.keySet().iterator(); iterator.hasNext();)
  190. {
  191. ParticipantType coreParticipant = (ParticipantType) iterator.next();
  192. ParticipantMovedListener pml = (ParticipantMovedListener) _listeningList.get(coreParticipant);
  193. coreParticipant.removeParticipantMovedListener(pml);
  194. }
  195. // now clear it
  196. _listeningList.clear();
  197. _listeningList = null;
  198. }
  199. if (_tpo != null)
  200. {
  201. _tpo.tearDown(theScenario);
  202. _tpo = null;
  203. }
  204. if (_dro != null)
  205. {
  206. _dro.tearDown(theScenario);
  207. _dro = null;
  208. }
  209. if (_cvo != null)
  210. {
  211. _cvo.tearDown(theScenario);
  212. _cvo = null;
  213. }
  214. }
  215. /**
  216. * create a random location within the indicated area
  217. *
  218. * @param bounding_area the area to create a location within
  219. * @return the new location
  220. */
  221. public static WorldLocation createLocation(WorldArea bounding_area)
  222. {
  223. double theLat = bounding_area.getBottomLeft().getLat();
  224. double theLong = bounding_area.getBottomLeft().getLong();
  225. double theLatDelta = ASSET.Util.RandomGenerator.nextRandom() * (bounding_area.getTopRight().getLat() - theLat);
  226. double theLongDelta = ASSET.Util.RandomGenerator.nextRandom() * (bounding_area.getTopRight().getLong() - theLong);
  227. return new WorldLocation(theLat + theLatDelta, theLong + theLongDelta, 0);
  228. }
  229. /**
  230. * create a location using user-configurable units
  231. *
  232. * @param latVal
  233. * @param longVal
  234. * @return
  235. */
  236. public static WorldLocation createLocation(WorldDistance latVal, WorldDistance longVal)
  237. {
  238. return new WorldLocation(latVal.getValueIn(WorldDistance.DEGS),
  239. longVal.getValueIn(WorldDistance.DEGS), 0);
  240. }
  241. /**
  242. * quickly create a test location, using metre coordinates
  243. *
  244. * @param x_m longitude in metres
  245. * @param y_m latitude in metres
  246. * @return the new location
  247. */
  248. public static WorldLocation createLocation(double x_m, double y_m)
  249. {
  250. return new WorldLocation(MWC.Algorithms.Conversions.m2Degs(y_m),
  251. MWC.Algorithms.Conversions.m2Degs(x_m),
  252. 0);
  253. }
  254. public static void outputLocation(WorldLocation loc)
  255. {
  256. if (loc != null)
  257. {
  258. String res = toXYString(loc);
  259. System.out.print(res);
  260. }
  261. }
  262. public static String toXYString(WorldLocation loc)
  263. {
  264. String res = " x," + (int) MWC.Algorithms.Conversions.Degs2m(loc.getLong());
  265. res += ",y," + (int) MWC.Algorithms.Conversions.Degs2m(loc.getLat());
  266. return res;
  267. }
  268. /**
  269. * output this series of destinations to a file, in replay format
  270. *
  271. * @param fileName
  272. * @param destinations
  273. */
  274. public void outputTheseToRep(String fileName, WorldPath destinations)
  275. {
  276. try
  277. {
  278. FileWriter writer = new FileWriter(TEST_DIR + fileName);
  279. Collection<WorldLocation> points = destinations.getPoints();
  280. int counter = 1;
  281. for (Iterator<WorldLocation> iterator = points.iterator(); iterator.hasNext();)
  282. {
  283. WorldLocation worldLocation = (WorldLocation) iterator.next();
  284. String thisLine = ";CIRCLE: @@ ";
  285. thisLine += MWC.Utilities.TextFormatting.DebriefFormatLocation.toString(worldLocation);
  286. thisLine += " 50 pt_" + counter++ + System.getProperty("line.separator");
  287. writer.write(thisLine);
  288. }
  289. writer.close();
  290. }
  291. catch (IOException e)
  292. {
  293. e.printStackTrace(); //To change body of catch statement use Options | File Templates.
  294. }
  295. }
  296. ////////////////////////////////////////////////////////////
  297. // narrative support
  298. ////////////////////////////////////////////////////////////
  299. /**
  300. * a destination for writing our narrative
  301. */
  302. private static java.io.FileWriter _fo = null;
  303. /**
  304. * get ready to record a narrative
  305. *
  306. * @param fileName the file to record the narrative to
  307. */
  308. public static void setupNarrative(String fileName)
  309. {
  310. if (_fo == null)
  311. {
  312. try
  313. {
  314. _fo = new FileWriter(fileName);
  315. }
  316. catch (IOException e)
  317. {
  318. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  319. }
  320. }
  321. }
  322. /**
  323. * utility testing method to write a line of text to the narrative file (if one has been setup)
  324. *
  325. * @param msg the message to store
  326. * @param trk the track this message relates to
  327. * @param dtg the time of the message
  328. * @param source
  329. */
  330. public static void recordThis(String msg, String trk, long dtg, Object source)
  331. {
  332. try
  333. {
  334. if (_fo != null)
  335. {
  336. _fo.write(";NARRATIVE: " + MWC.Utilities.TextFormatting.DebriefFormatDateTime.toString(dtg) + " " + trk + " " + msg + " (" + source.toString() + ")");
  337. _fo.write(System.getProperty("line.separator"));
  338. _fo.flush();
  339. }
  340. }
  341. catch (IOException e)
  342. {
  343. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  344. }
  345. }
  346. public static void stopNarrative()
  347. {
  348. if (_fo != null)
  349. {
  350. try
  351. {
  352. _fo.flush();
  353. _fo.close();
  354. }
  355. catch (IOException e)
  356. {
  357. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  358. }
  359. }
  360. }
  361. ////////////////////////////////////////////////////////////
  362. // auto-test support
  363. ////////////////////////////////////////////////////////////
  364. public static void callTestMethods(SupportTesting tt)
  365. {
  366. // find and run all methods beginning with test
  367. Method[] methods = tt.getClass().getMethods();
  368. for (int i = 0; i < methods.length; i++)
  369. {
  370. Method thisMethod = methods[i];
  371. if (thisMethod.getName().startsWith("test"))
  372. {
  373. Object params[] = {};
  374. try
  375. {
  376. thisMethod.invoke(tt, params);
  377. System.out.println("called:" + thisMethod.getName());
  378. }
  379. catch (IllegalAccessException e)
  380. {
  381. e.printStackTrace(); //To change body of catch statement use Options | File Templates.
  382. }
  383. catch (IllegalArgumentException e)
  384. {
  385. e.printStackTrace(); //To change body of catch statement use Options | File Templates.
  386. }
  387. catch (InvocationTargetException e)
  388. {
  389. e.printStackTrace(); //To change body of catch statement use Options | File Templates.
  390. }
  391. }
  392. }
  393. }
  394. //////////////////////////////////////////////////
  395. // add property editing testing
  396. //////////////////////////////////////////////////
  397. abstract public static class EditableTesting extends SupportTesting
  398. {
  399. public EditableTesting()
  400. {
  401. super("Testing editable properties");
  402. }
  403. public EditableTesting(String name)
  404. {
  405. super(name);
  406. }
  407. /**
  408. * run through tests of the editable properties
  409. */
  410. // TODO FIX-TEST
  411. public final void NtestMyParams()
  412. {
  413. // just check that our ASSET-specific editors are loaded
  414. PropertyEditor pe = PropertyEditorManager.findEditor(TargetType.class);
  415. if (pe == null)
  416. CoreGUISwing.registerEditors();
  417. // ok, get on with it
  418. Editable toBeTested = getEditable();
  419. Editable.editableTesterSupport.testTheseParameters(toBeTested);
  420. }
  421. /**
  422. * get an object which we can test
  423. *
  424. * @return Editable object which we can check the properties for
  425. */
  426. abstract public Editable getEditable();
  427. }
  428. }