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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.is9VMOrGreater;
  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. //
  65. // The reason for setting this parameter for Java 9+ instead of 16+ is that it helps to avoid the JVM printing
  66. // unwanted illegal access warnings during weaving in 'useFullLTW' mode, either making existing tests fail or
  67. // having to assert on the warning messages.
  68. vmargs += is9VMOrGreater() ? " --add-opens java.base/java.lang=ALL-UNNAMED" : "";
  69. AjcTestCase.RunResult rr = inTestCase.run(getClassToRun(), getModuleToRun(), args, vmargs, getClasspath(), getModulepath(), useLtw, "true".equalsIgnoreCase(usefullltw));
  70. if (stdErrSpec != null) {
  71. stdErrSpec.matchAgainst(rr.getStdErr(), orderedStderr);
  72. }
  73. if (stdOutSpec != null) {
  74. stdOutSpec.matchAgainst(rr.getStdOut());
  75. }
  76. } finally {
  77. restoreProperties();
  78. }
  79. }
  80. /*
  81. * Logic to save/restore system properties. Copied from LTWTests. As Matthew noted, need to refactor LTWTests to use this
  82. */
  83. private Properties savedProperties = new Properties();
  84. public void setSystemProperty(String key, String value) {
  85. Properties systemProperties = System.getProperties();
  86. copyProperty(key, systemProperties, savedProperties);
  87. systemProperties.setProperty(key, value);
  88. }
  89. private static void copyProperty(String key, Properties from, Properties to) {
  90. String value = from.getProperty(key, NULL);
  91. to.setProperty(key, value);
  92. }
  93. private final static String NULL = "null";
  94. protected void restoreProperties() {
  95. Properties systemProperties = System.getProperties();
  96. for (Enumeration<Object> enu = savedProperties.keys(); enu.hasMoreElements();) {
  97. String key = (String) enu.nextElement();
  98. String value = savedProperties.getProperty(key);
  99. if (value == NULL)
  100. systemProperties.remove(key);
  101. else
  102. systemProperties.setProperty(key, value);
  103. }
  104. }
  105. @Override
  106. public void addExpectedMessage(ExpectedMessageSpec message) {
  107. expected.add(message);
  108. }
  109. @Override
  110. public void setBaseDir(String dir) {
  111. this.baseDir = dir;
  112. }
  113. @Override
  114. public void setTest(AjcTest test) {
  115. this.myTest = test;
  116. }
  117. public AjcTest getTest() {
  118. return this.myTest;
  119. }
  120. public String getOptions() {
  121. return options;
  122. }
  123. public void setOptions(String options) {
  124. this.options = options;
  125. }
  126. public String getClasspath() {
  127. return cpath;
  128. }
  129. public String getModulepath() {
  130. return mpath;
  131. }
  132. public void setModulepath(String mpath) {
  133. this.mpath = mpath.replace('/', File.separatorChar).replace(',', File.pathSeparatorChar);
  134. }
  135. public void setClasspath(String cpath) {
  136. this.cpath = cpath.replace('/', File.separatorChar).replace(',', File.pathSeparatorChar);
  137. }
  138. public void addStdErrSpec(OutputSpec spec) {
  139. this.stdErrSpec = spec;
  140. }
  141. public void addStdOutSpec(OutputSpec spec) {
  142. this.stdOutSpec = spec;
  143. }
  144. public void setOrderedStderr(String orderedStderr) {
  145. this.orderedStderr = orderedStderr;
  146. }
  147. public String getClassToRun() {
  148. return classToRun;
  149. }
  150. public void setClassToRun(String classToRun) {
  151. this.classToRun = classToRun;
  152. }
  153. public void setModuleToRun(String moduleToRun) {
  154. this.moduleToRun = moduleToRun;
  155. }
  156. public String getModuleToRun() {
  157. return this.moduleToRun;
  158. }
  159. public String getLtwFile() {
  160. return ltwFile;
  161. }
  162. public void setLtwFile(String ltwFile) {
  163. this.ltwFile = ltwFile;
  164. }
  165. private String[] buildArgs() {
  166. if (options == null)
  167. return new String[0];
  168. StringTokenizer strTok = new StringTokenizer(options, ",");
  169. String[] ret = new String[strTok.countTokens()];
  170. for (int i = 0; i < ret.length; i++) {
  171. ret[i] = strTok.nextToken();
  172. }
  173. return ret;
  174. }
  175. private boolean copyLtwFile(File sandboxDirectory) {
  176. boolean useLtw = false;
  177. if (ltwFile != null) {
  178. // TODO maw use flag rather than empty file name
  179. if (ltwFile.trim().length() == 0)
  180. return true;
  181. File from = new File(baseDir, ltwFile);
  182. File to = new File(sandboxDirectory, "META-INF" + File.separator + "aop.xml");
  183. // System.out.println("RunSpec.copyLtwFile() from=" + from.getAbsolutePath() + " to=" + to.getAbsolutePath());
  184. try {
  185. FileUtil.copyFile(from, to);
  186. useLtw = true;
  187. } catch (IOException ex) {
  188. AjcTestCase.fail(ex.toString());
  189. }
  190. }
  191. return useLtw;
  192. }
  193. public String getXlintFile() {
  194. return xlintFile;
  195. }
  196. public void setXlintFile(String xlintFile) {
  197. this.xlintFile = xlintFile;
  198. }
  199. public void setVmargs(String vmargs) {
  200. this.vmargs = vmargs;
  201. }
  202. public String getVmargs() {
  203. return vmargs;
  204. }
  205. public String getUsefullltw() {
  206. return usefullltw;
  207. }
  208. public void setUsefullltw(String usefullltw) {
  209. this.usefullltw = usefullltw;
  210. }
  211. private void copyXlintFile(File sandboxDirectory) {
  212. if (xlintFile != null) {
  213. File from = new File(baseDir, xlintFile);
  214. File to = new File(sandboxDirectory, File.separator + xlintFile);
  215. try {
  216. FileUtil.copyFile(from, to);
  217. } catch (IOException ex) {
  218. AjcTestCase.fail(ex.toString());
  219. }
  220. }
  221. }
  222. }