]> source.dussan.org Git - aspectj.git/blob
0c864c850127b36b089c366c82f6c35d0f295f66
[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.ByteArrayOutputStream;
16 import java.io.DataOutputStream;
17 import java.io.File;
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;
23 import java.net.URL;
24 import java.net.URLClassLoader;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Set;
28
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;
34
35 public class AbstractMultiProjectIncrementalAjdeInteractionTestbed extends AjdeInteractionTestbed {
36
37         public static void dumptree(IProgramElement node, int indent) {
38                 for (int i = 0; i < indent; i++) {
39                         System.out.print(" ");
40                 }
41                 String loc = "";
42                 if (node != null) {
43                         if (node.getSourceLocation() != null) {
44                                 loc = Integer.toString(node.getSourceLocation().getLine());
45                         }
46                 }
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()));
50                 if (node != null) {
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;
55                         // if (m != null) {
56                         // Set keys = m.keySet();
57                         // for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
58                         // Object object = (Object) iterator.next();
59                         //
60                         // for (int i = 0; i < indent; i++)
61                         // System.out.print(" ");
62                         // System.out.println("kvp: " + object + " = " + m.get(object));
63                         // }
64                         // }
65                         for (IProgramElement iProgramElement : node.getChildren()) {
66                                 dumptree(iProgramElement, indent + 2);
67                         }
68                 }
69         }
70
71         protected void setUp() throws Exception {
72                 super.setUp();
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         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;
87                 try {
88                         System.setOut(new PrintStream(baos));
89                         clazz.getDeclaredMethod(methodname).invoke(null);
90                 } finally {
91                         System.setOut(realOut);
92                 }
93                 return new String(baos.toByteArray());
94         }
95
96         protected File getProjectOutputRelativePath(String p, String filename) {
97                 File projDir = new File(getWorkingDir(), p);
98                 return new File(projDir, "bin" + File.separator + filename);
99         }
100         
101         public void build(String projectName) {
102                 constructUpToDateLstFile(projectName, "build.lst");
103                 doBuild(projectName);
104                 if (AjdeInteractionTestbed.VERBOSE) {
105                         printBuildReport(projectName);
106                 }
107         }
108
109         public int getRelationshipCount(String project) {
110                 IRelationshipMap relmap = getModelFor(project).getRelationshipMap();
111                 int ctr = 0;
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();
117                         }
118                 }
119                 return ctr;
120         }
121
122         public void fullBuild(String projectName) {
123                 constructUpToDateLstFile(projectName, "build.lst");
124                 doFullBuild(projectName);
125                 if (AjdeInteractionTestbed.VERBOSE) {
126                         printBuildReport(projectName);
127                 }
128         }
129
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);
135
136                 try {
137                         FileOutputStream fos = new FileOutputStream(toConstruct);
138                         DataOutputStream dos = new DataOutputStream(fos);
139                         for (String file: filesForCompilation) {
140                                 dos.writeBytes(file + "\n");
141                         }
142                         dos.close();
143                 } catch (IOException ioe) {
144                         ioe.printStackTrace();
145                 }
146         }
147
148         private void collectUpFiles(File location, File base, List<String> collectionPoint) {
149                 String contents[] = location.list();
150                 if (contents == null) {
151                         return;
152                 }
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"))) {
158                                 String fileFound;
159                                 try {
160                                         fileFound = f.getCanonicalPath();
161                                         String toRemove = base.getCanonicalPath();
162                                         if (!fileFound.startsWith(toRemove)) {
163                                                 throw new RuntimeException("eh? " + fileFound + "   " + toRemove);
164                                         }
165                                         collectionPoint.add(fileFound.substring(toRemove.length() + 1));// +1 captures extra separator
166                                 } catch (IOException e) {
167                                         e.printStackTrace();
168                                 }
169                         }
170                 }
171         }
172
173         /**
174          * Fill in the working directory with the project base files, from the 'base' folder.
175          */
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()) {
180                         destination.mkdir();
181                 }
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);
186         }
187
188         /**
189          * Applies an overlay onto the project being tested - copying the contents of the specified overlay directory.
190          */
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);
194
195                 if (AjdeInteractionTestbed.VERBOSE) {
196                         System.out.println("Altering project " + projectName);
197                 }
198                 copy(projectSrc, destination);
199         }
200
201         /**
202          * Copy the contents of some directory to another location - the copy is recursive.
203          */
204         protected void copy(File from, File to) {
205                 String contents[] = from.list();
206                 if (contents == null) {
207                         return;
208                 }
209                 for (String string : contents) {
210                         File f = new File(from, string);
211                         File t = new File(to, string);
212
213                         if (f.isDirectory() && !f.getName().startsWith("inc")) {
214                                 t.mkdir();
215                                 copy(f, t);
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());
222                                 }
223                         }
224                 }
225         }
226
227         /**
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.
231          */
232         protected void checkXMLAspectCount(String projectName, String aspectName, int expectedOccurrences, String outputDir) {
233                 int aspectCount = 0;
234                 File aopXML = new File(outputDir + File.separatorChar + "META-INF" + File.separatorChar + "aop-ajc.xml");
235
236                 if (!aopXML.exists()) {
237                         fail("Expected file " + aopXML.getAbsolutePath() + " to exist but it doesn't");
238                 }
239                 try {
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=\"")) {
244                                         aspectCount++;
245                                 } else if (line.contains("aspect name=\"" + aspectName + "\"")) {
246                                         aspectCount++;
247                                 }
248                                 line = reader.readLine();
249                         }
250                         reader.close();
251                 } catch (IOException ie) {
252                         ie.printStackTrace();
253                 }
254                 if (aspectCount != expectedOccurrences) {
255                         fail("Expected aspect " + aspectName + " to appear " + expectedOccurrences + " times"
256                                         + " in the aop.xml file but found " + aspectCount + " occurrences");
257                 }
258         }
259
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 + "'");
264                 }
265         }
266
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);
272                 pw.flush();
273         }
274
275         protected File getProjectRelativePath(String p, String filename) {
276                 File projDir = new File(getWorkingDir(), p);
277                 return new File(projDir, filename);
278         }
279
280         protected void assertNoErrors(String projectName) {
281                 assertTrue("Should be no errors, but got " + getErrorMessages(projectName), getErrorMessages(projectName).size() == 0);
282         }
283 }