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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.tools.ajc.Ajc;
  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 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 = Ajc.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 (int i = 0; i < contents.length; i++) {
  88. String string = contents[i];
  89. File f = new File(from, string);
  90. File t = new File(to, string);
  91. if (f.isDirectory()) {
  92. t.mkdir();
  93. copy(f, t);
  94. } else if (f.isFile()) {
  95. try {
  96. org.aspectj.util.FileUtil.copyFile(f, t);
  97. } catch (IOException e) {
  98. throw new AssertionFailedError("Unable to copy " + f + " to " + t);
  99. }
  100. }
  101. }
  102. }
  103. /**
  104. * Run the ajdoc command with the given visibility argument, the default source level and the given input files.
  105. */
  106. public void runAjdoc(String visibility, File[] inputFiles) {
  107. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  108. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  109. }
  110. if (inputFiles.length == 0) {
  111. fail("need to pass some files into ajdoc");
  112. }
  113. String[] args = new String[5 + inputFiles.length];
  114. args[0] = "-" + visibility;
  115. args[1] = "-classpath";
  116. args[2] = AjdocTests.ASPECTJRT_PATH.getPath();
  117. args[3] = "-d";
  118. args[4] = getAbsolutePathOutdir();
  119. for (int i = 0; i < inputFiles.length; i++) {
  120. args[5 + i] = inputFiles[i].getAbsolutePath();
  121. }
  122. org.aspectj.tools.ajdoc.Main.main(args);
  123. }
  124. /**
  125. * Run the ajdoc command with the default visibility and source level and the given input files.
  126. */
  127. public void runAjdoc(File[] inputFiles) {
  128. if (inputFiles.length == 0) {
  129. fail("need to pass some files into ajdoc");
  130. }
  131. String[] args = new String[4 + inputFiles.length];
  132. args[0] = "-classpath";
  133. args[1] = AjdocTests.ASPECTJRT_PATH.getPath();
  134. args[2] = "-d";
  135. args[3] = getAbsolutePathOutdir();
  136. for (int i = 0; i < inputFiles.length; i++) {
  137. args[4 + i] = inputFiles[i].getAbsolutePath();
  138. }
  139. org.aspectj.tools.ajdoc.Main.main(args);
  140. }
  141. /**
  142. * Run the ajdoc command with the default visibility and source level, the given input files and the given aspectj options
  143. */
  144. public void runAjdoc(File[] inputFiles, String sourceLevel, String[] ajOptions) {
  145. if (inputFiles.length == 0) {
  146. fail("need to pass some files into ajdoc");
  147. }
  148. if (!sourceLevel.equals("1.3") &&
  149. !sourceLevel.equals("1.4") &&
  150. !sourceLevel.equals("1.5") &&
  151. !sourceLevel.equals("1.6") &&
  152. !sourceLevel.equals("1.7") &&
  153. !sourceLevel.equals("1.8") &&
  154. !sourceLevel.equals("1.9") &&
  155. !sourceLevel.equals("10")) {
  156. fail("need to pass ajdoc '1.3' > '1.9' as the source level");
  157. }
  158. String[] args = new String[6 + inputFiles.length + ajOptions.length];
  159. args[0] = "-source";
  160. args[1] = sourceLevel;
  161. args[2] = "-classpath";
  162. args[3] = AjdocTests.ASPECTJRT_PATH.getPath();
  163. args[4] = "-d";
  164. args[5] = getAbsolutePathOutdir();
  165. for (int i = 0; i < ajOptions.length; i++) {
  166. args[6 + i] = ajOptions[i];
  167. }
  168. for (int i = 0; i < inputFiles.length; i++) {
  169. args[6 + i + ajOptions.length] = inputFiles[i].getAbsolutePath();
  170. }
  171. org.aspectj.tools.ajdoc.Main.main(args);
  172. }
  173. /**
  174. * Run the ajdoc command with the given visibility argument, the given source level argument and the given input files.
  175. */
  176. public void runAjdoc(String visibility, String sourceLevel, File[] inputFiles) {
  177. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  178. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  179. }
  180. if (!sourceLevel.equals("1.3") &&
  181. !sourceLevel.equals("1.4") &&
  182. !sourceLevel.equals("1.5") &&
  183. !sourceLevel.equals("1.6") &&
  184. !sourceLevel.equals("1.7") &&
  185. !sourceLevel.equals("1.8") &&
  186. !sourceLevel.equals("1.9") &&
  187. !sourceLevel.startsWith("9") &&
  188. !sourceLevel.startsWith("10")) {
  189. fail("need to pass suitable version to ajdoc as the source level");
  190. }
  191. if (inputFiles.length == 0) {
  192. fail("need to pass some files into ajdoc");
  193. }
  194. for (int i = 0; i < inputFiles.length; i++) {
  195. if (!inputFiles[i].exists()) {
  196. fail(inputFiles[i].getAbsolutePath() + " does not exist");
  197. }
  198. }
  199. String[] args = new String[7 + inputFiles.length];
  200. args[0] = "-" + visibility;
  201. args[1] = "-source";
  202. args[2] = sourceLevel;
  203. args[3] = "-classpath";
  204. StringBuilder classpath = new StringBuilder();
  205. if (LangUtil.is19VMOrGreater()) {
  206. classpath.append(LangUtil.getJrtFsFilePath()).append(File.pathSeparator);
  207. }
  208. classpath.append(AjdocTests.ASPECTJRT_PATH.getPath());
  209. args[4] = classpath.toString();
  210. args[5] = "-d";
  211. args[6] = getAbsolutePathOutdir();
  212. // args[7] = "-Xset:minimalModel=false";
  213. for (int i = 0; i < inputFiles.length; i++) {
  214. args[7 + i] = inputFiles[i].getAbsolutePath();
  215. }
  216. org.aspectj.tools.ajdoc.Main.main(args);
  217. }
  218. /**
  219. * Run the ajdoc command with the given visibility argument, the default source level and the given input directories.
  220. */
  221. public void runAjdoc(String visibility, String[] directoryNames) {
  222. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  223. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  224. }
  225. if (directoryNames.length == 0) {
  226. fail("need to pass some directories into ajdoc");
  227. }
  228. String[] args = new String[7 + directoryNames.length];
  229. args[0] = "-" + visibility;
  230. args[1] = "-classpath";
  231. args[2] = AjdocTests.ASPECTJRT_PATH.getPath();
  232. args[3] = "-d";
  233. args[4] = getAbsolutePathOutdir();
  234. args[5] = "-sourcepath";
  235. args[6] = getAbsoluteProjectDir();
  236. for (int i = 0; i < directoryNames.length; i++) {
  237. args[7 + i] = directoryNames[i];
  238. }
  239. org.aspectj.tools.ajdoc.Main.main(args);
  240. }
  241. /**
  242. * Run the ajdoc command with the default visibility and source level and the given input directories.
  243. */
  244. public void runAjdoc(String[] directoryNames) {
  245. if (directoryNames.length == 0) {
  246. fail("need to pass some directories into ajdoc");
  247. }
  248. String[] args = new String[6 + directoryNames.length];
  249. args[0] = "-classpath";
  250. args[1] = AjdocTests.ASPECTJRT_PATH.getPath();
  251. args[2] = "-d";
  252. args[3] = getAbsolutePathOutdir();
  253. args[4] = "-sourcepath";
  254. args[5] = getAbsoluteProjectDir();
  255. for (int i = 0; i < directoryNames.length; i++) {
  256. args[6 + i] = directoryNames[i];
  257. }
  258. org.aspectj.tools.ajdoc.Main.main(args);
  259. }
  260. /**
  261. * Run the ajdoc command with the given visibility argument, the default source level and the given input directories.
  262. */
  263. public void runAjdoc(String visibility, String lstFile) {
  264. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  265. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  266. }
  267. String[] args = new String[8];
  268. args[0] = "-" + visibility;
  269. args[1] = "-classpath";
  270. args[2] = AjdocTests.ASPECTJRT_PATH.getPath();
  271. args[3] = "-d";
  272. args[4] = getAbsolutePathOutdir();
  273. args[5] = "-sourcepath";
  274. args[6] = getAbsoluteProjectDir();
  275. args[7] = "@" + getAbsoluteProjectDir() + File.separatorChar + lstFile;
  276. org.aspectj.tools.ajdoc.Main.main(args);
  277. }
  278. /**
  279. * Run the ajdoc command with the given options
  280. */
  281. public void runAjdoc(List options) {
  282. String[] args = new String[options.size()];
  283. int i = 0;
  284. for (Iterator iter = options.iterator(); iter.hasNext();) {
  285. String element = (String) iter.next();
  286. args[i] = element;
  287. i++;
  288. }
  289. org.aspectj.tools.ajdoc.Main.main(args);
  290. }
  291. }