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
8
* Contributors: IBM Corporation - initial API and implementation
9
* Helen Hawkins - initial version
10
*******************************************************************/
11
package org.aspectj.systemtest.model;
13
import java.io.BufferedReader;
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;
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;
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.
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>).
42
public abstract class ModelTestCase extends XMLBasedAjcTestCase {
44
protected static boolean regenerate = false;
45
protected static boolean debugTest = false;
47
private final String expectedOutDir = "../tests/model/expected" + File.separator;
48
private String testid;
50
private String modelFilename;
52
private IElementHandleProvider handleProvider;
55
* @see junit.framework.TestCase#setUp()
57
protected void setUp() throws Exception {
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()));
72
static class TestFilter implements IModelFilter {
73
String sandboxDirectory ;
74
public TestFilter(String sandboxDirectory) {
75
this.sandboxDirectory = sandboxDirectory;
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('/','\\');
86
// don't report the column number since this is sometimes
87
// different on windows and linux
88
int column = sub.lastIndexOf(':');
90
return "TEST_SANDBOX" + sub.substring(0,column);
92
return "TEST_SANDBOX"+sub;
97
public boolean wantsHandleIds() {
103
* @see junit.framework.TestCase#tearDown()
105
protected void tearDown() throws Exception {
107
AsmManager.getDefault().setHandleProvider(handleProvider);
108
AsmManager.setDontReport();
109
ajc.setShouldEmptySandbox(true);
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
121
protected void runModelTest(String title,String testid) {
122
this.testid = testid;
127
private void verifyModel() {
128
File expectedOutput = new File(expectedOutDir + testid + ".txt");
131
saveModel(expectedOutput);
133
// Verify the file matches what we have
134
compareModel(expectedOutput);
138
private void compareModel(File expectedF) {
139
if (debugTest) System.out.println("comparing with model in file " + expectedF.getAbsolutePath());
140
List fileContents = new ArrayList();
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);
151
List expectedFileContents = new ArrayList();
152
expectedFileContents.addAll(fileContents);
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);
161
// int j = foundLine.indexOf("(targets=");
163
foundFileContents.add(foundLine);
165
// foundFileContents.add(foundLine.substring(j));
168
// String newLine = foundLine.substring(0,i) + tempDir
169
// + foundLine.substring(i + sandboxDir.length());
170
// foundFileContents.add(newLine);
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)) {
182
System.err.println("couldn't find: " + line);
183
for (Iterator iterator = fileContents.iterator(); iterator
185
String element = (String) iterator.next();
186
System.err.println("compared with: " + element);
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");
194
fileContents.remove(line);
198
if (debugTest && !fileContents.isEmpty()) {
199
for (Iterator iter = fileContents.iterator(); iter
201
String element = (String) iter.next();
202
System.err.println("remaining: " + element);
205
assertTrue("should have found all expected model output: " + fileContents,fileContents.isEmpty());
206
} catch (Exception e) {
207
fail("Unexpected exception comparing model files:"+e);
211
private void saveModel(File f) {
212
if (debugTest) System.out.println("Saving model into "+f.getAbsolutePath());
213
File modelFile = new File(modelFilename);
215
FileUtil.copyFile(modelFile, f);
216
} catch (IOException ioe) {
217
ioe.printStackTrace();
218
fail("Couldn't copy file to "+f.toString());