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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-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 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.indexOf("-1.5") != -1)) {
  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. /**
  90. * @return Returns the files.
  91. */
  92. public String getFiles() {
  93. return files;
  94. }
  95. /**
  96. * @param files The files to set.
  97. */
  98. public void setFiles(String files) {
  99. this.files = files;
  100. }
  101. /**
  102. * @return Returns the includeClassesDir.
  103. */
  104. public boolean isIncludeClassesDir() {
  105. return includeClassesDir;
  106. }
  107. /**
  108. * @param includeClassesDir The includeClassesDir to set.
  109. */
  110. public void setIncludeClassesDir(boolean includeClassesDir) {
  111. this.includeClassesDir = includeClassesDir;
  112. }
  113. /**
  114. * @return Returns the inpath.
  115. */
  116. public String getInpath() {
  117. return inpath;
  118. }
  119. /**
  120. * @param inpath The inpath to set.
  121. */
  122. public void setInpath(String inpath) {
  123. this.inpath = inpath.replace(',',File.pathSeparatorChar);
  124. }
  125. /**
  126. * @return Returns the options.
  127. */
  128. public String getOptions() {
  129. return options;
  130. }
  131. /**
  132. * @param options The options to set.
  133. */
  134. public void setOptions(String options) {
  135. int i = options.indexOf("!eclipse");
  136. if (i != -1) {
  137. this.options = options.substring(0,i);
  138. this.options += options.substring(i + "!eclipse".length());
  139. } else {
  140. this.options = options;
  141. }
  142. }
  143. /**
  144. * @return Returns the outjar.
  145. */
  146. public String getOutjar() {
  147. return outjar;
  148. }
  149. /**
  150. * @param outjar The outjar to set.
  151. */
  152. public void setOutjar(String outjar) {
  153. this.outjar = outjar;
  154. }
  155. /**
  156. * @return Returns the sourceroots.
  157. */
  158. public String getSourceroots() {
  159. return sourceroots;
  160. }
  161. /**
  162. * @param sourceroots The sourceroots to set.
  163. */
  164. public void setSourceroots(String sourceroots) {
  165. this.sourceroots = sourceroots;
  166. }
  167. /**
  168. * @return Returns the xlintfile.
  169. */
  170. public String getXlintfile() {
  171. return xlintfile;
  172. }
  173. /**
  174. * @param xlintfile The xlintfile to set.
  175. */
  176. public void setXlintfile(String xlintfile) {
  177. this.xlintfile = xlintfile;
  178. }
  179. public String getExtdirs() { return extdirs;}
  180. public void setExtdirs(String extdirs) { this.extdirs = extdirs; }
  181. protected String[] buildArgs() {
  182. StringBuffer args = new StringBuffer();
  183. // add any set options, and then files to compile at the end
  184. if (getAspectpath() != null) {
  185. args.append("-aspectpath ");
  186. args.append(getAspectpath());
  187. args.append(" ");
  188. }
  189. if (getSourceroots() != null) {
  190. args.append("-sourceroots ");
  191. args.append(getSourceroots());
  192. args.append(" ");
  193. }
  194. if (getOutjar() != null) {
  195. args.append("-outjar ");
  196. args.append(getOutjar());
  197. args.append(" ");
  198. }
  199. if (getOptions() != null) {
  200. StringTokenizer strTok = new StringTokenizer(getOptions(),",");
  201. while (strTok.hasMoreTokens()) {
  202. args.append(strTok.nextToken());
  203. args.append(" ");
  204. }
  205. }
  206. if (getClasspath() != null) {
  207. args.append("-classpath ");
  208. args.append(getClasspath());
  209. args.append(" ");
  210. }
  211. if (getXlintfile() != null) {
  212. args.append("-Xlintfile ");
  213. args.append(getXlintfile());
  214. args.append(" ");
  215. }
  216. if (getExtdirs() != null) {
  217. args.append("-extdirs ");
  218. args.append(getExtdirs());
  219. args.append(" ");
  220. }
  221. List fileList = new ArrayList();
  222. List jarList = new ArrayList();
  223. // convention that any jar on file list should be added to inpath
  224. String files = getFiles();
  225. if (files == null) files = "";
  226. StringTokenizer strTok = new StringTokenizer(files,",");
  227. while (strTok.hasMoreTokens()) {
  228. final String file = strTok.nextToken();
  229. if (file.endsWith(".jar")) {
  230. jarList.add(file);
  231. } else {
  232. fileList.add(file);
  233. }
  234. }
  235. if ((getInpath() != null) || !jarList.isEmpty()) {
  236. args.append("-inpath ");
  237. if (getInpath() != null) args.append(getInpath());
  238. for (Iterator iter = jarList.iterator(); iter.hasNext();) {
  239. String jar = (String) iter.next();
  240. args.append(File.pathSeparator);
  241. args.append(jar);
  242. }
  243. args.append(" ");
  244. }
  245. for (Iterator iter = fileList.iterator(); iter.hasNext();) {
  246. String file = (String) iter.next();
  247. args.append(file);
  248. args.append(" ");
  249. }
  250. String argumentString = args.toString();
  251. strTok = new StringTokenizer(argumentString," ");
  252. String[] ret = new String[strTok.countTokens()];
  253. for (int i = 0; i < ret.length; i++) {
  254. ret[i] = strTok.nextToken();
  255. }
  256. return ret;
  257. }
  258. protected AjcTestCase.MessageSpec buildMessageSpec() {
  259. List infos = null;
  260. List warnings = new ArrayList();
  261. List errors = new ArrayList();
  262. List fails = new ArrayList();
  263. List weaveInfos = new ArrayList();
  264. for (Iterator iter = expected.iterator(); iter.hasNext();) {
  265. ExpectedMessageSpec exMsg = (ExpectedMessageSpec) iter.next();
  266. String kind = exMsg.getKind();
  267. if (kind.equals("info")) {
  268. if (infos == null) infos = new ArrayList();
  269. infos.add(exMsg.toMessage());
  270. } else if (kind.equals("warning")) {
  271. warnings.add(exMsg.toMessage());
  272. } else if (kind.equals("error")) {
  273. errors.add(exMsg.toMessage());
  274. } else if (kind.equals("fail")) {
  275. fails.add(exMsg.toMessage());
  276. } else if (kind.equals("abort")) {
  277. fails.add(exMsg.toMessage());
  278. } else if (kind.equals("weave")) {
  279. weaveInfos.add(exMsg.toMessage());
  280. }
  281. }
  282. return new AjcTestCase.MessageSpec(infos,warnings,errors,fails,weaveInfos);
  283. }
  284. }