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
9 * Adrian Colyer initial implementation
10 * Helen Hawkins Converted to new interface (bug 148190)
11 *******************************************************************/
12 package org.aspectj.systemtest.incremental.tools;
14 import java.io.BufferedReader;
15 import java.io.ByteArrayOutputStream;
16 import java.io.DataOutputStream;
18 import java.io.FileOutputStream;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.io.PrintStream;
22 import java.io.PrintWriter;
24 import java.net.URLClassLoader;
25 import java.util.ArrayList;
26 import java.util.List;
29 import org.aspectj.ajdt.internal.core.builder.AjState;
30 import org.aspectj.asm.IProgramElement;
31 import org.aspectj.asm.IRelationship;
32 import org.aspectj.asm.IRelationshipMap;
33 import org.aspectj.testing.util.FileUtil;
35 public class AbstractMultiProjectIncrementalAjdeInteractionTestbed extends AjdeInteractionTestbed {
37 public static void dumptree(IProgramElement node, int indent) {
38 for (int i = 0; i < indent; i++) {
39 System.out.print(" ");
43 if (node.getSourceLocation() != null) {
44 loc = Integer.toString(node.getSourceLocation().getLine());
47 // System.out.println(node + " [" + (node == null ? "null" : node.getKind().toString()) + "] " + loc);
48 System.out.println(node + " [" + (node == null ? "null" : node.getKind().toString()) + "] " + loc
49 + (node == null ? "" : " hid:" + node.getHandleIdentifier()));
51 // for (int i = 0; i < indent; i++)
52 // System.out.print(" ");
53 // System.out.println(" hid is " + node.getHandleIdentifier());
54 // Map m = ((ProgramElement) node).kvpairs;
56 // Set keys = m.keySet();
57 // for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
58 // Object object = (Object) iterator.next();
60 // for (int i = 0; i < indent; i++)
61 // System.out.print(" ");
62 // System.out.println("kvp: " + object + " = " + m.get(object));
65 for (IProgramElement iProgramElement : node.getChildren()) {
66 dumptree(iProgramElement, indent + 2);
71 protected void setUp() throws Exception {
73 AjState.FORCE_INCREMENTAL_DURING_TESTING = true;
76 protected void tearDown() throws Exception {
78 AjState.FORCE_INCREMENTAL_DURING_TESTING = false;
81 protected String runMethod(String projectName, String classname, String methodname) throws Exception {
82 File f = getProjectOutputRelativePath(projectName, "");
83 ClassLoader cl = new URLClassLoader(new URL[] { f.toURI().toURL() });
84 Class<?> clazz = Class.forName(classname, false, cl);
85 ByteArrayOutputStream baos = new ByteArrayOutputStream();
86 PrintStream realOut = System.out;
88 System.setOut(new PrintStream(baos));
89 clazz.getDeclaredMethod(methodname).invoke(null);
91 System.setOut(realOut);
93 return new String(baos.toByteArray());
96 protected File getProjectOutputRelativePath(String p, String filename) {
97 File projDir = new File(getWorkingDir(), p);
98 return new File(projDir, "bin" + File.separator + filename);
101 public void build(String projectName) {
102 constructUpToDateLstFile(projectName, "build.lst");
103 doBuild(projectName);
104 if (AjdeInteractionTestbed.VERBOSE) {
105 printBuildReport(projectName);
109 public int getRelationshipCount(String project) {
110 IRelationshipMap relmap = getModelFor(project).getRelationshipMap();
112 Set<String> entries = relmap.getEntries();
113 for (String hid : entries) {
114 List<IRelationship> rels = relmap.get(hid);
115 for (IRelationship rel : rels) {
116 ctr += rel.getTargets().size();
122 public void fullBuild(String projectName) {
123 constructUpToDateLstFile(projectName, "build.lst");
124 doFullBuild(projectName);
125 if (AjdeInteractionTestbed.VERBOSE) {
126 printBuildReport(projectName);
130 private void constructUpToDateLstFile(String pname, String configname) {
131 File projectBase = new File(sandboxDir, pname);
132 File toConstruct = new File(projectBase, configname);
133 List<String> filesForCompilation = new ArrayList<>();
134 collectUpFiles(projectBase, projectBase, filesForCompilation);
137 FileOutputStream fos = new FileOutputStream(toConstruct);
138 DataOutputStream dos = new DataOutputStream(fos);
139 for (String file: filesForCompilation) {
140 dos.writeBytes(file + "\n");
143 } catch (IOException ioe) {
144 ioe.printStackTrace();
148 private void collectUpFiles(File location, File base, List<String> collectionPoint) {
149 String contents[] = location.list();
150 if (contents == null) {
153 for (String string : contents) {
154 File f = new File(location, string);
155 if (f.isDirectory()) {
156 collectUpFiles(f, base, collectionPoint);
157 } else if (f.isFile() && (f.getName().endsWith(".aj") || f.getName().endsWith(".java"))) {
160 fileFound = f.getCanonicalPath();
161 String toRemove = base.getCanonicalPath();
162 if (!fileFound.startsWith(toRemove)) {
163 throw new RuntimeException("eh? " + fileFound + " " + toRemove);
165 collectionPoint.add(fileFound.substring(toRemove.length() + 1));// +1 captures extra separator
166 } catch (IOException e) {
174 * Fill in the working directory with the project base files, from the 'base' folder.
176 protected void initialiseProject(String p) {
177 File projectSrc = new File(testdataSrcDir + File.separatorChar + p + File.separatorChar + "base");
178 File destination = new File(getWorkingDir(), p);
179 if (!destination.exists()) {
182 copy(projectSrc, destination);// ,false);
183 // create the AjCompiler instance associated with this project
184 // (has id of the form c:\temp\ajcSandbox\<workspace_name>\<project_name>)
185 CompilerFactory.getCompilerForProjectWithDir(sandboxDir + File.separator + p);
189 * Applies an overlay onto the project being tested - copying the contents of the specified overlay directory.
191 public void alter(String projectName, String overlayDirectory) {
192 File projectSrc = new File(testdataSrcDir + File.separatorChar + projectName + File.separatorChar + overlayDirectory);
193 File destination = new File(getWorkingDir(), projectName);
195 if (AjdeInteractionTestbed.VERBOSE) {
196 System.out.println("Altering project " + projectName);
198 copy(projectSrc, destination);
202 * Copy the contents of some directory to another location - the copy is recursive.
204 protected void copy(File from, File to) {
205 String contents[] = from.list();
206 if (contents == null) {
209 for (String string : contents) {
210 File f = new File(from, string);
211 File t = new File(to, string);
213 if (f.isDirectory() && !f.getName().startsWith("inc")) {
216 } else if (f.isFile()) {
217 StringBuffer sb = new StringBuffer();
218 // if (VERBOSE) System.err.println("Copying "+f+" to "+t);
219 FileUtil.copyFile(f, t, sb);
220 if (sb.length() != 0) {
221 System.err.println(sb.toString());
228 * Count the number of times a specified aspectName appears in the default aop.xml file and compare with the expected number of
229 * occurrences. If just want to count the number of aspects mentioned within the file then pass "" for the aspectName,
230 * otherwise, specify the name of the aspect interested in.
232 protected void checkXMLAspectCount(String projectName, String aspectName, int expectedOccurrences, String outputDir) {
234 File aopXML = new File(outputDir + File.separatorChar + "META-INF" + File.separatorChar + "aop-ajc.xml");
236 if (!aopXML.exists()) {
237 fail("Expected file " + aopXML.getAbsolutePath() + " to exist but it doesn't");
240 BufferedReader reader = new BufferedReader(new FileReader(aopXML));
241 String line = reader.readLine();
242 while (line != null) {
243 if (aspectName.equals("") && line.contains("aspect name=\"")) {
245 } else if (line.contains("aspect name=\"" + aspectName + "\"")) {
248 line = reader.readLine();
251 } catch (IOException ie) {
252 ie.printStackTrace();
254 if (aspectCount != expectedOccurrences) {
255 fail("Expected aspect " + aspectName + " to appear " + expectedOccurrences + " times"
256 + " in the aop.xml file but found " + aspectCount + " occurrences");
260 protected void assertContains(String expectedSubstring, Object object) {
261 String actualString = object.toString();
262 if (!actualString.contains(expectedSubstring)) {
263 fail("Expected to find '" + expectedSubstring + "' in '" + actualString + "'");
267 /** @return the number of relationship pairs */
268 protected void printModel(String projectName) throws Exception {
269 dumptree(getModelFor(projectName).getHierarchy().getRoot(), 0);
270 PrintWriter pw = new PrintWriter(System.out);
271 getModelFor(projectName).dumprels(pw);
275 protected File getProjectRelativePath(String p, String filename) {
276 File projDir = new File(getWorkingDir(), p);
277 return new File(projDir, filename);
280 protected void assertNoErrors(String projectName) {
281 assertTrue("Should be no errors, but got " + getErrorMessages(projectName), getErrorMessages(projectName).size() == 0);