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

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