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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.List;
  17. import java.util.StringTokenizer;
  18. import org.aspectj.tools.ajc.AjcTestCase;
  19. import org.aspectj.util.FileUtil;
  20. /**
  21. * @author colyer
  22. *
  23. * TODO To change the template for this generated type comment go to
  24. * Window - Preferences - Java - Code Style - Code Templates
  25. */
  26. public class RunSpec implements ITestStep {
  27. private List expected = new ArrayList();
  28. private String classToRun;
  29. private String baseDir;
  30. private String options;
  31. private String cpath;
  32. private AjcTest myTest;
  33. private OutputSpec stdErrSpec;
  34. private OutputSpec stdOutSpec;
  35. private String ltwFile;
  36. public RunSpec() {
  37. }
  38. /* (non-Javadoc)
  39. * @see org.aspectj.testing.ITestStep#execute(org.aspectj.tools.ajc.AjcTestCase)
  40. */
  41. public void execute(AjcTestCase inTestCase) {
  42. if (!expected.isEmpty()) {
  43. System.err.println("Warning, message spec for run command is currently ignored (org.aspectj.testing.RunSpec)");
  44. }
  45. String[] args = buildArgs();
  46. // System.err.println("? execute() inTestCase='" + inTestCase + "', ltwFile=" + ltwFile);
  47. boolean useLtw = copyLtwFile(inTestCase.getSandboxDirectory());
  48. AjcTestCase.RunResult rr = inTestCase.run(getClassToRun(),args,getClasspath(),useLtw);
  49. if (stdErrSpec != null) {
  50. stdErrSpec.matchAgainst(rr.getStdErr());
  51. }
  52. if (stdOutSpec != null) {
  53. stdOutSpec.matchAgainst(rr.getStdOut());
  54. }
  55. }
  56. public void addExpectedMessage(ExpectedMessageSpec message) {
  57. expected.add(message);
  58. }
  59. public void setBaseDir(String dir) {
  60. this.baseDir = dir;
  61. }
  62. public void setTest(AjcTest test) {
  63. this.myTest = test;
  64. }
  65. public String getOptions() {
  66. return options;
  67. }
  68. public void setOptions(String options) {
  69. this.options = options;
  70. }
  71. public String getClasspath() {
  72. if (cpath == null) return null;
  73. return this.cpath.replace('/', File.separatorChar);
  74. }
  75. public void setClasspath(String cpath) {
  76. this.cpath = cpath;
  77. }
  78. public void addStdErrSpec(OutputSpec spec) {
  79. this.stdErrSpec = spec;
  80. }
  81. public void addStdOutSpec(OutputSpec spec) {
  82. this.stdOutSpec = spec;
  83. }
  84. /**
  85. * @return Returns the classToRun.
  86. */
  87. public String getClassToRun() {
  88. return classToRun;
  89. }
  90. /**
  91. * @param classToRun The classToRun to set.
  92. */
  93. public void setClassToRun(String classToRun) {
  94. this.classToRun = classToRun;
  95. }
  96. public String getLtwFile() {
  97. return ltwFile;
  98. }
  99. public void setLtwFile(String ltwFile) {
  100. this.ltwFile = ltwFile;
  101. }
  102. private String[] buildArgs() {
  103. if (options == null) return new String[0];
  104. StringTokenizer strTok = new StringTokenizer(options,",");
  105. String[] ret = new String[strTok.countTokens()];
  106. for (int i = 0; i < ret.length; i++) {
  107. ret[i] = strTok.nextToken();
  108. }
  109. return ret;
  110. }
  111. private boolean copyLtwFile (File sandboxDirectory) {
  112. boolean useLtw = false;
  113. if (ltwFile != null) {
  114. // TODO maw use flag rather than empty file name
  115. if (ltwFile.trim().length() == 0) return true;
  116. File from = new File(baseDir,ltwFile);
  117. File to = new File(sandboxDirectory,"META-INF" + File.separator + "aop.xml");
  118. // System.out.println("RunSpec.copyLtwFile() from=" + from.getAbsolutePath() + " to=" + to.getAbsolutePath());
  119. try {
  120. FileUtil.copyFile(from,to);
  121. useLtw = true;
  122. }
  123. catch (IOException ex) {
  124. ex.printStackTrace();
  125. }
  126. }
  127. return useLtw;
  128. }
  129. }