Skip to main content
summaryrefslogtreecommitdiffstats
blob: 816ddecb0231fc18d8e546f6d7121ff07d8c2e4f (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
/****************************************************************************
* Copyright (c) 2004 Composent, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    Composent, Inc. - initial API and implementation
*****************************************************************************/

package org.eclipse.ecf.tests.discovery;

import org.eclipse.ecf.core.ContainerCreateException;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.discovery.IDiscoveryContainerAdapter;
import org.eclipse.ecf.discovery.service.IDiscoveryService;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

public abstract class DiscoveryServiceTest extends DiscoveryTest {

	private static final String NO_PROVIDER_REGISTERED = "No discovery provider by that name seems to be registered";

	/**
	 * @param name
	 */
	public DiscoveryServiceTest(String name) {
		super(name);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.tests.discovery.DiscoveryTest#getAdapter(java.lang.Class)
	 */
	protected IDiscoveryContainerAdapter getAdapter(Class notNeeded) {
		final ServiceTracker serviceTracker = Activator.getDefault().getDiscoveryServiceTracker();
		assertNotNull(NO_PROVIDER_REGISTERED, serviceTracker);
		final ServiceReference[] serviceReferences = serviceTracker.getServiceReferences();
		assertNotNull(NO_PROVIDER_REGISTERED, serviceReferences);
		for(int i = 0; i < serviceReferences.length; i++) {
			ServiceReference sr = serviceReferences[i];
			if(containerUnderTest.equals(sr.getProperty(IDiscoveryService.CONTAINER_NAME))) {
				return (IDiscoveryContainerAdapter) serviceTracker.getService(sr);
			}
		}
		fail(NO_PROVIDER_REGISTERED);
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.tests.discovery.DiscoveryTest#getContainer(java.lang.String)
	 */
	protected IContainer getContainer(String containerUnderTest)
			throws ContainerCreateException {
		IContainer container = (IContainer) getAdapter(null);
		// we might get a pre used container but the tests expect a fresh instance
		container.disconnect();
		return container;
	}
	
	/*
	 * (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		discoveryContainer.unregisterService(serviceInfo);
	}
}

Back to the top