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.DataOutputStream;
17 import java.io.FileOutputStream;
18 import java.io.FileReader;
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
26 import org.aspectj.ajdt.internal.core.builder.AjState;
27 import org.aspectj.asm.IProgramElement;
28 import org.aspectj.asm.IRelationship;
29 import org.aspectj.asm.IRelationshipMap;
30 import org.aspectj.testing.util.FileUtil;
32 public class AbstractMultiProjectIncrementalAjdeInteractionTestbed extends AjdeInteractionTestbed {
34 public static boolean VERBOSE = false;
36 public static void dumptree(IProgramElement node, int indent) {
37 for (int i = 0; i < indent; i++) {
38 System.out.print(" ");
42 if (node.getSourceLocation() != null) {
43 loc = Integer.toString(node.getSourceLocation().getLine());
46 // System.out.println(node + " [" + (node == null ? "null" : node.getKind().toString()) + "] " + loc);
47 System.out.println(node + " [" + (node == null ? "null" : node.getKind().toString()) + "] " + loc
48 + (node == null ? "" : " hid:" + node.getHandleIdentifier()));
50 // for (int i = 0; i < indent; i++)
51 // System.out.print(" ");
52 // System.out.println(" hid is " + node.getHandleIdentifier());
53 // Map m = ((ProgramElement) node).kvpairs;
55 // Set keys = m.keySet();
56 // for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
57 // Object object = (Object) iterator.next();
59 // for (int i = 0; i < indent; i++)
60 // System.out.print(" ");
61 // System.out.println("kvp: " + object + " = " + m.get(object));
64 for (Iterator i = node.getChildren().iterator(); i.hasNext();) {
65 dumptree((IProgramElement) i.next(), indent + 2);
70 protected void setUp() throws Exception {
72 AjdeInteractionTestbed.VERBOSE = VERBOSE;
73 AjState.FORCE_INCREMENTAL_DURING_TESTING = true;
76 protected void tearDown() throws Exception {
78 AjState.FORCE_INCREMENTAL_DURING_TESTING = false;
81 public void build(String projectName) {
82 constructUpToDateLstFile(projectName, "build.lst");
84 if (AjdeInteractionTestbed.VERBOSE)
85 printBuildReport(projectName);
88 public int getRelationshipCount(String project) {
89 IRelationshipMap relmap = getModelFor(project).getRelationshipMap();
91 Set entries = relmap.getEntries();
92 for (Iterator iter = entries.iterator(); iter.hasNext();) {
93 String hid = (String) iter.next();
94 List rels = relmap.get(hid);
95 for (Iterator iterator = rels.iterator(); iterator.hasNext();) {
96 IRelationship ir = (IRelationship) iterator.next();
97 List targets = ir.getTargets();
98 for (Iterator iterator2 = targets.iterator(); iterator2.hasNext();) {
99 String thid = (String) iterator2.next();
107 public void fullBuild(String projectName) {
108 constructUpToDateLstFile(projectName, "build.lst");
109 doFullBuild(projectName);
110 if (AjdeInteractionTestbed.VERBOSE)
111 printBuildReport(projectName);
114 private void constructUpToDateLstFile(String pname, String configname) {
115 File projectBase = new File(sandboxDir, pname);
116 File toConstruct = new File(projectBase, configname);
117 List filesForCompilation = new ArrayList();
118 collectUpFiles(projectBase, projectBase, filesForCompilation);
121 FileOutputStream fos = new FileOutputStream(toConstruct);
122 DataOutputStream dos = new DataOutputStream(fos);
123 for (Iterator iter = filesForCompilation.iterator(); iter.hasNext();) {
124 String file = (String) iter.next();
125 dos.writeBytes(file + "\n");
128 } catch (IOException ioe) {
129 ioe.printStackTrace();
133 private void collectUpFiles(File location, File base, List collectionPoint) {
134 String contents[] = location.list();
135 if (contents == null)
137 for (int i = 0; i < contents.length; i++) {
138 String string = contents[i];
139 File f = new File(location, string);
140 if (f.isDirectory()) {
141 collectUpFiles(f, base, collectionPoint);
142 } else if (f.isFile() && (f.getName().endsWith(".aj") || f.getName().endsWith(".java"))) {
145 fileFound = f.getCanonicalPath();
146 String toRemove = base.getCanonicalPath();
147 if (!fileFound.startsWith(toRemove))
148 throw new RuntimeException("eh? " + fileFound + " " + toRemove);
149 collectionPoint.add(fileFound.substring(toRemove.length() + 1));// +1 captures extra separator
150 } catch (IOException e) {
158 * Fill in the working directory with the project base files, from the 'base' folder.
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()) {
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);
173 * Applies an overlay onto the project being tested - copying the contents of the specified overlay directory.
175 public void alter(String projectName, String overlayDirectory) {
176 File projectSrc = new File(testdataSrcDir + File.separatorChar + projectName + File.separatorChar + overlayDirectory);
177 File destination = new File(getWorkingDir(), projectName);
179 if (AjdeInteractionTestbed.VERBOSE)
180 System.out.println("Altering project " + projectName);
181 copy(projectSrc, destination);
185 * Copy the contents of some directory to another location - the copy is recursive.
187 protected void copy(File from, File to) {
188 String contents[] = from.list();
189 if (contents == null)
191 for (int i = 0; i < contents.length; i++) {
192 String string = contents[i];
193 File f = new File(from, string);
194 File t = new File(to, string);
196 if (f.isDirectory() && !f.getName().startsWith("inc")) {
199 } else if (f.isFile()) {
200 StringBuffer sb = new StringBuffer();
201 // if (VERBOSE) System.err.println("Copying "+f+" to "+t);
202 FileUtil.copyFile(f, t, sb);
203 if (sb.length() != 0) {
204 System.err.println(sb.toString());
211 * Count the number of times a specified aspectName appears in the default aop.xml file and compare with the expected number of
212 * occurrences. If just want to count the number of aspects mentioned within the file then pass "" for the aspectName,
213 * otherwise, specify the name of the aspect interested in.
215 protected void checkXMLAspectCount(String projectName, String aspectName, int expectedOccurrences, String outputDir) {
217 File aopXML = new File(outputDir + File.separatorChar + "META-INF" + File.separatorChar + "aop-ajc.xml");
219 if (!aopXML.exists()) {
220 fail("Expected file " + aopXML.getAbsolutePath() + " to exist but it doesn't");
223 BufferedReader reader = new BufferedReader(new FileReader(aopXML));
224 String line = reader.readLine();
225 while (line != null) {
226 if (aspectName.equals("") && line.indexOf("aspect name=\"") != -1) {
228 } else if (line.indexOf("aspect name=\"" + aspectName + "\"") != -1) {
231 line = reader.readLine();
234 } catch (IOException ie) {
235 ie.printStackTrace();
237 if (aspectCount != expectedOccurrences) {
238 fail("Expected aspect " + aspectName + " to appear " + expectedOccurrences + " times"
239 + " in the aop.xml file but found " + aspectCount + " occurrences");
243 protected void assertContains(String expectedSubstring, Object object) {
244 String actualString = object.toString();
245 if (actualString.indexOf(expectedSubstring) == -1) {
246 fail("Expected to find '" + expectedSubstring + "' in '" + actualString + "'");
250 /** @return the number of relationship pairs */
251 protected void printModel(String projectName) throws Exception {
252 dumptree(getModelFor(projectName).getHierarchy().getRoot(), 0);
253 PrintWriter pw = new PrintWriter(System.out);
254 getModelFor(projectName).dumprels(pw);
258 protected File getProjectRelativePath(String p, String filename) {
259 File projDir = new File(getWorkingDir(), p);
260 return new File(projDir, filename);
263 protected void assertNoErrors(String projectName) {
264 assertTrue("Should be no errors, but got " + getErrorMessages(projectName), getErrorMessages(projectName).size() == 0);