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

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