use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.
the class XStreamClassloaderExecutorTest method clonesStaticFinalNumberFields.
@Test
public void clonesStaticFinalNumberFields() throws Exception {
MockClassLoader classloader = createClassloader();
final MyStaticFinalNumberHolder expected = new MyStaticFinalNumberHolder();
MyStaticFinalNumberHolder actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyStaticFinalNumberHolder>() {
public MyStaticFinalNumberHolder call() throws Exception {
assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
MyStaticFinalNumberHolder actual = new MyStaticFinalNumberHolder();
assertEquals(expected.getMyLong(), actual.getMyLong());
return actual;
}
});
assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
assertEquals(expected.getMyLong(), actual.getMyLong());
}
use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.
the class XStreamClassloaderExecutorTest method loadsObjectGraphThatIncludesCollectionInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.
@Test
public void loadsObjectGraphThatIncludesCollectionInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
final MockClassLoader classloader = createClassloader();
final Collection<MyReturnValue> expected = new LinkedList<MyReturnValue>();
expected.add(new MyReturnValue(new MyArgument("one")));
expected.add(new MyReturnValue(new MyArgument("two")));
final MyCollectionHolder myClass = new MyCollectionHolder(expected);
Collection<?> actual = new SingleClassloaderExecutor(classloader).execute(new Callable<Collection<?>>() {
public Collection<?> call() throws Exception {
assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
Collection<?> myCollection = myClass.getMyCollection();
for (Object object : myCollection) {
assertEquals(MockClassLoader.class.getName(), object.getClass().getClassLoader().getClass().getName());
}
return myCollection;
}
});
assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
assertEquals(2, actual.size());
for (Object object : actual) {
final String value = ((MyReturnValue) object).getMyArgument().getValue();
assertTrue(value.equals("one") || value.equals("two"));
}
}
use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.
the class XStreamClassloaderExecutorTest method clonesStaticFinalObjectFields.
@Test
public void clonesStaticFinalObjectFields() throws Exception {
MockClassLoader classloader = createClassloader();
final MyStaticFinalArgumentHolder expected = new MyStaticFinalArgumentHolder();
MyStaticFinalArgumentHolder actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyStaticFinalArgumentHolder>() {
public MyStaticFinalArgumentHolder call() throws Exception {
assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
MyStaticFinalArgumentHolder actual = new MyStaticFinalArgumentHolder();
assertEquals(expected.getMyObject(), actual.getMyObject());
return actual;
}
});
assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
assertEquals(expected.getMyObject(), actual.getMyObject());
}
use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.
the class TestClassTransformerTest method preparedSubclassShouldNotGetPublicDeferConstructor.
@Test
public void preparedSubclassShouldNotGetPublicDeferConstructor() throws Exception {
MockClassLoader mockClassLoader = classLoaderCase.createMockClassLoaderThatPrepare(SupportClasses.SubClass.class);
final Class<?> clazz = Class.forName(SupportClasses.SubClass.class.getName(), true, mockClassLoader);
assertEquals("Original number of constructoprs", 1, SupportClasses.SubClass.class.getConstructors().length);
try {
fail("A public defer-constructor is not expected: " + clazz.getConstructor(IndicateReloadClass.class));
} catch (NoSuchMethodException is_expected) {
}
assertEquals("Number of (public) constructors in modified class", 1, clazz.getConstructors().length);
assertNotNull("But there should still be a non-public defer constructor!", clazz.getDeclaredConstructor(IndicateReloadClass.class));
}
use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.
the class TestClassTransformerTest method preparedClassConstructorsShouldKeepTheirAccessModifier.
@Test
public void preparedClassConstructorsShouldKeepTheirAccessModifier() throws Exception {
MockClassLoader mockClassLoader = classLoaderCase.createMockClassLoaderThatPrepare(SupportClasses.MultipleConstructors.class);
final Class<?> clazz = Class.forName(SupportClasses.MultipleConstructors.class.getName(), true, mockClassLoader);
for (Constructor<?> originalConstructor : SupportClasses.MultipleConstructors.class.getDeclaredConstructors()) {
Class[] paramTypes = originalConstructor.getParameterTypes();
int originalModifiers = originalConstructor.getModifiers();
int newModifiers = clazz.getDeclaredConstructor(paramTypes).getModifiers();
String constructorName = 0 == paramTypes.length ? "Default constructor " : paramTypes[0].getSimpleName() + " constructor ";
assertEquals(constructorName + "is public?", isPublic(originalModifiers), isPublic(newModifiers));
assertEquals(constructorName + "is protected?", isProtected(originalModifiers), isProtected(newModifiers));
assertEquals(constructorName + "is private?", isPrivate(originalModifiers), isPrivate(newModifiers));
}
}
Aggregations