001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.bus.extension;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.cxf.resource.DefaultResourceManager;
024: import org.apache.cxf.resource.ResourceManager;
025: import org.apache.cxf.resource.ResourceResolver;
026: import org.apache.cxf.resource.SinglePropertyResolver;
027: import org.junit.Assert;
028: import org.junit.Before;
029: import org.junit.Test;
030:
031: public class ExtensionManagerTest extends Assert {
032:
033: private static final String EXTENSIONMANAGER_TEST_RESOURECE_NAME = "extensionManagerTest";
034: private ExtensionManagerImpl manager;
035: private MyService myService;
036: private Map<Class, Object> extensions;
037:
038: @Before
039: public void setUp() {
040: ResourceResolver resolver = new SinglePropertyResolver(
041: EXTENSIONMANAGER_TEST_RESOURECE_NAME, this );
042: ResourceManager rm = new DefaultResourceManager(resolver);
043:
044: extensions = new HashMap<Class, Object>();
045: extensions.put(Integer.class, new Integer(0));
046:
047: manager = new ExtensionManagerImpl("test-extension.xml", Thread
048: .currentThread().getContextClassLoader(), extensions,
049: rm);
050: myService = null;
051: }
052:
053: @Test
054: public void testLoadAndRegister() {
055: Extension e = new Extension();
056: e.setClassname("java.lang.String");
057: e.setDeferred(false);
058: manager.loadAndRegister(e);
059:
060: String interfaceName = "java.lang.Runnable";
061: e.setDeferred(false);
062: e.setClassname("java.lang.Thread");
063: e.setInterfaceName(interfaceName);
064: assertNull("Object is registered.", extensions
065: .get(Runnable.class));
066: manager.loadAndRegister(e);
067: assertNotNull("Object was not registered.", extensions
068: .get(Runnable.class));
069:
070: interfaceName = "java.lang.Integer";
071: e.setInterfaceName(interfaceName);
072: e.setClassname("no.such.Class");
073: Object obj = extensions.get(Integer.class);
074: assertNotNull("Object is not registered.", obj);
075: manager.loadAndRegister(e);
076: assertSame("Registered object was replaced.", obj, extensions
077: .get(Integer.class));
078:
079: }
080:
081: @Test
082: public void testActivateViaNS() {
083:
084: Extension e = new Extension();
085: e.setClassname(MyService.class.getName());
086: String ns = "http://cxf.apache.org/integer";
087: e.getNamespaces().add(ns);
088: e.setDeferred(true);
089: manager.processExtension(e);
090: assertNull(myService);
091: manager.activateViaNS(ns);
092: assertNotNull(myService);
093: assertEquals(1, myService.getActivationNamespaces().size());
094: assertEquals(ns, myService.getActivationNamespaces().iterator()
095: .next());
096:
097: // second activation should be a no-op
098:
099: MyService first = myService;
100: manager.activateViaNS(ns);
101: assertSame(first, myService);
102: }
103:
104: public void setMyService(MyService m) {
105: myService = m;
106: }
107:
108: }
|