Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

WeaveSpec.java 5.0KB

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