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

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