~ubuntu-branches/ubuntu/natty/aspectj/natty

« back to all changes in this revision

Viewing changes to org.aspectj/modules/tests/src/org/aspectj/systemtest/model/ModelTestCase.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-10-04 16:37:23 UTC
  • mfrom: (1.1.3 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091004163723-ck4y7j7fhjxskkie
Tags: 1.6.6+dfsg-1
* New upstream release.
  - Update 02_use_gjdoc.diff patch
* Update my email address

Show diffs side-by-side

added added

removed removed

Lines of Context:
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: IBM Corporation - initial API and implementation 
9
 
 *                               Helen Hawkins   - initial version
10
 
 *******************************************************************/
11
 
package org.aspectj.systemtest.model;
12
 
 
13
 
import java.io.BufferedReader;
14
 
import java.io.File;
15
 
import java.io.FileReader;
16
 
import java.io.IOException;
17
 
import java.util.ArrayList;
18
 
import java.util.Iterator;
19
 
import java.util.List;
20
 
 
21
 
import org.aspectj.asm.AsmManager;
22
 
import org.aspectj.asm.IElementHandleProvider;
23
 
import org.aspectj.asm.IModelFilter;
24
 
import org.aspectj.asm.internal.JDTLikeHandleProvider;
25
 
import org.aspectj.testing.XMLBasedAjcTestCase;
26
 
import org.aspectj.util.FileUtil;
27
 
 
28
 
/**
29
 
 * This class provides an extension to the XMLBasedAjcTestCase to manage
30
 
 * testing the model. It assumes the testdata is in ../tests/model/<testid>
31
 
 * and that the expected model against which to do the comparison is in
32
 
 * the file ../tests/model/expected/<testid>.txt. One test ensures that both
33
 
 * the model and the relationship map are as expected for the given testdata.
34
 
 * 
35
 
 * To write a testcase, create a testdata directory containing the data
36
 
 * for the test run and a file containing the expected model (this can be 
37
 
 * generated by setting the regenerate flag to true). Add the required 
38
 
 * configuration to model.xml. Finally, create a testcase in either ModelTests 
39
 
 * or Model5Tests (depending on whether the testcase has a requirement 
40
 
 * on Java5) and call runModelTest(<title of test>,<testid>). 
41
 
 */
42
 
public abstract class ModelTestCase extends XMLBasedAjcTestCase {
43
 
 
44
 
        protected static boolean regenerate = false;
45
 
        protected static boolean debugTest = false;
46
 
 
47
 
        private final String expectedOutDir = "../tests/model/expected" + File.separator;               
48
 
        private String testid;
49
 
        
50
 
        private String modelFilename;
51
 
        
52
 
        private IElementHandleProvider handleProvider;
53
 
        
54
 
        /* (non-Javadoc)
55
 
         * @see junit.framework.TestCase#setUp()
56
 
         */
57
 
        protected void setUp() throws Exception {
58
 
                super.setUp();
59
 
                // using the JDTLikeHandleProvider because this produces consistent handles
60
 
                // over different compiles
61
 
                handleProvider = AsmManager.getDefault().getHandleProvider();
62
 
                AsmManager.getDefault().setHandleProvider(new JDTLikeHandleProvider());
63
 
                // We are about to create a sandbox for the model output file, don't let the
64
 
                // following compile wipe it.
65
 
                ajc.setShouldEmptySandbox(false);
66
 
                // report all information - model, relationships delta processing
67
 
                modelFilename = ajc.getSandboxDirectory().getAbsolutePath() + File.separator + "model.txt";
68
 
                AsmManager.setReporting(modelFilename, 
69
 
                                true,true,true,false,new TestFilter(ajc.getSandboxDirectory().getCanonicalPath()));
70
 
        }
71
 
        
72
 
        static class TestFilter implements IModelFilter {
73
 
                String sandboxDirectory ;
74
 
                public TestFilter(String sandboxDirectory) {
75
 
                        this.sandboxDirectory = sandboxDirectory;
76
 
                }
77
 
 
78
 
                public String processFilelocation(String loc) {
79
 
                        if (loc.toLowerCase().startsWith(sandboxDirectory.toLowerCase())) {
80
 
                                String sub = loc.substring(sandboxDirectory.length());
81
 
                                int forwardSlash = sub.indexOf("/");
82
 
                                // replace all "/" with "\" - to ensure platform independence
83
 
                                if (forwardSlash != -1) {
84
 
                                        sub = sub.replace('/','\\');
85
 
                                }
86
 
                                // don't report the column number since this is sometimes
87
 
                                // different on windows and linux
88
 
                                int column = sub.lastIndexOf(':');
89
 
                                if (column != -1) {
90
 
                                        return "TEST_SANDBOX" + sub.substring(0,column);
91
 
                                }
92
 
                                return "TEST_SANDBOX"+sub;
93
 
                        }
94
 
                        return loc;
95
 
                }
96
 
 
97
 
                public boolean wantsHandleIds() {
98
 
                        return false;
99
 
                }
100
 
        }
101
 
        
102
 
        /* (non-Javadoc)
103
 
         * @see junit.framework.TestCase#tearDown()
104
 
         */
105
 
        protected void tearDown() throws Exception {
106
 
                super.tearDown();
107
 
                AsmManager.getDefault().setHandleProvider(handleProvider);
108
 
                AsmManager.setDontReport();
109
 
                ajc.setShouldEmptySandbox(true);
110
 
        }
111
 
        
112
 
        /**
113
 
         * Firstly sets the testid which is both the name of the expected output
114
 
         * file and the name of the testdata directory. It then
115
 
         * invokes XMLBasedAjcTestCase.runTest(String) with the given
116
 
         * title and finally verifies that the model file created from this test
117
 
         * run is the same as the expected output (includes model information, the
118
 
         * relationship map and various properties about the model) contained
119
 
         * in ../tests/model/expected/<testid>.txt
120
 
         */
121
 
        protected void runModelTest(String title,String testid) {
122
 
                this.testid = testid;
123
 
                runTest(title);
124
 
                verifyModel();
125
 
        }
126
 
        
127
 
        private void verifyModel() {
128
 
                File expectedOutput = new File(expectedOutDir + testid + ".txt");
129
 
                if (regenerate) {
130
 
                        // Create the file
131
 
                        saveModel(expectedOutput);
132
 
                } else {
133
 
                        // Verify the file matches what we have
134
 
                        compareModel(expectedOutput);
135
 
                }
136
 
        }
137
 
        
138
 
        private void compareModel(File expectedF) {
139
 
                if (debugTest) System.out.println("comparing with model in file " + expectedF.getAbsolutePath());
140
 
                List fileContents = new ArrayList();
141
 
                try {
142
 
                        String sandboxDir = ajc.getSandboxDirectory().getAbsolutePath();
143
 
                        String modelOutput = modelFilename;
144
 
                        // Load the file with the expected output
145
 
                        BufferedReader expect = new BufferedReader(new FileReader(expectedF));
146
 
//                      String tempDir = expect.readLine();
147
 
                        String expectedLine = null;
148
 
                        while((expectedLine=expect.readLine())!=null) {
149
 
                                fileContents.add(expectedLine);                                 
150
 
                        }
151
 
                        List expectedFileContents = new ArrayList();
152
 
                        expectedFileContents.addAll(fileContents);
153
 
                        
154
 
                        // Load the file with the output from this test run
155
 
                        BufferedReader found = new BufferedReader(new FileReader(new File(modelOutput)));
156
 
                        String foundLine = null;
157
 
                        List foundFileContents = new ArrayList();
158
 
                        while((foundLine=found.readLine())!=null) {
159
 
//                              int i = foundLine.indexOf(sandboxDir);
160
 
//                              if (i == -1) {
161
 
//                                      int j = foundLine.indexOf("(targets=");
162
 
//                                      if (j == -1) {
163
 
                                                foundFileContents.add(foundLine);
164
 
//                                      } else {
165
 
//                                              foundFileContents.add(foundLine.substring(j));
166
 
//                                      }
167
 
//                              } else {
168
 
//                                      String newLine = foundLine.substring(0,i) + tempDir 
169
 
//                                              + foundLine.substring(i + sandboxDir.length());
170
 
//                                      foundFileContents.add(newLine);
171
 
//                              }
172
 
                        }
173
 
                        
174
 
                        // iterate over what we found
175
 
                        for (Iterator iter = foundFileContents.iterator(); iter.hasNext();) {
176
 
                                String line = (String) iter.next();
177
 
                                if (debugTest) System.err.println("looking at model entry: " + line);
178
 
                                if (!fileContents.contains(line)) {
179
 
//                          if (!((String)fileContents.get(lineNumber)).equals(line)) {
180
 
                                
181
 
                                        if(debugTest) {
182
 
                                                System.err.println("couldn't find: " + line);
183
 
                                                for (Iterator iterator = fileContents.iterator(); iterator
184
 
                                                                .hasNext();) {
185
 
                                                        String element = (String) iterator.next();
186
 
                                                        System.err.println("compared with: " + element);
187
 
                                                }
188
 
                                        }
189
 
                                
190
 
//                                      StringBuffer errorData = new StringBuffer();
191
 
//                                      errorData.append("Problem with comparison at line number: "+)
192
 
                                        fail("couldn't find model entry '" + line + "' in expected output");
193
 
                                } else {
194
 
                                        fileContents.remove(line);
195
 
                                }
196
 
                        }
197
 
                        
198
 
                        if (debugTest && !fileContents.isEmpty()) {
199
 
                                for (Iterator iter = fileContents.iterator(); iter
200
 
                                                .hasNext();) {
201
 
                                        String element = (String) iter.next();
202
 
                                        System.err.println("remaining: " + element);
203
 
                                }
204
 
                        }
205
 
                        assertTrue("should have found all expected model output: " + fileContents,fileContents.isEmpty());
206
 
                } catch (Exception e) {
207
 
                        fail("Unexpected exception comparing model files:"+e);
208
 
                }
209
 
        }
210
 
        
211
 
        private void saveModel(File f) {
212
 
                if (debugTest) System.out.println("Saving model into "+f.getAbsolutePath());
213
 
                File modelFile = new File(modelFilename);       
214
 
                try {
215
 
                        FileUtil.copyFile(modelFile, f);
216
 
                } catch (IOException ioe) {
217
 
                        ioe.printStackTrace();
218
 
                        fail("Couldn't copy file to "+f.toString());
219
 
                }
220
 
        }
221
 
 
222
 
}