Author: alessio.soldano(a)jboss.com
Date: 2011-02-24 07:41:01 -0500 (Thu, 24 Feb 2011)
New Revision: 13794
Added:
stack/cxf/trunk/modules/client/src/test/
stack/cxf/trunk/modules/client/src/test/java/
stack/cxf/trunk/modules/client/src/test/java/org/
stack/cxf/trunk/modules/client/src/test/java/org/jboss/
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/ProviderImplTest.java
Modified:
stack/cxf/trunk/modules/client/pom.xml
stack/cxf/trunk/modules/client/src/main/java/org/jboss/wsf/stack/cxf/client/ProviderImpl.java
Log:
[JBWS-3223] Minor refactoring of ProviderImpl + addition of unit tests
Modified: stack/cxf/trunk/modules/client/pom.xml
===================================================================
--- stack/cxf/trunk/modules/client/pom.xml 2011-02-24 09:31:39 UTC (rev 13793)
+++ stack/cxf/trunk/modules/client/pom.xml 2011-02-24 12:41:01 UTC (rev 13794)
@@ -222,6 +222,13 @@
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
@@ -280,6 +287,12 @@
</archive>
</configuration>
</plugin>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <failIfNoTests>false</failIfNoTests>
+ </configuration>
+ </plugin>
</plugins>
</build>
Modified:
stack/cxf/trunk/modules/client/src/main/java/org/jboss/wsf/stack/cxf/client/ProviderImpl.java
===================================================================
---
stack/cxf/trunk/modules/client/src/main/java/org/jboss/wsf/stack/cxf/client/ProviderImpl.java 2011-02-24
09:31:39 UTC (rev 13793)
+++
stack/cxf/trunk/modules/client/src/main/java/org/jboss/wsf/stack/cxf/client/ProviderImpl.java 2011-02-24
12:41:01 UTC (rev 13794)
@@ -79,15 +79,16 @@
@Override
public Endpoint createEndpoint(String bindingId, Object implementor) {
- ClassLoader origClassLoader = null;
+ ClassLoader origClassLoader = getContextClassLoader();
+ boolean restoreTCCL = false;
try
{
- origClassLoader = checkAndFixContextClassLoader();
+ restoreTCCL = checkAndFixContextClassLoader(origClassLoader);
return new DelegateEndpointImpl(super.createEndpoint(bindingId, implementor));
}
finally
{
- if (origClassLoader != null)
+ if (restoreTCCL)
setContextClassLoader(origClassLoader);
}
}
@@ -96,15 +97,16 @@
public Endpoint createEndpoint(String bindingId,
Object implementor,
WebServiceFeature ... features) {
- ClassLoader origClassLoader = null;
+ ClassLoader origClassLoader = getContextClassLoader();
+ boolean restoreTCCL = false;
try
{
- origClassLoader = checkAndFixContextClassLoader();
+ restoreTCCL = checkAndFixContextClassLoader(origClassLoader);
return new DelegateEndpointImpl(super.createEndpoint(bindingId, implementor,
features));
}
finally
{
- if (origClassLoader != null)
+ if (restoreTCCL)
setContextClassLoader(origClassLoader);
}
}
@@ -113,10 +115,11 @@
@Override
public ServiceDelegate createServiceDelegate(URL url, QName qname, Class cls)
{
- ClassLoader origClassLoader = null;
+ ClassLoader origClassLoader = getContextClassLoader();
+ boolean restoreTCCL = false;
try
{
- origClassLoader = checkAndFixContextClassLoader();
+ restoreTCCL = checkAndFixContextClassLoader(origClassLoader);
//we override this method to prevent using the default bus when the current
//thread is not already associated to a bus. In those situations we create
//a new bus from scratch instead and link that to the thread.
@@ -129,7 +132,7 @@
}
finally
{
- if (origClassLoader != null)
+ if (restoreTCCL)
setContextClassLoader(origClassLoader);
}
}
@@ -139,10 +142,11 @@
public ServiceDelegate createServiceDelegate(URL wsdlDocumentLocation, QName
serviceName, Class serviceClass,
WebServiceFeature... features)
{
- ClassLoader origClassLoader = null;
+ ClassLoader origClassLoader = getContextClassLoader();
+ boolean restoreTCCL = false;
try
{
- origClassLoader = checkAndFixContextClassLoader();
+ restoreTCCL = checkAndFixContextClassLoader(origClassLoader);
//we override this method to prevent using the default bus when the current
//thread is not already associated to a bus. In those situations we create
//a new bus from scratch instead and link that to the thread.
@@ -155,7 +159,7 @@
}
finally
{
- if (origClassLoader != null)
+ if (restoreTCCL)
setContextClassLoader(origClassLoader);
}
}
@@ -163,11 +167,10 @@
/**
* Ensure the current context classloader can load this ProviderImpl class.
*
- * @return The original classloader or null if it's not been changed
+ * @return true if the TCCL has been changed, false otherwise
*/
- private static ClassLoader checkAndFixContextClassLoader()
+ static boolean checkAndFixContextClassLoader(ClassLoader origClassLoader)
{
- ClassLoader origClassLoader = getContextClassLoader();
try
{
origClassLoader.loadClass(ProviderImpl.class.getName());
@@ -189,9 +192,9 @@
//then setup a new TCCL having visibility over both the client path (JBossWS
//client module on AS7) and the the former TCCL (i.e. the deployment
classloader)
setContextClassLoader(new DelegateClassLoader(clientClassLoader,
origClassLoader));
- return origClassLoader;
+ return true;
}
- return null;
+ return false;
}
/**
@@ -247,7 +250,7 @@
* that sets the TCCL before doing publish.
*
*/
- private static final class DelegateEndpointImpl extends Endpoint
+ static final class DelegateEndpointImpl extends Endpoint
{
private Endpoint delegate;
@@ -271,15 +274,16 @@
@Override
public void publish(String address)
{
- ClassLoader origClassLoader = null;
+ ClassLoader origClassLoader = getContextClassLoader();
+ boolean restoreTCCL = false;
try
{
- origClassLoader = checkAndFixContextClassLoader();
+ restoreTCCL = checkAndFixContextClassLoader(origClassLoader);
delegate.publish(address);
}
finally
{
- if (origClassLoader != null)
+ if (restoreTCCL)
setContextClassLoader(origClassLoader);
}
}
@@ -287,15 +291,16 @@
@Override
public void publish(Object serverContext)
{
- ClassLoader origClassLoader = null;
+ ClassLoader origClassLoader = getContextClassLoader();
+ boolean restoreTCCL = false;
try
{
- origClassLoader = checkAndFixContextClassLoader();
+ restoreTCCL = checkAndFixContextClassLoader(origClassLoader);
delegate.publish(serverContext);
}
finally
{
- if (origClassLoader != null)
+ if (restoreTCCL)
setContextClassLoader(origClassLoader);
}
}
@@ -361,7 +366,7 @@
}
}
- private static final class DelegateClassLoader extends SecureClassLoader
+ static final class DelegateClassLoader extends SecureClassLoader
{
private ClassLoader delegate;
@@ -411,7 +416,10 @@
final ArrayList<Enumeration<URL>> foundResources = new
ArrayList<Enumeration<URL>>();
foundResources.add(delegate.getResources(name));
- foundResources.add(parent.getResources(name));
+ if (parent != null)
+ {
+ foundResources.add(parent.getResources(name));
+ }
return new Enumeration<URL>()
{
Added:
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/ProviderImplTest.java
===================================================================
---
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/ProviderImplTest.java
(rev 0)
+++
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/ProviderImplTest.java 2011-02-24
12:41:01 UTC (rev 13794)
@@ -0,0 +1,248 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.stack.cxf.client;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executor;
+
+import javax.xml.transform.Source;
+import javax.xml.ws.Binding;
+import javax.xml.ws.Endpoint;
+import javax.xml.ws.EndpointReference;
+import javax.xml.ws.spi.Provider;
+
+import junit.framework.TestCase;
+
+import org.jboss.wsf.stack.cxf.client.ProviderImpl.DelegateClassLoader;
+import org.jboss.wsf.stack.cxf.client.ProviderImpl.DelegateEndpointImpl;
+import org.w3c.dom.Element;
+
+/**
+ * A test case for jbossws-cxf javax.xml.ws.spi.Provider implementation
+ *
+ * @author alessio.soldano(a)jboss.com
+ * @since 24-Feb-2011
+ *
+ */
+public class ProviderImplTest extends TestCase
+{
+ public void testGetProvider()
+ {
+ //just check we get the jbossws-cxf provider impl when the default maven tccl is
set
+ Provider providerImpl = Provider.provider();
+ assertTrue(providerImpl instanceof ProviderImpl);
+ }
+
+ public void testCheckAndFixContextClassLoaderWithDefaultMavenClassLoader()
+ {
+ ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+ try
+ {
+ assertNotNull(tccl);
+ assertFalse(ProviderImpl.checkAndFixContextClassLoader(tccl));
+ verifyClassLoaderCanLoadProviderImpl(tccl);
+ assertEquals(tccl, Thread.currentThread().getContextClassLoader());
+ }
+ finally
+ {
+ Thread.currentThread().setContextClassLoader(tccl);
+ }
+ }
+
+ public void testCheckAndFixContextClassLoaderWithNullClassLoader()
+ {
+ ClassLoader orig = Thread.currentThread().getContextClassLoader();
+ try
+ {
+ Thread.currentThread().setContextClassLoader(null);
+ ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+ assertTrue(ProviderImpl.checkAndFixContextClassLoader(tccl));
+ ClassLoader modifiedTccl = Thread.currentThread().getContextClassLoader();
+ assertNotNull(modifiedTccl);
+ assertTrue(modifiedTccl instanceof DelegateClassLoader);
+ verifyClassLoaderCanLoadProviderImpl(modifiedTccl);
+ }
+ finally
+ {
+ Thread.currentThread().setContextClassLoader(orig);
+ }
+ }
+
+ public void testCheckAndFixContextClassLoaderWithTestClassLoader()
+ {
+ ClassLoader orig = Thread.currentThread().getContextClassLoader();
+ try
+ {
+ ClassLoader testCl = new TestClassLoader();
+ assertFalse(canLoad(testCl, ProviderImpl.class.getName()));
+ Thread.currentThread().setContextClassLoader(testCl);
+ assertTrue(ProviderImpl.checkAndFixContextClassLoader(testCl));
+ ClassLoader modifiedTccl = Thread.currentThread().getContextClassLoader();
+ assertNotNull(modifiedTccl);
+ assertTrue(modifiedTccl instanceof DelegateClassLoader);
+ verifyClassLoaderCanLoadProviderImpl(modifiedTccl);
+ }
+ finally
+ {
+ Thread.currentThread().setContextClassLoader(orig);
+ }
+ }
+
+ public void testEndpointImplPublishCorrectlySetsTCCL()
+ {
+ ClassLoader orig = Thread.currentThread().getContextClassLoader();
+ try
+ {
+ Endpoint ep1 = new DelegateEndpointImpl(new TestEndpoint());
+ ep1.publish(new Integer(1)); //just to ensure the publish(Object arg) is used
+ ep1.publish("foo");
+ Thread.currentThread().setContextClassLoader(new TestClassLoader());
+ Endpoint ep2 = new DelegateEndpointImpl(new TestEndpoint());
+ ep2.publish(new Integer(1)); //just to ensure the publish(Object arg) is used
+ ep2.publish("foo");
+ }
+ finally
+ {
+ Thread.currentThread().setContextClassLoader(orig);
+ }
+ }
+
+ private static boolean canLoad(ClassLoader cl, String className)
+ {
+ try
+ {
+ cl.loadClass(className);
+ return true;
+ }
+ catch (Exception e)
+ {
+ return false;
+ }
+ }
+
+ private static void verifyClassLoaderCanLoadProviderImpl(ClassLoader cl)
+ {
+ final String impl = ProviderImpl.class.getName();
+ boolean canLoad = canLoad(cl, impl);
+ assertTrue("ClassLoader " + cl + " was not able to load " +
impl + "!", canLoad);
+ }
+
+ //------ Test classes ------
+
+ private static final class TestClassLoader extends ClassLoader
+ {
+ public TestClassLoader()
+ {
+ super(null);
+ }
+
+ @Override
+ public Class<?> loadClass(final String className) throws
ClassNotFoundException
+ {
+ throw new ClassNotFoundException("TestClassLoader does not load
anything!");
+ }
+ }
+
+ private static final class TestEndpoint extends Endpoint
+ {
+ @Override
+ public Binding getBinding()
+ {
+ return null;
+ }
+
+ @Override
+ public Object getImplementor()
+ {
+ return null;
+ }
+
+ @Override
+ public void publish(String address)
+ {
+
verifyClassLoaderCanLoadProviderImpl(Thread.currentThread().getContextClassLoader());
+ }
+
+ @Override
+ public void publish(Object serverContext)
+ {
+
verifyClassLoaderCanLoadProviderImpl(Thread.currentThread().getContextClassLoader());
+ }
+
+ @Override
+ public void stop()
+ {
+ }
+
+ @Override
+ public boolean isPublished()
+ {
+ return false;
+ }
+
+ @Override
+ public List<Source> getMetadata()
+ {
+ return null;
+ }
+
+ @Override
+ public void setMetadata(List<Source> metadata)
+ {
+ }
+
+ @Override
+ public Executor getExecutor()
+ {
+ return null;
+ }
+
+ @Override
+ public void setExecutor(Executor executor)
+ {
+ }
+
+ @Override
+ public Map<String, Object> getProperties()
+ {
+ return null;
+ }
+
+ @Override
+ public void setProperties(Map<String, Object> properties)
+ {
+ }
+
+ @Override
+ public EndpointReference getEndpointReference(Element... referenceParameters)
+ {
+ return null;
+ }
+
+ @Override
+ public <T extends EndpointReference> T getEndpointReference(Class<T>
clazz, Element... referenceParameters)
+ {
+ return null;
+ }
+ }
+}