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.

RunSpec.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, Abraham Nevado (lucierna)
  11. * ******************************************************************/
  12. package org.aspectj.testing;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Enumeration;
  17. import java.util.List;
  18. import java.util.Properties;
  19. import java.util.StringTokenizer;
  20. import org.aspectj.tools.ajc.AjcTestCase;
  21. import org.aspectj.util.FileUtil;
  22. /**
  23. * @author Adrian Colyer
  24. */
  25. public class RunSpec implements ITestStep {
  26. private List<ExpectedMessageSpec> expected = new ArrayList<ExpectedMessageSpec>();
  27. private String classToRun;
  28. private String baseDir;
  29. private String options;
  30. private String cpath;
  31. private String orderedStderr;
  32. private AjcTest myTest;
  33. private OutputSpec stdErrSpec;
  34. private OutputSpec stdOutSpec;
  35. private String ltwFile;
  36. private String xlintFile;
  37. private String vmargs;
  38. private String usefullltw;
  39. public String toString() {
  40. return "RunSpec: Running '"+classToRun+"' in directory '"+baseDir+"'. Classpath of '"+cpath+"'";
  41. }
  42. public RunSpec() {
  43. }
  44. public void execute(AjcTestCase inTestCase) {
  45. if (!expected.isEmpty()) {
  46. System.err.println("Warning, message spec for run command is currently ignored (org.aspectj.testing.RunSpec)");
  47. }
  48. String[] args = buildArgs();
  49. // System.err.println("? execute() inTestCase='" + inTestCase + "', ltwFile=" + ltwFile);
  50. boolean useLtw = copyLtwFile(inTestCase.getSandboxDirectory());
  51. copyXlintFile(inTestCase.getSandboxDirectory());
  52. try {
  53. setSystemProperty("test.base.dir", inTestCase.getSandboxDirectory().getAbsolutePath());
  54. AjcTestCase.RunResult rr = inTestCase.run(getClassToRun(), args, vmargs, getClasspath(), useLtw, "true".equalsIgnoreCase(usefullltw));
  55. if (stdErrSpec != null) {
  56. stdErrSpec.matchAgainst(rr.getStdErr(), orderedStderr);
  57. }
  58. if (stdOutSpec != null) {
  59. stdOutSpec.matchAgainst(rr.getStdOut());
  60. }
  61. } finally {
  62. restoreProperties();
  63. }
  64. }
  65. /*
  66. * Logic to save/restore system properties. Copied from LTWTests. As Matthew noted, need to refactor LTWTests to use this
  67. */
  68. private Properties savedProperties = new Properties();
  69. public void setSystemProperty(String key, String value) {
  70. Properties systemProperties = System.getProperties();
  71. copyProperty(key, systemProperties, savedProperties);
  72. systemProperties.setProperty(key, value);
  73. }
  74. private static void copyProperty(String key, Properties from, Properties to) {
  75. String value = from.getProperty(key, NULL);
  76. to.setProperty(key, value);
  77. }
  78. private final static String NULL = "null";
  79. protected void restoreProperties() {
  80. Properties systemProperties = System.getProperties();
  81. for (Enumeration<Object> enu = savedProperties.keys(); enu.hasMoreElements();) {
  82. String key = (String) enu.nextElement();
  83. String value = savedProperties.getProperty(key);
  84. if (value == NULL)
  85. systemProperties.remove(key);
  86. else
  87. systemProperties.setProperty(key, value);
  88. }
  89. }
  90. public void addExpectedMessage(ExpectedMessageSpec message) {
  91. expected.add(message);
  92. }
  93. public void setBaseDir(String dir) {
  94. this.baseDir = dir;
  95. }
  96. public void setTest(AjcTest test) {
  97. this.myTest = test;
  98. }
  99. public String getOptions() {
  100. return options;
  101. }
  102. public void setOptions(String options) {
  103. this.options = options;
  104. }
  105. public String getClasspath() {
  106. if (cpath == null)
  107. return null;
  108. return this.cpath.replace('/', File.separatorChar).replace(',', File.pathSeparatorChar);
  109. }
  110. public void setClasspath(String cpath) {
  111. this.cpath = cpath;
  112. }
  113. public void addStdErrSpec(OutputSpec spec) {
  114. this.stdErrSpec = spec;
  115. }
  116. public void addStdOutSpec(OutputSpec spec) {
  117. this.stdOutSpec = spec;
  118. }
  119. public void setOrderedStderr(String orderedStderr) {
  120. this.orderedStderr = orderedStderr;
  121. }
  122. /**
  123. * @return Returns the classToRun.
  124. */
  125. public String getClassToRun() {
  126. return classToRun;
  127. }
  128. /**
  129. * @param classToRun The classToRun to set.
  130. */
  131. public void setClassToRun(String classToRun) {
  132. this.classToRun = classToRun;
  133. }
  134. public String getLtwFile() {
  135. return ltwFile;
  136. }
  137. public void setLtwFile(String ltwFile) {
  138. this.ltwFile = ltwFile;
  139. }
  140. private String[] buildArgs() {
  141. if (options == null)
  142. return new String[0];
  143. StringTokenizer strTok = new StringTokenizer(options, ",");
  144. String[] ret = new String[strTok.countTokens()];
  145. for (int i = 0; i < ret.length; i++) {
  146. ret[i] = strTok.nextToken();
  147. }
  148. return ret;
  149. }
  150. private boolean copyLtwFile(File sandboxDirectory) {
  151. boolean useLtw = false;
  152. if (ltwFile != null) {
  153. // TODO maw use flag rather than empty file name
  154. if (ltwFile.trim().length() == 0)
  155. return true;
  156. File from = new File(baseDir, ltwFile);
  157. File to = new File(sandboxDirectory, "META-INF" + File.separator + "aop.xml");
  158. // System.out.println("RunSpec.copyLtwFile() from=" + from.getAbsolutePath() + " to=" + to.getAbsolutePath());
  159. try {
  160. FileUtil.copyFile(from, to);
  161. useLtw = true;
  162. } catch (IOException ex) {
  163. AjcTestCase.fail(ex.toString());
  164. }
  165. }
  166. return useLtw;
  167. }
  168. public String getXlintFile() {
  169. return xlintFile;
  170. }
  171. public void setXlintFile(String xlintFile) {
  172. this.xlintFile = xlintFile;
  173. }
  174. public void setVmargs(String vmargs) {
  175. this.vmargs = vmargs;
  176. }
  177. public String getVmargs() {
  178. return vmargs;
  179. }
  180. public String getUsefullltw() {
  181. return usefullltw;
  182. }
  183. public void setUsefullltw(String usefullltw) {
  184. this.usefullltw = usefullltw;
  185. }
  186. private void copyXlintFile(File sandboxDirectory) {
  187. if (xlintFile != null) {
  188. File from = new File(baseDir, xlintFile);
  189. File to = new File(sandboxDirectory, File.separator + xlintFile);
  190. try {
  191. FileUtil.copyFile(from, to);
  192. } catch (IOException ex) {
  193. AjcTestCase.fail(ex.toString());
  194. }
  195. }
  196. }
  197. }