Repository: aries-rsa Updated Branches: refs/heads/master [created] 5f4c6604a
Allow multiple interfaces Project: http://git-wip-us.apache.org/repos/asf/aries-rsa/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-rsa/commit/84f6be48 Tree: http://git-wip-us.apache.org/repos/asf/aries-rsa/tree/84f6be48 Diff: http://git-wip-us.apache.org/repos/asf/aries-rsa/diff/84f6be48 Branch: refs/heads/master Commit: 84f6be48ae20645e9c54ab3c0dee0b339c9dfbff Parents: df71ec1 Author: Christian Schneider <[email protected]> Authored: Mon Mar 7 14:54:16 2016 +0100 Committer: Christian Schneider <[email protected]> Committed: Mon Mar 7 14:54:16 2016 +0100 ---------------------------------------------------------------------- .../cxf/dosgi/dsw/api/DistributionProvider.java | 5 +- .../dsw/handlers/CXFDistributionProvider.java | 6 +- .../JaxRSPojoConfigurationTypeHandler.java | 6 +- .../handlers/PojoConfigurationTypeHandler.java | 6 +- .../handlers/WsdlConfigurationTypeHandler.java | 8 +- .../dsw/service/RemoteServiceAdminCore.java | 84 ++++----- .../cxf/dosgi/dsw/handlers/ClassUtilsTest.java | 134 ++++++++++++++ .../dsw/handlers/ClientServiceFactoryTest.java | 103 ----------- .../PojoConfigurationTypeHandlerTest.java | 10 +- .../dsw/service/ClientServiceFactoryTest.java | 90 ++++++++++ .../dosgi/dsw/service/EventProducerTest.java | 7 +- .../dsw/service/RemoteServiceAdminCoreTest.java | 179 +++++++++++++++---- .../cxf/dosgi/dsw/service/StringPlusTest.java | 63 +++++++ .../apache/cxf/dosgi/dsw/service/UtilsTest.java | 166 ----------------- .../cxf/dosgi/dsw/util/ClassUtilsTest.java | 134 -------------- 15 files changed, 500 insertions(+), 501 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/api/DistributionProvider.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/api/DistributionProvider.java b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/api/DistributionProvider.java index 9a8c0ca..c7328db 100644 --- a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/api/DistributionProvider.java +++ b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/api/DistributionProvider.java @@ -24,6 +24,7 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.remoteserviceadmin.EndpointDescription; +@SuppressWarnings("rawtypes") public interface DistributionProvider { String[] getSupportedTypes(); @@ -36,7 +37,7 @@ public interface DistributionProvider { */ Endpoint exportService(ServiceReference<?> sref, Map<String, Object> effectiveProperties, - String exportedInterface); + Class[] exportedInterfaces); /** * @param sref reference of the service offered to the requesting bundle @@ -46,7 +47,7 @@ public interface DistributionProvider { * @throws IntentUnsatisfiedException */ Object importEndpoint(BundleContext consumerContext, - Class<?>[] interfaces, + Class[] interfaces, EndpointDescription endpoint) throws IntentUnsatisfiedException; http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/CXFDistributionProvider.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/CXFDistributionProvider.java b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/CXFDistributionProvider.java index 0be2fbf..09f5a3e 100644 --- a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/CXFDistributionProvider.java +++ b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/CXFDistributionProvider.java @@ -72,14 +72,14 @@ public class CXFDistributionProvider implements DistributionProvider { @Override public Endpoint exportService(ServiceReference<?> sref, Map<String, Object> effectiveProperties, - String exportedInterface) { + Class[] exportedInterfaces) { List<String> configurationTypes = determineConfigurationTypes(effectiveProperties); DistributionProvider handler = getHandler(configurationTypes, effectiveProperties); - return handler != null ? handler.exportService(sref, effectiveProperties, exportedInterface) : null; + return handler != null ? handler.exportService(sref, effectiveProperties, exportedInterfaces) : null; } @Override - public Object importEndpoint(BundleContext consumerContext, Class<?>[] iClass, EndpointDescription endpoint) + public Object importEndpoint(BundleContext consumerContext, Class[] iClass, EndpointDescription endpoint) throws IntentUnsatisfiedException { List<String> configurationTypes = determineConfigTypesForImport(endpoint); DistributionProvider handler = getHandler(configurationTypes, endpoint.getProperties()); http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/JaxRSPojoConfigurationTypeHandler.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/JaxRSPojoConfigurationTypeHandler.java b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/JaxRSPojoConfigurationTypeHandler.java index e67bc31..3068e8b 100644 --- a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/JaxRSPojoConfigurationTypeHandler.java +++ b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/JaxRSPojoConfigurationTypeHandler.java @@ -56,7 +56,7 @@ public class JaxRSPojoConfigurationTypeHandler extends AbstractPojoConfiguration } public Object importEndpoint(BundleContext consumerContext, - Class<?>[] interfaces, + Class[] interfaces, EndpointDescription endpoint) { Class<?> iClass = interfaces[0]; String address = getPojoAddress(endpoint, iClass); @@ -111,12 +111,12 @@ public class JaxRSPojoConfigurationTypeHandler extends AbstractPojoConfiguration public Endpoint exportService(ServiceReference<?> sref, Map<String, Object> sd, - String exportedInterface) throws IntentUnsatisfiedException { + Class[] exportedInterfaces) throws IntentUnsatisfiedException { BundleContext callingContext = sref.getBundle().getBundleContext(); Object serviceBean = callingContext.getService(sref); - Class<?> iClass = ClassUtils.getInterfaceClass(serviceBean, exportedInterface); String contextRoot = getServletContextRoot(sd); String address; + Class<?> iClass = exportedInterfaces[0]; if (contextRoot == null) { address = getServerAddress(sd, iClass); } else { http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandler.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandler.java b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandler.java index 0451a8e..129b579 100644 --- a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandler.java +++ b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandler.java @@ -55,7 +55,7 @@ public class PojoConfigurationTypeHandler extends AbstractPojoConfigurationTypeH } public Object importEndpoint(BundleContext consumerContext, - Class<?>[] interfaces, + Class[] interfaces, EndpointDescription endpoint) throws IntentUnsatisfiedException { Class<?> iClass = interfaces[0]; Map<String, Object> sd = endpoint.getProperties(); @@ -91,10 +91,10 @@ public class PojoConfigurationTypeHandler extends AbstractPojoConfigurationTypeH public Endpoint exportService(ServiceReference<?> sref, Map<String, Object> sd, - String exportedInterface) throws IntentUnsatisfiedException { + Class[] exportedInterfaces) throws IntentUnsatisfiedException { BundleContext callingContext = sref.getBundle().getBundleContext(); Object serviceBean = callingContext.getService(sref); - Class<?> iClass = ClassUtils.getInterfaceClass(serviceBean, exportedInterface); + Class<?> iClass = exportedInterfaces[0]; String address = getPojoAddress(sd, iClass); ServerFactoryBean factory = createServerFactoryBean(sd, iClass); factory.setDataBinding(getDataBinding(sd, iClass)); http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/WsdlConfigurationTypeHandler.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/WsdlConfigurationTypeHandler.java b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/WsdlConfigurationTypeHandler.java index 9384958..dd477b6 100644 --- a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/WsdlConfigurationTypeHandler.java +++ b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/WsdlConfigurationTypeHandler.java @@ -55,7 +55,7 @@ public class WsdlConfigurationTypeHandler extends AbstractPojoConfigurationTypeH } public Object importEndpoint(BundleContext consumerContext, - Class<?>[] interfaces, + Class[] interfaces, EndpointDescription endpoint) { Class<?> iClass = interfaces[0]; String wsdlAddressProp = getWsdlAddress(endpoint, iClass); @@ -102,10 +102,10 @@ public class WsdlConfigurationTypeHandler extends AbstractPojoConfigurationTypeH public Endpoint exportService(ServiceReference<?> sref, Map<String, Object> sd, - String exportedInterface) { + Class[] exportedInterfaces) { BundleContext callingContext = sref.getBundle().getBundleContext(); Object serviceBean = callingContext.getService(sref); - Class<?> iClass = ClassUtils.getInterfaceClass(serviceBean, exportedInterface); + Class<?> iClass = exportedInterfaces[0]; String location = OsgiUtils.getProperty(sd, Constants.WSDL_LOCATION); if (location == null) { throw new RuntimeException("WSDL location property is unavailable"); @@ -131,7 +131,7 @@ public class WsdlConfigurationTypeHandler extends AbstractPojoConfigurationTypeH factory.setServiceClass(iClass); factory.setAddress(address != null ? address : "/"); factory.getServiceFactory().setDataBinding(databinding); - factory.setServiceBean(callingContext.getService(sref)); + factory.setServiceBean(serviceBean); addWsInterceptorsFeaturesProps(factory, callingContext, sd); http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCore.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCore.java b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCore.java index 2a3469c..9f4e5fa 100644 --- a/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCore.java +++ b/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCore.java @@ -98,39 +98,48 @@ public class RemoteServiceAdminCore implements RemoteServiceAdmin { } Map<String, Object> key = makeKey(serviceProperties); - List<String> interfaces = getInterfaces(serviceProperties); + List<String> interfaceNames = getInterfaceNames(serviceProperties); if (isImportedService(serviceReference)) { return Collections.emptyList(); } - List<ExportRegistration> exportRegs = getExistingRegs(key, interfaces); + List<ExportRegistration> exportRegs = getExistingAndLock(key, interfaceNames); if (exportRegs != null) { return exportRegs; } try { - // do the export - exportRegs = exportInterfaces(interfaces, serviceReference, serviceProperties); - if (!exportRegs.isEmpty()) { - // enlist initial export registrations in global list of exportRegistrations - synchronized (exportedServices) { - exportedServices.put(key, new ArrayList<ExportRegistration>(exportRegs)); - } - eventProducer.publishNotification(exportRegs); - } + ExportRegistration exportReg = exportService(interfaceNames, serviceReference, serviceProperties); + exportRegs = new ArrayList<>(); + exportRegs.add(exportReg); + store(key, exportRegs); return exportRegs; } finally { + unlock(key); + } + } + + private void store(Map<String, Object> key, List<ExportRegistration> exportRegs) { + if (!exportRegs.isEmpty()) { + // enlist initial export registrations in global list of exportRegistrations synchronized (exportedServices) { - if (exportedServices.get(key) == exportInProgress) { - exportedServices.remove(key); - } - exportedServices.notifyAll(); // in any case, always notify waiting threads + exportedServices.put(key, new ArrayList<ExportRegistration>(exportRegs)); + } + eventProducer.publishNotification(exportRegs); + } + } + + private void unlock(Map<String, Object> key) { + synchronized (exportedServices) { + if (exportedServices.get(key) == exportInProgress) { + exportedServices.remove(key); } + exportedServices.notifyAll(); // in any case, always notify waiting threads } } - private List<ExportRegistration> getExistingRegs(Map<String, Object> key, List<String> interfaces) { + private List<ExportRegistration> getExistingAndLock(Map<String, Object> key, List<String> interfaces) { synchronized (exportedServices) { // check if it is already exported... Collection<ExportRegistration> existingRegs = exportedServices.get(key); @@ -158,26 +167,25 @@ public class RemoteServiceAdminCore implements RemoteServiceAdmin { return null; } - private List<ExportRegistration> exportInterfaces(List<String> interfaces, + private ExportRegistration exportService(List<String> interfaceNames, ServiceReference<?> serviceReference, Map<String, Object> serviceProperties) { - LOG.info("interfaces selected for export: " + interfaces); - List<ExportRegistration> exportRegs = new ArrayList<ExportRegistration>(1); - for (String iface : interfaces) { - ExportRegistrationImpl exportRegistration; - try { - LOG.info("creating server for interface " + iface); - Endpoint endpoint = provider.exportService(serviceReference, serviceProperties, iface); - exportRegistration = new ExportRegistrationImpl(serviceReference, endpoint, this); - LOG.info("created server for interface " + iface); - } catch (Exception e) { - LOG.debug("failed to create server for interface " + iface, e); - serviceProperties.put(RemoteConstants.ENDPOINT_ID, "failed"); - serviceProperties.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, "none"); - exportRegistration = new ExportRegistrationImpl(this, e); - } - exportRegs.add(exportRegistration); + LOG.info("interfaces selected for export: " + interfaceNames); + try { + Class<?>[] interfaces = getInterfaces(interfaceNames, serviceReference.getBundle()); + Endpoint endpoint = provider.exportService(serviceReference, serviceProperties, interfaces); + return new ExportRegistrationImpl(serviceReference, endpoint, this); + } catch (Exception e) { + return new ExportRegistrationImpl(this, e); } - return exportRegs; + } + + private Class<?>[] getInterfaces(List<String> interfaceNames, + Bundle serviceBundle) throws ClassNotFoundException { + List<Class<?>> interfaces = new ArrayList<>(); + for (String interfaceName : interfaceNames) { + interfaces.add(serviceBundle.loadClass(interfaceName)); + } + return interfaces.toArray(new Class[]{}); } /** @@ -190,7 +198,7 @@ public class RemoteServiceAdminCore implements RemoteServiceAdmin { * @see org.osgi.framework.Constants#OBJECTCLASS * @see RemoteConstants#SERVICE_EXPORTED_INTERFACES */ - private List<String> getInterfaces(Map<String, Object> serviceProperties) { + private List<String> getInterfaceNames(Map<String, Object> serviceProperties) { String[] providedInterfaces = (String[])serviceProperties.get(org.osgi.framework.Constants.OBJECTCLASS); if (providedInterfaces == null || providedInterfaces.length == 0) { throw new IllegalArgumentException("service is missing the objectClass property"); @@ -327,12 +335,6 @@ public class RemoteServiceAdminCore implements RemoteServiceAdmin { LOG.info("No matching interfaces found for remote endpoint {}.", endpoint.getId()); return null; } - if (matchingInterfaces.size() > 1) { - LOG.info("More than one interface {} found for remote endpoint {}. This is not supported.", - endpoint.getInterfaces(), - endpoint.getId()); - return null; - } LOG.info("Importing service {} with interfaces {} using handler {}.", endpoint.getId(), endpoint.getInterfaces(), provider.getClass()); http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/ClassUtilsTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/ClassUtilsTest.java b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/ClassUtilsTest.java new file mode 100644 index 0000000..a0774be --- /dev/null +++ b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/ClassUtilsTest.java @@ -0,0 +1,134 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.cxf.dosgi.dsw.handlers; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.cxf.dosgi.dsw.util.Provider; +import org.easymock.EasyMock; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; + +public class ClassUtilsTest extends TestCase { + + public void testGetInterfaceClass() { + assertEquals(String.class, + ClassUtils.getInterfaceClass("Hello", "java.lang.String")); + assertNull(ClassUtils.getInterfaceClass("Hello", "java.lang.Integer")); + assertEquals(List.class, ClassUtils.getInterfaceClass( + new ArrayList<String>(), "java.util.List")); + assertEquals(Collection.class, ClassUtils.getInterfaceClass( + new ArrayList<String>(), "java.util.Collection")); + } + + public void testGetInterfaceClassFromSubclass() { + assertEquals(Map.class, ClassUtils.getInterfaceClass( + new MySubclassFour(), "java.util.Map")); + assertNull(ClassUtils.getInterfaceClass(new MySubclassFour(), + "java.util.UnknownType")); + } + + public void testLoadProvidersAsString() throws Exception { + BundleContext bc = mockBundleContext(); + Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", Provider.class.getName()); + List<Object> providers = ClassUtils.loadProviderClasses(bc, sd, "providers"); + assertEquals(1, providers.size()); + assertTrue(providers.get(0) instanceof Provider); + } + + public void testLoadProvidersAsStringArray() throws Exception { + BundleContext bc = mockBundleContext(); + Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", + new String[]{Provider.class.getName()}); + List<Object> providers = ClassUtils.loadProviderClasses(bc, sd, "providers"); + assertEquals(1, providers.size()); + assertTrue(providers.get(0) instanceof Provider); + } + + public void testLoadProvidersAsObject() throws Exception { + Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", new Provider()); + List<Object> providers = ClassUtils.loadProviderClasses(null, sd, "providers"); + assertEquals(1, providers.size()); + assertTrue(providers.get(0) instanceof Provider); + } + + public void testLoadProvidersAsObjectArray() throws Exception { + Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", new Object[]{new Provider()}); + List<Object> providers = ClassUtils.loadProviderClasses(null, sd, "providers"); + assertEquals(1, providers.size()); + assertTrue(providers.get(0) instanceof Provider); + } + + public void testLoadProvidersAsObjectList() throws Exception { + List<Object> list = new LinkedList<Object>(); + list.add(new Provider()); + Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", list); + List<Object> providers = ClassUtils.loadProviderClasses(null, sd, "providers"); + assertEquals(1, providers.size()); + assertTrue(providers.get(0) instanceof Provider); + } + + public void testLoadProvidersAsStringList() throws Exception { + List<Object> list = new LinkedList<Object>(); + list.add(Provider.class.getName()); + Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", list); + List<Object> providers = ClassUtils.loadProviderClasses(mockBundleContext(), sd, "providers"); + assertEquals(1, providers.size()); + assertTrue(providers.get(0) instanceof Provider); + } + + private BundleContext mockBundleContext() throws Exception { + BundleContext bc = EasyMock.createMock(BundleContext.class); + Bundle bundle = EasyMock.createMock(Bundle.class); + bc.getBundle(); + EasyMock.expectLastCall().andReturn(bundle); + bundle.loadClass(Provider.class.getName()); + EasyMock.expectLastCall().andReturn(Provider.class); + EasyMock.replay(bc, bundle); + return bc; + } + + @SuppressWarnings({ "serial", "rawtypes" }) + private static class MyMapSubclass extends HashMap { + } + + @SuppressWarnings("serial") + static class MySubclassOne extends MyMapSubclass { + } + + @SuppressWarnings("serial") + static class MySubclassTwo extends MySubclassOne { + } + + @SuppressWarnings("serial") + static class MySubclassThree extends MySubclassTwo { + } + + @SuppressWarnings("serial") + static class MySubclassFour extends MySubclassThree { + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/ClientServiceFactoryTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/ClientServiceFactoryTest.java b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/ClientServiceFactoryTest.java deleted file mode 100644 index 8de459d..0000000 --- a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/ClientServiceFactoryTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.cxf.dosgi.dsw.handlers; - - -import java.util.HashMap; -import java.util.Map; - -import junit.framework.TestCase; - -import org.apache.cxf.dosgi.dsw.api.DistributionProvider; -import org.apache.cxf.dosgi.dsw.api.Endpoint; -import org.apache.cxf.dosgi.dsw.api.IntentUnsatisfiedException; -import org.apache.cxf.dosgi.dsw.service.ClientServiceFactory; -import org.apache.cxf.dosgi.dsw.service.ImportRegistrationImpl; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; -import org.osgi.service.remoteserviceadmin.EndpointDescription; -import org.osgi.service.remoteserviceadmin.RemoteConstants; - -public class ClientServiceFactoryTest extends TestCase { - - @SuppressWarnings({ - "rawtypes", "unchecked" - }) - public void testGetService() throws ClassNotFoundException { - final Object myTestProxyObject = new Object(); - - IMocksControl control = EasyMock.createControl(); - EndpointDescription endpoint = createTestEndpointDesc(); - ImportRegistrationImpl iri = new ImportRegistrationImpl(endpoint, null); - - BundleContext requestingContext = control.createMock(BundleContext.class); - Bundle requestingBundle = control.createMock(Bundle.class); - EasyMock.expect(requestingBundle.loadClass(String.class.getName())).andReturn((Class)String.class); - EasyMock.expect(requestingBundle.getBundleContext()).andReturn(requestingContext); - ServiceRegistration sreg = control.createMock(ServiceRegistration.class); - - DistributionProvider handler = mockDistributionProvider(myTestProxyObject); - control.replay(); - - ClientServiceFactory csf = new ClientServiceFactory(endpoint, handler, iri); - assertSame(myTestProxyObject, csf.getService(requestingBundle, sreg)); - } - - /** - * Creating dummy class as I was not able to really mock it - * @param myTestProxyObject - * @return - */ - private DistributionProvider mockDistributionProvider(final Object myTestProxyObject) { - return new DistributionProvider() { - - @Override - public Object importEndpoint(BundleContext consumerContext, Class<?>[] interfaces, - EndpointDescription endpoint) - throws IntentUnsatisfiedException { - return myTestProxyObject; - } - - @Override - public String[] getSupportedTypes() { - return null; - } - - @Override - public Endpoint exportService(ServiceReference<?> sref, Map<String, Object> effectiveProperties, - String exportedInterface) { - return null; - } - }; - } - - private EndpointDescription createTestEndpointDesc() { - Map<String, Object> map = new HashMap<String, Object>(); - map.put(RemoteConstants.ENDPOINT_ID, "http://google.de"); - map.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, "myGreatConfiguration"); - map.put(Constants.OBJECTCLASS, new String[]{String.class.getName()}); - EndpointDescription endpoint = new EndpointDescription(map); - return endpoint; - } -} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java index 4058de4..93b1171 100644 --- a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java +++ b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java @@ -191,7 +191,7 @@ public class PojoConfigurationTypeHandlerTest extends TestCase { Map<String, Object> props = new HashMap<String, Object>(); props.put(Constants.WS_ADDRESS_PROPERTY, "http://alternate_host:80/myString"); - Endpoint exportResult = p.exportService(sref, props, String.class.getName()); + Endpoint exportResult = p.exportService(sref, props, new Class[]{String.class}); Map<String, Object> edProps = exportResult.description().getProperties(); assertNotNull(edProps.get(RemoteConstants.SERVICE_IMPORTED_CONFIGS)); @@ -255,7 +255,7 @@ public class PojoConfigurationTypeHandlerTest extends TestCase { EasyMock.replay(sref); - Endpoint result = handler.exportService(sref, properties, Runnable.class.getName()); + Endpoint result = handler.exportService(sref, properties, new Class[]{Runnable.class}); Map<String, Object> props = result.description().getProperties(); assertEquals(expectedAddress, props.get("org.apache.cxf.ws.address")); assertEquals("Version of java. package is always 0", "0.0.0", props.get("endpoint.package.version.java.lang")); @@ -289,7 +289,7 @@ public class PojoConfigurationTypeHandlerTest extends TestCase { Runnable myService = EasyMock.createMock(Runnable.class); EasyMock.replay(myService); try { - handler.exportService(sref, props, Runnable.class.getName()); + handler.exportService(sref, props, new Class[]{Runnable.class}); fail("Expected TestException"); } catch (TestException e) { // Expected @@ -390,7 +390,7 @@ public class PojoConfigurationTypeHandlerTest extends TestCase { c.replay(); ServerWrapper serverWrapper = (ServerWrapper)handler.exportService(sref, sd, - MyJaxWsEchoService.class.getName()); + new Class[]{MyJaxWsEchoService.class}); c.verify(); org.apache.cxf.endpoint.Endpoint ep = serverWrapper.getServer().getEndpoint(); @@ -413,7 +413,7 @@ public class PojoConfigurationTypeHandlerTest extends TestCase { c.replay(); ServerWrapper serverWrapper = (ServerWrapper)handler.exportService(sref, sd, - MySimpleEchoService.class.getName()); + new Class[]{MySimpleEchoService.class}); c.verify(); org.apache.cxf.endpoint.Endpoint ep = serverWrapper.getServer().getEndpoint(); http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/ClientServiceFactoryTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/ClientServiceFactoryTest.java b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/ClientServiceFactoryTest.java new file mode 100644 index 0000000..2f32ab7 --- /dev/null +++ b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/ClientServiceFactoryTest.java @@ -0,0 +1,90 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.cxf.dosgi.dsw.service; + + + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.cxf.dosgi.dsw.api.DistributionProvider; +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.remoteserviceadmin.EndpointDescription; +import org.osgi.service.remoteserviceadmin.RemoteConstants; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.isA; + + +public class ClientServiceFactoryTest extends TestCase { + + @SuppressWarnings({ + "rawtypes", "unchecked" + }) + public void testGetService() throws ClassNotFoundException { + final Object myTestProxyObject = new Object(); + + IMocksControl control = EasyMock.createControl(); + EndpointDescription endpoint = createTestEndpointDesc(); + ImportRegistrationImpl iri = new ImportRegistrationImpl(endpoint, null); + + BundleContext requestingContext = control.createMock(BundleContext.class); + Bundle requestingBundle = control.createMock(Bundle.class); + EasyMock.expect(requestingBundle.loadClass(String.class.getName())).andReturn((Class)String.class); + EasyMock.expect(requestingBundle.getBundleContext()).andReturn(requestingContext); + ServiceRegistration sreg = control.createMock(ServiceRegistration.class); + + + DistributionProvider handler = mockDistributionProvider(myTestProxyObject); + control.replay(); + + ClientServiceFactory csf = new ClientServiceFactory(endpoint, handler, iri); + assertSame(myTestProxyObject, csf.getService(requestingBundle, sreg)); + } + + /** + * Creating dummy class as I was not able to really mock it + * @param myTestProxyObject + * @return + */ + private DistributionProvider mockDistributionProvider(final Object proxy) { + DistributionProvider handler = EasyMock.createMock(DistributionProvider.class); + EasyMock.expect(handler.importEndpoint(anyObject(BundleContext.class), + isA(Class[].class), + anyObject(EndpointDescription.class))).andReturn(proxy); + EasyMock.replay(handler); + return handler; + } + + private EndpointDescription createTestEndpointDesc() { + Map<String, Object> map = new HashMap<String, Object>(); + map.put(RemoteConstants.ENDPOINT_ID, "http://google.de"); + map.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, "myGreatConfiguration"); + map.put(Constants.OBJECTCLASS, new String[]{String.class.getName()}); + EndpointDescription endpoint = new EndpointDescription(map); + return endpoint; + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/EventProducerTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/EventProducerTest.java b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/EventProducerTest.java index 98b4cbf..71d3ed7 100644 --- a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/EventProducerTest.java +++ b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/EventProducerTest.java @@ -24,7 +24,7 @@ import java.util.Hashtable; import java.util.List; import java.util.UUID; -import org.apache.cxf.dosgi.dsw.handlers.ServerWrapper; +import org.apache.cxf.dosgi.dsw.api.Endpoint; import org.easymock.EasyMock; import org.easymock.IAnswer; import org.junit.Assert; @@ -113,9 +113,12 @@ public class EventProducerTest { EasyMock.expect(bc.getAllServiceReferences(EventAdmin.class.getName(), null)) .andReturn(new ServiceReference[] {eaSref}).anyTimes(); EasyMock.expect(bc.getService(eaSref)).andReturn(ea).anyTimes(); + Endpoint endpoint = EasyMock.mock(Endpoint.class); + EasyMock.expect(endpoint.description()).andReturn(epd); + EasyMock.replay(endpoint); EasyMock.replay(bc); EventProducer eventProducer = new EventProducer(bc); - ServerWrapper endpoint = new ServerWrapper(epd, null); + ExportRegistrationImpl ereg = new ExportRegistrationImpl(sref, endpoint, remoteServiceAdminCore); eventProducer.publishNotification(ereg); } http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java index 285885a..c9eae7b 100644 --- a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java +++ b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java @@ -18,6 +18,7 @@ */ package org.apache.cxf.dosgi.dsw.service; +import java.io.IOException; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collection; @@ -29,13 +30,6 @@ import java.util.Map; import org.apache.cxf.dosgi.dsw.api.DistributionProvider; import org.apache.cxf.dosgi.dsw.api.Endpoint; -import org.apache.cxf.dosgi.dsw.handlers.CXFDistributionProvider; -import org.apache.cxf.dosgi.dsw.handlers.HttpServiceManager; -import org.apache.cxf.dosgi.dsw.handlers.ServerWrapper; -import org.apache.cxf.dosgi.dsw.qos.DefaultIntentMapFactory; -import org.apache.cxf.dosgi.dsw.qos.IntentManager; -import org.apache.cxf.dosgi.dsw.qos.IntentManagerImpl; -import org.apache.cxf.dosgi.dsw.qos.IntentMap; import org.easymock.EasyMock; import org.easymock.IAnswer; import org.easymock.IMocksControl; @@ -52,6 +46,8 @@ import org.osgi.service.remoteserviceadmin.ExportRegistration; import org.osgi.service.remoteserviceadmin.ImportRegistration; import org.osgi.service.remoteserviceadmin.RemoteConstants; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.isA; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -63,6 +59,8 @@ import static org.junit.Assert.assertTrue; }) public class RemoteServiceAdminCoreTest { + private static final String MYCONFIG = "myconfig"; + @Test public void testDontExportOwnServiceProxies() throws InvalidSyntaxException { IMocksControl c = EasyMock.createControl(); @@ -116,43 +114,29 @@ public class RemoteServiceAdminCoreTest { EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes(); EasyMock.expect(b.getSymbolicName()).andReturn("BundleName").anyTimes(); - Map<String, Object> p = new HashMap<String, Object>(); - p.put(RemoteConstants.ENDPOINT_ID, "http://google.de"); - p.put(Constants.OBJECTCLASS, new String[] { - "es.schaaf.my.class" - }); - p.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, "unsupportedConfiguration"); - EndpointDescription endpoint = new EndpointDescription(p); - IntentMap intentMap = new IntentMap(new DefaultIntentMapFactory().create()); - IntentManager intentManager = new IntentManagerImpl(intentMap, 10000); - HttpServiceManager httpServiceManager = c.createMock(HttpServiceManager.class); - CXFDistributionProvider provider = new CXFDistributionProvider(bc, intentManager, httpServiceManager); + EndpointDescription endpoint = creatEndpointDesc("unsupportedConfiguration"); + + DistributionProvider provider = c.createMock(DistributionProvider.class); + EasyMock.expect(provider.getSupportedTypes()) + .andReturn(new String[]{MYCONFIG}).atLeastOnce(); c.replay(); - RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc, provider) { - @Override - protected ImportRegistrationImpl exposeServiceFactory(String interfaceName, - EndpointDescription epd, - DistributionProvider handler) { - return new ImportRegistrationImpl(epd, this); - } - }; + RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc, provider); // must be null as the endpoint doesn't contain any usable configurations assertNull(rsaCore.importService(endpoint)); // must be empty assertEquals(0, rsaCore.getImportedEndpoints().size()); - p.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, org.apache.cxf.dosgi.dsw.Constants.WS_CONFIG_TYPE); - endpoint = new EndpointDescription(p); + EndpointDescription endpoint2 = creatEndpointDesc(MYCONFIG); - ImportRegistration ireg = rsaCore.importService(endpoint); + ImportRegistration ireg = rsaCore.importService(endpoint2); assertNotNull(ireg); assertEquals(1, rsaCore.getImportedEndpoints().size()); // lets import the same endpoint once more -> should get a copy of the ImportRegistration - ImportRegistration ireg2 = rsaCore.importService(endpoint); + ImportRegistration ireg2 = rsaCore.importService(endpoint2); assertNotNull(ireg2); assertEquals(2, rsaCore.getImportedEndpoints().size()); @@ -174,6 +158,17 @@ public class RemoteServiceAdminCoreTest { c.verify(); } + private EndpointDescription creatEndpointDesc(String configType) { + Map<String, Object> p = new HashMap<String, Object>(); + p.put(RemoteConstants.ENDPOINT_ID, "http://google.de"); + p.put(Constants.OBJECTCLASS, new String[] { + "es.schaaf.my.class" + }); + p.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, configType); + EndpointDescription endpoint = new EndpointDescription(p); + return endpoint; + } + @Test public void testExport() throws Exception { BundleContext bc = EasyMock.createMock(BundleContext.class); @@ -208,10 +203,22 @@ public class RemoteServiceAdminCoreTest { Map<String, Object> eProps = new HashMap<String, Object>(sProps); eProps.put("endpoint.id", "http://something"); eProps.put("service.imported.configs", new String[] {"org.apache.cxf.ws"}); - Endpoint er = new ServerWrapper(new EndpointDescription(eProps), null); + final EndpointDescription epd = new EndpointDescription(eProps); + Endpoint er = new Endpoint() { + + @Override + public void close() throws IOException { + } + + @Override + public EndpointDescription description() { + return epd; + } + }; - DistributionProvider handler = EasyMock.createNiceMock(DistributionProvider.class); - EasyMock.expect(handler.exportService(sref, sProps, Runnable.class.getName())).andReturn(er).once(); + DistributionProvider handler = EasyMock.createMock(DistributionProvider.class); + EasyMock.expect(handler.exportService(anyObject(ServiceReference.class), + anyObject(Map.class), isA(Class[].class))).andReturn(er); EasyMock.replay(handler); RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc, handler); @@ -301,7 +308,8 @@ public class RemoteServiceAdminCoreTest { eProps.put("service.imported.configs", new String[] {"org.apache.cxf.ws"}); DistributionProvider handler = EasyMock.createMock(DistributionProvider.class); - EasyMock.expect(handler.exportService(sref, sProps, Runnable.class.getName())).andThrow(new TestException()); + EasyMock.expect(handler.exportService(anyObject(ServiceReference.class), + anyObject(Map.class), isA(Class[].class))).andThrow(new TestException()); EasyMock.replay(handler); RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc, handler); @@ -322,11 +330,12 @@ public class RemoteServiceAdminCoreTest { } - private ServiceReference mockServiceReference(final Map<String, Object> sProps) { + private ServiceReference mockServiceReference(final Map<String, Object> sProps) throws ClassNotFoundException { BundleContext bc = EasyMock.createNiceMock(BundleContext.class); Bundle b = EasyMock.createNiceMock(Bundle.class); EasyMock.expect(b.getBundleContext()).andReturn(bc).anyTimes(); + EasyMock.expect((Class)b.loadClass(Runnable.class.getName())).andReturn(Runnable.class); EasyMock.replay(b); EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes(); @@ -349,6 +358,7 @@ public class RemoteServiceAdminCoreTest { private static class TestException extends RuntimeException { } + @Test public void testOverlayProperties() { Map<String, Object> sProps = new HashMap<String, Object>(); Map<String, Object> aProps = new HashMap<String, Object>(); @@ -379,4 +389,103 @@ public class RemoteServiceAdminCoreTest { assertEquals("Should not be possible to override the service.id property", 17L, sProps.get(Constants.SERVICE_ID)); } + + @Test + public void testOverlayProperties2() { + Map<String, Object> original = new HashMap<String, Object>(); + + original.put("MyProp", "my value"); + original.put(Constants.OBJECTCLASS, "myClass"); + + Map<String, Object> copy = new HashMap<String, Object>(); + copy.putAll(original); + + // nothing should change here + Map<String, Object> overload = new HashMap<String, Object>(); + RemoteServiceAdminCore.overlayProperties(copy, overload); + + assertEquals(original.size(), copy.size()); + for (Object key : original.keySet()) { + assertEquals(original.get(key), copy.get(key)); + } + + copy.clear(); + copy.putAll(original); + + // a property should be added + overload = new HashMap<String, Object>(); + overload.put("new", "prop"); + + RemoteServiceAdminCore.overlayProperties(copy, overload); + + assertEquals(original.size() + 1, copy.size()); + for (Object key : original.keySet()) { + assertEquals(original.get(key), copy.get(key)); + } + assertNotNull(overload.get("new")); + assertEquals("prop", overload.get("new")); + + copy.clear(); + copy.putAll(original); + + // only one property should be added + overload = new HashMap<String, Object>(); + overload.put("new", "prop"); + overload.put("NEW", "prop"); + + RemoteServiceAdminCore.overlayProperties(copy, overload); + + assertEquals(original.size() + 1, copy.size()); + for (Object key : original.keySet()) { + assertEquals(original.get(key), copy.get(key)); + } + assertNotNull(overload.get("new")); + assertEquals("prop", overload.get("new")); + + copy.clear(); + copy.putAll(original); + + // nothing should change here + overload = new HashMap<String, Object>(); + overload.put(Constants.OBJECTCLASS, "assd"); + overload.put(Constants.SERVICE_ID, "asasdasd"); + RemoteServiceAdminCore.overlayProperties(copy, overload); + + assertEquals(original.size(), copy.size()); + for (Object key : original.keySet()) { + assertEquals(original.get(key), copy.get(key)); + } + + copy.clear(); + copy.putAll(original); + + // overwrite own prop + overload = new HashMap<String, Object>(); + overload.put("MyProp", "newValue"); + RemoteServiceAdminCore.overlayProperties(copy, overload); + + assertEquals(original.size(), copy.size()); + for (Object key : original.keySet()) { + if (!"MyProp".equals(key)) { + assertEquals(original.get(key), copy.get(key)); + } + } + assertEquals("newValue", copy.get("MyProp")); + + copy.clear(); + copy.putAll(original); + + // overwrite own prop in different case + overload = new HashMap<String, Object>(); + overload.put("MYPROP", "newValue"); + RemoteServiceAdminCore.overlayProperties(copy, overload); + + assertEquals(original.size(), copy.size()); + for (Object key : original.keySet()) { + if (!"MyProp".equals(key)) { + assertEquals(original.get(key), copy.get(key)); + } + } + assertEquals("newValue", copy.get("MyProp")); + } } http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/StringPlusTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/StringPlusTest.java b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/StringPlusTest.java new file mode 100644 index 0000000..5447966 --- /dev/null +++ b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/StringPlusTest.java @@ -0,0 +1,63 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.cxf.dosgi.dsw.service; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class StringPlusTest { + + @Test + public void testSplitString() { + String[] values = StringPlus.normalize("1, 2"); + assertEquals(2, values.length); + assertEquals(values[0], "1"); + assertEquals(values[1], "2"); + } + + @Test + public void testNormalizeStringPlus() { + String s1 = "s1"; + String s2 = "s2"; + String s3 = "s3"; + + String[] sa = new String[] { + s1, s2, s3 + }; + + Collection<Object> sl = new ArrayList<Object>(4); + sl.add(s1); + sl.add(s2); + sl.add(s3); + sl.add(new Object()); // must be skipped + + assertArrayEquals(null, StringPlus.normalize(new Object())); + assertArrayEquals(new String[] { + s1 + }, StringPlus.normalize(s1)); + assertArrayEquals(sa, StringPlus.normalize(sa)); + assertArrayEquals(sa, StringPlus.normalize(sl)); + } + +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/UtilsTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/UtilsTest.java b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/UtilsTest.java deleted file mode 100644 index 29e177f..0000000 --- a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/UtilsTest.java +++ /dev/null @@ -1,166 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.cxf.dosgi.dsw.service; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.cxf.dosgi.dsw.util.StringPlus; -import org.junit.Test; -import org.osgi.framework.Constants; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class UtilsTest { - - @Test - public void testSplitString() { - String[] values = StringPlus.normalize("1, 2"); - assertEquals(2, values.length); - assertEquals(values[0], "1"); - assertEquals(values[1], "2"); - } - - @Test - public void testNormalizeStringPlus() { - String s1 = "s1"; - String s2 = "s2"; - String s3 = "s3"; - - String[] sa = new String[] { - s1, s2, s3 - }; - - Collection<Object> sl = new ArrayList<Object>(4); - sl.add(s1); - sl.add(s2); - sl.add(s3); - sl.add(new Object()); // must be skipped - - assertArrayEquals(null, StringPlus.normalize(new Object())); - assertArrayEquals(new String[] { - s1 - }, StringPlus.normalize(s1)); - assertArrayEquals(sa, StringPlus.normalize(sa)); - assertArrayEquals(sa, StringPlus.normalize(sl)); - } - - @Test - public void testOverlayProperties() { - Map<String, Object> original = new HashMap<String, Object>(); - - original.put("MyProp", "my value"); - original.put(Constants.OBJECTCLASS, "myClass"); - - Map<String, Object> copy = new HashMap<String, Object>(); - copy.putAll(original); - - // nothing should change here - Map<String, Object> overload = new HashMap<String, Object>(); - RemoteServiceAdminCore.overlayProperties(copy, overload); - - assertEquals(original.size(), copy.size()); - for (Object key : original.keySet()) { - assertEquals(original.get(key), copy.get(key)); - } - - copy.clear(); - copy.putAll(original); - - // a property should be added - overload = new HashMap<String, Object>(); - overload.put("new", "prop"); - - RemoteServiceAdminCore.overlayProperties(copy, overload); - - assertEquals(original.size() + 1, copy.size()); - for (Object key : original.keySet()) { - assertEquals(original.get(key), copy.get(key)); - } - assertNotNull(overload.get("new")); - assertEquals("prop", overload.get("new")); - - copy.clear(); - copy.putAll(original); - - // only one property should be added - overload = new HashMap<String, Object>(); - overload.put("new", "prop"); - overload.put("NEW", "prop"); - - RemoteServiceAdminCore.overlayProperties(copy, overload); - - assertEquals(original.size() + 1, copy.size()); - for (Object key : original.keySet()) { - assertEquals(original.get(key), copy.get(key)); - } - assertNotNull(overload.get("new")); - assertEquals("prop", overload.get("new")); - - copy.clear(); - copy.putAll(original); - - // nothing should change here - overload = new HashMap<String, Object>(); - overload.put(Constants.OBJECTCLASS, "assd"); - overload.put(Constants.SERVICE_ID, "asasdasd"); - RemoteServiceAdminCore.overlayProperties(copy, overload); - - assertEquals(original.size(), copy.size()); - for (Object key : original.keySet()) { - assertEquals(original.get(key), copy.get(key)); - } - - copy.clear(); - copy.putAll(original); - - // overwrite own prop - overload = new HashMap<String, Object>(); - overload.put("MyProp", "newValue"); - RemoteServiceAdminCore.overlayProperties(copy, overload); - - assertEquals(original.size(), copy.size()); - for (Object key : original.keySet()) { - if (!"MyProp".equals(key)) { - assertEquals(original.get(key), copy.get(key)); - } - } - assertEquals("newValue", copy.get("MyProp")); - - copy.clear(); - copy.putAll(original); - - // overwrite own prop in different case - overload = new HashMap<String, Object>(); - overload.put("MYPROP", "newValue"); - RemoteServiceAdminCore.overlayProperties(copy, overload); - - assertEquals(original.size(), copy.size()); - for (Object key : original.keySet()) { - if (!"MyProp".equals(key)) { - assertEquals(original.get(key), copy.get(key)); - } - } - assertEquals("newValue", copy.get("MyProp")); - } -} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/84f6be48/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/util/ClassUtilsTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/util/ClassUtilsTest.java b/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/util/ClassUtilsTest.java deleted file mode 100644 index a21ada5..0000000 --- a/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/util/ClassUtilsTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.cxf.dosgi.dsw.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import junit.framework.TestCase; - -import org.apache.cxf.dosgi.dsw.handlers.ClassUtils; -import org.easymock.EasyMock; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; - -public class ClassUtilsTest extends TestCase { - - public void testGetInterfaceClass() { - assertEquals(String.class, - ClassUtils.getInterfaceClass("Hello", "java.lang.String")); - assertNull(ClassUtils.getInterfaceClass("Hello", "java.lang.Integer")); - assertEquals(List.class, ClassUtils.getInterfaceClass( - new ArrayList<String>(), "java.util.List")); - assertEquals(Collection.class, ClassUtils.getInterfaceClass( - new ArrayList<String>(), "java.util.Collection")); - } - - public void testGetInterfaceClassFromSubclass() { - assertEquals(Map.class, ClassUtils.getInterfaceClass( - new MySubclassFour(), "java.util.Map")); - assertNull(ClassUtils.getInterfaceClass(new MySubclassFour(), - "java.util.UnknownType")); - } - - public void testLoadProvidersAsString() throws Exception { - BundleContext bc = mockBundleContext(); - Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", Provider.class.getName()); - List<Object> providers = ClassUtils.loadProviderClasses(bc, sd, "providers"); - assertEquals(1, providers.size()); - assertTrue(providers.get(0) instanceof Provider); - } - - public void testLoadProvidersAsStringArray() throws Exception { - BundleContext bc = mockBundleContext(); - Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", - new String[]{Provider.class.getName()}); - List<Object> providers = ClassUtils.loadProviderClasses(bc, sd, "providers"); - assertEquals(1, providers.size()); - assertTrue(providers.get(0) instanceof Provider); - } - - public void testLoadProvidersAsObject() throws Exception { - Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", new Provider()); - List<Object> providers = ClassUtils.loadProviderClasses(null, sd, "providers"); - assertEquals(1, providers.size()); - assertTrue(providers.get(0) instanceof Provider); - } - - public void testLoadProvidersAsObjectArray() throws Exception { - Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", new Object[]{new Provider()}); - List<Object> providers = ClassUtils.loadProviderClasses(null, sd, "providers"); - assertEquals(1, providers.size()); - assertTrue(providers.get(0) instanceof Provider); - } - - public void testLoadProvidersAsObjectList() throws Exception { - List<Object> list = new LinkedList<Object>(); - list.add(new Provider()); - Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", list); - List<Object> providers = ClassUtils.loadProviderClasses(null, sd, "providers"); - assertEquals(1, providers.size()); - assertTrue(providers.get(0) instanceof Provider); - } - - public void testLoadProvidersAsStringList() throws Exception { - List<Object> list = new LinkedList<Object>(); - list.add(Provider.class.getName()); - Map<String, Object> sd = Collections.<String, Object>singletonMap("providers", list); - List<Object> providers = ClassUtils.loadProviderClasses(mockBundleContext(), sd, "providers"); - assertEquals(1, providers.size()); - assertTrue(providers.get(0) instanceof Provider); - } - - private BundleContext mockBundleContext() throws Exception { - BundleContext bc = EasyMock.createMock(BundleContext.class); - Bundle bundle = EasyMock.createMock(Bundle.class); - bc.getBundle(); - EasyMock.expectLastCall().andReturn(bundle); - bundle.loadClass(Provider.class.getName()); - EasyMock.expectLastCall().andReturn(Provider.class); - EasyMock.replay(bc, bundle); - return bc; - } - - @SuppressWarnings({ "serial", "rawtypes" }) - private static class MyMapSubclass extends HashMap { - } - - @SuppressWarnings("serial") - static class MySubclassOne extends MyMapSubclass { - } - - @SuppressWarnings("serial") - static class MySubclassTwo extends MySubclassOne { - } - - @SuppressWarnings("serial") - static class MySubclassThree extends MySubclassTwo { - } - - @SuppressWarnings("serial") - static class MySubclassFour extends MySubclassThree { - } -}
