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

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