<commit_before>/*
 * Copyright (c) OSGi Alliance (2011). All Rights Reserved.
 * 
 * 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.osgi.test.cases.coordinator.junit;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.service.coordinator.Coordination;
import org.osgi.service.coordinator.CoordinationException;
import org.osgi.service.coordinator.Coordinator;
import org.osgi.service.coordinator.Participant;
import org.osgi.test.support.concurrent.AtomicReference;

/**
 * Implementations must detect orphaned coordinations (i.e. coordinations that
 * have been dereferenced and are eligible for garbage collection) and fail
 * them with an ORPHANED failure reason.
 */
public class OrphanedCoordinationTest extends CoordinatorTest {
	private Bundle coordinatorBundle;
	
	/**
	 * Test explicit coordination.
	 * 
	 * @throws InterruptedException
	 * @throws BundleException 
	 */
	public void testOrphanedCoordinationExplicit() throws InterruptedException, BundleException {
		Coordination c = coordinator.create("c", 0);
		long id = c.getId();
		String name = c.getName();
		Bundle bundle = c.getBundle();
		OrphanedParticipant p = new OrphanedParticipant();
		c.addParticipant(p);
		Reference<Coordination> reference = new WeakReference<Coordination>(c, new ReferenceQueue<Coordination>());
		c = null;
		assertReferenceEnqueued(reference);
		coordinatorReference.getBundle().stop();
		assertOrphanedCoordination(p, id, name, bundle);
	}
	
	/**
	 * Test implicit coordination.
	 * 
	 * @throws InterruptedException
	 * @throws BundleException 
	 */
	public void testOrphanedCoordinationImplicit() throws InterruptedException, BundleException {
		assertEmptyStack();
		Coordination c = coordinator.begin("c", 0);
		assertAtTopOfStack(c);
		long id = c.getId();
		String name = c.getName();
		Bundle bundle = c.getBundle();
		OrphanedParticipant p = new OrphanedParticipant();
		c.addParticipant(p);
		Reference<Coordination> reference = new WeakReference<Coordination>(c, new ReferenceQueue<Coordination>());
		c = null;
		assertReferenceEnqueued(reference);
		Bundle b = coordinatorReference.getBundle();
		b.stop();
		assertOrphanedCoordination(p, id, name, bundle);
		b.start();
		coordinatorReference = getContext().getServiceReference(Coordinator.class);
		coordinator = getContext().getService(coordinatorReference);
		assertEmptyStack();
	}
	
	protected void setUp() throws Exception {
		super.setUp();
		coordinatorBundle = coordinatorReference.getBundle();
	}
	
	protected void tearDown() throws Exception {
		coordinatorBundle.start();
		super.tearDown();
	}
	<commit_msg>Bug 2158: [coordinator] Coordinator CT needs to test new orphaned coordination requirement.<commit_after>/*
 * Copyright (c) OSGi Alliance (2011). All Rights Reserved.
 * 
 * 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.osgi.test.cases.coordinator.junit;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.service.coordinator.Coordination;
import org.osgi.service.coordinator.CoordinationException;
import org.osgi.service.coordinator.Coordinator;
import org.osgi.service.coordinator.Participant;
import org.osgi.test.support.concurrent.AtomicReference;

/**
 * Implementations must detect orphaned coordinations (i.e. coordinations that
 * have been dereferenced and are eligible for garbage collection) and fail
 * them with an ORPHANED failure reason.
 */
public class OrphanedCoordinationTest extends CoordinatorTest {
	private Bundle coordinatorBundle;
	
	/**
	 * Test explicit coordination.
	 * 
	 * @throws InterruptedException
	 * @throws BundleException 
	 */
	public void testOrphanedCoordinationExplicit() throws InterruptedException, BundleException {
		Coordination c = coordinator.create("c", 0);
		long id = c.getId();
		String name = c.getName();
		Bundle bundle = c.getBundle();
		OrphanedParticipant p = new OrphanedParticipant();
		c.addParticipant(p);
		Reference<Coordination> reference = new WeakReference<Coordination>(c, new ReferenceQueue<Coordination>());
		c = null;
		assertReferenceEnqueued(reference);
		coordinatorBundle.stop();
		assertOrphanedCoordination(p, id, name, bundle);
	}
	
	/**
	 * Test implicit coordination.
	 * 
	 * @throws InterruptedException
	 * @throws BundleException 
	 */
	public void testOrphanedCoordinationImplicit() throws InterruptedException, BundleException {
		assertEmptyStack();
		Coordination c = coordinator.begin("c", 0);
		assertAtTopOfStack(c);
		long id = c.getId();
		String name = c.getName();
		Bundle bundle = c.getBundle();
		OrphanedParticipant p = new OrphanedParticipant();
		c.addParticipant(p);
		Reference<Coordination> reference = new WeakReference<Coordination>(c, new ReferenceQueue<Coordination>());
		c = null;
		assertReferenceEnqueued(reference);
		coordinatorBundle.stop();
		assertOrphanedCoordination(p, id, name, bundle);
		coordinatorBundle.start();
		coordinatorReference = getContext().getServiceReference(Coordinator.class);
		coordinator = getContext().getService(coordinatorReference);
		assertEmptyStack();
	}
	
	protected void setUp() throws Exception {
		super.setUp();
		coordinatorBundle = coordinatorReference.getBundle();
	}
	
	protected void tearDown() throws Exception {
		coordinatorBundle.start();
		super.tearDown();
	}
	
{'fraction_non_alphanumeric': 0.10003049710277524, 'fraction_numerical': 0.0036596523330283625, 'mean_word_length': 5.011915673693859, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5112468', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1785, 'n_words': 524}