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.

CompileSpec.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Adrian Colyer,
  11. * ******************************************************************/
  12. package org.aspectj.testing;
  13. import java.io.File;
  14. import java.util.ArrayList;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import java.util.StringTokenizer;
  18. import org.aspectj.tools.ajc.AjcTestCase;
  19. import org.aspectj.tools.ajc.CompilationResult;
  20. /**
  21. * @author colyer
  22. *
  23. * TODO To change the template for this generated type comment go to
  24. * Window - Preferences - Java - Code Style - Code Templates
  25. */
  26. public class CompileSpec implements ITestStep {
  27. private List expected = new ArrayList();
  28. private String files;
  29. private boolean includeClassesDir;
  30. private String aspectpath;
  31. private String classpath;
  32. private String inpath;
  33. private String sourceroots;
  34. private String outjar;
  35. private String outxml;
  36. private String xlintfile;
  37. private String options;
  38. private String baseDir;
  39. private String extdirs;
  40. private AjcTest myTest;
  41. public CompileSpec() {
  42. }
  43. public void execute(AjcTestCase inTestCase) {
  44. File base = new File(baseDir);
  45. String[] args = buildArgs();
  46. CompilationResult result = inTestCase.ajc(base,args);
  47. AjcTestCase.MessageSpec messageSpec = buildMessageSpec();
  48. String failMessage = "test \"" + myTest.getTitle() + "\" failed";
  49. inTestCase.assertMessages(result,failMessage,messageSpec);
  50. inTestCase.setShouldEmptySandbox(false); // so subsequent steps in same test see my results
  51. }
  52. public void addExpectedMessage(ExpectedMessageSpec message) {
  53. expected.add(message);
  54. }
  55. public void setBaseDir(String dir) {
  56. this.baseDir = dir;
  57. }
  58. protected String getBaseDir() { return baseDir; }
  59. public void setTest(AjcTest t) {
  60. this.myTest = t;
  61. if (options != null && (options.indexOf("-1.5") != -1)) {
  62. myTest.setVm("1.5");
  63. }
  64. }
  65. protected AjcTest getTest() { return myTest; }
  66. /**
  67. * @return Returns the aspectpath.
  68. */
  69. public String getAspectpath() {
  70. return aspectpath;
  71. }
  72. /**
  73. * @param aspectpath The aspectpath to set.
  74. */
  75. public void setAspectpath(String aspectpath) {
  76. this.aspectpath = aspectpath.replace(',',File.pathSeparatorChar);
  77. }
  78. /**
  79. * @return Returns the classpath.
  80. */
  81. public String getClasspath() {
  82. return classpath;
  83. }
  84. /**
  85. * @param classpath The classpath to set.
  86. */
  87. public void setClasspath(String classpath) {
  88. this.classpath = classpath.replace(',',File.pathSeparatorChar);
  89. }
  90. /**
  91. * @return Returns the files.
  92. */
  93. public String getFiles() {
  94. return files;
  95. }
  96. /**
  97. * @param files The files to set.
  98. */
  99. public void setFiles(String files) {
  100. this.files = files;
  101. }
  102. /**
  103. * @return Returns the includeClassesDir.
  104. */
  105. public boolean isIncludeClassesDir() {
  106. return includeClassesDir;
  107. }
  108. /**
  109. * @param includeClassesDir The includeClassesDir to set.
  110. */
  111. public void setIncludeClassesDir(boolean includeClassesDir) {
  112. this.includeClassesDir = includeClassesDir;
  113. }
  114. /**
  115. * @return Returns the inpath.
  116. */
  117. public String getInpath() {
  118. return inpath;
  119. }
  120. /**
  121. * @param inpath The inpath to set.
  122. */
  123. public void setInpath(String inpath) {
  124. this.inpath = inpath.replace(',',File.pathSeparatorChar).replace(';',File.pathSeparatorChar);
  125. }
  126. /**
  127. * @return Returns the options.
  128. */
  129. public String getOptions() {
  130. return options;
  131. }
  132. /**
  133. * @param options The options to set.
  134. */
  135. public void setOptions(String options) {
  136. int i = options.indexOf("!eclipse");
  137. if (i != -1) {
  138. this.options = options.substring(0,i);
  139. this.options += options.substring(i + "!eclipse".length());
  140. } else {
  141. this.options = options;
  142. }
  143. }
  144. /**
  145. * @return Returns the outjar.
  146. */
  147. public String getOutjar() {
  148. return outjar;
  149. }
  150. /**
  151. * @param outjar The outjar to set.
  152. */
  153. public void setOutjar(String outjar) {
  154. this.outjar = outjar;
  155. }
  156. /**
  157. * @return Returns the outxml.
  158. */
  159. public String getOutxmlfile() {
  160. return outxml;
  161. }
  162. /**
  163. * @param outxml The the of the aop.xml file to generate
  164. */
  165. public void setOutxmlfile(String outxml) {
  166. this.outxml = outxml;
  167. }
  168. /**
  169. * @return Returns the sourceroots.
  170. */
  171. public String getSourceroots() {
  172. return sourceroots;
  173. }
  174. /**
  175. * @param sourceroots The sourceroots to set.
  176. */
  177. public void setSourceroots(String sourceroots) {
  178. this.sourceroots = sourceroots;
  179. }
  180. /**
  181. * @return Returns the xlintfile.
  182. */
  183. public String getXlintfile() {
  184. return xlintfile;
  185. }
  186. /**
  187. * @param xlintfile The xlintfile to set.
  188. */
  189. public void setXlintfile(String xlintfile) {
  190. this.xlintfile = xlintfile;
  191. }
  192. public String getExtdirs() { return extdirs;}
  193. public void setExtdirs(String extdirs) { this.extdirs = extdirs; }
  194. protected String[] buildArgs() {
  195. StringBuffer args = new StringBuffer();
  196. // add any set options, and then files to compile at the end
  197. if (getAspectpath() != null) {
  198. args.append("-aspectpath ");
  199. args.append(getAspectpath());
  200. args.append(" ");
  201. }
  202. if (getSourceroots() != null) {
  203. args.append("-sourceroots ");
  204. args.append(getSourceroots());
  205. args.append(" ");
  206. }
  207. if (getOutjar() != null) {
  208. args.append("-outjar ");
  209. args.append(getOutjar());
  210. args.append(" ");
  211. }
  212. if (getOutxmlfile() != null) {
  213. args.append("-outxmlfile ");
  214. args.append(getOutxmlfile());
  215. args.append(" ");
  216. }
  217. if (getOptions() != null) {
  218. StringTokenizer strTok = new StringTokenizer(getOptions(),",");
  219. while (strTok.hasMoreTokens()) {
  220. // For an option containing a comma, pass in a { in its place
  221. args.append(strTok.nextToken().replace('{', ','));
  222. args.append(" ");
  223. }
  224. }
  225. if (getClasspath() != null) {
  226. args.append("-classpath ");
  227. args.append(getClasspath());
  228. args.append(" ");
  229. }
  230. if (getXlintfile() != null) {
  231. args.append("-Xlintfile ");
  232. args.append(getXlintfile());
  233. args.append(" ");
  234. }
  235. if (getExtdirs() != null) {
  236. args.append("-extdirs ");
  237. args.append(getExtdirs());
  238. args.append(" ");
  239. }
  240. List fileList = new ArrayList();
  241. List jarList = new ArrayList();
  242. // convention that any jar on file list should be added to inpath
  243. String files = getFiles();
  244. if (files == null) files = "";
  245. StringTokenizer strTok = new StringTokenizer(files,",");
  246. while (strTok.hasMoreTokens()) {
  247. final String file = strTok.nextToken();
  248. if (file.endsWith(".jar")) {
  249. jarList.add(file);
  250. } else {
  251. fileList.add(file);
  252. }
  253. }
  254. if ((getInpath() != null) || !jarList.isEmpty()) {
  255. args.append("-inpath ");
  256. if (getInpath() != null) args.append(getInpath());
  257. for (Iterator iter = jarList.iterator(); iter.hasNext();) {
  258. String jar = (String) iter.next();
  259. args.append(File.pathSeparator);
  260. args.append(jar);
  261. }
  262. args.append(" ");
  263. }
  264. for (Iterator iter = fileList.iterator(); iter.hasNext();) {
  265. String file = (String) iter.next();
  266. args.append(file);
  267. args.append(" ");
  268. }
  269. String argumentString = args.toString();
  270. strTok = new StringTokenizer(argumentString," ");
  271. String[] ret = new String[strTok.countTokens()];
  272. for (int i = 0; i < ret.length; i++) {
  273. ret[i] = strTok.nextToken();
  274. }
  275. return ret;
  276. }
  277. protected AjcTestCase.MessageSpec buildMessageSpec() {
  278. List<AjcTestCase.Message> infos = null;
  279. List<AjcTestCase.Message> warnings = new ArrayList<AjcTestCase.Message>();
  280. List<AjcTestCase.Message> errors = new ArrayList<AjcTestCase.Message>();
  281. List<AjcTestCase.Message> fails = new ArrayList<AjcTestCase.Message>();
  282. List<AjcTestCase.Message> weaveInfos = new ArrayList<AjcTestCase.Message>();
  283. for (Iterator iter = expected.iterator(); iter.hasNext();) {
  284. ExpectedMessageSpec exMsg = (ExpectedMessageSpec) iter.next();
  285. String kind = exMsg.getKind();
  286. if (kind.equals("info")) {
  287. if (infos == null) infos = new ArrayList<AjcTestCase.Message>();
  288. infos.add(exMsg.toMessage());
  289. } else if (kind.equals("warning")) {
  290. warnings.add(exMsg.toMessage());
  291. } else if (kind.equals("error")) {
  292. errors.add(exMsg.toMessage());
  293. } else if (kind.equals("fail")) {
  294. fails.add(exMsg.toMessage());
  295. } else if (kind.equals("abort")) {
  296. fails.add(exMsg.toMessage());
  297. } else if (kind.equals("weave")) {
  298. weaveInfos.add(exMsg.toMessage());
  299. }
  300. }
  301. return new AjcTestCase.MessageSpec(infos,warnings,errors,fails,weaveInfos);
  302. }
  303. }