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.7KB

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