[jboss-svn-commits] JBoss Common SVN: r4327 - in arquillian/trunk: impl-base/src/test/java/org/jboss/arquillian/impl and 4 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Apr 24 14:55:42 EDT 2010
Author: aslak
Date: 2010-04-24 14:55:41 -0400 (Sat, 24 Apr 2010)
New Revision: 4327
Added:
arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/DynamicServiceLoaderTestCase.java
arquillian/trunk/impl-base/src/test/resources/META-INF/
arquillian/trunk/impl-base/src/test/resources/META-INF/services/
arquillian/trunk/impl-base/src/test/resources/META-INF/services/org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service
arquillian/trunk/impl-base/src/test/resources/META-INF/services/org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service2
Modified:
arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/DynamicServiceLoader.java
arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/DefaultServiceLoader.java
Log:
ARQ-117 Added more descriptive exception messages
Modified: arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/DynamicServiceLoader.java
===================================================================
--- arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/DynamicServiceLoader.java 2010-04-24 14:36:45 UTC (rev 4326)
+++ arquillian/trunk/impl-base/src/main/java/org/jboss/arquillian/impl/DynamicServiceLoader.java 2010-04-24 18:55:41 UTC (rev 4327)
@@ -17,6 +17,7 @@
package org.jboss.arquillian.impl;
import java.util.Collection;
+import java.util.logging.Logger;
import org.jboss.arquillian.spi.ServiceLoader;
import org.jboss.arquillian.spi.util.DefaultServiceLoader;
@@ -29,24 +30,81 @@
*/
public class DynamicServiceLoader implements ServiceLoader
{
+ private static Logger logger = Logger.getLogger(DynamicServiceLoader.class.getName());
+ /* (non-Javadoc)
+ * @see org.jboss.arquillian.spi.ServiceLoader#all(java.lang.Class)
+ */
public <T> Collection<T> all(Class<T> serviceClass)
{
return DefaultServiceLoader.load(serviceClass).getProviders();
}
+ /* (non-Javadoc)
+ * @see org.jboss.arquillian.spi.ServiceLoader#onlyOne(java.lang.Class)
+ */
public <T> T onlyOne(Class<T> serviceClass)
{
Collection<T> providers = DefaultServiceLoader.load(serviceClass).getProviders();
+ verifyOnlyOneOrSameImplementation(serviceClass, providers);
+
+ return providers.iterator().next();
+ }
+
+ private void verifyOnlyOneOrSameImplementation(Class<?> serviceClass, Collection<?> providers)
+ {
if(providers == null || providers.size() == 0)
{
- throw new IllegalStateException("No implementation found for: " + serviceClass.getName() + ", check classpath");
+ throw new IllegalStateException(
+ "No implementation found for " +
+ serviceClass.getName() + ", please check your classpath");
}
if(providers.size() > 1)
{
- throw new IllegalStateException("Mutiple implementations found for: " + serviceClass.getName() + ", check classpath");
+ // verify that they all point to the same implementation, if not throw exception
+ verifySameImplementation(serviceClass, providers);
}
- return providers.iterator().next();
}
-
+
+ private void verifySameImplementation(Class<?> serviceClass, Collection<?> providers)
+ {
+ boolean providersAreTheSame = false;
+ Class<?> firstProvider = null;
+ for(Object provider : providers)
+ {
+ if(firstProvider == null)
+ {
+ // set the class to match
+ firstProvider = provider.getClass();
+ continue;
+ }
+ if(firstProvider == provider.getClass())
+ {
+ providersAreTheSame = true;
+ }
+ else
+ {
+ throw new IllegalStateException(
+ "More then one implementation found for "
+ + serviceClass.getName() + ", " +
+ "please check your classpath. The found implementations are " + toClassString(providers));
+ }
+ }
+ if(providersAreTheSame)
+ {
+ logger.warning(
+ "More then one reference to the same implementation was found for " +
+ serviceClass.getName() + ", please verify you classpath");
+ }
+ }
+
+ private String toClassString(Collection<?> providers)
+ {
+ StringBuilder sb = new StringBuilder();
+ for(Object provider : providers)
+ {
+ sb.append(provider.getClass().getName()).append(", ");
+ }
+ return sb.subSequence(0, sb.length()-2).toString();
+ }
}
Added: arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/DynamicServiceLoaderTestCase.java
===================================================================
--- arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/DynamicServiceLoaderTestCase.java (rev 0)
+++ arquillian/trunk/impl-base/src/test/java/org/jboss/arquillian/impl/DynamicServiceLoaderTestCase.java 2010-04-24 18:55:41 UTC (rev 4327)
@@ -0,0 +1,64 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.arquillian.impl;
+
+import java.util.Collection;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Verify the behavior of the Dynamic Service Loader
+ *
+ * @author <a href="mailto:aknutsen at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class DynamicServiceLoaderTestCase
+{
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldFailIfMultipleProvidersFound() throws Exception
+ {
+ new DynamicServiceLoader().onlyOne(Service.class);
+ }
+
+ @Test
+ public void shouldNotFailIfMultipleProvidersFoundPointingToSameImpl() throws Exception
+ {
+ Service2 service = new DynamicServiceLoader().onlyOne(Service2.class);
+
+ Assert.assertTrue(
+ "verify that a instance of Service2Impl was loaded",
+ service.getClass() == Service2Impl.class);
+ }
+
+ @Test
+ public void shouldLoadAllInstances() throws Exception {
+ Collection<Service> services = new DynamicServiceLoader().all(Service.class);
+
+ Assert.assertEquals(
+ "verify that all services where found and loaded",
+ 2, services.size());
+ }
+
+ public interface Service {}
+ public static class ServiceImpl1 implements Service {}
+ public static class ServiceImpl2 implements Service {}
+
+ public interface Service2 {}
+ public static class Service2Impl implements Service2 {}
+}
Added: arquillian/trunk/impl-base/src/test/resources/META-INF/services/org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service
===================================================================
--- arquillian/trunk/impl-base/src/test/resources/META-INF/services/org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service (rev 0)
+++ arquillian/trunk/impl-base/src/test/resources/META-INF/services/org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service 2010-04-24 18:55:41 UTC (rev 4327)
@@ -0,0 +1,2 @@
+org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$ServiceImpl1
+org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$ServiceImpl2
\ No newline at end of file
Added: arquillian/trunk/impl-base/src/test/resources/META-INF/services/org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service2
===================================================================
--- arquillian/trunk/impl-base/src/test/resources/META-INF/services/org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service2 (rev 0)
+++ arquillian/trunk/impl-base/src/test/resources/META-INF/services/org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service2 2010-04-24 18:55:41 UTC (rev 4327)
@@ -0,0 +1,2 @@
+org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service2Impl
+org.jboss.arquillian.impl.DynamicServiceLoaderTestCase$Service2Impl
\ No newline at end of file
Modified: arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/DefaultServiceLoader.java
===================================================================
--- arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/DefaultServiceLoader.java 2010-04-24 14:36:45 UTC (rev 4326)
+++ arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/DefaultServiceLoader.java 2010-04-24 18:55:41 UTC (rev 4327)
@@ -197,7 +197,7 @@
}
catch (ClassCastException e)
{
- throw new IllegalStateException("Extension " + line + " does not implement Extension");
+ throw new IllegalStateException(line + " does not implement " + expectedType);
}
Constructor<? extends S> constructor = serviceClass.getConstructor();
if(!constructor.isAccessible()) {
More information about the jboss-svn-commits
mailing list