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.

AbstractMultiProjectIncrementalAjdeInteractionTestbed.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /********************************************************************
  2. * Copyright (c) 2006 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution and is available at
  6. * http://eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Adrian Colyer initial implementation
  10. * Helen Hawkins Converted to new interface (bug 148190)
  11. *******************************************************************/
  12. package org.aspectj.systemtest.incremental.tools;
  13. import java.io.BufferedReader;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.DataOutputStream;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.FileReader;
  19. import java.io.IOException;
  20. import java.io.PrintStream;
  21. import java.io.PrintWriter;
  22. import java.net.URL;
  23. import java.net.URLClassLoader;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Set;
  28. import org.aspectj.ajdt.internal.core.builder.AjState;
  29. import org.aspectj.asm.IProgramElement;
  30. import org.aspectj.asm.IRelationship;
  31. import org.aspectj.asm.IRelationshipMap;
  32. import org.aspectj.testing.util.FileUtil;
  33. public class AbstractMultiProjectIncrementalAjdeInteractionTestbed extends AjdeInteractionTestbed {
  34. public static boolean VERBOSE = false;
  35. public static void dumptree(IProgramElement node, int indent) {
  36. for (int i = 0; i < indent; i++) {
  37. System.out.print(" ");
  38. }
  39. String loc = "";
  40. if (node != null) {
  41. if (node.getSourceLocation() != null) {
  42. loc = Integer.toString(node.getSourceLocation().getLine());
  43. }
  44. }
  45. // System.out.println(node + " [" + (node == null ? "null" : node.getKind().toString()) + "] " + loc);
  46. System.out.println(node + " [" + (node == null ? "null" : node.getKind().toString()) + "] " + loc
  47. + (node == null ? "" : " hid:" + node.getHandleIdentifier()));
  48. if (node != null) {
  49. // for (int i = 0; i < indent; i++)
  50. // System.out.print(" ");
  51. // System.out.println(" hid is " + node.getHandleIdentifier());
  52. // Map m = ((ProgramElement) node).kvpairs;
  53. // if (m != null) {
  54. // Set keys = m.keySet();
  55. // for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
  56. // Object object = (Object) iterator.next();
  57. //
  58. // for (int i = 0; i < indent; i++)
  59. // System.out.print(" ");
  60. // System.out.println("kvp: " + object + " = " + m.get(object));
  61. // }
  62. // }
  63. for (IProgramElement iProgramElement : node.getChildren()) {
  64. dumptree(iProgramElement, indent + 2);
  65. }
  66. }
  67. }
  68. protected void setUp() throws Exception {
  69. super.setUp();
  70. AjdeInteractionTestbed.VERBOSE = VERBOSE;
  71. AjState.FORCE_INCREMENTAL_DURING_TESTING = true;
  72. }
  73. protected void tearDown() throws Exception {
  74. super.tearDown();
  75. AjState.FORCE_INCREMENTAL_DURING_TESTING = false;
  76. }
  77. protected String runMethod(String projectName, String classname, String methodname) throws Exception {
  78. File f = getProjectOutputRelativePath(projectName, "");
  79. ClassLoader cl = new URLClassLoader(new URL[] { f.toURI().toURL() });
  80. Class<?> clazz = Class.forName(classname, false, cl);
  81. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  82. PrintStream realOut = System.out;
  83. try {
  84. System.setOut(new PrintStream(baos));
  85. clazz.getDeclaredMethod(methodname).invoke(null);
  86. } finally {
  87. System.setOut(realOut);
  88. }
  89. return new String(baos.toByteArray());
  90. }
  91. protected File getProjectOutputRelativePath(String p, String filename) {
  92. File projDir = new File(getWorkingDir(), p);
  93. return new File(projDir, "bin" + File.separator + filename);
  94. }
  95. public void build(String projectName) {
  96. constructUpToDateLstFile(projectName, "build.lst");
  97. doBuild(projectName);
  98. if (AjdeInteractionTestbed.VERBOSE)
  99. printBuildReport(projectName);
  100. }
  101. public int getRelationshipCount(String project) {
  102. IRelationshipMap relmap = getModelFor(project).getRelationshipMap();
  103. int ctr = 0;
  104. Set<String> entries = relmap.getEntries();
  105. for (String hid : entries) {
  106. List<IRelationship> rels = relmap.get(hid);
  107. for (IRelationship rel : rels) {
  108. ctr += rel.getTargets().size();
  109. }
  110. }
  111. return ctr;
  112. }
  113. public void fullBuild(String projectName) {
  114. constructUpToDateLstFile(projectName, "build.lst");
  115. doFullBuild(projectName);
  116. if (AjdeInteractionTestbed.VERBOSE)
  117. printBuildReport(projectName);
  118. }
  119. private void constructUpToDateLstFile(String pname, String configname) {
  120. File projectBase = new File(sandboxDir, pname);
  121. File toConstruct = new File(projectBase, configname);
  122. List<String> filesForCompilation = new ArrayList<String>();
  123. collectUpFiles(projectBase, projectBase, filesForCompilation);
  124. try {
  125. FileOutputStream fos = new FileOutputStream(toConstruct);
  126. DataOutputStream dos = new DataOutputStream(fos);
  127. for (String file: filesForCompilation) {
  128. dos.writeBytes(file + "\n");
  129. }
  130. dos.close();
  131. } catch (IOException ioe) {
  132. ioe.printStackTrace();
  133. }
  134. }
  135. private void collectUpFiles(File location, File base, List<String> collectionPoint) {
  136. String contents[] = location.list();
  137. if (contents == null)
  138. return;
  139. for (String string : contents) {
  140. File f = new File(location, string);
  141. if (f.isDirectory()) {
  142. collectUpFiles(f, base, collectionPoint);
  143. } else if (f.isFile() && (f.getName().endsWith(".aj") || f.getName().endsWith(".java"))) {
  144. String fileFound;
  145. try {
  146. fileFound = f.getCanonicalPath();
  147. String toRemove = base.getCanonicalPath();
  148. if (!fileFound.startsWith(toRemove))
  149. throw new RuntimeException("eh? " + fileFound + " " + toRemove);
  150. collectionPoint.add(fileFound.substring(toRemove.length() + 1));// +1 captures extra separator
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * Fill in the working directory with the project base files, from the 'base' folder.
  159. */
  160. protected void initialiseProject(String p) {
  161. File projectSrc = new File(testdataSrcDir + File.separatorChar + p + File.separatorChar + "base");
  162. File destination = new File(getWorkingDir(), p);
  163. if (!destination.exists()) {
  164. destination.mkdir();
  165. }
  166. copy(projectSrc, destination);// ,false);
  167. // create the AjCompiler instance associated with this project
  168. // (has id of the form c:\temp\ajcSandbox\<workspace_name>\<project_name>)
  169. CompilerFactory.getCompilerForProjectWithDir(sandboxDir + File.separator + p);
  170. }
  171. /**
  172. * Applies an overlay onto the project being tested - copying the contents of the specified overlay directory.
  173. */
  174. public void alter(String projectName, String overlayDirectory) {
  175. File projectSrc = new File(testdataSrcDir + File.separatorChar + projectName + File.separatorChar + overlayDirectory);
  176. File destination = new File(getWorkingDir(), projectName);
  177. if (AjdeInteractionTestbed.VERBOSE)
  178. System.out.println("Altering project " + projectName);
  179. copy(projectSrc, destination);
  180. }
  181. /**
  182. * Copy the contents of some directory to another location - the copy is recursive.
  183. */
  184. protected void copy(File from, File to) {
  185. String contents[] = from.list();
  186. if (contents == null)
  187. return;
  188. for (String string : contents) {
  189. File f = new File(from, string);
  190. File t = new File(to, string);
  191. if (f.isDirectory() && !f.getName().startsWith("inc")) {
  192. t.mkdir();
  193. copy(f, t);
  194. } else if (f.isFile()) {
  195. StringBuffer sb = new StringBuffer();
  196. // if (VERBOSE) System.err.println("Copying "+f+" to "+t);
  197. FileUtil.copyFile(f, t, sb);
  198. if (sb.length() != 0) {
  199. System.err.println(sb.toString());
  200. }
  201. }
  202. }
  203. }
  204. /**
  205. * Count the number of times a specified aspectName appears in the default aop.xml file and compare with the expected number of
  206. * occurrences. If just want to count the number of aspects mentioned within the file then pass "" for the aspectName,
  207. * otherwise, specify the name of the aspect interested in.
  208. */
  209. protected void checkXMLAspectCount(String projectName, String aspectName, int expectedOccurrences, String outputDir) {
  210. int aspectCount = 0;
  211. File aopXML = new File(outputDir + File.separatorChar + "META-INF" + File.separatorChar + "aop-ajc.xml");
  212. if (!aopXML.exists()) {
  213. fail("Expected file " + aopXML.getAbsolutePath() + " to exist but it doesn't");
  214. }
  215. try {
  216. BufferedReader reader = new BufferedReader(new FileReader(aopXML));
  217. String line = reader.readLine();
  218. while (line != null) {
  219. if (aspectName.equals("") && line.contains("aspect name=\"")) {
  220. aspectCount++;
  221. } else if (line.contains("aspect name=\"" + aspectName + "\"")) {
  222. aspectCount++;
  223. }
  224. line = reader.readLine();
  225. }
  226. reader.close();
  227. } catch (IOException ie) {
  228. ie.printStackTrace();
  229. }
  230. if (aspectCount != expectedOccurrences) {
  231. fail("Expected aspect " + aspectName + " to appear " + expectedOccurrences + " times"
  232. + " in the aop.xml file but found " + aspectCount + " occurrences");
  233. }
  234. }
  235. protected void assertContains(String expectedSubstring, Object object) {
  236. String actualString = object.toString();
  237. if (!actualString.contains(expectedSubstring)) {
  238. fail("Expected to find '" + expectedSubstring + "' in '" + actualString + "'");
  239. }
  240. }
  241. /** @return the number of relationship pairs */
  242. protected void printModel(String projectName) throws Exception {
  243. dumptree(getModelFor(projectName).getHierarchy().getRoot(), 0);
  244. PrintWriter pw = new PrintWriter(System.out);
  245. getModelFor(projectName).dumprels(pw);
  246. pw.flush();
  247. }
  248. protected File getProjectRelativePath(String p, String filename) {
  249. File projDir = new File(getWorkingDir(), p);
  250. return new File(projDir, filename);
  251. }
  252. protected void assertNoErrors(String projectName) {
  253. assertTrue("Should be no errors, but got " + getErrorMessages(projectName), getErrorMessages(projectName).size() == 0);
  254. }
  255. }