Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BuildArgParserTestCase.java 16KB

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