Search in sources :

Example 21 with MockClassLoader

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());
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyStaticFinalNumberHolder(powermock.classloading.classes.MyStaticFinalNumberHolder) Test(org.junit.Test)

Example 22 with MockClassLoader

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"));
    }
}
Also used : MyArgument(powermock.classloading.classes.MyArgument) MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) LinkedList(java.util.LinkedList) MyCollectionHolder(powermock.classloading.classes.MyCollectionHolder) Collection(java.util.Collection) MyReturnValue(powermock.classloading.classes.MyReturnValue) Test(org.junit.Test)

Example 23 with MockClassLoader

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());
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyStaticFinalArgumentHolder(powermock.classloading.classes.MyStaticFinalArgumentHolder) Test(org.junit.Test)

Example 24 with MockClassLoader

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));
}
Also used : SupportClasses(powermock.test.support.MainMockTransformerTestSupport.SupportClasses) MockClassLoader(org.powermock.core.classloader.MockClassLoader) IndicateReloadClass(org.powermock.core.IndicateReloadClass) Test(org.junit.Test)

Example 25 with MockClassLoader

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));
    }
}
Also used : SupportClasses(powermock.test.support.MainMockTransformerTestSupport.SupportClasses) MockClassLoader(org.powermock.core.classloader.MockClassLoader) IndicateReloadClass(org.powermock.core.IndicateReloadClass) Test(org.junit.Test)

Aggregations

MockClassLoader (org.powermock.core.classloader.MockClassLoader)35 Test (org.junit.Test)26 SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)25 MyArgument (powermock.classloading.classes.MyArgument)8 MyReturnValue (powermock.classloading.classes.MyReturnValue)8 LinkedList (java.util.LinkedList)5 MockTransformer (org.powermock.core.transformers.MockTransformer)4 MyClass (powermock.classloading.classes.MyClass)4 Method (java.lang.reflect.Method)3 Collection (java.util.Collection)2 CtClass (javassist.CtClass)2 Ignore (org.junit.Ignore)2 IndicateReloadClass (org.powermock.core.IndicateReloadClass)2 ClassMockTransformer (org.powermock.core.transformers.impl.ClassMockTransformer)2 InterfaceMockTransformer (org.powermock.core.transformers.impl.InterfaceMockTransformer)2 MyCollectionHolder (powermock.classloading.classes.MyCollectionHolder)2 MyEnum (powermock.classloading.classes.MyEnum)2 MyEnumHolder (powermock.classloading.classes.MyEnumHolder)2 MyHierarchicalFieldHolder (powermock.classloading.classes.MyHierarchicalFieldHolder)2 MyHierarchicalOverloadedFieldHolder (powermock.classloading.classes.MyHierarchicalOverloadedFieldHolder)2