<commit_before>/**
 * Copyright (c) 2014-2016 by the respective copyright holders.
 * 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
 */
package org.eclipse.smarthome.test

import static org.hamcrest.CoreMatchers.*
import static org.junit.Assert.*
import static org.junit.matchers.JUnitMatchers.*

import org.eclipse.smarthome.core.autoupdate.AutoUpdateBindingConfigProvider
import org.eclipse.smarthome.test.storage.VolatileStorageService
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.osgi.framework.BundleContext
import org.osgi.framework.FrameworkUtil
import org.osgi.framework.ServiceReference
import org.osgi.framework.ServiceRegistration

/**
 * {@link OSGiTest} is an abstract base class for OSGi based tests. It provides
 * convenience methods to register and unregister mocks as OSGi services. All services, which
 * are registered through the {@link OSGiTest#registerService} methods, are unregistered
 * automatically in the tear down of the test.
 *
 * @author <NAME> - Initial contribution
 */
abstract class OSGiTest {

    BundleContext bundleContext
    Map<String, ServiceRegistration> registeredServices = [:]

    @Before
    public void bindBundleContext() {
        bundleContext = getBundleContext()
        assertThat bundleContext, is(notNullValue())
    }

    /**
     * Returns the {@link BundleContext}, which is used for registration and unregistration of OSGi
     * services. By default it uses the bundle context of the test class itself. This method can be overridden
     * by concrete implementations to provide another bundle context.
     *
     * @return bundle context
     */
    protected BundleContext getBundleContext() {
        def bundle = FrameworkUtil.getBundle(this.getClass())
        return bundle?.getBundleContext()
    }

    /**
     * Returns an OSGi service for the given class.
     *
     * @param clazz class under which the OSGi service is registered
     * @return OSGi service or null if no service can be found for the given class
     */
    protected <T> T getService(Class<T> clazz){
        def serviceReference = bundleContext.getServiceReference(clazz.name)
        return serviceReference ? bundleContext.getService(serviceReference) : null
    }

    /**
     * Returns an OSGi service for the given class and the given filter.
     *
     * @param clazz class under which the OSGi service is registered
     * @param filter
     * @return OSGi service or null if no service can be found for the given class
     */
    protected <T> T getService(Class<T> clazz, Closure<Boolean> filter){
        def serviceReferences = bundleContext.getServiceReferences(clazz.name, null)
        def filteredServiceReferences = serviceReferences.findAll(filter)

        if (filteredServiceReferences.size() > 1) {
            Assert.fail("More than 1 service matching the filter is registered.")
        }
        if (filteredServiceReferences.empty) {
            null
        } else {
            bundleContext.getService(filteredServiceReferences.first())
        }
    }

    /**
     * Returns an OSGi service for the given class and the given filter.
     *
     * @param clazz class under which the OSGi service is registered
     * @param filter
     * @return OSGi service or null if no service can be found for the given class
     */
    protected <T> T getService(Class<T> clazz, Class<? extends T> implementationClass){
        getService(clazz, { ServiceReference<?> serviceReference ->
            implementationClass.isAssignableFrom(bundleContext.getService(serviceReference).getClass())
        })
    }

    /**
     * Registers the given object as OSGi service. The first interface is used as OSGi service
     * interface name.
     *
     * @param service service to be registered
     * @param properties OSGi service properties
     * @return service registration object
     */
    protected registerService(def service, Hashtable properties = [:]) {
        def interfaceName = getInterfaceName(service)
        assertThat interfaceName, is(notNullValue())
        registeredServices.put(interfaceName, bundleContext.registerService(interfaceName, service, properties))
    }

    /**
     * Registers the given object as OSGi service. The given interface name as String is used as OSGi service
     * interface name.
     *
     * @param service service to be registered
     * @param interfaceName interface name of the OSGi service
     * @param properties OSGi service properties
     * @return service registration object
     */<commit_msg>OSGi test: return implementation class (#3075)<commit_after>/**
 * Copyright (c) 2014-2016 by the respective copyright holders.
 * 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
 */
package org.eclipse.smarthome.test

import static org.hamcrest.CoreMatchers.*
import static org.junit.Assert.*
import static org.junit.matchers.JUnitMatchers.*

import org.eclipse.smarthome.core.autoupdate.AutoUpdateBindingConfigProvider
import org.eclipse.smarthome.test.storage.VolatileStorageService
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.osgi.framework.BundleContext
import org.osgi.framework.FrameworkUtil
import org.osgi.framework.ServiceReference
import org.osgi.framework.ServiceRegistration

/**
 * {@link OSGiTest} is an abstract base class for OSGi based tests. It provides
 * convenience methods to register and unregister mocks as OSGi services. All services, which
 * are registered through the {@link OSGiTest#registerService} methods, are unregistered
 * automatically in the tear down of the test.
 *
 * @author <NAME> - Initial contribution
 */
abstract class OSGiTest {

    BundleContext bundleContext
    Map<String, ServiceRegistration> registeredServices = [:]

    @Before
    public void bindBundleContext() {
        bundleContext = getBundleContext()
        assertThat bundleContext, is(notNullValue())
    }

    /**
     * Returns the {@link BundleContext}, which is used for registration and unregistration of OSGi
     * services. By default it uses the bundle context of the test class itself. This method can be overridden
     * by concrete implementations to provide another bundle context.
     *
     * @return bundle context
     */
    protected BundleContext getBundleContext() {
        def bundle = FrameworkUtil.getBundle(this.getClass())
        return bundle?.getBundleContext()
    }

    /**
     * Returns an OSGi service for the given class.
     *
     * @param clazz class under which the OSGi service is registered
     * @return OSGi service or null if no service can be found for the given class
     */
    protected <T> T getService(Class<T> clazz){
        def serviceReference = bundleContext.getServiceReference(clazz.name)
        return serviceReference ? bundleContext.getService(serviceReference) : null
    }

    /**
     * Returns an OSGi service for the given class and the given filter.
     *
     * @param clazz class under which the OSGi service is registered
     * @param filter
     * @return OSGi service or null if no service can be found for the given class
     */
    protected <T> T getService(Class<T> clazz, Closure<Boolean> filter){
        def serviceReferences = bundleContext.getServiceReferences(clazz.name, null)
        def filteredServiceReferences = serviceReferences.findAll(filter)

        if (filteredServiceReferences.size() > 1) {
            Assert.fail("More than 1 service matching the filter is registered.")
        }
        if (filteredServiceReferences.empty) {
            null
        } else {
            bundleContext.getService(filteredServiceReferences.first())
        }
    }

    /**
     * Returns an OSGi service for the given class and the given filter.
     *
     * @param clazz class under which the OSGi service is registered
     * @param filter
     * @return OSGi service or null if no service can be found for the given class
     */
    protected <T,I extends T> I getService(Class<T> clazz, Class<I> implementationClass){
        (I) getService(clazz, { ServiceReference<?> serviceReference ->
            implementationClass.isAssignableFrom(bundleContext.getService(serviceReference).getClass())
        })
    }

    /**
     * Registers the given object as OSGi service. The first interface is used as OSGi service
     * interface name.
     *
     * @param service service to be registered
     * @param properties OSGi service properties
     * @return service registration object
     */
    protected registerService(def service, Hashtable properties = [:]) {
        def interfaceName = getInterfaceName(service)
        assertThat interfaceName, is(notNullValue())
        registeredServices.put(interfaceName, bundleContext.registerService(interfaceName, service, properties))
    }

    /**
     * Registers the given object as OSGi service. The given interface name as String is used as OSGi service
     * interface name.
     *
     * @param service service to be registered
     * @param interfaceName interface name of the OSGi service
     * @param properties OSGi service properties
     * @return service registration object
     */
{'fraction_non_alphanumeric': 0.07401986408782017, 'fraction_numerical': 0.00334553058024046, 'mean_word_length': 3.242128603104213, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2659215', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2353, 'n_words': 884}