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

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