<commit_before>/**
 * OSGi Test Suite Implementation. OSGi Confidential.
 */
package org.osgi.test.cases.discovery;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

import junit.framework.TestCase;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.discovery.DiscoveredServiceNotification;
import org.osgi.service.discovery.DiscoveredServiceTracker;
import org.osgi.service.discovery.Discovery;
import org.osgi.service.discovery.ServiceEndpointDescription;
import org.osgi.service.discovery.ServicePublication;
import org.osgi.test.cases.discovery.internal.DiscoveredServiceTrackerImpl;
import org.osgi.test.cases.discovery.internal.DiscoveryTestServiceImpl;
import org.osgi.test.cases.discovery.internal.DiscoveryTestServiceInterface;

public class DiscoveryTestCase extends TestCase {
	BundleContext context;
	Properties tckProperties;

	public void setBundleContext(BundleContext context) {
		this.context = context;
	}

	protected void setUp() throws Exception {
		tckProperties = new Properties();
		tckProperties.put("discovery.tck.discover.timeout", "10000");
		tckProperties.put("discovery.tck.undiscover.timeout", "10000");
		
		try {
			tckProperties.load(new FileInputStream("tck.properties"));
		} catch (Exception e) {
			System.err.println("Failed to read TCK properties from tck.properties file");
		}
	}

	protected void tearDown() throws Exception {
	}

	public void testDiscoveryPresence() {
		ServiceReference discoveryServiceRef = context.getServiceReference(Discovery.class.getName());
		Discovery discovery = (Discovery) context.getService(discoveryServiceRef);
		
		assertNotNull(discovery);
		
		System.out.println("Discovery:");
		System.out.println("vendor name:         " + discoveryServiceRef.getProperty(Discovery.PROP_KEY_VENDOR_NAME));
		System.out.println("product name:        " + discoveryServiceRef.getProperty(Discovery.PROP_KEY_PRODUCT_NAME));
		System.out.println("product version:     " + discoveryServiceRef.getProperty(Discovery.PROP_KEY_PRODUCT_VERSION));
		System.out.println("supported protocols: " + discoveryServiceRef.getProperty(Discovery.PROP_KEY_SUPPORTED_PROTOCOLS));
	}
	
	/**
	 * Create a DiscoveryTestService instance and register it as ServicePublication. The discovery
	 * implementation will pick it up
	 */
	public void testPublish() throws Exception {
		// first setup the notification listener
		Collection trackerinterfaces = new ArrayList();
		trackerinterfaces.add(DiscoveryTestServiceInterface.class.getName());
		
		Hashtable trackerprops = new Hashtable();
		trackerprops.put(DiscoveredServiceTracker.PROP_KEY_MATCH_CRITERIA_INTERFACES, trackerinterfaces);
		
		DiscoveredServiceTrackerImpl tracker = new DiscoveredServiceTrackerImpl();
		ServiceRegistration tsr = context.registerService(DiscoveredServiceTracker.class.getName(), tracker, trackerprops);
		assertNotNull(tsr);
		
		// then register a service
		Hashtable properties = new Hashtable();
		properties.put("myprop", "myvalue");

		DiscoveryTestServiceImpl serviceimpl = new DiscoveryTestServiceImpl();
		
		ServiceRegistration sr = context.registerService(DiscoveryTestServiceInterface.class.getName(), serviceimpl, properties);
		assertNotNull(sr);
		
		// fill out the properties for Discovery
		Collection ifnames = new ArrayList();
		ifnames.add(DiscoveryTestServiceInterface.class.getName());
		Collection ifversions = new ArrayList();
		ifversions.add(DiscoveryTestServiceInterface.class.getName() + ServicePublication.SEPARATOR + "1.0.0"); // TODO test case with bad version number
		
		Hashtable spprops = new Hashtable();
		spprops.put(ServicePublication.PROP_KEY_SERVICE_INTERFACE_NAME, ifnames);
		spprops.put(ServicePublication.PROP_KEY_SERVICE_INTERFACE_VERSION, ifversions);
		Map propertiesMap = new HashMap();
		propertiesMap.put("myprop", properties.get("myprop"));
		spprops.put(ServicePublication.PROP_KEY_SERVICE_PROPERTIES, propertiesMap);
		// TODO add test case with non existent interface
		
		// some optional properties
		spprops.put(ServicePublication.PROP_KEY_ENDPOINT_ID, "myid");
		
		Collection epifnames = new ArrayList();
		epifnames.add("osgi:service:" + DiscoveryTestServiceInterface.class.getName() 
				+ "/version=1.0.0");
		spprops.put(ServicePublication.PROP_KEY_ENDPOINT_INTERFACE_NAME, epifnames);
		
		// TODO add some bad metadata test cases
		
		ServicePublication sp = new ServicePublication() {};
		ServiceRegistration spsr = context.registerService(ServicePublication.class.getName(), sp, spprops);
		assertNotNull(spsr);
		
		int dtimeout = Integer.parseInt(tckProperties.getProperty("discovery.tck.discover.timeout"));
			<commit_msg>fixed bug in discovery test with the external interface<commit_after>/**
 * OSGi Test Suite Implementation. OSGi Confidential.
 */
package org.osgi.test.cases.discovery;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

import junit.framework.TestCase;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.discovery.DiscoveredServiceNotification;
import org.osgi.service.discovery.DiscoveredServiceTracker;
import org.osgi.service.discovery.Discovery;
import org.osgi.service.discovery.ServiceEndpointDescription;
import org.osgi.service.discovery.ServicePublication;
import org.osgi.test.cases.discovery.internal.DiscoveredServiceTrackerImpl;
import org.osgi.test.cases.discovery.internal.DiscoveryTestServiceImpl;
import org.osgi.test.cases.discovery.internal.DiscoveryTestServiceInterface;

public class DiscoveryTestCase extends TestCase {
	BundleContext context;
	Properties tckProperties;

	public void setBundleContext(BundleContext context) {
		this.context = context;
	}

	protected void setUp() throws Exception {
		tckProperties = new Properties();
		tckProperties.put("discovery.tck.discover.timeout", "10000");
		tckProperties.put("discovery.tck.undiscover.timeout", "10000");
		
		try {
			tckProperties.load(new FileInputStream("tck.properties"));
		} catch (Exception e) {
			System.err.println("Failed to read TCK properties from tck.properties file");
		}
	}

	protected void tearDown() throws Exception {
	}

	public void testDiscoveryPresence() {
		ServiceReference discoveryServiceRef = context.getServiceReference(Discovery.class.getName());
		Discovery discovery = (Discovery) context.getService(discoveryServiceRef);
		
		assertNotNull(discovery);
		
		System.out.println("Discovery:");
		System.out.println("vendor name:         " + discoveryServiceRef.getProperty(Discovery.PROP_KEY_VENDOR_NAME));
		System.out.println("product name:        " + discoveryServiceRef.getProperty(Discovery.PROP_KEY_PRODUCT_NAME));
		System.out.println("product version:     " + discoveryServiceRef.getProperty(Discovery.PROP_KEY_PRODUCT_VERSION));
		System.out.println("supported protocols: " + discoveryServiceRef.getProperty(Discovery.PROP_KEY_SUPPORTED_PROTOCOLS));
	}
	
	/**
	 * Create a DiscoveryTestService instance and register it as ServicePublication. The discovery
	 * implementation will pick it up
	 */
	public void testPublish() throws Exception {
		// first setup the notification listener
		Collection trackerinterfaces = new ArrayList();
		trackerinterfaces.add(DiscoveryTestServiceInterface.class.getName());
		
		Hashtable trackerprops = new Hashtable();
		trackerprops.put(DiscoveredServiceTracker.PROP_KEY_MATCH_CRITERIA_INTERFACES, trackerinterfaces);
		
		DiscoveredServiceTrackerImpl tracker = new DiscoveredServiceTrackerImpl();
		ServiceRegistration tsr = context.registerService(DiscoveredServiceTracker.class.getName(), tracker, trackerprops);
		assertNotNull(tsr);
		
		// then register a service
		Hashtable properties = new Hashtable();
		properties.put("myprop", "myvalue");

		DiscoveryTestServiceImpl serviceimpl = new DiscoveryTestServiceImpl();
		
		ServiceRegistration sr = context.registerService(DiscoveryTestServiceInterface.class.getName(), serviceimpl, properties);
		assertNotNull(sr);
		
		// fill out the properties for Discovery
		Collection ifnames = new ArrayList();
		ifnames.add(DiscoveryTestServiceInterface.class.getName());
		Collection ifversions = new ArrayList();
		ifversions.add(DiscoveryTestServiceInterface.class.getName() + ServicePublication.SEPARATOR + "1.0.0"); // TODO test case with bad version number
		
		Hashtable spprops = new Hashtable();
		spprops.put(ServicePublication.PROP_KEY_SERVICE_INTERFACE_NAME, ifnames);
		spprops.put(ServicePublication.PROP_KEY_SERVICE_INTERFACE_VERSION, ifversions);
		Map propertiesMap = new HashMap();
		propertiesMap.put("myprop", properties.get("myprop"));
		spprops.put(ServicePublication.PROP_KEY_SERVICE_PROPERTIES, propertiesMap);
		// TODO add test case with non existent interface
		
		// some optional properties
		spprops.put(ServicePublication.PROP_KEY_ENDPOINT_ID, "myid");
		
		Collection epifnames = new ArrayList();
		epifnames.add(DiscoveryTestServiceInterface.class.getName()  + ServicePublication.SEPARATOR
				+ "osgi:service:" + DiscoveryTestServiceInterface.class.getName() 
				+ "/version=1.0.0");
		spprops.put(ServicePublication.PROP_KEY_ENDPOINT_INTERFACE_NAME, epifnames);
		
		// TODO add some bad metadata test cases
		
		ServicePublication sp = new ServicePublication() {};
		ServiceRegistration spsr = context.registerService(ServicePublication.class.getName(), sp, spprops);
		assertNotNull(spsr);
		
		int dtimeout = Integer.parseInt(tckProperties.getProperty("discovery.tck.discover.timeout"));
			
{'fraction_non_alphanumeric': 0.09933035714285714, 'fraction_numerical': 0.003246753246753247, 'mean_word_length': 5.888190076869322, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4559580', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2696, 'n_words': 544}