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.

AjcTaskCompileCommandTest.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Wes Isberg initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing.taskdefs;
  13. import java.io.File;
  14. import java.util.ArrayList;
  15. import org.aspectj.bridge.*;
  16. import org.aspectj.bridge.MessageHandler;
  17. import org.aspectj.testing.harness.bridge.Globals;
  18. import org.aspectj.util.*;
  19. import org.aspectj.util.LangUtil;
  20. import junit.framework.TestCase;
  21. /**
  22. * Test AjcTaskCompileCommand adapter.
  23. * This assumes it is run from a peer directory of the
  24. * taskdefs module, which contains the target files.
  25. */
  26. public class AjcTaskCompileCommandTest extends TestCase {
  27. static boolean loggedWarning = false;
  28. static boolean runAllTests = true;
  29. static ArrayList tempFiles = new ArrayList();
  30. private static File getClassesDir() {
  31. File tempDir = FileUtil.getTempDir("AjcTaskCompileCommandTest-classes");
  32. tempFiles.add(tempDir);
  33. return tempDir;
  34. }
  35. private static void addCommonArgs(ArrayList list) {
  36. list.add("-d");
  37. list.add(getClassesDir().getAbsolutePath());
  38. list.add("-classpath");
  39. list.add(Globals.F_aspectjrt_jar.getAbsolutePath());
  40. }
  41. static boolean doWait(IMessageHolder holder, int seconds, int timeout) {
  42. return AjcTaskCompileCommand
  43. .waitUntilMessagesQuiet(holder, seconds, timeout);
  44. }
  45. public AjcTaskCompileCommandTest(String name) {
  46. super(name);
  47. }
  48. public void testWaitUntilMessagesQuiet_InputErrors() {
  49. MessageHandler mhandler = new MessageHandler();
  50. assertFalse(doWait(mhandler, 0, 10));
  51. assertFalse(doWait(mhandler, 10, 0));
  52. assertFalse(doWait(mhandler, 1, 1));
  53. assertFalse(doWait(mhandler, 10, 1));
  54. boolean thrown = false;
  55. try {
  56. doWait(null, 1, 10);
  57. } catch (IllegalArgumentException e) {
  58. thrown = true;
  59. }
  60. assertTrue("no exception thrown", thrown);
  61. }
  62. public void testDefault() {
  63. runSimpleTest("../taskdefs/testdata/Default.java", 0);
  64. }
  65. public void testDefaultList() {
  66. runSimpleTest("../taskdefs/testdata/default.lst", 0);
  67. }
  68. public void testCompileErrorList() {
  69. runSimpleTest("../taskdefs/testdata/compileError.lst", 1);
  70. }
  71. public void testWaitUntilMessagesQuiet_1_2() {
  72. if (runAllTests) checkWait(1, 2, 0, 0);
  73. }
  74. public void testWaitUntilMessagesQuiet_1_10() {
  75. if (runAllTests) checkWait(1, 10, 0, 0);
  76. }
  77. public void testWaitUntilMessagesQuiet_8_10() {
  78. checkWait(8, 10, 0, 0);
  79. }
  80. // XXX two async tests might fail if adder thread starved
  81. public void testWaitUntilMessagesQuiet_1_10_4_1() {
  82. if (runAllTests) checkWait(1, 10, 4, 1);
  83. }
  84. public void testWaitUntilMessagesQuiet_8_10_2_1() {
  85. if (runAllTests) checkWait(8, 20, 2, 1);
  86. }
  87. void runSimpleTest(String path, int expectedErrors) {
  88. File file = new File(path);
  89. assertTrue(path, file.canRead());
  90. ArrayList list = new ArrayList();
  91. addCommonArgs(list);
  92. if (path.endsWith(".lst")) {
  93. list.add("-argfile");
  94. list.add(file.getAbsolutePath());
  95. } else if (FileUtil.hasSourceSuffix(path)) {
  96. list.add(file.getAbsolutePath());
  97. } else {
  98. assertTrue("unrecognized file: " + path, false);
  99. return;
  100. }
  101. runTest(list, expectedErrors);
  102. }
  103. void runTest(ArrayList args, int expectedErrors) {
  104. AjcTaskCompileCommand command =
  105. new AjcTaskCompileCommand();
  106. MessageHandler handler = new MessageHandler();
  107. String[] parms = (String[]) args.toArray(new String[0]);
  108. boolean result = command.runCommand(parms, handler);
  109. boolean expectPass = (0 == expectedErrors);
  110. final boolean pass = (result == expectPass);
  111. if (!pass) {
  112. String m = expectPass ? "pass" : "fail";
  113. assertTrue("expected " + m + ": " + args, false);
  114. }
  115. }
  116. void checkWait(final int seconds, final int timeout, int toAdd, int addInterval) {
  117. final String testCase = "checkWait(seconds="
  118. + seconds + ", timeout=" + timeout;
  119. final MessageHandler mhandler = new MessageHandler();
  120. final long startTime = System.currentTimeMillis();
  121. final long testTimeout = startTime + (timeout * 2000l);
  122. final boolean result;
  123. if (0 == toAdd) { // do no-adds synchronously
  124. result = doWait(mhandler, seconds, timeout);
  125. assertTrue("result " + testCase, result);
  126. } else {
  127. if (!loggedWarning) {
  128. System.out.println("warning - test will fail if adder thread starved");
  129. loggedWarning = true;
  130. }
  131. final MessageAdder adder
  132. = new MessageAdder(mhandler, toAdd, addInterval);
  133. final String label = testCase + " wait(" + toAdd + ", " + addInterval + ")";
  134. class Result {
  135. boolean result;
  136. Thread addedThread;
  137. }
  138. final Result waitResult = new Result();
  139. Thread testThread = new Thread( new Runnable() {
  140. public void run() {
  141. waitResult.addedThread
  142. = new Thread(adder, label + "-child");
  143. waitResult.addedThread.start();
  144. waitResult.result =
  145. AjcTaskCompileCommandTest.doWait(mhandler, seconds, timeout);
  146. }
  147. }, label);
  148. testThread.start();
  149. try {
  150. testThread.join(testTimeout - startTime);
  151. } catch (InterruptedException e) {
  152. // ignore
  153. }
  154. try {
  155. if (null != waitResult.addedThread) {
  156. long wait = testTimeout - System.currentTimeMillis();
  157. if (0 < wait) {
  158. waitResult.addedThread.join(wait);
  159. }
  160. }
  161. } catch (InterruptedException e) {
  162. // ignore
  163. }
  164. result = waitResult.result;
  165. int added = adder.getNumAdded();
  166. assertEquals(testCase + " added", added, toAdd);
  167. if (!result) {
  168. assertTrue(testCase + " result " + adder, false);
  169. }
  170. }
  171. long endTime = System.currentTimeMillis();
  172. long elapsed = endTime - startTime;
  173. assertTrue(seconds + " seconds: " + elapsed, elapsed >= (seconds*1000));
  174. assertTrue(timeout + " timeout: " + elapsed, elapsed <= (timeout*1000));
  175. }
  176. }
  177. class MessageAdder implements Runnable {
  178. /** 30-second max test */
  179. public static long MAX_MILLIS = 1000 * 30;
  180. public boolean stop;
  181. public boolean wait;
  182. private final IMessageHolder messages;
  183. private final int numToAdd;
  184. private final int interval;
  185. private int numAdded;
  186. /**
  187. * @param holder the IMessageHolder to add to
  188. * @param num the int number of messages to add
  189. * @param interval the int seconds between messages added
  190. */
  191. MessageAdder(IMessageHolder holder, int num, int interval) {
  192. LangUtil.throwIaxIfNull(holder, "holder");
  193. LangUtil.throwIaxIfFalse(num > 0, "numToAdd: " + num);
  194. LangUtil.throwIaxIfFalse(interval > 0, "interval: " + interval);
  195. LangUtil.throwIaxIfFalse(num*interval*1000 < MAX_MILLIS, "too long");
  196. this.messages = holder;
  197. this.numToAdd = num;
  198. this.interval = interval;
  199. }
  200. public void run() {
  201. final long waitBetweenAdds = interval * 1000l;
  202. long curTime = System.currentTimeMillis();
  203. final long timeout = curTime + MAX_MILLIS;
  204. // final Thread thread = Thread.currentThread();
  205. int numAdded = 0;
  206. while (!stop && (timeout > curTime)
  207. && (numAdded < numToAdd)) {
  208. long targetTime = curTime + waitBetweenAdds;
  209. while (!stop && (curTime < timeout)
  210. && (curTime < targetTime)) {
  211. try {
  212. Thread.sleep(targetTime - curTime);
  213. } catch (InterruptedException e) {
  214. // ignore
  215. }
  216. curTime = System.currentTimeMillis();
  217. }
  218. if (!stop && (curTime < timeout)) {
  219. MessageUtil.info(messages, "time is " + curTime);
  220. numAdded++;
  221. }
  222. }
  223. this.numAdded = numAdded;
  224. }
  225. int getNumAdded() {
  226. return numAdded;
  227. }
  228. public String toString() {
  229. return "MessageAdder("
  230. + "numAdded=" + numAdded
  231. + ", numToAdd=" + numToAdd
  232. + ", interval=" + interval
  233. + ", stop=" + stop
  234. + ", wait=" + wait
  235. + ", numMessages="
  236. + (null == messages
  237. ? 0
  238. : messages.numMessages(null, true))
  239. + ")";
  240. }
  241. }