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.

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