You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tester.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2000 Xerox Corporation.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing;
  13. import org.aspectj.bridge.IMessage;
  14. import org.aspectj.bridge.IMessageHandler;
  15. import org.aspectj.bridge.Message;
  16. import org.aspectj.util.LangUtil;
  17. import java.util.*;
  18. import java.io.*;
  19. /**
  20. * Testing client interface for checking results and reporting
  21. * to a delegate IMessageHandler.
  22. * Harnesses providing this interface for test clients must
  23. * set it up by calling
  24. * {@link #setBASEDIR(File)}
  25. * {@link #setMessageHandler(IMessageHandler)} and
  26. * {@link #clear()} for each test, as appropriate.
  27. * (That means that IMessageHandler must be loaded from a class
  28. * loader common to the harness and Tester.)
  29. * If clients submit a failing check, this registers the message
  30. * and throws an AbortException holding the message; this
  31. * AbortException <b>will not</b> have the correct stack trace;
  32. * all the information should be encoded in the message.
  33. * Find any original exception thrown in the message itself.
  34. */
  35. // XXX consider creating exception for new API throwFailure(String m)
  36. public class Tester {
  37. /** delegate for reporting results */
  38. private static IMessageHandler messageHandler;
  39. /** base directory for calculating relative paths to event files */
  40. private static File BASEDIR;
  41. /**
  42. * collection of notes submitted
  43. */
  44. private static Set notes;
  45. /** <code>List</code> to hold events submitted. */
  46. private static List<String> actualEvents = new ArrayList<>();
  47. /** <code>List</code> to hold events we expect. */
  48. private static List<String> expectedEvents = new ArrayList<>();
  49. static {
  50. setBASEDIR(new File("."));
  51. setMessageHandler(IMessageHandler.SYSTEM_ERR);
  52. clear();
  53. }
  54. /**
  55. * Set directory used for calculating relative paths
  56. * (currently only to an events file)
  57. * @param baseDir the File for an existing directory
  58. */
  59. public static void setBASEDIR(File baseDir) {
  60. if (null == baseDir) throw new IllegalArgumentException("null baseDir");
  61. if (!baseDir.isDirectory()) throw new IllegalArgumentException("not a directory: " + baseDir);
  62. BASEDIR = baseDir;
  63. }
  64. public static File getBASEDIR() {
  65. return BASEDIR;
  66. }
  67. /**
  68. * Set the message handler used for this Tester.
  69. * When given a message of kind FAIL, this handler
  70. * must complete abruptly or return false (i.e., not handled completely)
  71. * so the Tester throws an AbortException.
  72. * @see checkFailed(..).
  73. */
  74. public static void setMessageHandler(IMessageHandler handler) {
  75. if (null == handler) throw new IllegalArgumentException("null handler");
  76. if (messageHandler != handler) messageHandler = handler;
  77. }
  78. public static void clear() {
  79. clearNotes();
  80. clearEvents();
  81. }
  82. /** XXX deprecated #clear() */
  83. public static void clearNotes() {
  84. notes = new HashSet();
  85. }
  86. /** XXX deprecated #clear() */
  87. public static void clearEvents() {
  88. actualEvents = new ArrayList<>();
  89. expectedEvents = new ArrayList<>();
  90. }
  91. /** Add an actual event */
  92. public static void event(String s) {
  93. actualEvents.add(s);
  94. }
  95. /**
  96. * Add a note to {@link #notes}.
  97. * @param note Message to add.
  98. * XXX deprecated event(String)
  99. */
  100. public static void note(Object note) {
  101. notes.add(note);
  102. }
  103. /**
  104. * Checks that <code>note</code> was added using {@link #note},
  105. * and fails using <code>note.toString()</code> is it wasn't found.
  106. *
  107. * @param note Message that should've been added using {@link #note}.
  108. * XXX deprecated checkEvent(String)
  109. */
  110. public static void check(Object note) {
  111. check(note, "expected note \"" + note.toString() + "\"");
  112. }
  113. /**
  114. * Checks that <code>note</code> was added using {@link #note},
  115. * and fails using <code>message</code> is it wasn't found.
  116. *
  117. * @param note Message that should've been added using {@link #note}.
  118. * @param message Message with which to fail if <code>node</code>
  119. * wasn't added.
  120. */
  121. public static void check(Object note, String message) {
  122. check(notes.contains(note), message);
  123. }
  124. /**
  125. * Reports that <code>t</code> shouldn't have been thrown.
  126. * using <code>t</code> as the message.
  127. *
  128. * @param t Thrown exception.
  129. * @see #throwable(Throwable,String)
  130. */
  131. public static void throwable(Throwable t) {
  132. throwable(t, null);
  133. }
  134. /**
  135. * Reports that <code>t</code> shouldn't have been thrown.
  136. * using <code>msg</code> as the message.
  137. *
  138. * @param thrown Thrown exception.
  139. * @param msg Message with which to report error.
  140. */
  141. public static void throwable(Throwable thrown, String msg) {
  142. handle(msg, thrown, true);
  143. }
  144. /**
  145. * Report the error <code>message</code> unconditionally.
  146. *
  147. * @param message Error to report.
  148. */
  149. public static void checkFailed(String message) {
  150. handle(message, null, true);
  151. }
  152. /**
  153. * Check that <code>expectedNotes</code> is equal to {@link #notes}
  154. * , fail with <code>msg</code> and create a new instance of {@link #notes}.
  155. * <i>NOTE: <code>expectedNotes</code> is a <code>String</code>, so
  156. * it must match with {@link java.util.HashSet#toString()}</i>.
  157. *
  158. * @param expectedNotes <code>String</code> we expect
  159. * {@link #notes} to match.
  160. * @param msg Message with which to fail.
  161. */
  162. public static void checkAndClear(String expectedNotes, String msg) {
  163. checkEqual(notes, expectedNotes, msg);
  164. clearNotes();
  165. }
  166. /**
  167. * Reports an error using <code>message</code> if
  168. * <code>test == false</code>.
  169. *
  170. * @param test Determines whether we call {@link #checkFailed}.
  171. * @param message Message to pass {@link #checkFailed} if
  172. * <code>test == false</code>.
  173. */
  174. public static void check(boolean test, String message) {
  175. if (!test) checkFailed(message);
  176. }
  177. /**
  178. * Checks that the values of <code>value</code> and
  179. * <code>expectedValue</code> are equal. Both or either
  180. * can be null. Calls {@link #checkFailed} with <code>message</code>
  181. * if the arrays aren't equal.
  182. *
  183. * @param value One test set.
  184. * @param expectedValue The other test set.
  185. * @param message Message with which to fail.
  186. */
  187. public static void checkEqual(Object[] value,
  188. Object[] expectedValue,
  189. String message)
  190. {
  191. if (value == null) {
  192. if (expectedValue == null) return;
  193. checkFailed(message+" null array found");
  194. return;
  195. }
  196. int n = value.length;
  197. if (n != expectedValue.length) {
  198. checkFailed(message+" expected array of length "+expectedValue.length
  199. +" got "+ n);
  200. return;
  201. }
  202. for(int i=0; i<n; i++) {
  203. if (!value[i].equals(expectedValue[i])) {
  204. checkFailed(message+": "+value[i]+" != "+
  205. expectedValue[i]+" at index "+i);
  206. }
  207. }
  208. }
  209. /**
  210. * Fails if <code>s == t</code>.
  211. *
  212. * @param s a known value.
  213. * @param t another known value.
  214. */
  215. public static void checkNotEqual(boolean s, boolean t) {
  216. checkNotEqual(s, t, s + " shouldn't equal " + t);
  217. }
  218. /**
  219. * Fails with message <code>msg</code> if <code>s == t</code>.
  220. *
  221. * @param s a known value.
  222. * @param t another known value.
  223. * @param msg the failure message.
  224. */
  225. public static void checkNotEqual(boolean s, boolean t, String msg) {
  226. if (s == t) checkFailed(msg);
  227. }
  228. /**
  229. * Fails if <code>s == t</code>.
  230. *
  231. * @param s a known value.
  232. * @param t another known value.
  233. */
  234. public static void checkNotEqual(byte s, byte t) {
  235. checkNotEqual(s, t, s + " shouldn't equal " + t);
  236. }
  237. /**
  238. * Fails with message <code>msg</code> if <code>s == t</code>.
  239. *
  240. * @param s a known value.
  241. * @param t another known value.
  242. * @param msg the failure message.
  243. */
  244. public static void checkNotEqual(byte s, byte t, String msg) {
  245. if (s == t) checkFailed(msg);
  246. }
  247. /**
  248. * Fails if <code>s == t</code>.
  249. *
  250. * @param s a known value.
  251. * @param t another known value.
  252. */
  253. public static void checkNotEqual(char s, char t) {
  254. checkNotEqual(s, t, s + " shouldn't equal " + t);
  255. }
  256. /**
  257. * Fails with message <code>msg</code> if <code>s == t</code>.
  258. *
  259. * @param s a known value.
  260. * @param t another known value.
  261. * @param msg the failure message.
  262. */
  263. public static void checkNotEqual(char s, char t, String msg) {
  264. if (s == t) checkFailed(msg);
  265. }
  266. /**
  267. * Fails if <code>s == t</code>.
  268. *
  269. * @param s a known value.
  270. * @param t another known value.
  271. */
  272. public static void checkNotEqual(short s, short t) {
  273. checkNotEqual(s, t, s + " shouldn't equal " + t);
  274. }
  275. /**
  276. * Fails with message <code>msg</code> if <code>s == t</code>.
  277. *
  278. * @param s a known value.
  279. * @param t another known value.
  280. * @param msg the failure message.
  281. */
  282. public static void checkNotEqual(short s, short t, String msg) {
  283. if (s == t) checkFailed(msg);
  284. }
  285. /**
  286. * Fails if <code>s == t</code>.
  287. *
  288. * @param s a known value.
  289. * @param t another known value.
  290. */
  291. public static void checkNotEqual(int s, int t) {
  292. checkNotEqual(s, t, s + " shouldn't equal " + t);
  293. }
  294. /**
  295. * Fails with message <code>msg</code> if <code>s == t</code>.
  296. *
  297. * @param s a known value.
  298. * @param t another known value.
  299. * @param msg the failure message.
  300. */
  301. public static void checkNotEqual(int s, int t, String msg) {
  302. if (s == t) checkFailed(msg);
  303. }
  304. /**
  305. * Fails if <code>s == t</code>.
  306. *
  307. * @param s a known value.
  308. * @param t another known value.
  309. */
  310. public static void checkNotEqual(long s, long t) {
  311. checkNotEqual(s, t, s + " shouldn't equal " + t);
  312. }
  313. /**
  314. * Fails with message <code>msg</code> if <code>s == t</code>.
  315. *
  316. * @param s a known value.
  317. * @param t another known value.
  318. * @param msg the failure message.
  319. */
  320. public static void checkNotEqual(long s, long t, String msg) {
  321. if (s == t) checkFailed(msg);
  322. }
  323. /**
  324. * Fails if <code>s == t</code>.
  325. *
  326. * @param s a known value.
  327. * @param t another known value.
  328. */
  329. public static void checkNotEqual(float s, float t) {
  330. checkNotEqual(s, t, s + " shouldn't equal " + t);
  331. }
  332. /**
  333. * Fails with message <code>msg</code> if <code>s == t</code>.
  334. *
  335. * @param s a known value.
  336. * @param t another known value.
  337. * @param msg the failure message.
  338. */
  339. public static void checkNotEqual(float s, float t, String msg) {
  340. if (s == t) checkFailed(msg);
  341. }
  342. /**
  343. * Fails if <code>s == t</code>.
  344. *
  345. * @param s a known value.
  346. * @param t another known value.
  347. */
  348. public static void checkNotEqual(double s, double t) {
  349. checkNotEqual(s, t, s + " shouldn't equal " + t);
  350. }
  351. /**
  352. * Fails with message <code>msg</code> if <code>s == t</code>.
  353. *
  354. * @param s a known value.
  355. * @param t another known value.
  356. * @param msg the failure message.
  357. */
  358. public static void checkNotEqual(double s, double t, String msg) {
  359. if (s == t) checkFailed(msg);
  360. }
  361. /**
  362. * Fails if <code>s == t</code>.
  363. *
  364. * @param s a known value.
  365. * @param t another known value.
  366. */
  367. public static void checkNotEqual(Object s, Object t) {
  368. checkNotEqual(s, t, s + " shouldn't equal " + t);
  369. }
  370. /**
  371. * Fails with message <code>msg</code> if <code>s == t</code>
  372. * or both <code>s</code> and <code>t</code> are <code>null</code>.
  373. *
  374. * @param s a known value.
  375. * @param t another known value.
  376. * @param msg the failure message.
  377. */
  378. public static void checkNotEqual(Object s, Object t, String msg) {
  379. if ((s != null && s.equals(t)) ||
  380. (t != null && t.equals(s)) ||
  381. (s == null && t == null)) {
  382. checkFailed(msg);
  383. }
  384. }
  385. /**
  386. * Compares <code>value</code> and <code>expectedValue</code>
  387. * with failing message <code>"compare"</code>.
  388. *
  389. * @param value Unkown value.
  390. * @param expectedValue Expected value.
  391. * @see #checkEqual(int,int,String)
  392. */
  393. public static void checkEqual(int value, int expectedValue) {
  394. checkEqual(value, expectedValue, "compare");
  395. }
  396. /**
  397. * Fails if the passed in value is <code>null</code>.
  398. *
  399. * @param o the expected non-null thing.
  400. * @param name the name of <code>o</code>.
  401. */
  402. public static void checkNonNull(Object o, String name) {
  403. if (o == null) checkFailed(name + " shouldn't be null");
  404. }
  405. /**
  406. * Compared <code>value</code> and <code>expectedValue</code>
  407. * and fails with <code>message</code> if they aren't equal.
  408. *
  409. * @param value Unkown value.
  410. * @param expectedValue Expected value.
  411. * @param msg Message with which to fail.
  412. */
  413. public static void checkEqual(int value, int expectedValue, String message) {
  414. if (value == expectedValue) return;
  415. if (value < expectedValue) {
  416. message = message+": "+value+" < "+expectedValue;
  417. } else {
  418. message = message+": "+value+" > "+expectedValue;
  419. }
  420. checkFailed(message);
  421. }
  422. /**
  423. * Compares <code>value</code> and <code>expectedValue</code>
  424. * with failing message <code>"compare"</code>.
  425. *
  426. * @param value Unkown value.
  427. * @param expectedValue Expected value.
  428. * @see #checkEqual(float,float,String)
  429. */
  430. public static void checkEqual(float value, float expectedValue) {
  431. checkEqual(value, expectedValue, "compare");
  432. }
  433. /**
  434. * Compared <code>value</code> and <code>expectedValue</code>
  435. * and fails with <code>message</code> if they aren't equal.
  436. *
  437. * @param value Unkown value.
  438. * @param expectedValue Expected value.
  439. * @param msg Message with which to fail.
  440. */
  441. public static void checkEqual(float value, float expectedValue, String msg) {
  442. if (Float.isNaN(value) && Float.isNaN(expectedValue)) return;
  443. if (value == expectedValue) return;
  444. if (value < expectedValue) {
  445. msg = msg+": "+value+" < "+expectedValue;
  446. } else {
  447. msg = msg+": "+value+" > "+expectedValue;
  448. }
  449. checkFailed(msg);
  450. }
  451. /**
  452. * Compares <code>value</code> and <code>expectedValue</code>
  453. * with failing message <code>"compare"</code>.
  454. *
  455. * @param value Unkown value.
  456. * @param expectedValue Expected value.
  457. * @see #checkEqual(long,long,String)
  458. */
  459. public static void checkEqual(long value, long expectedValue) {
  460. checkEqual(value, expectedValue, "compare");
  461. }
  462. /**
  463. * Compared <code>value</code> and <code>expectedValue</code>
  464. * and fails with <code>message</code> if they aren't equal.
  465. *
  466. * @param value Unkown value.
  467. * @param expectedValue Expected value.
  468. * @param msg Message with which to fail.
  469. */
  470. public static void checkEqual(long value, long expectedValue, String msg) {
  471. if (value == expectedValue) return;
  472. if (value < expectedValue) {
  473. msg = msg+": "+value+" < "+expectedValue;
  474. } else {
  475. msg = msg+": "+value+" > "+expectedValue;
  476. }
  477. checkFailed(msg);
  478. }
  479. /**
  480. * Compares <code>value</code> and <code>expectedValue</code>
  481. * with failing message <code>"compare"</code>.
  482. *
  483. * @param value Unkown value.
  484. * @param expectedValue Expected value.
  485. * @see #checkEqual(double,double,String)
  486. */
  487. public static void checkEqual(double value, double expectedValue) {
  488. checkEqual(value, expectedValue, "compare");
  489. }
  490. /**
  491. * Compared <code>value</code> and <code>expectedValue</code>
  492. * and fails with <code>message</code> if they aren't equal.
  493. *
  494. * @param value Unkown value.
  495. * @param expectedValue Expected value.
  496. * @param msg Message with which to fail.
  497. */
  498. public static void checkEqual(double value, double expectedValue, String msg) {
  499. if (Double.isNaN(value) && Double.isNaN(expectedValue)) return;
  500. if (value == expectedValue) return;
  501. if (value < expectedValue) {
  502. msg = msg+": "+value+" < "+expectedValue;
  503. } else {
  504. msg = msg+": "+value+" > "+expectedValue;
  505. }
  506. checkFailed(msg);
  507. }
  508. /**
  509. * Compares <code>value</code> and <code>expectedValue</code>
  510. * with failing message <code>"compare"</code>.
  511. *
  512. * @param value Unkown value.
  513. * @param expectedValue Expected value.
  514. * @see #checkEqual(short,short,String)
  515. */
  516. public static void checkEqual(short value, short expectedValue) {
  517. checkEqual(value, expectedValue, "compare");
  518. }
  519. /**
  520. * Compared <code>value</code> and <code>expectedValue</code>
  521. * and fails with <code>message</code> if they aren't equal.
  522. *
  523. * @param value Unkown value.
  524. * @param expectedValue Expected value.
  525. * @param msg Message with which to fail.
  526. */
  527. public static void checkEqual(short value, short expectedValue, String msg) {
  528. if (value == expectedValue) return;
  529. if (value < expectedValue) {
  530. msg = msg+": "+value+" < "+expectedValue;
  531. } else {
  532. msg = msg+": "+value+" > "+expectedValue;
  533. }
  534. checkFailed(msg);
  535. }
  536. /**
  537. * Compares <code>value</code> and <code>expectedValue</code>
  538. * with failing message <code>"compare"</code>.
  539. *
  540. * @param value Unkown value.
  541. * @param expectedValue Expected value.
  542. * @see #checkEqual(byte,byte,String)
  543. */
  544. public static void checkEqual(byte value, byte expectedValue) {
  545. checkEqual(value, expectedValue, "compare");
  546. }
  547. /**
  548. * Compares <code>value</code> and <code>expectedValue</code>
  549. * with failing message <code>msg</code>.
  550. *
  551. * @param value Unkown value.
  552. * @param expectedValue Expected value.
  553. * @param msg Message with which to fail.
  554. */
  555. public static void checkEqual(byte value, byte expectedValue, String msg) {
  556. if (value == expectedValue) return;
  557. if (value < expectedValue) {
  558. msg = msg+": "+value+" < "+expectedValue;
  559. } else {
  560. msg = msg+": "+value+" > "+expectedValue;
  561. }
  562. checkFailed(msg);
  563. }
  564. /**
  565. * Compares <code>value</code> and <code>expectedValue</code>
  566. * with failing message <code>"compare"</code>.
  567. *
  568. * @param value Unkown value.
  569. * @param expectedValue Expected value.
  570. * @see #checkEqual(char,char,String)
  571. */
  572. public static void checkEqual(char value, char expectedValue) {
  573. checkEqual(value, expectedValue, "compare");
  574. }
  575. /**
  576. * Compares <code>value</code> and <code>expectedValue</code>
  577. * with failing message <code>msg</code>.
  578. *
  579. * @param value Unkown value.
  580. * @param expectedValue Expected value.
  581. * @param msg Message with which to fail.
  582. */
  583. public static void checkEqual(char value, char expectedValue, String msg) {
  584. if (value == expectedValue) return;
  585. if (value < expectedValue) {
  586. msg = msg+": "+value+" < "+expectedValue;
  587. } else {
  588. msg = msg+": "+value+" > "+expectedValue;
  589. }
  590. checkFailed(msg);
  591. }
  592. /**
  593. * Compares <code>value</code> and <code>expectedValue</code>
  594. * with failing message <code>"compare"</code>.
  595. *
  596. * @param value Unkown value.
  597. * @param expectedValue Expected value.
  598. * @see #checkEqual(boolean,boolean,String)
  599. */
  600. public static void checkEqual(boolean value, boolean expectedValue) {
  601. checkEqual(value, expectedValue, "compare");
  602. }
  603. /**
  604. * Compares <code>value</code> and <code>expectedValue</code>
  605. * with failing message <code>msg</code>.
  606. *
  607. * @param value Unkown value.
  608. * @param expectedValue Expected value.
  609. * @param msg Message with which to fail.
  610. */
  611. public static void checkEqual(boolean value, boolean expectedValue, String msg) {
  612. if (value == expectedValue) return;
  613. msg = msg+": "+value+" != "+expectedValue;
  614. checkFailed(msg);
  615. }
  616. /**
  617. * Checks whether the entries of <code>set</code> are equal
  618. * using <code>equals</code> to the corresponding String in
  619. * <code>expectedSet</code> and fails with message <code>msg</code>.
  620. *
  621. * @param set Unkown set of values.
  622. * @param expectedSet Expected <code>String</code> of values.
  623. * @param msg Message with which to fail.
  624. */
  625. public static void checkEqual(Collection set, String expectedSet, String msg) {
  626. checkEqual(set, LangUtil.split(expectedSet), msg);
  627. }
  628. /**
  629. * Checks whether the entries of <code>set</code> are equal
  630. * using <code>equals</code> to the corresponding entry in
  631. * <code>expectedSet</code> and fails with message <code>msg</code>,
  632. * except that duplicate actual entries are ignored.
  633. * This issues fail messages for each failure; when
  634. * aborting on failure, only the first will be reported.
  635. *
  636. * @param set Unkown set of values.
  637. * @param expectedSet Expected <code>String</code> of values.
  638. * @param msg Message with which to fail.
  639. */
  640. public static void checkEqualIgnoreDups(Collection set, String[] expected, String msg,
  641. boolean ignoreDups) {
  642. String[] diffs = diffIgnoreDups(set, expected, msg, ignoreDups);
  643. if (0 < diffs.length) {
  644. check(false, "" + Arrays.asList(diffs));
  645. }
  646. // for (int i = 0; i < diffs.length; i++) {
  647. // check(false, diffs[i]);
  648. // }
  649. }
  650. /** @return String[] of differences '{un}expected msg "..." {not} found' */
  651. private static String[] diffIgnoreDups(Collection<String> set, String[] expected, String msg,
  652. boolean ignoreDups) {
  653. ArrayList<String> result = new ArrayList<>();
  654. ArrayList<String> actual = new ArrayList<>(set);
  655. BitSet hits = new BitSet();
  656. for (int i = 0; i < expected.length; i++) {
  657. if (!actual.remove(expected[i])) {
  658. result.add(" expected " + msg + " \"" + expected[i] + "\" not found");
  659. } else {
  660. hits.set(i);
  661. if (ignoreDups) {
  662. while (actual.remove(expected[i])) ; // remove all instances of it
  663. }
  664. }
  665. }
  666. for (String act: actual) {
  667. result.add(" unexpected " + msg + " \"" + act + "\" found");
  668. }
  669. return (String[]) result.toArray(new String[0]);
  670. }
  671. /**
  672. * Checks whether the entries of <code>set</code> are equal
  673. * using <code>equals</code> to the corresponding entry in
  674. * <code>expectedSet</code> and fails with message <code>msg</code>.
  675. *
  676. * @param set Unkown set of values.
  677. * @param expectedSet Expected <code>String</code> of values.
  678. * @param msg Message with which to fail.
  679. */
  680. public static void checkEqual(Collection set, String[] expected, String msg) {
  681. checkEqualIgnoreDups(set, expected, msg, false);
  682. }
  683. /**
  684. * Compares <code>value</code> and <code>expectedValue</code>
  685. * with failing message <code>"compare"</code>.
  686. *
  687. * @param value Unkown value.
  688. * @param expectedValue Expected value.
  689. * @see #checkEqual(Object,Object,String)
  690. */
  691. public static void checkEqual(Object value, Object expectedValue) {
  692. checkEqual(value, expectedValue, "compare");
  693. }
  694. /**
  695. * Checks whether the entries of <code>set</code> are equal
  696. * using <code>equals</code> to the corresponding String in
  697. * <code>expectedSet</code> and fails with message <code>msg</code>.
  698. *
  699. * @param set Unkown set of values.
  700. * @param expectedSet Expected <code>String</code> of values.
  701. * @param msg Message with which to fail.
  702. */
  703. public static void checkEqual(Object value, Object expectedValue, String msg) {
  704. if (value == null && expectedValue == null) return;
  705. if (value != null && value.equals(expectedValue)) return;
  706. msg = msg+": "+value+" !equals "+expectedValue;
  707. checkFailed(msg);
  708. }
  709. /**
  710. * Checks whether the entries of <code>set</code> are equal
  711. * using <code>equals</code> to the corresponding String in
  712. * <code>expectedSet</code> and fails with message <code>msg</code>.
  713. *
  714. * @param set Unkown set of values.
  715. * @param expectedSet Expected <code>String</code> of values.
  716. * @param msg Message with which to fail.
  717. */
  718. public static void checkEq(Object value, Object expectedValue, String msg) {
  719. if (value == expectedValue) return;
  720. msg = msg+": "+value+" != "+expectedValue;
  721. checkFailed(msg);
  722. }
  723. /** add expected events */
  724. public static void expectEvent(String s) {
  725. if (null != s) {
  726. expectedEvents.add(s);
  727. }
  728. }
  729. /** add expected events */
  730. public static void expectEvent(Object s) {
  731. if (null != s) {
  732. expectEvent(s.toString());
  733. }
  734. }
  735. /**
  736. * add expected events, parse out ; from string
  737. * Expect those messages in <code>s</code> separated by
  738. * <code>":;, "</code>.
  739. *
  740. * @param s String containg delimited,expected messages.
  741. */
  742. public static void expectEventsInString(String s) {
  743. if (null != s) {
  744. StringTokenizer tok = new StringTokenizer(s, ":;, ");
  745. while (tok.hasMoreTokens()) {
  746. expectEvent(tok.nextToken());
  747. }
  748. }
  749. }
  750. public static void expectEventsInString(String[] ra) {
  751. expectEvents((Object[]) ra);
  752. }
  753. /** add expected events */
  754. public static void expectEvents(Object[] events) {
  755. if (null != events) {
  756. for (int i = 0; i < events.length; i++) {
  757. if (null != events[i]) {
  758. expectEvent(events[i].toString());
  759. }
  760. }
  761. }
  762. }
  763. /** add expected events */
  764. public static void expectEvents(String[] events) {
  765. if (null != events) {
  766. for (int i = 0; i < events.length; i++) {
  767. if (null != events[i]) {
  768. expectEvent(events[i].toString());
  769. }
  770. }
  771. }
  772. }
  773. /** check actual and expected have same members */
  774. public static void checkAllEvents() {
  775. checkAndClearEvents((String[]) expectedEvents.toArray(new String[0]));
  776. }
  777. /** also ignore duplicate actual entries for expected */
  778. public static void checkAllEventsIgnoreDups() {
  779. final boolean ignoreDups = true;
  780. final String[] exp = (String[]) expectedEvents.toArray(new String[0]);
  781. checkEqualIgnoreDups(actualEvents, exp, "event", ignoreDups);
  782. clearEvents();
  783. }
  784. /** Check events, file is line-delimited. If there is a non-match, signalls
  785. * a single error for the first event that does not match the next event in
  786. * the file. The equivalence is {@link #checkEqualLists}. Blank lines are
  787. * ignored. lines that start with '//' are ignored. */
  788. public static void checkEventsFromFile(String eventsFile) {
  789. // XXX bug reads into current expected and checks all - separate read and check
  790. try {
  791. File file = new File(getBASEDIR(), eventsFile); // XXX TestDriver
  792. BufferedReader in = new BufferedReader(new FileReader(file));
  793. //final File parentDir = (null == file? null : file.getParentFile());
  794. String line;
  795. List expEvents = new ArrayList();
  796. while ((line = in.readLine()) != null) {
  797. line = line.trim();
  798. if ((line.length() < 1) || (line.startsWith("//"))) continue;
  799. expEvents.add(line);
  800. }
  801. checkEqualLists(actualEvents, expEvents, " from " + eventsFile);
  802. } catch (IOException ioe) {
  803. throwable(ioe);
  804. }
  805. }
  806. /** Check to see that two lists of strings are the same. Order is important.
  807. * Trimmable whitespace is not important. Case is important.
  808. *
  809. * @param actual one list to check
  810. * @param expected another list
  811. * @param message a context string for the resulting error message if the test fails.
  812. */
  813. public static void checkEqualLists(List/*String*/ actual, List/*String*/ expected,
  814. String message) {
  815. Iterator a = actual.iterator();
  816. Iterator e = expected.iterator();
  817. int ai = 0;
  818. int ei = 0;
  819. for (; a.hasNext(); ) {
  820. if (! e.hasNext()) {
  821. checkFailed("unexpected [" + ai + "] \"" + a.next() + "\" " + message);
  822. return;
  823. }
  824. String a0 = ((String) a.next()).trim();
  825. String e0 = ((String) e.next()).trim();
  826. if (! a0.equals(e0)) {
  827. checkFailed("expected [" + ei + "] \"" + e0
  828. + "\"\n but found [" + ai + "] \"" + a0 + "\"\n " + message);
  829. return;
  830. }
  831. ai++;
  832. ei++;
  833. }
  834. while (e.hasNext()) {
  835. checkFailed("expected [" + ei + "] \"" + e.next() + "\" " + message);
  836. ei++;
  837. }
  838. }
  839. /** Check events, expEvents is space delimited */
  840. public static void checkEvents(String expEvents) {
  841. checkEqual(actualEvents, expEvents, "event");
  842. }
  843. /** Check events, expEvents is an array */
  844. public static void checkEvents(String[] expEvents) {
  845. checkEqual(actualEvents, expEvents, "event");
  846. }
  847. /** Check events and clear after check*/
  848. public static void checkAndClearEvents(String expEvents) {
  849. checkEvents(expEvents);
  850. clearEvents();
  851. }
  852. /** Check events and clear after check*/
  853. public static void checkAndClearEvents(String[] expEvents) {
  854. checkEvents(expEvents);
  855. clearEvents();
  856. }
  857. /** XXX deprecated */
  858. public static void printEvents() { // XXX no clients?
  859. for (Iterator i = actualEvents.iterator(); i.hasNext(); ) {
  860. System.out.println(i.next()); // XXX System.out
  861. }
  862. }
  863. /**
  864. * Report an uncaught exeption as an error
  865. * @param thrown <code>Throwable</code> to print.
  866. * @see #maxStackTrace
  867. */
  868. public void unexpectedExceptionFailure(Throwable thrown) {
  869. handle("unexpectedExceptionFailure", thrown, true);
  870. }
  871. /**
  872. * Handle message by delegation to message handler, doing
  873. * IMessage.FAIL if (fail || (thrown != null) and IMessage.INFO
  874. * otherwise.
  875. */
  876. private static void handle(String message, Throwable thrown, boolean fail) {
  877. final boolean failed = fail || (null != thrown);
  878. IMessage.Kind kind = (failed ? IMessage.FAIL : IMessage.INFO);
  879. IMessage m = new Message(message, kind, thrown, null);
  880. /*final boolean handled = */messageHandler.handleMessage(m);
  881. }
  882. // private static void resofhandle(String message, Throwable thrown, boolean fail) {
  883. // /* If FAIL and the message handler returns false (normally),
  884. // * Then this preserves "abort" semantics by throwing an
  885. // * abort exception.
  886. // */
  887. // if (failed) {
  888. // if (handled) {
  889. // String s = "Tester expecting handler to return false or "
  890. // + "complete abruptly when passed a fail, for " + m;
  891. // m = new Message(s, IMessage.DEBUG, null, null);
  892. // messageHandler.handleMessage(m);
  893. // } else {
  894. // throw AbortException.borrowPorter(m);
  895. // }
  896. // }
  897. // }
  898. }