<commit_before>/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.aries.subsystem.itests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.ops4j.pax.exam.CoreOptions.equinox;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.aries.subsystem.core.archive.ProvisionPolicyDirective;
import org.apache.aries.subsystem.core.archive.SubsystemTypeHeader;
import org.apache.aries.subsystem.core.internal.BundleResource;
import org.apache.aries.subsystem.core.internal.ResourceHelper;
import org.apache.aries.subsystem.core.internal.SubsystemIdentifier;
import org.apache.aries.subsystem.itests.util.TestRepository;
import org.apache.aries.subsystem.itests.util.Utils;
import org.apache.aries.unittest.fixture.ArchiveFixture;
import org.apache.aries.unittest.fixture.ArchiveFixture.JarFixture;
import org.apache.aries.unittest.fixture.ArchiveFixture.ManifestFixture;
import org.apache.aries.unittest.fixture.ArchiveFixture.ZipFixture;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.container.def.PaxRunnerOptions;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.Version;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.resource.Resource;
import org.osgi.service.repository.Repository;
import org.osgi.service.repository.RepositoryContent;
import org.osgi.service.subsystem.Subsystem;
import org.osgi.service.subsystem.Subsystem.State;
import org.osgi.service.subsystem.SubsystemConstants;

public abstract class SubsystemTest extends IntegrationTest {
	protected static class SubsystemEventHandler implements ServiceListener {
		private static class ServiceEventInfo {
			private final ServiceEvent event;
			private final long id;
			private final State state;
			private final String symbolicName;
			private final String type;
			private final Version version;
			
			public ServiceEventInfo(ServiceEvent event) {
				id = (Long)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_ID_PROPERTY);
				state = (State)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_STATE_PROPERTY);
				symbolicName = (String)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME_PROPERTY);
				type = (String)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_TYPE_PROPERTY);
				version = (Version)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_VERSION_PROPERTY);
				this.event = event;
			}
			
			public int getEventType() {
				return event.getType();
			}
			
			public long getId() {
				return id;
			}
			
			public State getState() {
				return state;
			}
			
			public String getSymbolicName() {
				return symbolicName;
			}
			
			public String getType() {
				return type;
			}
			
			public Version getVersion() {
				return version;
			}
		}
		
		private final Map<Long, List<ServiceEventInfo>> subsystemIdToEvents = new HashMap<Long, List<ServiceEventInfo>>();
		
		public void clear() {
			synchronized (subsystemIdToEvents) {
				subsystemIdToEvents.clear();
			}
		}
		
		public ServiceEventInfo poll(long subsystemId) throws InterruptedException {
			return poll(subsystemId, 0);
		}
		
		public ServiceEventInfo poll(long subsystemId, long timeout) throws InterruptedException {
			List<ServiceEventInfo> events;
			synchronized (subsystemIdToEvents) {
				events = subsystemIdToEvents.get(subsystemId);
				if (events == null) {
					events = new ArrayList<ServiceEventInfo>();
					subsystemIdToEvents.put(subsystemId, events);
				}
			}
			synchronized (events) {
				if (events.isEmpty()) {
					events.wait(timeout);
					if (events.isEmpty()) {
						return null;
					}
				}
				return events.remove(0);
			}
		}
		
		public void serviceChanged(ServiceEvent event) {
			Long subsystemId = (Long)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_ID_PROPERTY);
			synchronized (subsystemIdToEvents) {
				List<ServiceEventInfo> events = subsystemIdToEvents.get(subsystemId);
				if (events == null) {
					events = new ArrayList<ServiceEventInfo>();
					subsystemIdToEvents.put(subsystemId, events);
				}
				synchronized (events) {
					events.add(new ServiceEventInfo(event));
					events.notify();
				}
			}
		}
		
		public int size() {
			synchronized (subsystemIdToEvents) {
				return subsystemIdToEvents.size();
			}
		}
	}
	
	@org.ops4j.pax.exam.junit.Configuration
	public static Option[] configuration() {
		Option[] options = options(
				// this is how you set the default log level when using pax
				// logging (logProfile)
				systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("INFO"),
				systemProperty("org.osgi.framework.bsnversion").value("multiple"),
				// Log
				mavenBundle("org.ops4j.pax.logging", "pax-logging-api"),
				mavenBundle("org.ops4j.pax.logging", "pax-logging-service"),
				// Felix mvn url handler
				mavenBundle("org.ops4j.pax.url", "pax-url-mvn"),
				// Bundles
				mavenBundle("org.apache.aries",             "org.apache.aries.util"),
				mavenBundle("org.apache.aries.application", "org.apache.aries.application.api"),
				mavenBundle("org.apache.aries.application", "org.apache.aries.application.modeller").version("1.0.0"),
				mavenBundle("org.apache.aries.application", "org.apache.aries.application.utils"),
				mavenBundle("org.apache.aries.blueprint",   "org.apache.aries.blueprint").version("1.0.0"),
				mavenBundle("org.apache.aries.proxy",       "org.apache.aries.proxy").version("1.0.1-SNAPSHOT"),
				mavenBundle("org.apache.aries.subsystem",   "org.apache.aries.subsystem.api"),
				mavenBundle("org.apache.aries.subsystem",   "org.apache.aries.subsystem.core"),
				mavenBundle("org.apache.aries.subsystem",   "org.apache.aries.subsystem.itest.interfaces"),
				mavenBundle("org.apache.aries.testsupport", "org.apache.aries.testsupport.unit"),
				mavenBundle("org.apache.felix",             "org.apache.felix.resolver"),
				mavenBundle("org.eclipse.equinox",          "org.eclipse.equinox.coordinator").version("1.1.0.v20120522-1841"),
				mavenBundle("org.eclipse.equinox",          "org.eclipse.equinox.event").version("1.2.200.v20120522-2049"),
				mavenBundle("org.eclipse.equinox",          "org.eclipse.equinox.region").version("1.1.0.v20120522-1841"),
				mavenBundle("org.osgi",                     "org.osgi.enterprise").version("5.0.0"),
        //org.ops4j.pax.exam.container.def.PaxRunnerOptions.vmOption("-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=7777"),
				PaxRunnerOptions.rawPaxRunnerOption("config", "classpath:ss-runner.properties"),
				equinox().version("3.8.0.V20120529-1548"));
		options = updateOptions(options);
		return options;
	}
	
	protected final SubsystemEventHandler subsystemEvents = new SubsystemEventHandler();
	
	protected Collection<ServiceRegistration<?>> serviceRegistrations = new ArrayList<ServiceRegistration<?>>();
	
	public void setUp() throws Exception {
		super.setUp();
		try {
			bundleContext.getBundle(0).getBundleContext().addServiceListener(subsystemEvents, '(' + Constants.OBJECTCLASS + '=' + Subsystem.class.getName() + ')');
		}
		catch (InvalidSyntaxException e) {
			fail("Invalid filter: " + e.getMessage());<commit_msg>Remove bundle versions from the configuration() method - they're declared in the pom.xml. <commit_after>/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.aries.subsystem.itests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.ops4j.pax.exam.CoreOptions.equinox;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.aries.subsystem.core.archive.ProvisionPolicyDirective;
import org.apache.aries.subsystem.core.archive.SubsystemTypeHeader;
import org.apache.aries.subsystem.core.internal.BundleResource;
import org.apache.aries.subsystem.core.internal.ResourceHelper;
import org.apache.aries.subsystem.core.internal.SubsystemIdentifier;
import org.apache.aries.subsystem.itests.util.TestRepository;
import org.apache.aries.subsystem.itests.util.Utils;
import org.apache.aries.unittest.fixture.ArchiveFixture;
import org.apache.aries.unittest.fixture.ArchiveFixture.JarFixture;
import org.apache.aries.unittest.fixture.ArchiveFixture.ManifestFixture;
import org.apache.aries.unittest.fixture.ArchiveFixture.ZipFixture;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.container.def.PaxRunnerOptions;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.Version;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.resource.Resource;
import org.osgi.service.repository.Repository;
import org.osgi.service.repository.RepositoryContent;
import org.osgi.service.subsystem.Subsystem;
import org.osgi.service.subsystem.Subsystem.State;
import org.osgi.service.subsystem.SubsystemConstants;

public abstract class SubsystemTest extends IntegrationTest {
	protected static class SubsystemEventHandler implements ServiceListener {
		private static class ServiceEventInfo {
			private final ServiceEvent event;
			private final long id;
			private final State state;
			private final String symbolicName;
			private final String type;
			private final Version version;
			
			public ServiceEventInfo(ServiceEvent event) {
				id = (Long)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_ID_PROPERTY);
				state = (State)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_STATE_PROPERTY);
				symbolicName = (String)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME_PROPERTY);
				type = (String)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_TYPE_PROPERTY);
				version = (Version)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_VERSION_PROPERTY);
				this.event = event;
			}
			
			public int getEventType() {
				return event.getType();
			}
			
			public long getId() {
				return id;
			}
			
			public State getState() {
				return state;
			}
			
			public String getSymbolicName() {
				return symbolicName;
			}
			
			public String getType() {
				return type;
			}
			
			public Version getVersion() {
				return version;
			}
		}
		
		private final Map<Long, List<ServiceEventInfo>> subsystemIdToEvents = new HashMap<Long, List<ServiceEventInfo>>();
		
		public void clear() {
			synchronized (subsystemIdToEvents) {
				subsystemIdToEvents.clear();
			}
		}
		
		public ServiceEventInfo poll(long subsystemId) throws InterruptedException {
			return poll(subsystemId, 0);
		}
		
		public ServiceEventInfo poll(long subsystemId, long timeout) throws InterruptedException {
			List<ServiceEventInfo> events;
			synchronized (subsystemIdToEvents) {
				events = subsystemIdToEvents.get(subsystemId);
				if (events == null) {
					events = new ArrayList<ServiceEventInfo>();
					subsystemIdToEvents.put(subsystemId, events);
				}
			}
			synchronized (events) {
				if (events.isEmpty()) {
					events.wait(timeout);
					if (events.isEmpty()) {
						return null;
					}
				}
				return events.remove(0);
			}
		}
		
		public void serviceChanged(ServiceEvent event) {
			Long subsystemId = (Long)event.getServiceReference().getProperty(SubsystemConstants.SUBSYSTEM_ID_PROPERTY);
			synchronized (subsystemIdToEvents) {
				List<ServiceEventInfo> events = subsystemIdToEvents.get(subsystemId);
				if (events == null) {
					events = new ArrayList<ServiceEventInfo>();
					subsystemIdToEvents.put(subsystemId, events);
				}
				synchronized (events) {
					events.add(new ServiceEventInfo(event));
					events.notify();
				}
			}
		}
		
		public int size() {
			synchronized (subsystemIdToEvents) {
				return subsystemIdToEvents.size();
			}
		}
	}
	
	@org.ops4j.pax.exam.junit.Configuration
	public static Option[] configuration() {
		Option[] options = options(
				// this is how you set the default log level when using pax
				// logging (logProfile)
				systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("INFO"),
				systemProperty("org.osgi.framework.bsnversion").value("multiple"),
				// Log
				mavenBundle("org.ops4j.pax.logging", "pax-logging-api"),
				mavenBundle("org.ops4j.pax.logging", "pax-logging-service"),
				// Felix mvn url handler
				mavenBundle("org.ops4j.pax.url", "pax-url-mvn"),
				// Bundles
				mavenBundle("org.apache.aries",             "org.apache.aries.util"),
				mavenBundle("org.apache.aries.application", "org.apache.aries.application.api"),
				mavenBundle("org.apache.aries.application", "org.apache.aries.application.modeller"),
				mavenBundle("org.apache.aries.application", "org.apache.aries.application.utils"),
				mavenBundle("org.apache.aries.blueprint",   "org.apache.aries.blueprint"),
				mavenBundle("org.apache.aries.proxy",       "org.apache.aries.proxy"),
				mavenBundle("org.apache.aries.subsystem",   "org.apache.aries.subsystem.api"),
				mavenBundle("org.apache.aries.subsystem",   "org.apache.aries.subsystem.core"),
				mavenBundle("org.apache.aries.subsystem",   "org.apache.aries.subsystem.itest.interfaces"),
				mavenBundle("org.apache.aries.testsupport", "org.apache.aries.testsupport.unit"),
				mavenBundle("org.apache.felix",             "org.apache.felix.resolver"),
				mavenBundle("org.eclipse.equinox",          "org.eclipse.equinox.coordinator").version("1.1.0.v20120522-1841"),
				mavenBundle("org.eclipse.equinox",          "org.eclipse.equinox.event").version("1.2.200.v20120522-2049"),
				mavenBundle("org.eclipse.equinox",          "org.eclipse.equinox.region").version("1.1.0.v20120522-1841"),
				mavenBundle("org.osgi",                     "org.osgi.enterprise").version("5.0.0"),
        //org.ops4j.pax.exam.container.def.PaxRunnerOptions.vmOption("-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=7777"),
				PaxRunnerOptions.rawPaxRunnerOption("config", "classpath:ss-runner.properties"),
				equinox().version("3.8.0.V20120529-1548"));
		options = updateOptions(options);
		return options;
	}
	
	protected final SubsystemEventHandler subsystemEvents = new SubsystemEventHandler();
	
	protected Collection<ServiceRegistration<?>> serviceRegistrations = new ArrayList<ServiceRegistration<?>>();
	
	public void setUp() throws Exception {
		super.setUp();
		try {
			bundleContext.getBundle(0).getBundleContext().addServiceListener(subsystemEvents, '(' + Constants.OBJECTCLASS + '=' + Subsystem.class.getName() + ')');
		}
		catch (InvalidSyntaxException e) {
			fail("Invalid filter: " + e.getMessage());
{'fraction_non_alphanumeric': 0.11910048391688016, 'fraction_numerical': 0.010418445772843723, 'mean_word_length': 4.529115517784073, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, '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': '6207650', 'n_tokens_mistral': 5978, 'n_tokens_neox': 5239, 'n_words': 957}