Hi Charles,

I guess your unit test never fork a new JVM for testing.
For the code you just showed, I can tell you doesn't setup the bus for the 
JaxWsProxyFactoryBean, so CXF will try to use the default Bus which could be 
static variable. When you run the two unit test one by one, the first Bus's 
configuration context (which is could be linked to the Spring Application 
Context of the first Unit test) could be closed, but the second unit test wants 
to reuse the Bus.

To fix the issue, you can change the mvn surefire plugin to fork a new JVM per 
test or clean up the Bus after the test by call the below method
BusFactory.setDefaultBus(null);


--  
Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) 
(English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem





On Wednesday, January 16, 2013 at 4:37 PM, Charles Moulliard wrote:

> Hi Willem,
>  
> I remember this point that we have discussed in the past and here was the
> response coming from Dan Kulp but nevertheless, I use here 2 separate Unit
> Test classes, each one has a Unit Test, each one create a different
> camel-cxf endpoint 9090 & 9191
>  
> Code
>  
> public class WebServiceAuthenticateCustomerTest extends
> CamelSpringTestSupport {
>  
> // should be the same address as we have in our route
> private static final String URL = "
> http://localhost:9090/training/WebService";;
>  
> protected CamelContext camel;
> protected static JaxWsProxyFactoryBean factory;
>  
> @Override
> protected AbstractXmlApplicationContext createApplicationContext() {
> return new
> ClassPathXmlApplicationContext("/META-INF/spring/CamelContext1.xml");
> }
>  
> protected static CustomerService createCXFClient(String url) {
>  
> List<Interceptor<? extends Message>> outInterceptors = new
> ArrayList<Interceptor<? extends Message>>();
>  
> // Define WSS4j properties for flow outgoing
> Map<String, Object> outProps = new HashMap<String, Object>();
> outProps.put("action", "UsernameToken Timestamp");
>  
> /* CONFIG USING PASSWORD DIGEST
> outProps.put("passwordType", "PasswordDigest");
> outProps.put("user", "jim");
> outProps.put("passwordCallbackClass",
> "com.redhat.fuse.example.camel.UTPasswordCallback");
> */
>  
> // CONFIG WITH CLEAR PASSWORD
> outProps.put("passwordType", "PasswordText");
> outProps.put("user", "charles");
> outProps.put("passwordCallbackClass",
> "com.redhat.fuse.example.camel.UTPasswordCallback");
>  
> WSS4JOutInterceptor wss4j = new WSS4JOutInterceptor(outProps);
>  
> // Add LoggingOutInterceptor
> LoggingOutInterceptor loggingOutInterceptor = new
> LoggingOutInterceptor();
>  
> outInterceptors.add(wss4j);
> outInterceptors.add(loggingOutInterceptor);
>  
> // we use CXF to create a client for us as its easier than JAXWS
> and works
> factory = new JaxWsProxyFactoryBean();
> factory.setOutInterceptors(outInterceptors);
> factory.setServiceClass(CustomerService.class);
> factory.setAddress(url);
> return (CustomerService) factory.create();
> }
>  
> @Test
> public void testGetAllCustomers() throws Exception {
>  
> String client = "Fuse";
>  
> // Create Get Customer By Name
> GetCustomerByName req = new GetCustomerByName();
> req.setName(client);
>  
> // create the webservice client and send the request
> String url = context.resolvePropertyPlaceholders(URL);
> CustomerService customerService = createCXFClient(url);
>  
> GetCustomerByNameResponse result =
> customerService.getCustomerByName(req);
> System.out.println(">>> Response : " + result);
>  
> // Assert get Fuse customer
> assertEquals("Fuse", result.getReturn().get(0).getName());
> assertEquals("FuseSource
> Office",result.getReturn().get(0).getAddress().get(0));
>  
> assertEquals(CustomerType.BUSINESS,result.getReturn().get(0).getType());
>  
> Thread.sleep(60000);
>  
> }
> }
>  
> Dan Kulp suggestions
>  
> It's a semi-Jetty issue. If you use Keep-Alive connections (the default),
> Jetty has issues shutting down the port for some reason. It holds the
> port in some sort of strange semi-open state that accepts connections but
> doesn't properly process things. Never really had time to investigate any
> potential workarounds or fixes.
>  
> Couple of solutions:
> 1) Turn off the keep-alives in the connections
>  
> 2) Put a "Thread.sleep(60000)" between the tests. (might just be 10000)
> to wait for the keep-alive to timeout. Yea, not recommended, but thought
> I'd mention it. :-)
>  
> 3) Set the System property:
> "org.apache.cxf.transports.http_jetty.DontClosePort." + port
> (port is the port number you are using) to true.
>  
> which tells CXF to just keep the port open and not make any attempt to
> close it. Thus, when the second server starts up, it just registers on
> the already running instance. This is what we do for all the Camel and
> CXF unit/system tests.
>  
>  
>  
>  
> On Wed, Jan 16, 2013 at 9:15 AM, Willem Jiang <willem.ji...@gmail.com 
> (mailto:willem.ji...@gmail.com)>wrote:
>  
> > There is an known issue that jetty server will not accept the request if
> > you restart it in the same JVM , due to the HTTP keep alive. You may
> > consider to start the application context per test class.
> >  
> > 发自我的 iPhone
> >  
> > 在 2013-1-16,下午2:50,Charles Moulliard <ch0...@gmail.com 
> > (mailto:ch0...@gmail.com)> 写道:
> >  
> > > Hi,
> > >  
> > > I have 2 unit tests that I can run perfectly but when they are run during
> > > mvn test phase, one of the two fails for the following reason
> > >  
> > > - Both class extends extends CamelSpringTestSupport
> > > - They override createApplicationContext()
> > >  
> > > Could it be related to the timeout issue with Jetty Server when it has
> > been
> > > created in a previous Test but within same JVM ?
> > >  
> > > java.lang.IllegalStateException: BeanFactory not initialized or already
> > > closed - call 'refresh' before accessing beans via the ApplicationContext
> > > at
> >  
> >  
> > org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:171)
> > > at
> >  
> >  
> > org.springframework.context.support.AbstractApplicationContext.containsBean(AbstractApplicationContext.java:1091)
> > > at
> >  
> >  
> > org.apache.cxf.configuration.spring.ConfigurerImpl.configureBean(ConfigurerImpl.java:145)
> > > at
> >  
> >  
> > org.apache.cxf.configuration.spring.ConfigurerImpl.configureBean(ConfigurerImpl.java:115)
> > > at
> >  
> >  
> > org.apache.cxf.frontend.ClientProxyFactoryBean.configureObject(ClientProxyFactoryBean.java:104)
> > > at
> >  
> >  
> > org.apache.cxf.frontend.ClientProxyFactoryBean.create(ClientProxyFactoryBean.java:131)
> > > at
> >  
> >  
> > org.apache.cxf.jaxws.JaxWsProxyFactoryBean.create(JaxWsProxyFactoryBean.java:156)
> > > at
> >  
> >  
> > com.redhat.fuse.example.camel.WebServiceAuthenticateCustomerUsingRealmTest.createCXFClient(WebServiceAuthenticateCustomerUsingRealmTest.java:73)
> > > at
> >  
> >  
> > com.redhat.fuse.example.camel.WebServiceAuthenticateCustomerUsingRealmTest.testGetAllCustomers(WebServiceAuthenticateCustomerUsingRealmTest.java:87)
> > > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> > > at
> >  
> >  
> > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> > > at
> >  
> >  
> > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> > > at java.lang.reflect.Method.invoke(Method.java:597)
> > > at
> >  
> >  
> > org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
> > > at
> >  
> >  
> > org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
> > > at
> >  
> >  
> > org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
> > > at
> >  
> >  
> > org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
> > > at
> >  
> >  
> > org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
> > > at
> >  
> >  
> > org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
> > > at org.junit.rules.TestWatchman$1.evaluate(TestWatchman.java:48)
> > > at
> >  
> >  
> > org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
> > >  
> > > Regards
> > >  
> > > --
> > > Charles Moulliard
> > > Apache Committer / Sr. Enterprise Architect (RedHat)
> > > Twitter : @cmoulliard | Blog : http://cmoulliard.blogspot.com
> >  
>  
>  
>  
>  
>  
> --  
> Charles Moulliard
> Apache Committer / Sr. Enterprise Architect (RedHat)
> Twitter : @cmoulliard | Blog : http://cmoulliard.blogspot.com



Reply via email to