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.

BuildArgParserTestCase.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 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. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.ajc;
  13. import java.io.*;
  14. import java.util.*;
  15. import junit.framework.TestCase;
  16. import org.aspectj.ajdt.internal.core.builder.*;
  17. import org.aspectj.bridge.CountingMessageHandler;
  18. import org.aspectj.bridge.IMessage;
  19. import org.aspectj.bridge.IMessageHandler;
  20. import org.aspectj.bridge.MessageWriter;
  21. import org.aspectj.testing.util.TestUtil;
  22. import org.eclipse.jdt.core.compiler.InvalidInputException;
  23. import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  24. /**
  25. * Some black-box test is happening here.
  26. */
  27. public class BuildArgParserTestCase extends TestCase {
  28. private static final String TEST_DIR = AjdtAjcTests.TESTDATA_PATH + File.separator + "ajc" + File.separator;
  29. private MessageWriter messageWriter = new MessageWriter(new PrintWriter(System.out), false);
  30. public BuildArgParserTestCase(String name) {
  31. super(name);
  32. }
  33. private AjBuildConfig genBuildConfig(String[] args, IMessageHandler handler) {
  34. return new BuildArgParser(handler).genBuildConfig(args);
  35. }
  36. public void testDefaultClasspathAndTargetCombo() throws InvalidInputException {
  37. String ENTRY = "1.jar;2.jar";
  38. final String classpath = System.getProperty("java.class.path");
  39. try {
  40. System.setProperty("java.class.path", ENTRY); // see finally below
  41. BuildArgParser parser = new BuildArgParser(messageWriter);
  42. AjBuildConfig config = parser.genBuildConfig(new String[] { });
  43. String err = parser.getOtherMessages(true);
  44. //!!!assertTrue(err, null == err);
  45. assertTrue(
  46. config.getClasspath().toString(),
  47. config.getClasspath().contains("1.jar"));
  48. assertTrue(
  49. config.getClasspath().toString(),
  50. config.getClasspath().contains("2.jar"));
  51. config = genBuildConfig(new String[] { "-1.3" }, messageWriter);
  52. // these errors are deffered to the compiler now
  53. //err = parser.getOtherMessages(true);
  54. //!!!assertTrue(err, null == err);
  55. assertTrue(
  56. config.getClasspath().toString(),
  57. config.getClasspath().contains("1.jar"));
  58. assertTrue(
  59. config.getClasspath().toString(),
  60. config.getClasspath().contains("2.jar"));
  61. parser = new BuildArgParser(messageWriter);
  62. config = parser.genBuildConfig(new String[] { "-1.3" });
  63. err = parser.getOtherMessages(true);
  64. //!!!assertTrue(err, null == err);
  65. assertTrue(
  66. config.getClasspath().toString(),
  67. config.getClasspath().contains("1.jar"));
  68. assertTrue(
  69. config.getClasspath().toString(),
  70. config.getClasspath().contains("2.jar"));
  71. config = genBuildConfig(new String[] {
  72. "-classpath", ENTRY, "-1.4" }, messageWriter);
  73. // these errors are deffered to the compiler now
  74. //err = parser.getOtherMessages(true);
  75. //assertTrue("expected errors for missing jars", null != err);
  76. assertTrue(
  77. config.getClasspath().toString(),
  78. config.getClasspath().contains("1.jar"));
  79. assertTrue(
  80. config.getClasspath().toString(),
  81. config.getClasspath().contains("2.jar"));
  82. } finally {
  83. // do finally to avoid messing up classpath for other tests
  84. System.setProperty("java.class.path", classpath);
  85. String setPath = System.getProperty("java.class.path");
  86. String m = "other tests will fail - classpath not reset";
  87. assertEquals(m, classpath, setPath);
  88. }
  89. }
  90. public void testAjOptions() throws InvalidInputException {
  91. AjBuildConfig config = genBuildConfig(new String[] { "-Xlint" }, messageWriter);
  92. assertTrue(
  93. "default options",
  94. config.getAjOptions().get(AjCompilerOptions.OPTION_Xlint).equals(
  95. CompilerOptions.GENERATE));
  96. }
  97. public void testAspectpath() throws InvalidInputException {
  98. final String SOURCE_JAR = AjdtAjcTests.TESTDATA_PATH + "/testclasses.jar";
  99. final String SOURCE_JARS = AjdtAjcTests.TESTDATA_PATH + "/testclasses.jar" + File.pathSeparator
  100. + "../weaver/testdata/tracing.jar" + File.pathSeparator
  101. + "../weaver/testdata/dummyAspect.jar";
  102. AjBuildConfig config = genBuildConfig(new String[] {
  103. "-aspectpath", SOURCE_JAR },
  104. messageWriter);
  105. assertTrue(((File)config.getAspectpath().get(0)).getName(), ((File)config.getAspectpath().get(0)).getName().equals("testclasses.jar"));
  106. config = genBuildConfig(new String[] {
  107. "-aspectpath", SOURCE_JARS },
  108. messageWriter);
  109. assertTrue("size", + config.getAspectpath().size() == 3);
  110. }
  111. public void testInJars() throws InvalidInputException {
  112. final String SOURCE_JAR = AjdtAjcTests.TESTDATA_PATH + "/testclasses.jar";
  113. final String SOURCE_JARS = AjdtAjcTests.TESTDATA_PATH + "/testclasses.jar" + File.pathSeparator
  114. + "../weaver/testdata/tracing.jar" + File.pathSeparator
  115. + "../weaver/testdata/dummyAspect.jar";
  116. AjBuildConfig config = genBuildConfig(new String[] {
  117. "-injars", SOURCE_JAR },
  118. messageWriter);
  119. //XXX don't let this remain in both places in beta1
  120. assertTrue(
  121. "" + config.getAjOptions().get(AjCompilerOptions.OPTION_InJARs),
  122. config.getAjOptions().get(AjCompilerOptions.OPTION_InJARs).equals(CompilerOptions.PRESERVE));
  123. assertTrue(((File)config.getInJars().get(0)).getName(), ((File)config.getInJars().get(0)).getName().equals("testclasses.jar"));
  124. config = genBuildConfig(new String[] {
  125. "-injars", SOURCE_JARS },
  126. messageWriter);
  127. assertTrue("size", + config.getInJars().size() == 3);
  128. }
  129. public void testBadInJars() throws InvalidInputException {
  130. final String SOURCE_JARS = AjdtAjcTests.TESTDATA_PATH + "/testclasses.jar" + File.pathSeparator + "b.far" + File.pathSeparator + "c.jar";
  131. AjBuildConfig config = genBuildConfig(new String[] {
  132. "-injars", SOURCE_JARS },
  133. messageWriter);
  134. assertTrue("size: " + config.getInJars().size(), config.getInJars().size() == 1);
  135. }
  136. public void testBadPathToSourceFiles() {
  137. CountingMessageHandler countingHandler = new CountingMessageHandler(messageWriter);
  138. AjBuildConfig config = genBuildConfig(new String[]{ "inventedDir/doesntexist/*.java"},countingHandler);
  139. assertTrue("Expected an error for the invalid path.",countingHandler.numMessages(IMessage.ERROR,false)==1);
  140. }
  141. public void testMultipleSourceRoots() throws InvalidInputException, IOException {
  142. final String SRCROOT_1 = AjdtAjcTests.TESTDATA_PATH + "/src1/p1";
  143. final String SRCROOT_2 = AjdtAjcTests.TESTDATA_PATH + "/ajc";
  144. AjBuildConfig config = genBuildConfig(new String[] {
  145. "-sourceroots", SRCROOT_1 + File.pathSeparator + SRCROOT_2 },
  146. messageWriter);
  147. assertEquals(getCanonicalPath(new File(SRCROOT_1)), ((File)config.getSourceRoots().get(0)).getAbsolutePath());
  148. Collection expectedFiles = Arrays.asList(new File[] {
  149. new File(SRCROOT_1+File.separator+"A.java").getCanonicalFile(),
  150. new File(SRCROOT_1+File.separator+"Foo.java").getCanonicalFile(),
  151. new File(SRCROOT_2+File.separator+"A.java").getCanonicalFile(),
  152. new File(SRCROOT_2+File.separator+"B.java").getCanonicalFile(),
  153. new File(SRCROOT_2+File.separator+"X.aj").getCanonicalFile(),
  154. new File(SRCROOT_2+File.separator+"Y.aj").getCanonicalFile(),
  155. new File(SRCROOT_2+File.separator+"pkg"+File.separator+"Hello.java").getCanonicalFile(),
  156. });
  157. //System.out.println(config.getFiles());
  158. TestUtil.assertSetEquals(expectedFiles, config.getFiles());
  159. }
  160. /**
  161. * @param file
  162. * @return String
  163. */
  164. private String getCanonicalPath(File file) {
  165. String ret = "";
  166. try {
  167. ret = file.getCanonicalPath();
  168. } catch (IOException ioEx) {
  169. fail("Unable to canonicalize " + file + " : " + ioEx);
  170. }
  171. return ret;
  172. }
  173. public void testSourceRootDir() throws InvalidInputException, IOException {
  174. final String SRCROOT = AjdtAjcTests.TESTDATA_PATH + "/ajc";
  175. AjBuildConfig config = genBuildConfig(new String[] {
  176. "-sourceroots", SRCROOT },
  177. messageWriter);
  178. assertEquals(getCanonicalPath(new File(SRCROOT)), ((File)config.getSourceRoots().get(0)).getAbsolutePath());
  179. Collection expectedFiles = Arrays.asList(new File[] {
  180. new File(SRCROOT+File.separator+"A.java").getCanonicalFile(),
  181. new File(SRCROOT+File.separator+"B.java").getCanonicalFile(),
  182. new File(SRCROOT+File.separator+"X.aj").getCanonicalFile(),
  183. new File(SRCROOT+File.separator+"Y.aj").getCanonicalFile(),
  184. new File(SRCROOT+File.separator+"pkg"+File.separator+"Hello.java").getCanonicalFile(),
  185. });
  186. //System.out.println(config.getFiles());
  187. TestUtil.assertSetEquals(expectedFiles, config.getFiles());
  188. }
  189. public void testBadSourceRootDir() throws InvalidInputException {
  190. AjBuildConfig config = genBuildConfig(new String[] {
  191. "-sourceroots",
  192. AjdtAjcTests.TESTDATA_PATH + "/mumbleDoesNotExist;"
  193. + AjdtAjcTests.TESTDATA_PATH + "/ajc" },
  194. messageWriter);
  195. assertTrue(config.getSourceRoots().toString(), config.getSourceRoots().size() == 1);
  196. config = genBuildConfig(new String[] {
  197. "-sourceroots" },
  198. messageWriter);
  199. assertTrue("" + config.getSourceRoots(), config.getSourceRoots().size() == 0);
  200. }
  201. //??? we've decided not to make this an error
  202. public void testSourceRootDirWithFiles() throws InvalidInputException, IOException {
  203. final String SRCROOT = AjdtAjcTests.TESTDATA_PATH + "/ajc/pkg";
  204. AjBuildConfig config = genBuildConfig(new String[] {
  205. "-sourceroots", SRCROOT, AjdtAjcTests.TESTDATA_PATH + "/src1/A.java"},
  206. messageWriter);
  207. assertEquals(getCanonicalPath(new File(SRCROOT)), ((File)config.getSourceRoots().get(0)).getAbsolutePath());
  208. Collection expectedFiles = Arrays.asList(new File[] {
  209. new File(SRCROOT+File.separator+"Hello.java").getCanonicalFile(),
  210. new File(AjdtAjcTests.TESTDATA_PATH +File.separator+"src1"+File.separator+"A.java").getCanonicalFile(),
  211. });
  212. TestUtil.assertSetEquals(expectedFiles, config.getFiles());
  213. }
  214. public void testExtDirs() throws InvalidInputException {
  215. final String DIR = AjdtAjcTests.TESTDATA_PATH;
  216. AjBuildConfig config = genBuildConfig(new String[] {
  217. "-extdirs", DIR },
  218. messageWriter);
  219. assertTrue(config.getClasspath().toString(), config.getClasspath().contains(
  220. new File(DIR + File.separator + "testclasses.jar").getAbsolutePath()
  221. ));
  222. }
  223. public void testBootclasspath() throws InvalidInputException {
  224. final String PATH = "mumble/rt.jar";
  225. AjBuildConfig config = genBuildConfig(new String[] {
  226. "-bootclasspath", PATH },
  227. messageWriter);
  228. assertTrue(config.getClasspath().toString(), config.getClasspath().get(0).equals(PATH));
  229. config = genBuildConfig(new String[] {
  230. },
  231. messageWriter);
  232. assertTrue(config.getClasspath().toString(), !config.getClasspath().get(0).equals(PATH));
  233. }
  234. public void testOutputJar() throws InvalidInputException {
  235. final String OUT_JAR = AjdtAjcTests.TESTDATA_PATH + "/testclasses.jar";
  236. AjBuildConfig config = genBuildConfig(new String[] {
  237. "-outjar", OUT_JAR },
  238. messageWriter);
  239. //XXX don't let this remain in both places in beta1
  240. assertTrue(
  241. "will generate: " + config.getAjOptions().get(AjCompilerOptions.OPTION_OutJAR),
  242. config.getAjOptions().get(AjCompilerOptions.OPTION_OutJAR).equals(CompilerOptions.GENERATE));
  243. assertEquals(
  244. getCanonicalPath(new File(OUT_JAR)),config.getOutputJar().getAbsolutePath());
  245. File nonExistingJar = new File(AjdtAjcTests.TESTDATA_PATH + "/mumbleDoesNotExist.jar");
  246. config = genBuildConfig(new String[] {
  247. "-outjar", nonExistingJar.getAbsolutePath() },
  248. messageWriter);
  249. assertEquals(
  250. getCanonicalPath(nonExistingJar),
  251. config.getOutputJar().getAbsolutePath());
  252. nonExistingJar.delete();
  253. }
  254. //XXX shouldn't need -1.4 to get this to pass
  255. public void testCombinedOptions() throws InvalidInputException {
  256. AjBuildConfig config = genBuildConfig(new String[] { "-Xlint", "-target", "1.4", "-1.4" }, messageWriter);
  257. String TARGET = "1.4";
  258. assertTrue(
  259. "target set",
  260. config.getJavaOptions().get(CompilerOptions.OPTION_TargetPlatform).equals(TARGET));
  261. assertTrue(
  262. "Xlint option set",
  263. config.getAjOptions().get(AjCompilerOptions.OPTION_Xlint).equals(
  264. CompilerOptions.GENERATE));
  265. }
  266. public void testOutputDirectorySetting() throws InvalidInputException {
  267. AjBuildConfig config = genBuildConfig(new String[] { "-d", TEST_DIR }, messageWriter);
  268. assertTrue(
  269. new File(config.getOutputDir().getPath()).getAbsolutePath() + " ?= " +
  270. new File(TEST_DIR).getAbsolutePath(),
  271. config.getOutputDir().getAbsolutePath().equals((new File(TEST_DIR)).getAbsolutePath()));
  272. }
  273. public void testClasspathSetting() throws InvalidInputException {
  274. String ENTRY = "1.jar;2.jar";
  275. AjBuildConfig config = genBuildConfig(new String[] { "-classpath", ENTRY }, messageWriter);
  276. assertTrue(
  277. config.getClasspath().toString(),
  278. config.getClasspath().contains("1.jar"));
  279. assertTrue(
  280. config.getClasspath().toString(),
  281. config.getClasspath().contains("2.jar"));
  282. }
  283. public void testArgInConfigFile() throws InvalidInputException {
  284. String FILE_PATH = "@" + TEST_DIR + "configWithArgs.lst";
  285. String OUT_PATH = "bin";
  286. AjBuildConfig config = genBuildConfig(new String[] { FILE_PATH }, messageWriter);
  287. assertNotNull(config);
  288. File outputDir = config.getOutputDir();
  289. assertNotNull(outputDir);
  290. assertEquals(outputDir.getPath(), OUT_PATH);
  291. }
  292. public void testNonExistentConfigFile() throws IOException {
  293. String FILE_PATH = "@" + TEST_DIR + "../bug-40257/d1/test.lst";
  294. AjBuildConfig config = genBuildConfig(new String[] { FILE_PATH }, messageWriter);
  295. String a = new File(TEST_DIR + "../bug-40257/d1/A.java").getCanonicalPath();
  296. String b = new File(TEST_DIR + "../bug-40257/d1/d2/B.java").getCanonicalPath();
  297. String c = new File(TEST_DIR + "../bug-40257/d3/C.java").getCanonicalPath();
  298. List pathList = new ArrayList();
  299. for (Iterator it = config.getFiles().iterator(); it.hasNext(); ) {
  300. pathList.add(((File)it.next()).getCanonicalPath());
  301. }
  302. assertTrue(pathList.contains(a));
  303. assertTrue(pathList.contains(b));
  304. assertTrue(pathList.contains(c));
  305. }
  306. public void testXlint() throws InvalidInputException {
  307. AjdtCommand command = new AjdtCommand();
  308. AjBuildConfig config = genBuildConfig(new String[] {"-Xlint"}, messageWriter);
  309. assertTrue("", config.getLintMode().equals(AjBuildConfig.AJLINT_DEFAULT));
  310. config = genBuildConfig(new String[] {"-Xlint:warn"}, messageWriter);
  311. assertTrue("", config.getLintMode().equals(AjBuildConfig.AJLINT_WARN));
  312. config = genBuildConfig(new String[] {"-Xlint:error"}, messageWriter);
  313. assertTrue("", config.getLintMode().equals(AjBuildConfig.AJLINT_ERROR));
  314. config = genBuildConfig(new String[] {"-Xlint:ignore"}, messageWriter);
  315. assertTrue("", config.getLintMode().equals(AjBuildConfig.AJLINT_IGNORE));
  316. }
  317. public void testXlintfile() throws InvalidInputException {
  318. String lintFile = AjdtAjcTests.TESTDATA_PATH + "/lintspec.properties";
  319. String badLintFile = "lint.props";
  320. AjBuildConfig config = genBuildConfig(new String[] {"-Xlintfile", lintFile}, messageWriter);
  321. assertTrue(new File(lintFile).exists());
  322. assertEquals(getCanonicalPath(new File(lintFile)),config.getLintSpecFile().getAbsolutePath());
  323. }
  324. public void testOptions() throws InvalidInputException {
  325. AjdtCommand command = new AjdtCommand();
  326. String TARGET = "1.4";
  327. AjBuildConfig config = genBuildConfig(new String[] {"-target", TARGET, "-source", TARGET}, messageWriter);
  328. assertTrue(
  329. "target set",
  330. config.getJavaOptions().get(CompilerOptions.OPTION_TargetPlatform).equals(TARGET));
  331. assertTrue(
  332. "source set",
  333. config.getJavaOptions().get(CompilerOptions.OPTION_Compliance).equals(CompilerOptions.VERSION_1_4));
  334. }
  335. public void testLstFileExpansion() throws IOException, FileNotFoundException, InvalidInputException {
  336. String FILE_PATH = TEST_DIR + "config.lst";
  337. String SOURCE_PATH_1 = "A.java";
  338. String SOURCE_PATH_2 = "B.java";
  339. File f = new File(FILE_PATH);
  340. AjBuildConfig config = genBuildConfig(new String[] { "@" + FILE_PATH }, messageWriter);
  341. List resultList = config.getFiles();
  342. assertTrue("correct number of files", resultList.size() == 2);
  343. assertTrue(resultList.toString() + new File(TEST_DIR + SOURCE_PATH_1).getCanonicalFile(),
  344. resultList.contains(new File(TEST_DIR + SOURCE_PATH_1).getCanonicalFile()));
  345. assertTrue(resultList.toString() + SOURCE_PATH_2,
  346. resultList.contains(new File(TEST_DIR + SOURCE_PATH_2).getCanonicalFile()));
  347. }
  348. //??? do we need to remove this limitation
  349. // public void testArgInConfigFileAndRelativizingPathParam() throws InvalidInputException {
  350. // String FILE_PATH = "@" + TEST_DIR + "configWithArgs.lst";
  351. // String OUT_PATH = TEST_DIR + "bin";
  352. // AjBuildConfig config = genBuildConfig(new String[] { FILE_PATH });
  353. //
  354. // assertTrue(
  355. // config.getOutputDir().getPath() + " ?= " + OUT_PATH,
  356. // config.getOutputDir().getAbsolutePath().equals((new File(OUT_PATH)).getAbsolutePath()));
  357. // }
  358. public void testAjFileInclusion() throws InvalidInputException {
  359. genBuildConfig(new String[] { TEST_DIR + "X.aj", TEST_DIR + "Y.aj"}, messageWriter);
  360. }
  361. protected void setUp() throws Exception {
  362. super.setUp();
  363. }
  364. protected void tearDown() throws Exception {
  365. super.tearDown();
  366. }
  367. }