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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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 orderedStdout;
  35. private String orderedStderr;
  36. private AjcTest myTest;
  37. private OutputSpec stdErrSpec;
  38. private OutputSpec stdOutSpec;
  39. private String ltwFile;
  40. private String xlintFile;
  41. private String vmargs;
  42. private String usefullltw;
  43. @Override
  44. public String toString() {
  45. return "RunSpec: Running '"+classToRun+"' in directory '"+baseDir+"'. Classpath of '"+cpath+"'";
  46. }
  47. public RunSpec() {
  48. }
  49. @Override
  50. public void execute(AjcTestCase inTestCase) {
  51. if (!expected.isEmpty()) {
  52. System.err.println("Warning, message spec for run command is currently ignored (org.aspectj.testing.RunSpec)");
  53. }
  54. String[] args = buildArgs();
  55. // System.err.println("? execute() inTestCase='" + inTestCase + "', ltwFile=" + ltwFile);
  56. boolean useLtw = copyLtwFile(inTestCase.getSandboxDirectory());
  57. copyXlintFile(inTestCase.getSandboxDirectory());
  58. try {
  59. setSystemProperty("test.base.dir", inTestCase.getSandboxDirectory().getAbsolutePath());
  60. if (vmargs == null)
  61. vmargs = "";
  62. // On Java 16+, LTW did not work on AspectJ 1.9.7 to 1.9.21 without this parameter. So, we added the argument here
  63. // and not in AjcTestCase::run, because without 'useLTW' or 'useFullLTW', there might have been a need for weaver
  64. // attachment during runtime. See also docs/release/README-1.8.7.adoc.
  65. //
  66. // Since AspectJ 1.9.21.1, '--add-opens' is no longer necessary, because we found a workaround for defining
  67. // classes in arbitrary class loaders. But some tests, e.g. AtAjLTWTests.testLTWUnweavable, still use
  68. // ClassLoader::defineClass to inject dynamically generated classes into the current class loader. Therefore, we
  69. // still set the parameters, so they can be used on demand - not for LTW as such, but for class injection. See
  70. // also tests/java5/ataspectj/ataspectj/UnweavableTest.java.
  71. //
  72. // The reason for setting this parameter for Java 9+ instead of 16+ is that it helps to avoid the JVM printing
  73. // unwanted illegal access warnings during weaving in 'useFullLTW' mode, either making existing tests fail or
  74. // having to assert on the warning messages.
  75. //
  76. // vmargs += is16VMOrGreater() ? " --add-opens java.base/java.lang=ALL-UNNAMED" : "";
  77. AjcTestCase.RunResult rr = inTestCase.run(getClassToRun(), getModuleToRun(), args, vmargs, getClasspath(), getModulepath(), useLtw, "true".equalsIgnoreCase(usefullltw));
  78. if (stdErrSpec != null) {
  79. stdErrSpec.matchAgainst(rr.getStdErr(), orderedStderr);
  80. }
  81. if (stdOutSpec != null) {
  82. stdOutSpec.matchAgainst(rr.getStdOut(), orderedStdout);
  83. }
  84. } finally {
  85. restoreProperties();
  86. }
  87. }
  88. /*
  89. * Logic to save/restore system properties. Copied from LTWTests. As Matthew noted, need to refactor LTWTests to use this
  90. */
  91. private Properties savedProperties = new Properties();
  92. public void setSystemProperty(String key, String value) {
  93. Properties systemProperties = System.getProperties();
  94. copyProperty(key, systemProperties, savedProperties);
  95. systemProperties.setProperty(key, value);
  96. }
  97. private static void copyProperty(String key, Properties from, Properties to) {
  98. String value = from.getProperty(key, NULL);
  99. to.setProperty(key, value);
  100. }
  101. private final static String NULL = "null";
  102. protected void restoreProperties() {
  103. Properties systemProperties = System.getProperties();
  104. for (Enumeration<Object> enu = savedProperties.keys(); enu.hasMoreElements();) {
  105. String key = (String) enu.nextElement();
  106. String value = savedProperties.getProperty(key);
  107. if (value == NULL)
  108. systemProperties.remove(key);
  109. else
  110. systemProperties.setProperty(key, value);
  111. }
  112. }
  113. @Override
  114. public void addExpectedMessage(ExpectedMessageSpec message) {
  115. expected.add(message);
  116. }
  117. @Override
  118. public void setBaseDir(String dir) {
  119. this.baseDir = dir;
  120. }
  121. @Override
  122. public void setTest(AjcTest test) {
  123. this.myTest = test;
  124. }
  125. public AjcTest getTest() {
  126. return this.myTest;
  127. }
  128. public String getOptions() {
  129. return options;
  130. }
  131. public void setOptions(String options) {
  132. this.options = options;
  133. }
  134. public String getClasspath() {
  135. return cpath;
  136. }
  137. public String getModulepath() {
  138. return mpath;
  139. }
  140. public void setModulepath(String mpath) {
  141. this.mpath = mpath.replace('/', File.separatorChar).replace(',', File.pathSeparatorChar);
  142. }
  143. public void setClasspath(String cpath) {
  144. this.cpath = cpath.replace('/', File.separatorChar).replace(',', File.pathSeparatorChar);
  145. }
  146. public void addStdErrSpec(OutputSpec spec) {
  147. this.stdErrSpec = spec;
  148. }
  149. public void addStdOutSpec(OutputSpec spec) {
  150. this.stdOutSpec = spec;
  151. }
  152. public void setOrderedStderr(String orderedStderr) {
  153. this.orderedStderr = orderedStderr;
  154. }
  155. public void setOrderedStdout(String orderedStdout) {
  156. this.orderedStdout = orderedStdout;
  157. }
  158. public String getClassToRun() {
  159. return classToRun;
  160. }
  161. public void setClassToRun(String classToRun) {
  162. this.classToRun = classToRun;
  163. }
  164. public void setModuleToRun(String moduleToRun) {
  165. this.moduleToRun = moduleToRun;
  166. }
  167. public String getModuleToRun() {
  168. return this.moduleToRun;
  169. }
  170. public String getLtwFile() {
  171. return ltwFile;
  172. }
  173. public void setLtwFile(String ltwFile) {
  174. this.ltwFile = ltwFile;
  175. }
  176. private String[] buildArgs() {
  177. if (options == null)
  178. return new String[0];
  179. StringTokenizer strTok = new StringTokenizer(options, ",");
  180. String[] ret = new String[strTok.countTokens()];
  181. for (int i = 0; i < ret.length; i++) {
  182. ret[i] = strTok.nextToken();
  183. }
  184. return ret;
  185. }
  186. private boolean copyLtwFile(File sandboxDirectory) {
  187. boolean useLtw = false;
  188. if (ltwFile != null) {
  189. // TODO maw use flag rather than empty file name
  190. if (ltwFile.trim().length() == 0)
  191. return true;
  192. File from = new File(baseDir, ltwFile);
  193. File to = new File(sandboxDirectory, "META-INF" + File.separator + "aop.xml");
  194. // System.out.println("RunSpec.copyLtwFile() from=" + from.getAbsolutePath() + " to=" + to.getAbsolutePath());
  195. try {
  196. FileUtil.copyFile(from, to);
  197. useLtw = true;
  198. } catch (IOException ex) {
  199. AjcTestCase.fail(ex.toString());
  200. }
  201. }
  202. return useLtw;
  203. }
  204. public String getXlintFile() {
  205. return xlintFile;
  206. }
  207. public void setXlintFile(String xlintFile) {
  208. this.xlintFile = xlintFile;
  209. }
  210. public void setVmargs(String vmargs) {
  211. this.vmargs = vmargs;
  212. }
  213. public String getVmargs() {
  214. return vmargs;
  215. }
  216. public String getUsefullltw() {
  217. return usefullltw;
  218. }
  219. public void setUsefullltw(String usefullltw) {
  220. this.usefullltw = usefullltw;
  221. }
  222. private void copyXlintFile(File sandboxDirectory) {
  223. if (xlintFile != null) {
  224. File from = new File(baseDir, xlintFile);
  225. File to = new File(sandboxDirectory, File.separator + xlintFile);
  226. try {
  227. FileUtil.copyFile(from, to);
  228. } catch (IOException ex) {
  229. AjcTestCase.fail(ex.toString());
  230. }
  231. }
  232. }
  233. }