]> source.dussan.org Git - aspectj.git/blob
f1e7caf8a0b9506ade82fbbb516b7f0f2bb9ac45
[aspectj.git] /
1 package org.aspectj.systemtest.incremental.tools;
2
3 import java.io.BufferedReader;
4 import java.io.DataOutputStream;
5 import java.io.File;
6 import java.io.FileOutputStream;
7 import java.io.FileReader;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.Iterator;
11 import java.util.List;
12
13 import org.aspectj.ajdt.internal.core.builder.AjState;
14 import org.aspectj.bridge.IMessage;
15 import org.aspectj.testing.util.FileUtil;
16
17 public class AbstractMultiProjectIncrementalAjdeInteractionTestbed extends
18                 AjdeInteractionTestbed {
19
20         public static boolean VERBOSE = false;
21
22         protected void setUp() throws Exception {
23                 super.setUp();
24                 AjdeInteractionTestbed.VERBOSE = VERBOSE;
25                 AjState.FORCE_INCREMENTAL_DURING_TESTING = true;
26         }
27
28         protected void tearDown() throws Exception {
29                 super.tearDown();
30                 AjState.FORCE_INCREMENTAL_DURING_TESTING = false;
31                 configureBuildStructureModel(false);
32                 MyBuildOptionsAdapter.reset();
33         }
34
35         public void build(String projectName) {
36                 constructUpToDateLstFile(projectName,"build.lst");
37                 build(projectName,"build.lst");
38                 if (AjdeInteractionTestbed.VERBOSE) printBuildReport();
39         }
40
41         public void fullBuild(String projectName) {
42                 constructUpToDateLstFile(projectName,"build.lst");
43                 fullBuild(projectName,"build.lst");
44                 if (AjdeInteractionTestbed.VERBOSE) printBuildReport();
45         }
46
47         private void constructUpToDateLstFile(String pname, String configname) {
48                 File projectBase = new File(sandboxDir,pname);
49                 File toConstruct = new File(projectBase,configname);
50                 List filesForCompilation = new ArrayList();
51                 collectUpFiles(projectBase,projectBase,filesForCompilation);
52         
53                 try {
54                         FileOutputStream fos = new FileOutputStream(toConstruct);
55                         DataOutputStream dos = new DataOutputStream(fos);
56                         for (Iterator iter = filesForCompilation.iterator(); iter.hasNext();) {
57                                 String file = (String) iter.next();
58                                 dos.writeBytes(file+"\n");
59                         }
60                         dos.close();
61                 } catch (IOException ioe) {
62                         ioe.printStackTrace();
63                 }
64         }
65
66         public void checkForError(String anError) {
67                 List messages = MyTaskListManager.getErrorMessages();
68                 for (Iterator iter = messages.iterator(); iter.hasNext();) {
69                         IMessage element = (IMessage) iter.next();
70                         if (element.getMessage().indexOf(anError)!=-1) return;
71                 }
72                 fail("Didn't find the error message:\n'"+anError+"'.\nErrors that occurred:\n"+MyTaskListManager.getErrorMessages());
73         }
74
75         private void collectUpFiles(File location, File base, List collectionPoint) {
76                 String contents[] = location.list();
77                 if (contents==null) return;
78                 for (int i = 0; i < contents.length; i++) {
79                         String string = contents[i];
80                         File f = new File(location,string);
81                         if (f.isDirectory()) {
82                                 collectUpFiles(f,base,collectionPoint);
83                         } else if (f.isFile() && (f.getName().endsWith(".aj") || f.getName().endsWith(".java"))) {
84                                 String fileFound;
85                                 try {
86                                         fileFound = f.getCanonicalPath();
87                                         String toRemove  = base.getCanonicalPath();
88                                         if (!fileFound.startsWith(toRemove)) throw new RuntimeException("eh? "+fileFound+"   "+toRemove);
89                                         collectionPoint.add(fileFound.substring(toRemove.length()+1));//+1 captures extra separator
90                                 } catch (IOException e) {
91                                         e.printStackTrace();
92                                 }
93                         }
94                 }
95         }
96
97         /**
98          * Fill in the working directory with the project base files,
99          * from the 'base' folder.
100          */
101         protected void initialiseProject(String p) {
102                 File projectSrc=new File(testdataSrcDir+File.separatorChar+p+File.separatorChar+"base");
103                 File destination=new File(getWorkingDir(),p);
104                 if (!destination.exists()) {destination.mkdir();}
105                 copy(projectSrc,destination);//,false);
106         }
107
108         /*
109          * Applies an overlay onto the project being tested - copying
110          * the contents of the specified overlay directory.
111          */
112         public void alter(String projectName,String overlayDirectory) {
113                 File projectSrc =new File(testdataSrcDir+File.separatorChar+projectName+
114                                                   File.separatorChar+overlayDirectory);
115                 File destination=new File(getWorkingDir(),projectName);
116                 copy(projectSrc,destination);
117         }
118         
119         /**
120          * Copy the contents of some directory to another location - the
121          * copy is recursive.
122          */
123         protected void copy(File from, File to) {
124                 String contents[] = from.list();
125                 if (contents==null) return;
126                 for (int i = 0; i < contents.length; i++) {
127                         String string = contents[i];
128                         File f = new File(from,string);
129                         File t = new File(to,string);
130                         
131                         if (f.isDirectory() && !f.getName().startsWith("inc")) {
132                                 t.mkdir();
133                                 copy(f,t);
134                         } else if (f.isFile()) {
135                                 StringBuffer sb = new StringBuffer();
136                                 //if (VERBOSE) System.err.println("Copying "+f+" to "+t);
137                                 FileUtil.copyFile(f,t,sb);
138                                 if (sb.length()!=0) { System.err.println(sb.toString());}
139                         } 
140                 }
141         }
142
143         /**
144          * Count the number of times a specified aspectName appears in the default
145          * aop.xml file and compare with the expected number of occurrences. If just 
146          * want to count the number of aspects mentioned within the file then 
147          * pass "" for the aspectName, otherwise, specify the name of the 
148          * aspect interested in.
149          */
150         protected void checkXMLAspectCount(String projectName, String aspectName,
151                         int expectedOccurrences, String outputDir) {
152                                 int aspectCount = 0;
153                                 File aopXML = new File(outputDir + File.separatorChar + "META-INF" + File.separatorChar + "aop-ajc.xml");
154                         
155                                 if (!aopXML.exists()) {
156                                         fail("Expected file " + aopXML.getAbsolutePath() + " to exist but it doesn't");
157                                 }
158                                 try {
159                                         BufferedReader reader = new BufferedReader(new FileReader(aopXML));
160                                         String line = reader.readLine();
161                                         while (line != null) {
162                                                 if (aspectName.equals("") && line.indexOf("aspect name=\"") != -1) {
163                                                         aspectCount++;
164                                                 } else if (line.indexOf("aspect name=\""+aspectName+"\"") != -1) {
165                                                         aspectCount++;
166                                                 }
167                                                 line = reader.readLine();
168                                         }
169                                         reader.close();
170                                 } catch (IOException ie) {
171                                         ie.printStackTrace();
172                                 }
173                                 if (aspectCount != expectedOccurrences) {
174                                         fail("Expected aspect " + aspectName + " to appear " + expectedOccurrences + " times" +
175                                                         " in the aop.xml file but found " + aspectCount + " occurrences");
176                                 }
177                         }
178
179 }