Search in sources :

Example 6 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class PowerMockClassloaderExecutor method forClass.

public static SingleClassloaderExecutor forClass(Class<?> testClass, MockPolicyInitializer mockPolicyInitializer) {
    List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
    mockTransformerChain.add(new ClassMockTransformer());
    mockTransformerChain.add(new InterfaceMockTransformer());
    MockClassLoader mockLoader = new MockClassLoader(new String[0], new String[0]);
    mockLoader.setMockTransformerChain(mockTransformerChain);
    mockLoader.addIgnorePackage(new PowerMockIgnorePackagesExtractorImpl().getPackagesToIgnore(testClass));
    mockLoader.addClassesToModify(new PrepareForTestExtractorImpl().getTestClasses(testClass));
    mockLoader.addClassesToModify(new StaticConstructorSuppressExtractorImpl().getTestClasses(testClass));
    mockPolicyInitializer.initialize(mockLoader);
    return new SingleClassloaderExecutor(mockLoader);
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) PowerMockIgnorePackagesExtractorImpl(org.powermock.tests.utils.impl.PowerMockIgnorePackagesExtractorImpl) StaticConstructorSuppressExtractorImpl(org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl) MockClassLoader(org.powermock.core.classloader.MockClassLoader) ArrayList(java.util.ArrayList) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) PrepareForTestExtractorImpl(org.powermock.tests.utils.impl.PrepareForTestExtractorImpl)

Example 7 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class XStreamClassloaderExecutorTest method loadsObjectGraphThatIncludesEnumsInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.

@Test
public void loadsObjectGraphThatIncludesEnumsInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
    MockClassLoader classloader = createClassloader();
    final MyEnum expected = MyEnum.MyEnum1;
    final MyEnumHolder myClass = new MyEnumHolder(expected);
    MyEnum actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyEnum>() {

        public MyEnum call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            MyEnum myEnum = myClass.getMyEnum();
            assertEquals(expected, myEnum);
            return myEnum;
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    assertEquals(expected, actual);
}
Also used : MyEnum(powermock.classloading.classes.MyEnum) MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyEnumHolder(powermock.classloading.classes.MyEnumHolder) Test(org.junit.Test)

Example 8 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class XStreamClassloaderExecutorTest method loadsObjectGraphThatIncludesPrimitiveValuesInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.

@Test
public void loadsObjectGraphThatIncludesPrimitiveValuesInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
    MockClassLoader classloader = createClassloader();
    final Integer expected = 42;
    final MyIntegerHolder myClass = new MyIntegerHolder(expected);
    Integer actual = new SingleClassloaderExecutor(classloader).execute(new Callable<Integer>() {

        public Integer call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            final int myInteger = myClass.getMyInteger();
            assertEquals((int) expected, myInteger);
            return myInteger;
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    assertEquals(expected, actual);
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyIntegerHolder(powermock.classloading.classes.MyIntegerHolder) Test(org.junit.Test)

Example 9 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class XStreamClassloaderExecutorTest method loadsObjectGraphInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.

@Test
public void loadsObjectGraphInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
    MockClassLoader classloader = createClassloader();
    final MyReturnValue expectedConstructorValue = new MyReturnValue(new MyArgument("first value"));
    final MyClass myClass = new MyClass(expectedConstructorValue);
    final MyArgument expected = new MyArgument("A value");
    MyReturnValue[] actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyReturnValue[]>() {

        public MyReturnValue[] call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            return myClass.myMethod(expected);
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    final MyReturnValue myReturnValue = actual[0];
    assertEquals(expectedConstructorValue.getMyArgument().getValue(), myReturnValue.getMyArgument().getValue());
    assertEquals(expected.getValue(), actual[1].getMyArgument().getValue());
}
Also used : MyArgument(powermock.classloading.classes.MyArgument) MyClass(powermock.classloading.classes.MyClass) MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyReturnValue(powermock.classloading.classes.MyReturnValue) Test(org.junit.Test)

Example 10 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class XStreamClassloaderExecutorTest method usesReferenceCloningWhenTwoFieldsPointToSameInstance.

@Test
public void usesReferenceCloningWhenTwoFieldsPointToSameInstance() throws Exception {
    final MockClassLoader classloader = createClassloader();
    final MyReferenceFieldHolder tested = new MyReferenceFieldHolder();
    assertSame(tested.getMyArgument1(), tested.getMyArgument2());
    assertSame(tested.getMyArgument1(), MyReferenceFieldHolder.MY_ARGUMENT);
    new SingleClassloaderExecutor(classloader).execute(new Runnable() {

        public void run() {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            assertEquals(tested.getMyArgument1(), tested.getMyArgument2());
            assertEquals(tested.getMyArgument1(), MyReferenceFieldHolder.MY_ARGUMENT);
            assertSame(tested.getMyArgument1(), tested.getMyArgument2());
        // FIXME: This assertion should work:
        // assertSame(tested.getMyArgument1(), MyReferenceFieldHolder.MY_ARGUMENT);
        }
    });
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) MyReferenceFieldHolder(powermock.classloading.classes.MyReferenceFieldHolder) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) Test(org.junit.Test)

Aggregations

SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)25 MockClassLoader (org.powermock.core.classloader.MockClassLoader)25 Test (org.junit.Test)24 MyArgument (powermock.classloading.classes.MyArgument)6 MyReturnValue (powermock.classloading.classes.MyReturnValue)6 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 LinkedList (java.util.LinkedList)2 Ignore (org.junit.Ignore)2 MyClass (powermock.classloading.classes.MyClass)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 MyIntegerHolder (powermock.classloading.classes.MyIntegerHolder)2 MyPrimitiveArrayHolder (powermock.classloading.classes.MyPrimitiveArrayHolder)2 MyReferenceFieldHolder (powermock.classloading.classes.MyReferenceFieldHolder)2 MyStaticFinalArgumentHolder (powermock.classloading.classes.MyStaticFinalArgumentHolder)2 MyStaticFinalNumberHolder (powermock.classloading.classes.MyStaticFinalNumberHolder)2