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.

AjdocTestCase.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /********************************************************************
  2. * Copyright (c) 2005 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution and is available at
  6. * http://eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: IBM Corporation - initial API and implementation
  9. * Helen Hawkins - iniital version
  10. *******************************************************************/
  11. package org.aspectj.tools.ajdoc;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import org.aspectj.testing.util.TestUtil;
  17. import org.aspectj.util.LangUtil;
  18. import junit.framework.AssertionFailedError;
  19. import junit.framework.TestCase;
  20. /**
  21. * This class is the super class of all Ajdoc tests. It creates a sandbox directory and provides utility methods for copying over
  22. * the test projects and running the ajdoc command
  23. */
  24. public abstract class AjdocTestCase extends TestCase {
  25. public final static String testdataSrcDir = "../ajdoc/testdata";
  26. protected static File sandboxDir;
  27. private String docOutdir, projectDir;
  28. @Override
  29. protected void setUp() throws Exception {
  30. super.setUp();
  31. docOutdir = null;
  32. projectDir = null;
  33. // Create a sandbox in which to work
  34. sandboxDir = TestUtil.createEmptySandbox();
  35. // create the ajdocworkdingdir in the sandbox
  36. Main.setOutputWorkingDir(getWorkingDir().getAbsolutePath());
  37. }
  38. @Override
  39. protected void tearDown() throws Exception {
  40. super.tearDown();
  41. // reset where ajdocworkingdir is created
  42. Main.resetOutputWorkingDir();
  43. }
  44. /**
  45. * Fill in the working directory with the project files and create a doc top level directory in which to generate the ajdoc
  46. * output.
  47. */
  48. public void initialiseProject(String projectName) {
  49. File projectSrc = new File(testdataSrcDir + File.separatorChar + projectName);
  50. File destination = new File(getWorkingDir(), projectName);
  51. if (!destination.exists()) {
  52. destination.mkdir();
  53. }
  54. copy(projectSrc, destination);
  55. projectDir = destination.getAbsolutePath();
  56. File docDestination = new File(getWorkingDir().toString() + File.separatorChar + projectName, "doc");
  57. if (!docDestination.exists()) {
  58. docDestination.mkdir();
  59. }
  60. docOutdir = docDestination.getAbsolutePath();
  61. }
  62. /**
  63. * @return the working directory
  64. */
  65. protected File getWorkingDir() {
  66. return sandboxDir;
  67. }
  68. /**
  69. * @return the absolute path of the project directory for example c:\temp\ajcSandbox\ajcTest15200.tmp\myProject
  70. */
  71. protected String getAbsoluteProjectDir() {
  72. return projectDir;
  73. }
  74. /**
  75. * @return the absolute path of the doc output directory for example c:\temp\ajcSandbox\ajcTest15200.tmp\myProject\doc
  76. */
  77. protected String getAbsolutePathOutdir() {
  78. return docOutdir;
  79. }
  80. /**
  81. * Copy the contents of some directory to another location - the copy is recursive.
  82. */
  83. private void copy(File from, File to) {
  84. String contents[] = from.list();
  85. if (contents == null)
  86. return;
  87. for (String string : contents) {
  88. File f = new File(from, string);
  89. File t = new File(to, string);
  90. if (f.isDirectory()) {
  91. t.mkdir();
  92. copy(f, t);
  93. } else if (f.isFile()) {
  94. try {
  95. org.aspectj.util.FileUtil.copyFile(f, t);
  96. } catch (IOException e) {
  97. throw new AssertionFailedError("Unable to copy " + f + " to " + t);
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Run the ajdoc command with the given visibility argument, the default source level and the given input files.
  104. */
  105. public void runAjdoc(String visibility, File[] inputFiles) {
  106. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  107. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  108. }
  109. if (inputFiles.length == 0) {
  110. fail("need to pass some files into ajdoc");
  111. }
  112. String[] args = new String[5 + inputFiles.length];
  113. args[0] = "-" + visibility;
  114. args[1] = "-classpath";
  115. args[2] = AjdocTests.ASPECTJRT_PATH.getPath();
  116. args[3] = "-d";
  117. args[4] = getAbsolutePathOutdir();
  118. for (int i = 0; i < inputFiles.length; i++) {
  119. args[5 + i] = inputFiles[i].getAbsolutePath();
  120. }
  121. org.aspectj.tools.ajdoc.Main.main(args);
  122. }
  123. /**
  124. * Run the ajdoc command with the default visibility and source level and the given input files.
  125. */
  126. public void runAjdoc(File[] inputFiles) {
  127. if (inputFiles.length == 0) {
  128. fail("need to pass some files into ajdoc");
  129. }
  130. String[] args = new String[4 + inputFiles.length];
  131. args[0] = "-classpath";
  132. args[1] = AjdocTests.ASPECTJRT_PATH.getPath();
  133. args[2] = "-d";
  134. args[3] = getAbsolutePathOutdir();
  135. for (int i = 0; i < inputFiles.length; i++) {
  136. args[4 + i] = inputFiles[i].getAbsolutePath();
  137. }
  138. org.aspectj.tools.ajdoc.Main.main(args);
  139. }
  140. /**
  141. * Run the ajdoc command with the default visibility and source level, the given input files and the given aspectj options
  142. */
  143. public void runAjdoc(File[] inputFiles, String sourceLevel, String[] ajOptions) {
  144. if (inputFiles.length == 0) {
  145. fail("need to pass some files into ajdoc");
  146. }
  147. if (!sourceLevel.equals("1.3") &&
  148. !sourceLevel.equals("1.4") &&
  149. !sourceLevel.equals("1.5") &&
  150. !sourceLevel.equals("1.6") &&
  151. !sourceLevel.equals("1.7") &&
  152. !sourceLevel.equals("1.8") &&
  153. !sourceLevel.equals("1.9") &&
  154. !sourceLevel.equals("10")) {
  155. fail("need to pass ajdoc '1.3' > '1.9' as the source level");
  156. }
  157. String[] args = new String[6 + inputFiles.length + ajOptions.length];
  158. args[0] = "-source";
  159. args[1] = sourceLevel;
  160. args[2] = "-classpath";
  161. args[3] = AjdocTests.ASPECTJRT_PATH.getPath();
  162. args[4] = "-d";
  163. args[5] = getAbsolutePathOutdir();
  164. for (int i = 0; i < ajOptions.length; i++) {
  165. args[6 + i] = ajOptions[i];
  166. }
  167. for (int i = 0; i < inputFiles.length; i++) {
  168. args[6 + i + ajOptions.length] = inputFiles[i].getAbsolutePath();
  169. }
  170. org.aspectj.tools.ajdoc.Main.main(args);
  171. }
  172. /**
  173. * Run the ajdoc command with the given visibility argument, the given source level argument and the given input files.
  174. */
  175. public void runAjdoc(String visibility, String sourceLevel, File[] inputFiles) {
  176. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  177. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  178. }
  179. if (!sourceLevel.equals("1.3") &&
  180. !sourceLevel.equals("1.4") &&
  181. !sourceLevel.equals("1.5") &&
  182. !sourceLevel.equals("1.6") &&
  183. !sourceLevel.equals("1.7") &&
  184. !sourceLevel.equals("1.8") &&
  185. !sourceLevel.equals("1.9") &&
  186. !sourceLevel.startsWith("9") &&
  187. !sourceLevel.startsWith("10")) {
  188. fail("need to pass suitable version to ajdoc as the source level");
  189. }
  190. if (inputFiles.length == 0) {
  191. fail("need to pass some files into ajdoc");
  192. }
  193. for (File inputFile : inputFiles) {
  194. if (!inputFile.exists()) {
  195. fail(inputFile.getAbsolutePath() + " does not exist");
  196. }
  197. }
  198. String[] args = new String[7 + inputFiles.length];
  199. args[0] = "-" + visibility;
  200. args[1] = "-source";
  201. args[2] = sourceLevel;
  202. args[3] = "-classpath";
  203. StringBuilder classpath = new StringBuilder();
  204. if (LangUtil.is19VMOrGreater()) {
  205. classpath.append(LangUtil.getJrtFsFilePath()).append(File.pathSeparator);
  206. }
  207. classpath.append(AjdocTests.ASPECTJRT_PATH.getPath());
  208. args[4] = classpath.toString();
  209. args[5] = "-d";
  210. args[6] = getAbsolutePathOutdir();
  211. // args[7] = "-Xset:minimalModel=false";
  212. for (int i = 0; i < inputFiles.length; i++) {
  213. args[7 + i] = inputFiles[i].getAbsolutePath();
  214. }
  215. org.aspectj.tools.ajdoc.Main.main(args);
  216. }
  217. /**
  218. * Run the ajdoc command with the given visibility argument, the default source level and the given input directories.
  219. */
  220. public void runAjdoc(String visibility, String[] directoryNames) {
  221. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  222. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  223. }
  224. if (directoryNames.length == 0) {
  225. fail("need to pass some directories into ajdoc");
  226. }
  227. String[] args = new String[7 + directoryNames.length];
  228. args[0] = "-" + visibility;
  229. args[1] = "-classpath";
  230. args[2] = AjdocTests.ASPECTJRT_PATH.getPath();
  231. args[3] = "-d";
  232. args[4] = getAbsolutePathOutdir();
  233. args[5] = "-sourcepath";
  234. args[6] = getAbsoluteProjectDir();
  235. for (int i = 0; i < directoryNames.length; i++) {
  236. args[7 + i] = directoryNames[i];
  237. }
  238. org.aspectj.tools.ajdoc.Main.main(args);
  239. }
  240. /**
  241. * Run the ajdoc command with the default visibility and source level and the given input directories.
  242. */
  243. public void runAjdoc(String[] directoryNames) {
  244. if (directoryNames.length == 0) {
  245. fail("need to pass some directories into ajdoc");
  246. }
  247. String[] args = new String[6 + directoryNames.length];
  248. args[0] = "-classpath";
  249. args[1] = AjdocTests.ASPECTJRT_PATH.getPath();
  250. args[2] = "-d";
  251. args[3] = getAbsolutePathOutdir();
  252. args[4] = "-sourcepath";
  253. args[5] = getAbsoluteProjectDir();
  254. for (int i = 0; i < directoryNames.length; i++) {
  255. args[6 + i] = directoryNames[i];
  256. }
  257. org.aspectj.tools.ajdoc.Main.main(args);
  258. }
  259. /**
  260. * Run the ajdoc command with the given visibility argument, the default source level and the given input directories.
  261. */
  262. public void runAjdoc(String visibility, String lstFile) {
  263. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  264. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  265. }
  266. String[] args = new String[8];
  267. args[0] = "-" + visibility;
  268. args[1] = "-classpath";
  269. args[2] = AjdocTests.ASPECTJRT_PATH.getPath();
  270. args[3] = "-d";
  271. args[4] = getAbsolutePathOutdir();
  272. args[5] = "-sourcepath";
  273. args[6] = getAbsoluteProjectDir();
  274. args[7] = "@" + getAbsoluteProjectDir() + File.separatorChar + lstFile;
  275. org.aspectj.tools.ajdoc.Main.main(args);
  276. }
  277. /**
  278. * Run the ajdoc command with the given options
  279. */
  280. public void runAjdoc(List options) {
  281. String[] args = new String[options.size()];
  282. int i = 0;
  283. for (Object option : options) {
  284. String element = (String) option;
  285. args[i] = element;
  286. i++;
  287. }
  288. org.aspectj.tools.ajdoc.Main.main(args);
  289. }
  290. }