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.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
24 import org.aspectj.ajdt.internal.core.builder.AjState;
25 import org.aspectj.testing.util.FileUtil;
27 public class AbstractMultiProjectIncrementalAjdeInteractionTestbed extends
28 AjdeInteractionTestbed {
30 public static boolean VERBOSE = false;
32 protected void setUp() throws Exception {
34 AjdeInteractionTestbed.VERBOSE = VERBOSE;
35 AjState.FORCE_INCREMENTAL_DURING_TESTING = true;
38 protected void tearDown() throws Exception {
40 AjState.FORCE_INCREMENTAL_DURING_TESTING = false;
43 public void build(String projectName) {
44 constructUpToDateLstFile(projectName,"build.lst");
46 if (AjdeInteractionTestbed.VERBOSE) printBuildReport(projectName);
49 public void fullBuild(String projectName) {
50 constructUpToDateLstFile(projectName,"build.lst");
51 doFullBuild(projectName);
52 if (AjdeInteractionTestbed.VERBOSE) printBuildReport(projectName);
55 private void constructUpToDateLstFile(String pname, String configname) {
56 File projectBase = new File(sandboxDir,pname);
57 File toConstruct = new File(projectBase,configname);
58 List filesForCompilation = new ArrayList();
59 collectUpFiles(projectBase,projectBase,filesForCompilation);
62 FileOutputStream fos = new FileOutputStream(toConstruct);
63 DataOutputStream dos = new DataOutputStream(fos);
64 for (Iterator iter = filesForCompilation.iterator(); iter.hasNext();) {
65 String file = (String) iter.next();
66 dos.writeBytes(file+"\n");
69 } catch (IOException ioe) {
70 ioe.printStackTrace();
74 private void collectUpFiles(File location, File base, List collectionPoint) {
75 String contents[] = location.list();
76 if (contents==null) return;
77 for (int i = 0; i < contents.length; i++) {
78 String string = contents[i];
79 File f = new File(location,string);
80 if (f.isDirectory()) {
81 collectUpFiles(f,base,collectionPoint);
82 } else if (f.isFile() && (f.getName().endsWith(".aj") || f.getName().endsWith(".java"))) {
85 fileFound = f.getCanonicalPath();
86 String toRemove = base.getCanonicalPath();
87 if (!fileFound.startsWith(toRemove)) throw new RuntimeException("eh? "+fileFound+" "+toRemove);
88 collectionPoint.add(fileFound.substring(toRemove.length()+1));//+1 captures extra separator
89 } catch (IOException e) {
97 * Fill in the working directory with the project base files,
98 * from the 'base' folder.
100 protected void initialiseProject(String p) {
101 File projectSrc=new File(testdataSrcDir+File.separatorChar+p+File.separatorChar+"base");
102 File destination=new File(getWorkingDir(),p);
103 if (!destination.exists()) {destination.mkdir();}
104 copy(projectSrc,destination);//,false);
105 // create the AjCompiler instance associated with this project
106 // (has id of the form c:\temp\ajcSandbox\<workspace_name>\<project_name>)
107 CompilerFactory.getCompilerForProjectWithDir(sandboxDir + File.separator + p);
111 * Applies an overlay onto the project being tested - copying
112 * the contents of the specified overlay directory.
114 public void alter(String projectName,String overlayDirectory) {
115 File projectSrc =new File(testdataSrcDir+File.separatorChar+projectName+
116 File.separatorChar+overlayDirectory);
117 File destination=new File(getWorkingDir(),projectName);
118 copy(projectSrc,destination);
122 * Copy the contents of some directory to another location - the
125 protected void copy(File from, File to) {
126 String contents[] = from.list();
127 if (contents==null) return;
128 for (int i = 0; i < contents.length; i++) {
129 String string = contents[i];
130 File f = new File(from,string);
131 File t = new File(to,string);
133 if (f.isDirectory() && !f.getName().startsWith("inc")) {
136 } else if (f.isFile()) {
137 StringBuffer sb = new StringBuffer();
138 //if (VERBOSE) System.err.println("Copying "+f+" to "+t);
139 FileUtil.copyFile(f,t,sb);
140 if (sb.length()!=0) { System.err.println(sb.toString());}
146 * Count the number of times a specified aspectName appears in the default
147 * aop.xml file and compare with the expected number of occurrences. If just
148 * want to count the number of aspects mentioned within the file then
149 * pass "" for the aspectName, otherwise, specify the name of the
150 * aspect interested in.
152 protected void checkXMLAspectCount(String projectName, String aspectName,
153 int expectedOccurrences, String outputDir) {
155 File aopXML = new File(outputDir + File.separatorChar + "META-INF" + File.separatorChar + "aop-ajc.xml");
157 if (!aopXML.exists()) {
158 fail("Expected file " + aopXML.getAbsolutePath() + " to exist but it doesn't");
161 BufferedReader reader = new BufferedReader(new FileReader(aopXML));
162 String line = reader.readLine();
163 while (line != null) {
164 if (aspectName.equals("") && line.indexOf("aspect name=\"") != -1) {
166 } else if (line.indexOf("aspect name=\""+aspectName+"\"") != -1) {
169 line = reader.readLine();
172 } catch (IOException ie) {
173 ie.printStackTrace();
175 if (aspectCount != expectedOccurrences) {
176 fail("Expected aspect " + aspectName + " to appear " + expectedOccurrences + " times" +
177 " in the aop.xml file but found " + aspectCount + " occurrences");