tiebin-zhang / powermock

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

DeferSupportingClassLoader does not override findResources(), causing it to break mocked classes that call findResources() #380

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Download and extract powermock-resource-bug-example.zip
2. Run unit tests (mvn test) - note that xsds are downloaded
3. Tweak hosts file so that www.springframework.org points to 127.0.0.1
4. Run unit tests (mvn test) - note that unit test now fails

What is the expected output? What do you see instead?
Spring should find its special 'META-INF/spring.schemas' files on the classpath 
by calling findResources().

What version of the product are you using? On what operating system?
1.4.12-SNAPSHOT, Windows 7 x64

Please provide any additional information below.

DeferSupportingClassLoader does not override ClassLoader.findResources(), which 
can cause classes loaded by MockClassLoader to be unable to find resources via 
ClassLoader.findResources(...).

I have only seen this in situations that match the following criteria:

- A Spring context (ClassPathXmlApplicationContext) is being constructed by a 
junit unit test being run via PowerMockRunner
- The unit test is being run on a machine that lacks internet connectivity.
- The unit test run is being managed by Maven's surefire plugin, and is running 
in an isolated classloader (forkMode=none)

The problem occurs when Spring attempts to load the various xsds to parse its 
context configuration xml file.  It attempts to find special mappings files on 
the classpath named "META-INF/spring.schemas" - these mapping files map the 
various spring xsd namespaces to xsds stored in various spring jars on the 
classpath.  If it cannot find these spring.schemas files, it will fall back and 
attempt to load the xsds over the network using their urls.  If the machine is 
on a network that cannot access the internet, loading the application context 
configuration file will fail (as will the unit test).

I have attached an example project (powermock-resource-bug-example.zip) that 
demonstrates this error.  With the current 1.4.12-SNAPSHOT build of powermock 
(and, I suspect, all prior releases), running the unit test in this project 
will result in Spring going out on the network and downloading the xsd.  If you 
tweak your hosts file to point www.springframework.org to 127.0.0.1, you should 
see the unit test fail.

I have also attached a fix for this (findResources.diff) to this bug report, as 
well as two new unit tests that verify that findResource() and findResources() 
are both properly deferred to the deferTo ClassLoader.

Original issue reported on code.google.com by wrussell...@gmail.com on 17 Mar 2012 at 4:06

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the very detailed description and a big thanks for the patch. I've 
applied it to trunk now and hopefully I'll find time to release a new version 
of PowerMock pretty soon.

Original comment by johan.ha...@gmail.com on 26 Mar 2012 at 8:55