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

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