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 9.9KB

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