選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RunStatus.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.run;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import org.aspectj.bridge.IMessage;
  19. import org.aspectj.bridge.IMessageHolder;
  20. import org.aspectj.bridge.MessageHandler;
  21. import org.aspectj.testing.util.BridgeUtil;
  22. import org.aspectj.util.LangUtil;
  23. /**
  24. * Default implementation of {@link IRunStatus}.
  25. * @author isberg
  26. */
  27. public class RunStatus implements IRunStatus {
  28. private static int INDEX;
  29. private final String name = "RunStatus[" + INDEX++ +"]";
  30. /** true after isCompleted() evaluated true */
  31. private boolean evaluated;
  32. /** true after starting() called */
  33. private boolean started; // set only in starting()
  34. /** true after finished(int) or thrown(Throwable) called */
  35. private boolean completed; // set only in completed(boolean)
  36. /** contains any id set */
  37. private Object id;
  38. /** after finished(Object) called, contains that parameter */
  39. private Object result;
  40. /** after aborted(Object) called, contains that parameter */
  41. private Object abortRequest;
  42. /** use to set exception thrown, if any */
  43. private Throwable thrown;
  44. /** list of any messages submitted */
  45. private IMessageHolder messageHolder;
  46. /** list of any child status */
  47. private ArrayList children;
  48. /** parent of this status */
  49. private IRunStatus parent;
  50. /** invoker for any subruns */
  51. private Runner runner;
  52. /** controls runResult() */
  53. private IRunValidator validator;
  54. // public RunStatus() {
  55. // reset();
  56. // validator = RunValidator.NORMAL;
  57. // }
  58. public RunStatus(IMessageHolder holder, Runner runner) {
  59. reset(holder, runner);
  60. validator = RunValidator.NORMAL;
  61. }
  62. //------------------- process controls
  63. /**
  64. * Set identifier associated with this run, if any
  65. * @throws IllegalArgumentException if id is null
  66. * @throws IllegalStateException if id has already been set
  67. */
  68. public void setIdentifier(Object id) {
  69. if (null == id) {
  70. throw new IllegalArgumentException("null id");
  71. } else if ((null != this.id) && (id != this.id)) {
  72. throw new IllegalStateException(
  73. "attempt to set id " + this.id + " to " + id);
  74. }
  75. this.id = id;
  76. }
  77. /**
  78. * Set the current validator.
  79. * @param delegate the RunValidatorI to use when calculating runStatus
  80. * @throws IllegalArgumentException if delegate is null
  81. */
  82. public void setValidator(IRunValidator delegate) {
  83. if (null == delegate) {
  84. throw new IllegalArgumentException("null delegate");
  85. }
  86. if (validator != delegate) {
  87. validator = delegate;
  88. }
  89. }
  90. /**
  91. * Call before any start() or after isCompleted() would return true
  92. * to reset this to its pre-start state
  93. * @throws IllegalStateException if start() has been called
  94. * and isCompleted() is not true.
  95. */
  96. public void reset() {
  97. reset((IMessageHolder) null, (Runner) null);
  98. }
  99. /**
  100. * Call before any start() or after isCompleted() would return true
  101. * to reset this to its pre-start state. Does not affect validator.
  102. * @param holder the IMessageHolder to use after resetting.
  103. * @throws IllegalStateException if start() has been called
  104. * and isCompleted() is not true.
  105. */
  106. public void reset(IMessageHolder holder, Runner runner) {
  107. if (null == runner) {
  108. throw new IllegalArgumentException("null runner");
  109. }
  110. if (started && (!isCompleted())) {
  111. throw new IllegalStateException("no reset() until isCompleted");
  112. }
  113. started = false;
  114. completed = false;
  115. result = null;
  116. abortRequest = null;
  117. thrown = null;
  118. parent = null;
  119. id = null;
  120. messageHolder = (null != holder ? holder : new MessageHandler());
  121. if (null != children) {
  122. children.clear();
  123. }
  124. this.runner = runner;
  125. evaluated = false;
  126. }
  127. /**
  128. * Call only once to signal this run has started.
  129. * @throws IllegalStateException if start() has been called
  130. */
  131. public void start() {
  132. if (started) {
  133. throw new IllegalStateException("started already");
  134. } else if (isCompleted()) {
  135. throw new IllegalStateException("start after completed (do reset)");
  136. }
  137. started = true;
  138. }
  139. /**
  140. * Call this or thrown only once after start()
  141. * to signal this run has ended.
  142. * If this represents a void process, use VOID.
  143. * @param result the Object returned by this run.
  144. * @throws IllegalStateException if start() was not called first
  145. * or if either completed(Object) or thrown(Throwable) have been called.
  146. */
  147. public void finish(Object result) {
  148. if (null == result) {
  149. throw new IllegalArgumentException("null result");
  150. } else if (isCompleted()) {
  151. throw new IllegalStateException(
  152. "completed then finish " + result);
  153. }
  154. this.result = result;
  155. }
  156. /**
  157. * Call to signal this run is ending by request.
  158. * If this represents a void process, use VOID.
  159. * If there is no message, use ABORT.
  160. * @param request the Object request to abort,
  161. * or ABORT if none is available.
  162. * @throws IllegalStateException if start() was not called first
  163. * or if either completed(Object) or thrown(Throwable) have been called.
  164. */
  165. public void abort(Object request) {
  166. if (null == request) {
  167. throw new IllegalArgumentException("null request");
  168. } else if (isCompleted()) {
  169. throw new IllegalStateException(
  170. "completed then abort " + request);
  171. }
  172. this.abortRequest = request;
  173. }
  174. /**
  175. * Call this or completed only once after start()
  176. * to signal this run has ended.
  177. * @throws IllegalStateException if start() was not called first
  178. * or if either completed(Object) or thrown(Throwable) have been called.
  179. */
  180. public void thrown(Throwable thrown) {
  181. if (null == thrown) {
  182. throw new IllegalArgumentException("null thrown");
  183. } else if (isCompleted()) {
  184. throw new IllegalStateException(
  185. "completed then thrown " + thrown);
  186. }
  187. this.thrown = thrown;
  188. }
  189. public void completeAbruptly() {
  190. throw new Error("completing abruptly"); // XXX configurable
  191. }
  192. /**
  193. * @return true if completed, not aborted, no thrown, no
  194. * messages of kind ERROR, FAIL or ABORT, and
  195. * result object is not IRunStatus.FAIL.
  196. * @see org.aspectj.testing.harness.newbridge.IRunStatus#runResult()
  197. */
  198. public boolean runResult() {
  199. return validator.runPassed(this);
  200. }
  201. public boolean hasAnyMessage(IMessage.Kind kind, boolean orGreater, boolean includeChildren) {
  202. if (messageHolder.hasAnyMessage(kind, orGreater)) {
  203. return true;
  204. }
  205. if (includeChildren) {
  206. IRunStatus[] kids = getChildren();
  207. for (int i = 0; i < kids.length; i++) {
  208. if (kids[i].hasAnyMessage(kind, orGreater, true)) {
  209. return true;
  210. }
  211. }
  212. }
  213. return false;
  214. }
  215. public IMessage[] getMessages(IMessage.Kind kind, boolean orGreater, boolean includeChildren) {
  216. IMessage[] result = getMessages(kind, orGreater);
  217. if (!includeChildren) {
  218. return result;
  219. }
  220. ArrayList sink = new ArrayList();
  221. if (!LangUtil.isEmpty(result)) {
  222. sink.addAll(Arrays.asList(result));
  223. }
  224. IRunStatus[] kids = getChildren();
  225. for (int i = 0; i < kids.length; i++) {
  226. result = kids[i].getMessages(kind, orGreater, includeChildren);
  227. if (!LangUtil.isEmpty(result)) {
  228. sink.addAll(Arrays.asList(result));
  229. }
  230. }
  231. return (IMessage[]) sink.toArray(new IMessage[0]);
  232. }
  233. //------------------- process messages
  234. /**
  235. * Call this any time before isCompleted() would return true
  236. * to signal any messages.
  237. * @throws IllegalStateException if isCompleted().
  238. */
  239. public boolean handleMessage(IMessage message) {
  240. return messageHolder.handleMessage(message);
  241. }
  242. public boolean isIgnoring(IMessage.Kind kind) {
  243. return messageHolder.isIgnoring(kind);
  244. }
  245. public void dontIgnore(IMessage.Kind kind) {
  246. ;
  247. }
  248. /**
  249. * @see org.aspectj.bridge.IMessageHolder#hasAnyMessage(Kind, boolean)
  250. */
  251. public boolean hasAnyMessage(IMessage.Kind kind, boolean orGreater) {
  252. return messageHolder.hasAnyMessage(kind, orGreater);
  253. }
  254. /**
  255. * @see org.aspectj.bridge.IMessageHolder#getMessages(Kind)
  256. */
  257. public IMessage[] getMessages(IMessage.Kind kind, boolean orGreater) {
  258. return messageHolder.getMessages(kind, orGreater);
  259. }
  260. /**
  261. * @see org.aspectj.bridge.IMessageHolder#numMessages(Kind)
  262. */
  263. public int numMessages(IMessage.Kind kind, boolean orGreater) {
  264. return messageHolder.numMessages(kind, orGreater);
  265. }
  266. //------------------- process display
  267. /** @return true if this run has started */
  268. public boolean started() {
  269. return started;
  270. }
  271. /** @return true if one of the result, abort request, or thrown is available */
  272. public boolean isCompleted() {
  273. if (!evaluated) {
  274. if (started
  275. && ((null != thrown)
  276. || (null != result)
  277. || (null != abortRequest))) {
  278. completed = true;
  279. evaluated = true;
  280. }
  281. }
  282. return completed;
  283. }
  284. /** @return true if this got an abort request */
  285. public boolean aborted() {
  286. return (completed && (null != abortRequest));
  287. }
  288. /** @return the Object result, if any, of this run */
  289. public Object getResult() {
  290. return result;
  291. }
  292. /** @return the Object abort request, if any, of this run */
  293. public Object getAbortRequest() {
  294. return abortRequest;
  295. }
  296. /** @return the Throwable thrown, if any, by this run */
  297. public Throwable getThrown() {
  298. return thrown;
  299. }
  300. /**
  301. * @see org.aspectj.bridge.IMessageHolder#getUnmodifiableListView()
  302. */
  303. public List getUnmodifiableListView() {
  304. return messageHolder.getUnmodifiableListView();
  305. }
  306. /** @return any Message[] signalled, or IMessage.NONE if none */
  307. public IMessage[] getMessages() {
  308. return messageHolder.getMessages(null, IMessageHolder.EQUAL);
  309. }
  310. /** @return the identifier set for this run, if any */
  311. public Object getIdentifier() {
  312. return id;
  313. }
  314. /**
  315. * @see org.aspectj.bridge.IMessageHolder#clearMessages()
  316. * @throws UnsupportedOperationException always
  317. */
  318. public void clearMessages() throws UnsupportedOperationException {
  319. throw new UnsupportedOperationException("use reset");
  320. }
  321. //------------------- subprocess
  322. /** get the invoker for any subrunners */
  323. public Runner getRunner() {
  324. return runner;
  325. }
  326. /**
  327. * Add a record for a child run
  328. * and install self as parent.
  329. * @throws IllegalArgumentException if child is null
  330. */
  331. public void addChild(IRunStatus child) {
  332. if (null == child) {
  333. throw new IllegalArgumentException("null child");
  334. }
  335. if (null == children) {
  336. children = new ArrayList();
  337. }
  338. children.add(child);
  339. }
  340. /**
  341. * Register this as the run parent.
  342. * (Any run that does addChild(IRunStatus) should register as parent.)
  343. * @throws IllegalArgumentException if parent is null
  344. * @throws IllegalStateException if parent exists already
  345. */
  346. public void registerParent(IRunStatus parent) {
  347. if (null == parent) {
  348. throw new IllegalArgumentException("null parent");
  349. } else if (null != this.parent) {
  350. throw new IllegalStateException(
  351. "adding parent " + parent + " to parent " + this.parent);
  352. }
  353. this.parent = parent;
  354. }
  355. /**
  356. * @return the current children of this run, or EMPTY_NEST if none
  357. */
  358. public IRunStatus[] getChildren() {
  359. if ((null == children) || (0 == children.size())) {
  360. return EMPTY_NEST;
  361. } else {
  362. return (IRunStatus[]) children.toArray(EMPTY_NEST);
  363. }
  364. }
  365. /**
  366. * @return the currently-registered parent, or null if none
  367. */
  368. public IRunStatus getParent() {
  369. return parent;
  370. }
  371. public String toString() {
  372. return BridgeUtil.toShortString(this);
  373. }
  374. public String toLongString() {
  375. StringBuffer sb = new StringBuffer();
  376. sb.append(BridgeUtil.toShortString(this));
  377. if ((null != children) && (0 < children.size())) {
  378. String label = "### --------- " + name;
  379. int index = 0;
  380. for (Iterator iter = children.iterator(); iter.hasNext();) {
  381. IRunStatus childStatus = (IRunStatus) iter.next();
  382. String childLabel =
  383. "\n" + label + " child[" + index++ +"] "
  384. + childStatus.getIdentifier();
  385. sb.append(childLabel + " ---- start\n");
  386. sb.append(childStatus.toString());
  387. sb.append(childLabel + " ---- end\n");
  388. }
  389. }
  390. return sb.toString();
  391. }
  392. }