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

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