/
InpathTests.java
345 lines (293 loc) · 12.1 KB
/
InpathTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* *******************************************************************
* Copyright (c) 2003 Contributors.
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Mik Kersten initial implementation
* Andy Clement Copied/changed for -inpath testing
* Helen Hawkins Changed to use new ajde interface (bug 148190)
* ******************************************************************/
package org.aspectj.ajde.core.tests;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
import org.aspectj.ajde.core.AjdeCoreTestCase;
import org.aspectj.ajde.core.TestCompilerConfiguration;
import org.aspectj.ajde.core.TestMessageHandler;
import org.aspectj.util.FileUtil;
public class InpathTests extends AjdeCoreTestCase {
public static final FileFilter aspectjResourceFileFilter = new FileFilter() {
public boolean accept(File pathname) {
String name = pathname.getName().toLowerCase();
return (!name.endsWith(".class") && !name.endsWith(".java") && !name.endsWith(".aj"));
}
};
public static final String indir1Name = "indir1";
public static final String indir2Name = "indir2";
public static final String injarName = "injar.jar";
public static final String outjarName = "/bin/output.jar";
private String[] build1 = new String[] { "src1" + File.separator + "Main.java" };
private String[] build2 = new String[] { "src2" + File.separator + "Aspect.java" };
private TestMessageHandler handler;
private TestCompilerConfiguration compilerConfig;
protected void setUp() throws Exception {
super.setUp();
initialiseProject("InpathTest");
handler = (TestMessageHandler) getCompiler().getMessageHandler();
compilerConfig = (TestCompilerConfiguration) getCompiler().getCompilerConfiguration();
}
protected void tearDown() throws Exception {
super.tearDown();
handler = null;
compilerConfig = null;
}
/**
* Inputs to the compiler: inpath = 'indir1/' source = 'src' output = a jar file
*
* Expected result = output jar file contains contents of indir1 and class file for source that was in src
*/
public void testInpathToOutjar() {
Set<File> inpath = new HashSet<File>();
File indir1 = openFile(indir1Name);
inpath.add(indir1);
compilerConfig.setInpath(inpath);
File outjar = openFile(outjarName);
compilerConfig.setOutjar(outjar.getAbsolutePath());
compilerConfig.setProjectSourceFiles(getSourceFileList(build1));
doBuild(true);
assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
Set<String> expectedOutputJarContents = new HashSet<String>();
// From indir1
// If we don't copy resources, these next three files won't make it.
// expectedOutputJarContents.add("META-INF/MANIFEST.MF");
// expectedOutputJarContents.add("META-INF/test.xml");
// expectedOutputJarContents.add("test/test.props");
expectedOutputJarContents.add("test/TestProperties.class");
// From src
expectedOutputJarContents.add("Main.class");
compareJars(indir1, "src", outjar, expectedOutputJarContents);
}
/**
* Similar to the first test but outputs to a directory rather than a jar.
*
*/
public void testInpathToBin() {
Set<File> inpath = new HashSet<File>();
File indir1 = openFile(indir1Name);
inpath.add(indir1);
compilerConfig.setInpath(inpath);
compilerConfig.setProjectSourceFiles(getSourceFileList(build1));
doBuild(true);
assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
Set<String> expectedBindirContents = new HashSet<String>();
// From indir1
// If we don't copy resources, these next three files won't make it
// expectedBindirContents.add("META-INF/MANIFEST.MF");
// expectedBindirContents.add("META-INF/test.xml");
// expectedBindirContents.add("test/test.props");
expectedBindirContents.add("test/TestProperties.class");
// From src
expectedBindirContents.add("Main.class");
compareIndirToBin(indir1, "src", "bin", expectedBindirContents);
}
/**
* Inputs to the compiler: inpath is 'indir2' that contains a helloworld source file and class file. source is 'src2' which
* contains Aspect.java which weaves before advice into the HelloWorld code from 'indir2'
*
* Expected result: HelloWorld copied through to output jar and 'weaved'. Compiled version of Aspect.java put into the output
* jar. The HelloWorld.java source file is also copied through to the output jar.
*
* An extra check is done at the end of this test to verify that HelloWorld has changed size (due to the weaving).
*/
public void testInpathToOutjar2() {
Set<File> inpath = new HashSet<File>();
File indir2 = openFile(indir2Name);
inpath.add(indir2);
compilerConfig.setInpath(inpath);
File outjar = openFile(outjarName);
compilerConfig.setOutjar(outjar.getAbsolutePath());
compilerConfig.setProjectSourceFiles(getSourceFileList(build2));
doBuild(true);
assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
Set<String> expectedOutputJarContents = new HashSet<String>();
// From indir1
expectedOutputJarContents.add("example/HelloWorld.class");
// If we don't copy resources, this file won't make it
// expectedOutputJarContents.add("example/HelloWorld.java");
// From src
expectedOutputJarContents.add("Aspect.class");
compareJars(indir2, "src", outjar, expectedOutputJarContents);
// Extra test. The HelloWorld class from the input directory should have been woven
// by the aspect - verify that the size of the HelloWorld class in the output directory
// is a different size to the input version.
int outputsize = fetchFromJar(outjar, "example/HelloWorld.class");
try {
FileInputStream fis = new FileInputStream(openFile(indir2Name + "/example/HelloWorld.class"));
byte[] filedata = FileUtil.readAsByteArray(fis);
int inputsize = filedata.length;
assertTrue("Weaving of Aspect should have occurred but the input and output size for HelloWorld.class are the same",
(inputsize != outputsize));
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
/**
* More complex inpath - a jar and a directory
*
* Inputs: -inpath injar.jar;indir2 source is 'src2' which contains Aspect.java
*
* Expected result: Result should be a directory containing the contents of injar.jar and indir2 and the Aspect.class file.
*
*/
public void testInpathAndInjarToBin() {
Set<File> inpath = new HashSet<File>();
File indir2 = openFile(indir2Name);
inpath.add(indir2);
inpath.add(openFile(injarName));
compilerConfig.setInpath(inpath);
compilerConfig.setProjectSourceFiles(getSourceFileList(build2));
doBuild(true);
assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
Set<String> expectedBindirContents = new HashSet<String>();
// From indir1
expectedBindirContents.add("example/HelloWorld.class");
// If we don't copy resources, this file won't make it
// expectedBindirContents.add("example/HelloWorld.java");
// From injar.jar
expectedBindirContents.add("props/resources.properties");
// From src
expectedBindirContents.add("Aspect.class");
compareIndirToBin(indir2, "src", "bin", expectedBindirContents);
// Check the input and output versions of HelloWorld.class are different sizes
try {
FileInputStream fis1 = new FileInputStream(openFile("indir2/example/HelloWorld.class"));
byte[] filedata1 = FileUtil.readAsByteArray(fis1);
int inputsize = filedata1.length;
FileInputStream fis2 = new FileInputStream(openFile("bin/example/HelloWorld.class"));
byte[] filedata2 = FileUtil.readAsByteArray(fis2);
int outputsize = filedata2.length;
assertTrue("Weaving of Aspect should have occurred but the input and output size for HelloWorld.class are the same",
(outputsize != inputsize));
fis1.close();
fis2.close();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
/*
* Ensure -outjar contains all non-Java resouces from injars
*/
public void compareJars(File dirFile, String sourceDir, File outjarFile, Set<String> expectedOutputJarContents) {
try {
assertTrue(
"outjar older than injar: outjarLastMod=" + outjarFile.lastModified() + " injarLastMod="
+ dirFile.lastModified(), (outjarFile.lastModified() >= dirFile.lastModified()));
// Go through the output jar file, for each element, remove it from
// the expectedOutputJarContents - when we finish, the expectedOutputJarContents
// set should be empty!
JarInputStream outjar = new JarInputStream(new java.io.FileInputStream(outjarFile));
ZipEntry entry;
while (null != (entry = outjar.getNextEntry())) {
String fileName = entry.getName();
fileName = fileName.replace('\\', '/');
if (fileName.indexOf("CVS") == -1) {
boolean b = expectedOutputJarContents.remove(fileName);
assertTrue("Unexpectedly found : " + fileName + " in outjar", b);
}
outjar.closeEntry();
}
outjar.close();
assertTrue("Didnt make it into the output jar: " + expectedOutputJarContents.toString(),
expectedOutputJarContents.isEmpty());
} catch (IOException ex) {
fail(ex.toString());
}
}
/*
* Ensure -outjar contains all non-Java resouces from source and injars
*/
public void compareSourceToOutjar(String indirName, File outjarFile) {
HashSet resources = new HashSet();
listSourceResources(indirName, resources);
try {
JarInputStream outjar = new JarInputStream(new java.io.FileInputStream(outjarFile));
ZipEntry entry;
while (null != (entry = outjar.getNextEntry())) {
String fileName = entry.getName();
if (!fileName.endsWith(".class")) {
boolean b = resources.remove(fileName);
assertTrue(fileName, b);
}
outjar.closeEntry();
}
outjar.close();
assertTrue("Missing resources: " + resources.toString(), resources.isEmpty());
} catch (IOException ex) {
fail(ex.toString());
}
}
/*
* Ensure bin contains all non-Java resouces from source and injars
*/
public void compareIndirToBin(File indirFile, String sourceDir, String outdirName, Set expectedOutdirContents) {
// byte[] inManifest = null;
File binBase = openFile(outdirName);
String[] toResources = FileUtil.listFiles(binBase);
for (int i = 0; i < toResources.length; i++) {
String fileName = toResources[i];
if (fileName.indexOf("CVS") == -1) {
boolean b = expectedOutdirContents.remove(fileName);
assertTrue("Extraneous resources: " + fileName, b);
}
}
assertTrue("Missing resources: " + expectedOutdirContents.toString(), expectedOutdirContents.isEmpty());
}
private void listSourceResources(String indirName, Set resources) {
File srcBase = openFile(indirName);
File[] fromResources = FileUtil.listFiles(srcBase, aspectjResourceFileFilter);
for (int i = 0; i < fromResources.length; i++) {
String name = FileUtil.normalizedPath(fromResources[i], srcBase);
// System.err.println("Checking "+name);
if (!name.startsWith("CVS/") && (-1 == name.indexOf("/CVS/")) && !name.endsWith("/CVS")) {
resources.add(name);
}
}
}
// Return the size of specified entry from the output jar file
public int fetchFromJar(File outjarFile, String filename) {
int ret = -1;
try {
JarInputStream outjar;
outjar = new JarInputStream(new java.io.FileInputStream(outjarFile));
ZipEntry entry;
while (null != (entry = outjar.getNextEntry())) {
String zipentryname = entry.getName();
if (zipentryname.equals(filename)) {
byte[] filedata = FileUtil.readAsByteArray(outjar);
ret = filedata.length;
outjar.closeEntry();
break;
}
outjar.closeEntry();
}
outjar.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
}