]> source.dussan.org Git - aspectj.git/blob
2917dda5e168cb92bfc45f276a9ed66a12ce321f
[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.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.aspectj.ajdt.internal.core.builder.AjState;
25 import org.aspectj.testing.util.FileUtil;
26
27 public class AbstractMultiProjectIncrementalAjdeInteractionTestbed extends
28                 AjdeInteractionTestbed {
29
30         public static boolean VERBOSE = false;
31
32         protected void setUp() throws Exception {
33                 super.setUp();
34                 AjdeInteractionTestbed.VERBOSE = VERBOSE;
35                 AjState.FORCE_INCREMENTAL_DURING_TESTING = true;
36         }
37
38         protected void tearDown() throws Exception {
39                 super.tearDown();
40                 AjState.FORCE_INCREMENTAL_DURING_TESTING = false;
41         }
42
43         public void build(String projectName) {
44                 constructUpToDateLstFile(projectName,"build.lst");
45                 doBuild(projectName);
46                 if (AjdeInteractionTestbed.VERBOSE) printBuildReport(projectName);
47         }
48
49         public void fullBuild(String projectName) {
50                 constructUpToDateLstFile(projectName,"build.lst");
51                 doFullBuild(projectName);
52                 if (AjdeInteractionTestbed.VERBOSE) printBuildReport(projectName);
53         }
54
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);
60         
61                 try {
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");
67                         }
68                         dos.close();
69                 } catch (IOException ioe) {
70                         ioe.printStackTrace();
71                 }
72         }
73
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"))) {
83                                 String fileFound;
84                                 try {
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) {
90                                         e.printStackTrace();
91                                 }
92                         }
93                 }
94         }
95
96         /**
97          * Fill in the working directory with the project base files,
98          * from the 'base' folder.
99          */
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);
108         }
109
110         /**
111          * Applies an overlay onto the project being tested - copying
112          * the contents of the specified overlay directory.
113          */
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);
119         }
120         
121         /**
122          * Copy the contents of some directory to another location - the
123          * copy is recursive.
124          */
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);
132                         
133                         if (f.isDirectory() && !f.getName().startsWith("inc")) {
134                                 t.mkdir();
135                                 copy(f,t);
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());}
141                         } 
142                 }
143         }
144
145         /**
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.
151          */
152         protected void checkXMLAspectCount(String projectName, String aspectName,
153                         int expectedOccurrences, String outputDir) {
154                                 int aspectCount = 0;
155                                 File aopXML = new File(outputDir + File.separatorChar + "META-INF" + File.separatorChar + "aop-ajc.xml");
156                         
157                                 if (!aopXML.exists()) {
158                                         fail("Expected file " + aopXML.getAbsolutePath() + " to exist but it doesn't");
159                                 }
160                                 try {
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) {
165                                                         aspectCount++;
166                                                 } else if (line.indexOf("aspect name=\""+aspectName+"\"") != -1) {
167                                                         aspectCount++;
168                                                 }
169                                                 line = reader.readLine();
170                                         }
171                                         reader.close();
172                                 } catch (IOException ie) {
173                                         ie.printStackTrace();
174                                 }
175                                 if (aspectCount != expectedOccurrences) {
176                                         fail("Expected aspect " + aspectName + " to appear " + expectedOccurrences + " times" +
177                                                         " in the aop.xml file but found " + aspectCount + " occurrences");
178                                 }
179                         }
180 }