Reputation: 639
I am running a Junit (SpringJUnit4ClassRunner) to access some spring beans in my integration test.
I need to load a few xml files via @ContextConfiguration, those files are deployed by a external approach to my Tomcat directly via a jar file. -- In other words, these files are in:
my_tomcat_home_path/webapps//WEB-INF/lib/external.jar
such as
my_tomcat_home_path/webapps//WEB-INF/lib/external.jar/a.suffix.xml
my_tomcat_home_path/webapps//WEB-INF/lib/external.jar/b.suffix.xml
...
I put Tomcat path into my eclipse classpath, then if I also include the that into my eclipse classpath, then the following code in my JUnit works
@ContextConfiguration(locations = {"classpath:*suffix.xml"})
However, if I don't put that into my eclipse classpath (because the jar file name may change from time to time), then my following code does NOT work (but Tomcat home "" is still in eclipse classpath):
@ContextConfiguration(locations = {"classpath:/webapps/<my_app>/WEB-INF/lib/external.jar/*suffix.xml"})
Could somebody please help me out?
Thanks a lot!!
Additional try -------------------------------
I tried to use the package path inside the jar, instead of using the external.jar itself. The the code looks like the following:
@ContextConfiguration(locations = {"classpath:/webapps/<my_app>/WEB-INF/lib/path_inside_the_external_jar/*suffix.xml"})
In this case, I no longer get "XML not found because file does not exist" problem. Instead, I get the following errors:
Caused by: java.io.FileNotFoundException: class path resource [webapps/my_app/WEB-INF/lib/path_inside_the_external_jar/] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:163)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.isJarResource(PathMatchingResourcePatternResolver.java:406)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:338)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:276)
at org.springframework.context.support.AbstractApplicationContext.getResources(AbstractApplicationContext.java:1018)
at org.springframework.context.support.GenericApplicationContext.getResources(GenericApplicationContext.java:192)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:177)
... 23 more
Upvotes: 2
Views: 8293
Reputation: 639
Thanks a lot to Didxga's help!!
I tried to remove the "/webapps//WEB-INF/lib", and only leave the package path to the *suffix.xml (which are actually *.hbm.xml), then the @ContextConfiguration(locations looks like the following:
"classpath:/internal_path_within_jar/*hbm.xml"
at first, which is still not working.
Afterward, I tried the modify the classpath pattern and the following code ALMOST works
"classpath*:/internal_path_within_jar/*hbm.xml"
This time, since hbm.xml files are loaded, and I get the following error:
Caused by: java.net.UnknownHostException: hibernate.sourceforge.net
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:411)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:525)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:208)
at sun.net.www.http.HttpClient.New(HttpClient.java:291)
at sun.net.www.http.HttpClient.New(HttpClient.java:310)
However, I think now it is a different problem because it is hibernate DTD load problem... Maybe I should NOT load hbm.xml in this approach... I was forced to do so, because those hbm.xml files are generated during the deployment progress of my application and will only appear in the Tomcat's websapp/my_app/external.jar and NOwhere else...
Hmm, so I need work on that part... but how to include a jar into my eclipse path with changing name but a certain pattern? Such as *-dao.jar. OMG...
Anyway, I suppose the problem reported in this post should be resolved.
Thanks a lot, didxga!
Upvotes: 2