]> source.dussan.org Git - aspectj.git/blob
a87d563c6013a84e376bafe11aa700781f066bdb
[aspectj.git] /
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
14 import java.io.BufferedReader;
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.PrintWriter;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Set;
25
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;
31
32 public class AbstractMultiProjectIncrementalAjdeInteractionTestbed extends AjdeInteractionTestbed {
33
34         public static boolean VERBOSE = false;
35
36         public static void dumptree(IProgramElement node, int indent) {
37                 for (int i = 0; i < indent; i++) {
38                         System.out.print(" ");
39                 }
40                 String loc = "";
41                 if (node != null) {
42                         if (node.getSourceLocation() != null) {
43                                 loc = Integer.toString(node.getSourceLocation().getLine());
44                         }
45                 }
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()));
49                 if (node != null) {
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;
54                         // if (m != null) {
55                         // Set keys = m.keySet();
56                         // for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
57                         // Object object = (Object) iterator.next();
58                         //
59                         // for (int i = 0; i < indent; i++)
60                         // System.out.print(" ");
61                         // System.out.println("kvp: " + object + " = " + m.get(object));
62                         // }
63                         // }
64                         for (Iterator i = node.getChildren().iterator(); i.hasNext();) {
65                                 dumptree((IProgramElement) i.next(), indent + 2);
66                         }
67                 }
68         }
69
70         protected void setUp() throws Exception {
71                 super.setUp();
72                 AjdeInteractionTestbed.VERBOSE = VERBOSE;
73                 AjState.FORCE_INCREMENTAL_DURING_TESTING = true;
74         }
75
76         protected void tearDown() throws Exception {
77                 super.tearDown();
78                 AjState.FORCE_INCREMENTAL_DURING_TESTING = false;
79         }
80
81         public void build(String projectName) {
82                 constructUpToDateLstFile(projectName, "build.lst");
83                 doBuild(projectName);
84                 if (AjdeInteractionTestbed.VERBOSE)
85                         printBuildReport(projectName);
86         }
87
88         public int getRelationshipCount(String project) {
89                 IRelationshipMap relmap = getModelFor(project).getRelationshipMap();
90                 int ctr = 0;
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();
100                                         ctr++;
101                                 }
102                         }
103                 }
104                 return ctr;
105         }
106
107         public void fullBuild(String projectName) {
108                 constructUpToDateLstFile(projectName, "build.lst");
109                 doFullBuild(projectName);
110                 if (AjdeInteractionTestbed.VERBOSE)
111                         printBuildReport(projectName);
112         }
113
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);
119
120                 try {
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");
126                         }
127                         dos.close();
128                 } catch (IOException ioe) {
129                         ioe.printStackTrace();
130                 }
131         }
132
133         private void collectUpFiles(File location, File base, List collectionPoint) {
134                 String contents[] = location.list();
135                 if (contents == null)
136                         return;
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"))) {
143                                 String fileFound;
144                                 try {
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) {
151                                         e.printStackTrace();
152                                 }
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         /**
173          * Applies an overlay onto the project being tested - copying the contents of the specified overlay directory.
174          */
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);
178
179                 if (AjdeInteractionTestbed.VERBOSE)
180                         System.out.println("Altering project " + projectName);
181                 copy(projectSrc, destination);
182         }
183
184         /**
185          * Copy the contents of some directory to another location - the copy is recursive.
186          */
187         protected void copy(File from, File to) {
188                 String contents[] = from.list();
189                 if (contents == null)
190                         return;
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);
195
196                         if (f.isDirectory() && !f.getName().startsWith("inc")) {
197                                 t.mkdir();
198                                 copy(f, t);
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());
205                                 }
206                         }
207                 }
208         }
209
210         /**
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.
214          */
215         protected void checkXMLAspectCount(String projectName, String aspectName, int expectedOccurrences, String outputDir) {
216                 int aspectCount = 0;
217                 File aopXML = new File(outputDir + File.separatorChar + "META-INF" + File.separatorChar + "aop-ajc.xml");
218
219                 if (!aopXML.exists()) {
220                         fail("Expected file " + aopXML.getAbsolutePath() + " to exist but it doesn't");
221                 }
222                 try {
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) {
227                                         aspectCount++;
228                                 } else if (line.indexOf("aspect name=\"" + aspectName + "\"") != -1) {
229                                         aspectCount++;
230                                 }
231                                 line = reader.readLine();
232                         }
233                         reader.close();
234                 } catch (IOException ie) {
235                         ie.printStackTrace();
236                 }
237                 if (aspectCount != expectedOccurrences) {
238                         fail("Expected aspect " + aspectName + " to appear " + expectedOccurrences + " times"
239                                         + " in the aop.xml file but found " + aspectCount + " occurrences");
240                 }
241         }
242
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 + "'");
247                 }
248         }
249
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);
255                 pw.flush();
256         }
257
258         protected File getProjectRelativePath(String p, String filename) {
259                 File projDir = new File(getWorkingDir(), p);
260                 return new File(projDir, filename);
261         }
262
263         protected void assertNoErrors(String projectName) {
264                 assertTrue("Should be no errors, but got " + getErrorMessages(projectName), getErrorMessages(projectName).size() == 0);
265         }
266 }