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.

WeaveSpec.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* *******************************************************************
  2. * Copyright (c) 2005 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.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.StringTokenizer;
  23. import java.util.jar.JarEntry;
  24. import java.util.jar.JarOutputStream;
  25. import org.aspectj.tools.ajc.AjcTestCase;
  26. import org.aspectj.tools.ajc.CompilationResult;
  27. /**
  28. * @author colyer
  29. *
  30. * TODO To change the template for this generated type comment go to
  31. * Window - Preferences - Java - Code Style - Code Templates
  32. */
  33. public class WeaveSpec extends CompileSpec {
  34. private String classesFiles;
  35. private String aspectsFiles;
  36. private List classFilesFromClasses;
  37. /* (non-Javadoc)
  38. * @see org.aspectj.testing.ITestStep#execute(org.aspectj.tools.ajc.AjcTestCase)
  39. */
  40. public void execute(AjcTestCase inTestCase) {
  41. String failMessage = "test \"" + getTest().getTitle() + "\" failed";
  42. try {
  43. File base = new File(getBaseDir());
  44. classFilesFromClasses = new ArrayList();
  45. setFiles(classesFiles);
  46. String[] args = buildArgs();
  47. CompilationResult result = inTestCase.ajc(base,args);
  48. inTestCase.assertNoMessages(result,failMessage);
  49. File sandbox = inTestCase.getSandboxDirectory();
  50. createJar(sandbox,"classes.jar",true);
  51. inTestCase.setShouldEmptySandbox(false);
  52. setFiles(aspectsFiles);
  53. String options = getOptions();
  54. if (options == null) {
  55. setOptions("");
  56. }
  57. setClasspath("classes.jar");
  58. args = buildArgs();
  59. result = inTestCase.ajc(base,args);
  60. inTestCase.assertNoMessages(result,failMessage);
  61. createJar(sandbox,"aspects.jar",false);
  62. args = buildWeaveArgs();
  63. inTestCase.setShouldEmptySandbox(false);
  64. result = inTestCase.ajc(base,args);
  65. AjcTestCase.MessageSpec messageSpec = buildMessageSpec();
  66. inTestCase.assertMessages(result,failMessage,messageSpec);
  67. inTestCase.setShouldEmptySandbox(false); // so subsequent steps in same test see my results
  68. } catch (IOException e) {
  69. AjcTestCase.fail(failMessage + " " + e);
  70. }
  71. }
  72. public void setClassesFiles(String files) {
  73. this.classesFiles = files;
  74. }
  75. public void setAspectsFiles(String files) {
  76. this.aspectsFiles = files;
  77. }
  78. /**
  79. * Find all the .class files under the dir, package them into a jar file,
  80. * and then delete them.
  81. * @param inDir
  82. * @param name
  83. */
  84. private void createJar(File inDir, String name, boolean isClasses) throws IOException {
  85. File outJar = new File(inDir,name);
  86. FileOutputStream fos = new FileOutputStream(outJar);
  87. JarOutputStream jarOut = new JarOutputStream(fos);
  88. List classFiles = new ArrayList();
  89. List toExclude = isClasses ? Collections.EMPTY_LIST : classFilesFromClasses;
  90. collectClassFiles(inDir,classFiles,toExclude);
  91. if (isClasses) classFilesFromClasses = classFiles;
  92. String prefix = inDir.getPath() + File.separator;
  93. for (Iterator iter = classFiles.iterator(); iter.hasNext();) {
  94. File f = (File) iter.next();
  95. String thisPath = f.getPath();
  96. if (thisPath.startsWith(prefix)) {
  97. thisPath = thisPath.substring(prefix.length());
  98. }
  99. JarEntry entry = new JarEntry(thisPath);
  100. jarOut.putNextEntry(entry);
  101. copyFile(f,jarOut);
  102. jarOut.closeEntry();
  103. }
  104. jarOut.flush();
  105. jarOut.close();
  106. }
  107. private void collectClassFiles(File inDir, List inList, List toExclude) {
  108. File[] contents = inDir.listFiles();
  109. for (int i = 0; i < contents.length; i++) {
  110. if (contents[i].getName().endsWith(".class")) {
  111. if (!toExclude.contains(contents[i])) {
  112. inList.add(contents[i]);
  113. }
  114. } else if (contents[i].isDirectory()) {
  115. collectClassFiles(contents[i],inList, toExclude);
  116. }
  117. }
  118. }
  119. private void copyFile(File f, OutputStream dest) throws IOException {
  120. FileInputStream fis = new FileInputStream(f);
  121. byte[] buf = new byte[4096];
  122. int read = -1;
  123. while((read = fis.read(buf)) != -1) {
  124. dest.write(buf,0,read);
  125. }
  126. }
  127. private String[] buildWeaveArgs() {
  128. StringBuffer args = new StringBuffer();
  129. if (getOptions() != null) {
  130. StringTokenizer strTok = new StringTokenizer(getOptions(),",");
  131. while (strTok.hasMoreTokens()) {
  132. args.append(strTok.nextToken());
  133. args.append(" ");
  134. }
  135. }
  136. args.append("-inpath ");
  137. args.append("classes.jar");
  138. args.append(File.pathSeparator);
  139. args.append("aspects.jar");
  140. args.append(" ");
  141. args.append("-aspectpath ");
  142. args.append("aspects.jar");
  143. String argumentString = args.toString();
  144. StringTokenizer strTok = new StringTokenizer(argumentString," ");
  145. String[] ret = new String[strTok.countTokens()];
  146. for (int i = 0; i < ret.length; i++) {
  147. ret[i] = strTok.nextToken();
  148. }
  149. return ret;
  150. }
  151. }