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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 junit.framework.AssertionFailedError;
  17. import junit.framework.TestCase;
  18. import org.aspectj.tools.ajc.Ajc;
  19. import org.aspectj.util.LangUtil;
  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. protected void setUp() throws Exception {
  29. super.setUp();
  30. docOutdir = null;
  31. projectDir = null;
  32. // Create a sandbox in which to work
  33. sandboxDir = Ajc.createEmptySandbox();
  34. // create the ajdocworkdingdir in the sandbox
  35. Main.setOutputWorkingDir(getWorkingDir().getAbsolutePath());
  36. }
  37. protected void tearDown() throws Exception {
  38. super.tearDown();
  39. // reset where ajdocworkingdir is created
  40. Main.resetOutputWorkingDir();
  41. }
  42. /**
  43. * Fill in the working directory with the project files and create a doc top level directory in which to generate the ajdoc
  44. * output.
  45. */
  46. public void initialiseProject(String projectName) {
  47. File projectSrc = new File(testdataSrcDir + File.separatorChar + projectName);
  48. File destination = new File(getWorkingDir(), projectName);
  49. if (!destination.exists()) {
  50. destination.mkdir();
  51. }
  52. copy(projectSrc, destination);
  53. projectDir = destination.getAbsolutePath();
  54. File docDestination = new File(getWorkingDir().toString() + File.separatorChar + projectName, "doc");
  55. if (!docDestination.exists()) {
  56. docDestination.mkdir();
  57. }
  58. docOutdir = docDestination.getAbsolutePath();
  59. }
  60. /**
  61. * @return the working directory
  62. */
  63. protected File getWorkingDir() {
  64. return sandboxDir;
  65. }
  66. /**
  67. * @return the absolute path of the project directory for example c:\temp\ajcSandbox\ajcTest15200.tmp\myProject
  68. */
  69. protected String getAbsoluteProjectDir() {
  70. return projectDir;
  71. }
  72. /**
  73. * @return the absolute path of the doc output directory for example c:\temp\ajcSandbox\ajcTest15200.tmp\myProject\doc
  74. */
  75. protected String getAbsolutePathOutdir() {
  76. return docOutdir;
  77. }
  78. /**
  79. * Copy the contents of some directory to another location - the copy is recursive.
  80. */
  81. private void copy(File from, File to) {
  82. String contents[] = from.list();
  83. if (contents == null)
  84. return;
  85. for (int i = 0; i < contents.length; i++) {
  86. String string = contents[i];
  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") && !sourceLevel.equals("1.4") && !sourceLevel.equals("1.5")) {
  147. fail("need to pass ajdoc '1.3', '1.4', or '1.5' as the source level");
  148. }
  149. String[] args = new String[6 + inputFiles.length + ajOptions.length];
  150. args[0] = "-source";
  151. args[1] = sourceLevel;
  152. args[2] = "-classpath";
  153. args[3] = AjdocTests.ASPECTJRT_PATH.getPath();
  154. args[4] = "-d";
  155. args[5] = getAbsolutePathOutdir();
  156. for (int i = 0; i < ajOptions.length; i++) {
  157. args[6 + i] = ajOptions[i];
  158. }
  159. for (int i = 0; i < inputFiles.length; i++) {
  160. args[6 + i + ajOptions.length] = inputFiles[i].getAbsolutePath();
  161. }
  162. org.aspectj.tools.ajdoc.Main.main(args);
  163. }
  164. /**
  165. * Run the ajdoc command with the given visibility argument, the given source level argument and the given input files.
  166. */
  167. public void runAjdoc(String visibility, String sourceLevel, File[] inputFiles) {
  168. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  169. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  170. }
  171. if (!sourceLevel.equals("1.3") && !sourceLevel.equals("1.4") && !sourceLevel.equals("1.5")) {
  172. fail("need to pass ajdoc '1.3', '1.4', or '1.5' as the source level");
  173. }
  174. if (inputFiles.length == 0) {
  175. fail("need to pass some files into ajdoc");
  176. }
  177. for (int i = 0; i < inputFiles.length; i++) {
  178. if (!inputFiles[i].exists()) {
  179. fail(inputFiles[i].getAbsolutePath() + " does not exist");
  180. }
  181. }
  182. String[] args = new String[7 + inputFiles.length];
  183. args[0] = "-" + visibility;
  184. args[1] = "-source";
  185. args[2] = sourceLevel;
  186. args[3] = "-classpath";
  187. StringBuilder classpath = new StringBuilder();
  188. if (LangUtil.is19VMOrGreater()) {
  189. classpath.append(LangUtil.getJrtFsFilePath()).append(File.pathSeparator);
  190. }
  191. classpath.append(AjdocTests.ASPECTJRT_PATH.getPath());
  192. args[4] = classpath.toString();
  193. args[5] = "-d";
  194. args[6] = getAbsolutePathOutdir();
  195. // args[7] = "-Xset:minimalModel=false";
  196. for (int i = 0; i < inputFiles.length; i++) {
  197. args[7 + i] = inputFiles[i].getAbsolutePath();
  198. }
  199. org.aspectj.tools.ajdoc.Main.main(args);
  200. }
  201. /**
  202. * Run the ajdoc command with the given visibility argument, the default source level and the given input directories.
  203. */
  204. public void runAjdoc(String visibility, String[] directoryNames) {
  205. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  206. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  207. }
  208. if (directoryNames.length == 0) {
  209. fail("need to pass some directories into ajdoc");
  210. }
  211. String[] args = new String[7 + directoryNames.length];
  212. args[0] = "-" + visibility;
  213. args[1] = "-classpath";
  214. args[2] = AjdocTests.ASPECTJRT_PATH.getPath();
  215. args[3] = "-d";
  216. args[4] = getAbsolutePathOutdir();
  217. args[5] = "-sourcepath";
  218. args[6] = getAbsoluteProjectDir();
  219. for (int i = 0; i < directoryNames.length; i++) {
  220. args[7 + i] = directoryNames[i];
  221. }
  222. org.aspectj.tools.ajdoc.Main.main(args);
  223. }
  224. /**
  225. * Run the ajdoc command with the default visibility and source level and the given input directories.
  226. */
  227. public void runAjdoc(String[] directoryNames) {
  228. if (directoryNames.length == 0) {
  229. fail("need to pass some directories into ajdoc");
  230. }
  231. String[] args = new String[6 + directoryNames.length];
  232. args[0] = "-classpath";
  233. args[1] = AjdocTests.ASPECTJRT_PATH.getPath();
  234. args[2] = "-d";
  235. args[3] = getAbsolutePathOutdir();
  236. args[4] = "-sourcepath";
  237. args[5] = getAbsoluteProjectDir();
  238. for (int i = 0; i < directoryNames.length; i++) {
  239. args[6 + i] = directoryNames[i];
  240. }
  241. org.aspectj.tools.ajdoc.Main.main(args);
  242. }
  243. /**
  244. * Run the ajdoc command with the given visibility argument, the default source level and the given input directories.
  245. */
  246. public void runAjdoc(String visibility, String lstFile) {
  247. if (!visibility.equals("public") && !visibility.equals("protected") && !visibility.equals("private")) {
  248. fail("need to pass 'public','protected' or 'private' visibility to ajdoc");
  249. }
  250. String[] args = new String[8];
  251. args[0] = "-" + visibility;
  252. args[1] = "-classpath";
  253. args[2] = AjdocTests.ASPECTJRT_PATH.getPath();
  254. args[3] = "-d";
  255. args[4] = getAbsolutePathOutdir();
  256. args[5] = "-sourcepath";
  257. args[6] = getAbsoluteProjectDir();
  258. args[7] = "@" + getAbsoluteProjectDir() + File.separatorChar + lstFile;
  259. org.aspectj.tools.ajdoc.Main.main(args);
  260. }
  261. /**
  262. * Run the ajdoc command with the given options
  263. */
  264. public void runAjdoc(List options) {
  265. String[] args = new String[options.size()];
  266. int i = 0;
  267. for (Iterator iter = options.iterator(); iter.hasNext();) {
  268. String element = (String) iter.next();
  269. args[i] = element;
  270. i++;
  271. }
  272. org.aspectj.tools.ajdoc.Main.main(args);
  273. }
  274. }