moving instrument.classloading.* unit tests from .testsuite -> .context
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.instrument.classloading;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class InstrumentableClassLoaderTests {
|
||||
|
||||
@Test
|
||||
public void testDefaultLoadTimeWeaver() {
|
||||
ClassLoader loader = new SimpleInstrumentableClassLoader(ClassUtils.getDefaultClassLoader());
|
||||
ReflectiveLoadTimeWeaver handler = new ReflectiveLoadTimeWeaver(loader);
|
||||
assertSame(loader, handler.getInstrumentableClassLoader());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.instrument.classloading;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.security.ProtectionDomain;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ReflectiveLoadTimeWeaver} class.
|
||||
*
|
||||
* @author Rick Evans
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class ReflectiveLoadTimeWeaverTests {
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCtorWithNullClassLoader() {
|
||||
new ReflectiveLoadTimeWeaver(null);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void testCtorWithClassLoaderThatDoesNotExposeAnAddTransformerMethod() {
|
||||
new ReflectiveLoadTimeWeaver(getClass().getClassLoader());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCtorWithClassLoaderThatDoesNotExposeAGetThrowawayClassLoaderMethodIsOkay() {
|
||||
JustAddTransformerClassLoader classLoader = new JustAddTransformerClassLoader();
|
||||
ReflectiveLoadTimeWeaver weaver = new ReflectiveLoadTimeWeaver(classLoader);
|
||||
weaver.addTransformer(new ClassFileTransformer() {
|
||||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
|
||||
return "CAFEDEAD".getBytes();
|
||||
}
|
||||
});
|
||||
assertEquals(1, classLoader.getNumTimesGetThrowawayClassLoaderCalled());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testAddTransformerWithNullTransformer() {
|
||||
new ReflectiveLoadTimeWeaver(new JustAddTransformerClassLoader()).addTransformer(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetThrowawayClassLoaderWithClassLoaderThatDoesNotExposeAGetThrowawayClassLoaderMethodYieldsFallbackClassLoader() {
|
||||
ReflectiveLoadTimeWeaver weaver = new ReflectiveLoadTimeWeaver(new JustAddTransformerClassLoader());
|
||||
ClassLoader throwawayClassLoader = weaver.getThrowawayClassLoader();
|
||||
assertNotNull(throwawayClassLoader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetThrowawayClassLoaderWithTotallyCompliantClassLoader() {
|
||||
TotallyCompliantClassLoader classLoader = new TotallyCompliantClassLoader();
|
||||
ReflectiveLoadTimeWeaver weaver = new ReflectiveLoadTimeWeaver(classLoader);
|
||||
ClassLoader throwawayClassLoader = weaver.getThrowawayClassLoader();
|
||||
assertNotNull(throwawayClassLoader);
|
||||
assertEquals(1, classLoader.getNumTimesGetThrowawayClassLoaderCalled());
|
||||
}
|
||||
|
||||
|
||||
public static class JustAddTransformerClassLoader extends ClassLoader {
|
||||
|
||||
private int numTimesAddTransformerCalled = 0;
|
||||
|
||||
|
||||
public int getNumTimesGetThrowawayClassLoaderCalled() {
|
||||
return this.numTimesAddTransformerCalled;
|
||||
}
|
||||
|
||||
|
||||
public void addTransformer(ClassFileTransformer transformer) {
|
||||
++this.numTimesAddTransformerCalled;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static final class TotallyCompliantClassLoader extends JustAddTransformerClassLoader {
|
||||
|
||||
private int numTimesGetThrowawayClassLoaderCalled = 0;
|
||||
|
||||
|
||||
public int getNumTimesGetThrowawayClassLoaderCalled() {
|
||||
return this.numTimesGetThrowawayClassLoaderCalled;
|
||||
}
|
||||
|
||||
|
||||
public ClassLoader getThrowawayClassLoader() {
|
||||
++this.numTimesGetThrowawayClassLoaderCalled;
|
||||
return getClass().getClassLoader();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.instrument.classloading;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Chris Beams
|
||||
* @since 2.0
|
||||
*/
|
||||
public class ResourceOverridingShadowingClassLoaderTests {
|
||||
|
||||
private static final String EXISTING_RESOURCE = "org/springframework/instrument/classloading/testResource.xml";
|
||||
|
||||
private ClassLoader thisClassLoader = getClass().getClassLoader();
|
||||
|
||||
private ResourceOverridingShadowingClassLoader overridingLoader = new ResourceOverridingShadowingClassLoader(thisClassLoader);
|
||||
|
||||
|
||||
@Test
|
||||
public void testFindsExistingResourceWithGetResourceAndNoOverrides() {
|
||||
assertNotNull(thisClassLoader.getResource(EXISTING_RESOURCE));
|
||||
assertNotNull(overridingLoader.getResource(EXISTING_RESOURCE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesNotFindExistingResourceWithGetResourceAndNullOverride() {
|
||||
assertNotNull(thisClassLoader.getResource(EXISTING_RESOURCE));
|
||||
overridingLoader.override(EXISTING_RESOURCE, null);
|
||||
assertNull(overridingLoader.getResource(EXISTING_RESOURCE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindsExistingResourceWithGetResourceAsStreamAndNoOverrides() {
|
||||
assertNotNull(thisClassLoader.getResourceAsStream(EXISTING_RESOURCE));
|
||||
assertNotNull(overridingLoader.getResourceAsStream(EXISTING_RESOURCE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesNotFindExistingResourceWithGetResourceAsStreamAndNullOverride() {
|
||||
assertNotNull(thisClassLoader.getResourceAsStream(EXISTING_RESOURCE));
|
||||
overridingLoader.override(EXISTING_RESOURCE, null);
|
||||
assertNull(overridingLoader.getResourceAsStream(EXISTING_RESOURCE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindsExistingResourceWithGetResourcesAndNoOverrides() throws IOException {
|
||||
assertNotNull(thisClassLoader.getResources(EXISTING_RESOURCE));
|
||||
assertNotNull(overridingLoader.getResources(EXISTING_RESOURCE));
|
||||
assertEquals(1, countElements(overridingLoader.getResources(EXISTING_RESOURCE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesNotFindExistingResourceWithGetResourcesAndNullOverride() throws IOException {
|
||||
assertNotNull(thisClassLoader.getResources(EXISTING_RESOURCE));
|
||||
overridingLoader.override(EXISTING_RESOURCE, null);
|
||||
assertEquals(0, countElements(overridingLoader.getResources(EXISTING_RESOURCE)));
|
||||
}
|
||||
|
||||
private int countElements(Enumeration<?> e) {
|
||||
int elts = 0;
|
||||
while (e.hasMoreElements()) {
|
||||
e.nextElement();
|
||||
++elts;
|
||||
}
|
||||
return elts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.instrument.classloading.glassfish;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.security.SecureClassLoader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.spi.ClassTransformer;
|
||||
|
||||
import org.easymock.ArgumentsMatcher;
|
||||
import org.easymock.MockControl;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.instrument.classloading.LoadTimeWeaver;
|
||||
|
||||
import com.sun.enterprise.loader.InstrumentableClassLoader;
|
||||
|
||||
// converting away from old-style EasyMock APIs was problematic with this class
|
||||
@SuppressWarnings("deprecation")
|
||||
public class GlassFishLoadTimeWeaverTests {
|
||||
|
||||
private MockControl<InstrumentableClassLoader> loaderCtrl;
|
||||
private InstrumentableClassLoader loader;
|
||||
private LoadTimeWeaver ltw;
|
||||
|
||||
private class DummyInstrumentableClassLoader extends SecureClassLoader implements InstrumentableClassLoader {
|
||||
|
||||
public DummyInstrumentableClassLoader() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DummyInstrumentableClassLoader(ClassLoader parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
private List<ClassTransformer> transformers = new ArrayList<ClassTransformer>();
|
||||
|
||||
public void addTransformer(ClassTransformer transformer) {
|
||||
transformers.add(transformer);
|
||||
}
|
||||
|
||||
public ClassLoader copy() {
|
||||
return new DummyInstrumentableClassLoader();
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
loaderCtrl = MockControl.createControl(InstrumentableClassLoader.class);
|
||||
loader = loaderCtrl.getMock();
|
||||
loaderCtrl.replay();
|
||||
|
||||
ltw = new GlassFishLoadTimeWeaver() {
|
||||
@Override
|
||||
protected InstrumentableClassLoader determineClassLoader(ClassLoader cl) {
|
||||
return loader;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
loaderCtrl.verify();
|
||||
ltw = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGlassFishLoadTimeWeaver() {
|
||||
try {
|
||||
ltw = new GlassFishLoadTimeWeaver();
|
||||
fail("expected exception");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGlassFishLoadTimeWeaverClassLoader() {
|
||||
try {
|
||||
ltw = new GlassFishLoadTimeWeaver(null);
|
||||
fail("expected exception");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
ClassLoader cl1 = new URLClassLoader(new URL[0]);
|
||||
ClassLoader cl2 = new URLClassLoader(new URL[0], cl1);
|
||||
ClassLoader cl3 = new DummyInstrumentableClassLoader(cl2);
|
||||
ClassLoader cl4 = new URLClassLoader(new URL[0], cl3);
|
||||
|
||||
ltw = new GlassFishLoadTimeWeaver(cl4);
|
||||
assertSame(cl3, ltw.getInstrumentableClassLoader());
|
||||
|
||||
cl1 = new URLClassLoader(new URL[0]);
|
||||
cl2 = new URLClassLoader(new URL[0], cl1);
|
||||
cl3 = new DummyInstrumentableClassLoader(cl2);
|
||||
cl4 = new DummyInstrumentableClassLoader(cl3);
|
||||
|
||||
ltw = new GlassFishLoadTimeWeaver(cl4);
|
||||
assertSame(cl4, ltw.getInstrumentableClassLoader());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddTransformer() {
|
||||
ClassFileTransformer transformer = MockControl.createNiceControl(
|
||||
ClassFileTransformer.class).getMock();
|
||||
loaderCtrl.reset();
|
||||
loader.addTransformer(new ClassTransformerAdapter(transformer));
|
||||
loaderCtrl.setMatcher(new ArgumentsMatcher() {
|
||||
|
||||
public boolean matches(Object[] arg0, Object[] arg1) {
|
||||
for (int i = 0; i < arg0.length; i++) {
|
||||
if (arg0 != null && arg0.getClass() != arg1.getClass())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String toString(Object[] arg0) {
|
||||
return Arrays.toString(arg0);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
loaderCtrl.replay();
|
||||
|
||||
ltw.addTransformer(transformer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetThrowawayClassLoader() {
|
||||
loaderCtrl.reset();
|
||||
ClassLoader cl = new URLClassLoader(new URL[0]);
|
||||
loaderCtrl.expectAndReturn(loader.copy(), cl);
|
||||
loaderCtrl.replay();
|
||||
|
||||
assertSame(ltw.getThrowawayClassLoader(), cl);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.instrument.classloading.oc4j;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.IllegalClassFormatException;
|
||||
import java.security.ProtectionDomain;
|
||||
|
||||
import org.easymock.AbstractMatcher;
|
||||
import org.easymock.MockControl;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link OC4JClassPreprocessorAdapter} class.
|
||||
*
|
||||
* @author Rick Evans
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class OC4JClassPreprocessorAdapterTests {
|
||||
|
||||
@Test
|
||||
public void testClassNameIsUnMangledPriorToTransformation() throws IllegalClassFormatException {
|
||||
final byte[] classBytes = "CAFEBABE".getBytes();
|
||||
final ClassLoader classLoader = getClass().getClassLoader();
|
||||
|
||||
ClassFileTransformer transformer = createMock(ClassFileTransformer.class);
|
||||
|
||||
expect(
|
||||
transformer.transform(eq(classLoader), eq("com/foo/Bar"), (Class<?>)isNull(), (ProtectionDomain)isNull(), isA(byte[].class))
|
||||
).andReturn(classBytes);
|
||||
replay(transformer);
|
||||
|
||||
OC4JClassPreprocessorAdapter processor = new OC4JClassPreprocessorAdapter(transformer);
|
||||
byte[] bytes = processor.processClass("com.foo.Bar", classBytes, 0, 0, null, classLoader);
|
||||
assertNotNull(bytes);
|
||||
|
||||
verify(transformer);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCtorWithNullClassFileTransformer() {
|
||||
new OC4JClassPreprocessorAdapter(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.instrument.classloading.oc4j;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link OC4JLoadTimeWeaver} class.
|
||||
*
|
||||
* @author Rick Evans
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class OC4JLoadTimeWeaverTests {
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCtorWithNullClassLoader() {
|
||||
new OC4JLoadTimeWeaver(null);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testAddTransformerWithNullTransformer() {
|
||||
new OC4JLoadTimeWeaver().addTransformer(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<!DOCTYPE root>
|
||||
<root>
|
||||
</root>
|
||||
Reference in New Issue
Block a user