1 package org.aspectj.systemtest.incremental.tools;
3 import java.io.BufferedReader;
4 import java.io.DataOutputStream;
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;
13 import org.aspectj.ajdt.internal.core.builder.AjState;
14 import org.aspectj.bridge.IMessage;
15 import org.aspectj.testing.util.FileUtil;
17 public class AbstractMultiProjectIncrementalAjdeInteractionTestbed extends
18 AjdeInteractionTestbed {
20 public static boolean VERBOSE = false;
22 protected void setUp() throws Exception {
24 AjdeInteractionTestbed.VERBOSE = VERBOSE;
25 AjState.FORCE_INCREMENTAL_DURING_TESTING = true;
28 protected void tearDown() throws Exception {
30 AjState.FORCE_INCREMENTAL_DURING_TESTING = false;
31 configureBuildStructureModel(false);
32 MyBuildOptionsAdapter.reset();
35 public void build(String projectName) {
36 constructUpToDateLstFile(projectName,"build.lst");
37 build(projectName,"build.lst");
38 if (AjdeInteractionTestbed.VERBOSE) printBuildReport();
41 public void fullBuild(String projectName) {
42 constructUpToDateLstFile(projectName,"build.lst");
43 fullBuild(projectName,"build.lst");
44 if (AjdeInteractionTestbed.VERBOSE) printBuildReport();
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);
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");
61 } catch (IOException ioe) {
62 ioe.printStackTrace();
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;
72 fail("Didn't find the error message:\n'"+anError+"'.\nErrors that occurred:\n"+MyTaskListManager.getErrorMessages());
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"))) {
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) {
98 * Fill in the working directory with the project base files,
99 * from the 'base' folder.
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);
109 * Applies an overlay onto the project being tested - copying
110 * the contents of the specified overlay directory.
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);
120 * Copy the contents of some directory to another location - the
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);
131 if (f.isDirectory() && !f.getName().startsWith("inc")) {
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());}
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.
150 protected void checkXMLAspectCount(String projectName, String aspectName,
151 int expectedOccurrences, String outputDir) {
153 File aopXML = new File(outputDir + File.separatorChar + "META-INF" + File.separatorChar + "aop-ajc.xml");
155 if (!aopXML.exists()) {
156 fail("Expected file " + aopXML.getAbsolutePath() + " to exist but it doesn't");
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) {
164 } else if (line.indexOf("aspect name=\""+aspectName+"\"") != -1) {
167 line = reader.readLine();
170 } catch (IOException ie) {
171 ie.printStackTrace();
173 if (aspectCount != expectedOccurrences) {
174 fail("Expected aspect " + aspectName + " to appear " + expectedOccurrences + " times" +
175 " in the aop.xml file but found " + aspectCount + " occurrences");