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.

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