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.

AjdtCommandTestCase.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  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. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.ajc;
  13. //import org.aspectj.ajdt.internal.core.builder.AjBuildConfig;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.io.PrintStream;
  18. import java.io.PrintWriter;
  19. import java.util.ArrayList;
  20. import java.util.ListIterator;
  21. import junit.framework.TestCase;
  22. import org.aspectj.ajdt.StreamPrintWriter;
  23. import org.aspectj.bridge.AbortException;
  24. import org.aspectj.bridge.CountingMessageHandler;
  25. import org.aspectj.bridge.IMessage;
  26. import org.aspectj.bridge.IMessageHolder;
  27. import org.aspectj.bridge.MessageHandler;
  28. import org.aspectj.bridge.MessageWriter;
  29. import org.aspectj.org.eclipse.jdt.core.compiler.InvalidInputException;
  30. import org.aspectj.util.FileUtil;
  31. /**
  32. * Some black-box test is happening here.
  33. */
  34. public class AjdtCommandTestCase extends TestCase {
  35. private ArrayList tempFiles = new ArrayList();
  36. private StreamPrintWriter outputWriter = new StreamPrintWriter(new PrintWriter(System.out));
  37. // private AjdtCommand command = new AjdtCommand();
  38. private MessageWriter messageWriter = new MessageWriter(outputWriter, false);
  39. private CountingMessageHandler counter = new CountingMessageHandler(messageWriter);
  40. public AjdtCommandTestCase(String name) {
  41. super(name);
  42. // command.buildArgParser.out = outputWriter;
  43. }
  44. private static boolean delete(File file) {
  45. if ((null == file) || !file.exists()) {
  46. return true;
  47. } else if (!file.canWrite()) {
  48. return false;
  49. }
  50. if (file.isDirectory()) {
  51. FileUtil.deleteContents(file);
  52. }
  53. return file.delete();
  54. }
  55. public void testIncrementalHandler() throws IOException {
  56. // verify that AjdtCommand respects handler parm
  57. // in runCommand and repeatCommand
  58. final String sig = getClass().getName() + ".testIncrementalHandler";
  59. boolean runTest = false;
  60. try {
  61. runTest = null != System.getProperty(sig);
  62. } catch (Throwable t) {
  63. }
  64. if (!runTest) {
  65. System.out.println("warning: to run " + sig + "(), set system property " + sig);
  66. return;
  67. }
  68. // setup initial compile
  69. File testBase = new File("testdata/ajdtCommand");
  70. assertTrue(testBase.isDirectory());
  71. assertTrue(testBase.canWrite());
  72. File genBase = new File(testBase, "genBase");
  73. tempFiles.add(genBase);
  74. if (genBase.exists()) {
  75. FileUtil.deleteContents(genBase);
  76. } else {
  77. genBase.mkdirs();
  78. }
  79. assertTrue(genBase.canWrite());
  80. File classesDir = new File(testBase, "classes");
  81. tempFiles.add(classesDir);
  82. assertTrue(classesDir.mkdirs());
  83. File mainSrc = new File(testBase, "Main-1.java");
  84. File main = new File(genBase, "Main.java");
  85. FileUtil.copyFile(mainSrc, main);
  86. assertTrue(main.canRead());
  87. long initialSize = main.length();
  88. // do initial compile
  89. String[] args = new String[] { "-d", classesDir.getPath(), "-classpath", "../lib/test/aspectjrt.jar", main.getPath() };
  90. AjdtCommand command = new AjdtCommand();
  91. IMessageHolder holder = new MessageHandler();
  92. boolean result = command.runCommand(args, holder);
  93. assertTrue(result);
  94. assertTrue(!holder.hasAnyMessage(IMessage.WARNING, true));
  95. int initialMessages = holder.numMessages(null, true);
  96. // do repeat compile, introducing an error
  97. mainSrc = new File(testBase, "Main-2.java");
  98. FileUtil.copyFile(mainSrc, main);
  99. assertTrue(main.canRead());
  100. long nextSize = main.length();
  101. assertTrue(nextSize > initialSize);
  102. IMessageHolder newHolder = new MessageHandler();
  103. result = command.repeatCommand(newHolder);
  104. // verify failed, no effect on first holder, error in second
  105. assertFalse(result);
  106. assertEquals(1, newHolder.numMessages(IMessage.ERROR, false));
  107. assertEquals(initialMessages, holder.numMessages(null, true));
  108. }
  109. public void testIncrementalOption() throws InvalidInputException {
  110. AjdtCommand.genBuildConfig(new String[] { "-incremental" }, counter);
  111. assertTrue("didn't specify source root", outputWriter.getContents().contains("specify a source root"));
  112. outputWriter.flushBuffer();
  113. AjdtCommand.genBuildConfig(new String[] { "-incremental", "-sourceroots", Constants.TESTDATA_PATH + "/src1" }, counter);
  114. assertTrue(outputWriter.getContents(), outputWriter.getContents().equals(""));
  115. outputWriter.flushBuffer();
  116. AjdtCommand.genBuildConfig(new String[] { "-incremental", "testdata/src1/Hello.java" }, counter);
  117. assertTrue("specified a file", outputWriter.getContents().contains("incremental mode only handles source files using -sourceroots"));
  118. }
  119. public void testBadOptionAndUsagePrinting() throws InvalidInputException {
  120. try {
  121. AjdtCommand.genBuildConfig(new String[] { "-mubleBadOption" }, counter);
  122. } catch (AbortException ae) {
  123. }
  124. // usage printed by caller to genBuildConfig now...
  125. assertTrue(outputWriter.getContents() + " contains? " + "Usage",
  126. outputWriter.getContents().contains("-mubleBadOption"));
  127. }
  128. public void testHelpUsagePrinting() {
  129. String[] args = new String[] { "-help" };
  130. PrintStream saveOut = System.out;
  131. ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
  132. PrintStream newOut = new PrintStream(byteArrayOut);
  133. System.setOut(newOut);
  134. try {
  135. try {
  136. AjdtCommand.genBuildConfig(args, counter);
  137. } catch (AbortException ae) {
  138. }
  139. } finally {
  140. System.setOut(saveOut);
  141. }
  142. String text = byteArrayOut.toString();
  143. assertTrue(text + " contains? " + "Usage", text.contains("Usage"));
  144. }
  145. public void q() throws InvalidInputException {
  146. String[] args = new String[] { "-version" };
  147. PrintStream saveOut = System.out;
  148. PrintStream saveErr = System.err;
  149. ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
  150. ByteArrayOutputStream byteArrayErr = new ByteArrayOutputStream();
  151. PrintStream newOut = new PrintStream(byteArrayOut);
  152. PrintStream newErr = new PrintStream(byteArrayErr);
  153. System.setOut(newOut);
  154. System.setErr(newErr);
  155. try {
  156. try {
  157. AjdtCommand.genBuildConfig(args, counter);
  158. } catch (AbortException ae) {
  159. }
  160. } finally {
  161. System.setOut(saveOut);
  162. System.setErr(saveErr);
  163. }
  164. String text = byteArrayOut.toString();
  165. // String text2 = byteArrayErr.toString();
  166. assertTrue("version output does not include 'AspectJ Compiler', output was:\n'" + text + "'", text.contains("AspectJ Compiler"));
  167. }
  168. public void testNonExistingLstFile() {
  169. AjdtCommand.genBuildConfig(new String[] { "@mumbleDoesNotExist" }, counter);
  170. assertTrue(outputWriter.getContents(), outputWriter.getContents().contains("file does not exist"));
  171. }
  172. protected void setUp() throws Exception {
  173. super.setUp();
  174. }
  175. protected void tearDown() throws Exception {
  176. super.tearDown();
  177. outputWriter.flushBuffer();
  178. for (ListIterator iter = tempFiles.listIterator(); iter.hasNext();) {
  179. File file = (File) iter.next();
  180. if (delete(file)) {
  181. iter.remove();
  182. }
  183. }
  184. }
  185. }