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
|
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*
* Contributors:
* Matthew Webster - initial implementation
*******************************************************************************/
package org.aspectj.weaver.loadtime;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.List;
import org.aspectj.weaver.tools.WeavingAdaptor;
import junit.framework.TestCase;
public class WeavingContextTest extends TestCase {
private boolean called;
public void testWeavingContext() {
URLClassLoader loader = new URLClassLoader(new URL[] {},null);
IWeavingContext context = new TestWeavingContext(loader);
ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor();
adaptor.initialize(loader,context);
}
public void testGetResources() {
URLClassLoader loader = new URLClassLoader(new URL[] {},null);
IWeavingContext context = new TestWeavingContext(loader) {
public Enumeration getResources(String name) throws IOException {
called = true;
return super.getResources(name);
}
};
ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor();
adaptor.initialize(loader,context);
assertTrue("IWeavingContext not called",called);
}
public void testGetBundleIdFromURL() {
URLClassLoader loader = new URLClassLoader(new URL[] {},null);
IWeavingContext context = new TestWeavingContext(loader) {
public String getBundleIdFromURL(URL url) {
throw new UnsupportedOperationException();
}
};
ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor();
try {
adaptor.initialize(loader,context);
}
catch (UnsupportedOperationException ex) {
fail("IWeavingContect.getBundleIdFromURL() is deprecated");
}
}
public void testGetClassLoaderName() {
URLClassLoader loader = new URLClassLoader(new URL[] {},null);
IWeavingContext context = new TestWeavingContext(loader) {
public String getClassLoaderName () {
called = true;
return super.getClassLoaderName();
}
};
ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor();
adaptor.initialize(loader,context);
assertTrue("IWeavingContext not called",called);
}
public void testGetFile() throws IOException {
File file = new File("../loadtime/testdata");
URL fileURL = file.getCanonicalFile().toURI().toURL();
URLClassLoader loader = new URLClassLoader(new URL[] { fileURL },null);
IWeavingContext context = new TestWeavingContext(loader) {
public String getFile (URL url) {
called = true;
return super.getFile(url);
}
};
ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor();
adaptor.initialize(loader,context);
assertTrue("IWeavingContext not called",called);
}
public void testGetId() throws IOException {
File file = new File("../loadtime/testdata");
URL fileURL = file.getCanonicalFile().toURI().toURL();
URLClassLoader loader = new URLClassLoader(new URL[] { fileURL },null);
IWeavingContext context = new TestWeavingContext(loader) {
public String getId () {
called = true;
return super.getId();
}
};
ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor();
adaptor.initialize(loader,context);
assertTrue("IWeavingContext not called",called);
}
public void testGetDefinitions () throws Exception {
File file = new File("../loadtime/testdata");
URL fileURL = file.getCanonicalFile().toURI().toURL();
URLClassLoader loader = new URLClassLoader(new URL[] { fileURL },null);
IWeavingContext context = new TestWeavingContext(loader) {
public List getDefinitions(ClassLoader loader, WeavingAdaptor adaptor) {
called = true;
return super.getDefinitions(loader,adaptor);
}
};
ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor();
adaptor.initialize(loader,context);
assertTrue("getDefinitions not called",called);
}
private static class TestWeavingContext implements IWeavingContext {
private ClassLoader loader;
public TestWeavingContext (ClassLoader classLoader) {
this.loader = classLoader;
}
public String getBundleIdFromURL(URL url) {
return null;
}
public String getClassLoaderName() {
return "ClassLoaderName";
}
public ClassLoader getClassLoader() { return this.loader;}
public String getFile(URL url) {
return "File";
}
public String getId() {
return "Id";
}
public Enumeration getResources(String name) throws IOException {
return loader.getResources(name);
}
public boolean isLocallyDefined(String classname) {
String asResource = classname.replace('.', '/').concat(".class");
URL localURL = loader.getResource(asResource);
if (localURL == null) return false;
boolean isLocallyDefined = true;
ClassLoader parent = loader.getParent();
if (parent != null) {
URL parentURL = parent.getResource(asResource);
if (localURL.equals(parentURL)) isLocallyDefined = false;
}
return isLocallyDefined;
}
public List getDefinitions(ClassLoader loader, WeavingAdaptor adaptor) {
return ((ClassLoaderWeavingAdaptor)adaptor).parseDefinitions(loader);
}
}
protected void setUp() throws Exception {
super.setUp();
this.called = false;
}
}
|