Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d8b0437c0d179f24a2686d886b653119a538edb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
 * Copyright (c) 2010 Oracle.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution. 
 * The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 * and the Apache License v2.0 is available at 
 *     http://www.opensource.org/licenses/apache2.0.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Bob Nettleton - Initial Developer tests for Reference Implementation
 ******************************************************************************/


/**
 * This class is a common base class for the developer tests for the Gemini 
 * Naming project.  
 */

package org.eclipse.gemini.naming.test;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.springframework.osgi.test.AbstractConfigurableBundleCreatorTests;

public abstract class NamingTestCase extends AbstractConfigurableBundleCreatorTests {

	private static String VERSION = "1.0.5-SNAPSHOT";
	
	private Map m_mapOfServicesToRegistrations = 
		new HashMap();

	protected void onSetUp() throws Exception {
		m_mapOfServicesToRegistrations = new HashMap();
	}
	
	protected void onTearDown() throws Exception {
		super.onTearDown();
        unregisterAllServices();
	}
	
	
	/**
	 * Override getTestFrameworkBundlesNames() in order to modify the
	 * dynamic dependency on Apache Log4J.  The Spring OSGi Test framework
	 * relies by default on a SNAPSHOT version of Log4J.  
	 * 
	 * This method should be considered a workaround for this issue.  If future
	 * versions of Spring OSGi Test use a later version of Log4J, then this 
	 * method can/should be removed.  
	 */
	protected String[] getTestFrameworkBundlesNames() {
		String[] originalBundleNames = super.getTestFrameworkBundlesNames();
		
		for(int i = 0; i < originalBundleNames.length; i++) {
			if(originalBundleNames[i].startsWith("org.springframework.osgi,log4j.osgi")) {
				// set framework to use apache log4j dependency instead
				String newBundleName = 
					"org.apache.log4j,com.springsource.org.apache.log4j,1.2.15";
				originalBundleNames[i] = newBundleName;
			}
		}
		
		return originalBundleNames;
	}
	
	
	/**
	 * Declaratively specify the dependency on the Gemini Naming bundle.  
	 */
	protected String[] getTestBundlesNames() {
        return new String[]{ "org.eclipse.gemini.naming, org.eclipse.gemini.naming," + VERSION };
    }


	protected void registerService(String serviceType, Object service, Dictionary properties) {
    	ServiceRegistration registration = 
    		bundleContext.registerService(serviceType, service, properties);
    	m_mapOfServicesToRegistrations.put(service, registration);
    	
    }
    
    protected void unregisterService(Object service) {
    	if(m_mapOfServicesToRegistrations.containsKey(service)) {
    		ServiceRegistration registration = 
    			(ServiceRegistration)m_mapOfServicesToRegistrations.get(service);
    		registration.unregister();
    	}
    }
    
    protected void unregisterAllServices() {
    	Set keySet = m_mapOfServicesToRegistrations.keySet();
    	Iterator iterator = keySet.iterator();
    	while(iterator.hasNext()) {
    		unregisterService(iterator.next());
    	}
    }
    
    protected BundleContext getContext() {
   	  	return bundleContext;
    }

	/**
	 * Indicates if the automatic manifest creation should consider only the
	 * test class (<code>true</code>) or all classes included in the test
	 * bundle(<code>false</code>). The latter should be used when the test
	 * bundle contains additional classes that help with the test case.
	 * 
	 * <p/> By default, this method returns <code>true</code>, meaning that
	 * only the test class will be searched for dependencies.
	 * 
	 * @return true if only the test hierarchy is searched for dependencies or
	 *         false if all classes discovered in the test archive need to be
	 *         parsed.
	 */
	protected boolean createManifestOnlyFromTestClass() {
		return false;
	}

}

Back to the top