tiebin-zhang / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

jvisualvm error 64 when trying to instrument powermockito code. #370

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm trying to use the unit test to do optimization by using the jvisualvm 
profiler to evaluate hotspots and code optimization progress.

I wrote simple junit4-10 test and mocked out new() method of External class 
that SUT class calls, but when trying to use jvisualvm (jdk 1.7) it throws 
exception.  

================
package org.pm;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
//@PowerMockIgnore("org.apache.log4j.*")
@PrepareForTest( { SUT.class } )
public class PMProfilerTest {

    protected final External ext = PowerMockito.mock(External.class);

    public String mockFilenamesAsArray[] = new String[1];

    @Before public void setup() throws Exception
    {
        MockitoAnnotations.initMocks(this); 

        whenNew(External.class).withNoArguments().thenAnswer(
                new Answer<Object>() {
                    public Object answer(InvocationOnMock invocation) throws Throwable {
                        when(ext.getMatchExpr()).thenReturn(".*20.*");
                        return ext;
                    }
                });
    }

    @Test public void testPowerMockProfiler()
    {
        int numStrs = 5000;
        List<String> list = new ArrayList<String>(numStrs);
        for (int cnt=0; cnt<numStrs; cnt++)
        {
            StringBuilder buf = new StringBuilder();
            buf.append("foo")
            .append(".")
            .append(("000" + cnt).substring( (cnt+"").length() ))
            .append(".")
            .append("bar");
            list.add(buf.toString());
        }

        SUT sut = new SUT();

        int matched = 0;
        long start = System.currentTimeMillis();
        for (String str:list)
        {
            if (sut.isMatch(str))
            {
                matched++;
                System.out.print("\n");
            }
        }
        long finished = System.currentTimeMillis();

        System.out.println("Found " + matched + " strings that matched \"" + ext.getMatchExpr() + "\".");
        System.out.println("Test took " + (finished - start) + " ms.");
    }

}

=============
package org.pm;

public class SUT {

    public boolean isMatch(String str) 
    {
        long start = System.currentTimeMillis();
        while (System.currentTimeMillis() - start < 1 ) ;
        System.out.print(System.currentTimeMillis() - start);
        return str.matches(new External().getMatchExpr());
    }
}
=============
package org.pm;

public class External {
    public String getMatchExpr()
    {
        return ".*2.*";
    }
}
=============

When the sut is called, I get 
java.lang.RuntimeException: javassist.NotFoundException: 
org.netbeans.lib.profiler.server.ProfilerRuntimeCPUFullInstr
    at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:187)
    at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:147)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at org.pm.PMProfilerTest$1.<init>(PMProfilerTest.java:32)
    at org.pm.PMProfilerTest.setup(PMProfilerTest.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:129)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:93)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javassist.NotFoundException: 
org.netbeans.lib.profiler.server.ProfilerRuntimeCPUFullInstr
    at javassist.ClassPool.get(ClassPool.java:440)
    at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:180)
    ... 29 more

Windows XP 2002 SP3, JDK 1.7, STS 2.6.1.RELEASE, jvisualvm 1.7.0_02 (Build 
1320-110325), powermockito 1-4-10-full, junit4.10

On a more complex test, I get 
JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED (64) and I can't profile 
the test.

Are there steps or other things that one has to do to get jvisualvm to work 
with a powermockito junit test?  Does it work at all today?

Original issue reported on code.google.com by william....@transcentra.com on 9 Feb 2012 at 1:29

GoogleCodeExporter commented 9 years ago
I think the problem may be that you're using JDK 7 since I don't think 
Javassist works 100% with Java 7 yet.

Original comment by johan.ha...@gmail.com on 9 Feb 2012 at 7:38

GoogleCodeExporter commented 9 years ago
I started off with 1.5 and 1.6 and got similar results.  Can you get the above 
code to work?  (I'm just not sure its possible)  My understanding is that a 
custom classloader is used in powermockito and I'm wondering if that will even 
work with jvisualvm today?  I'll try and confirm my findings for 1.5 and 1.6 
today also.

Original comment by william....@transcentra.com on 9 Feb 2012 at 3:56

GoogleCodeExporter commented 9 years ago
Same javassit exception using 1.6.  I left a post on jboss's javassist forum.

Original comment by william....@transcentra.com on 9 Feb 2012 at 9:28