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

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