package org.apache.aries.subsystem.itests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.OptionUtils.combine;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.service.subsystem.Subsystem;
import org.osgi.service.subsystem.SubsystemException;

/*
 * Simple iTest for dynamic imports. In the first instance we'll use a 
 * DynamicImport-Package header because it's the simplest to set up. 
 * _Hopefully_ if this works, then packages added by WeavingProxy services
 * will also work. If not, we'll need extra tests :-/ 
 */
@RunWith(JUnit4TestRunner.class)
public class DynamicImportTest extends SubsystemTest 
{
	private static boolean _testAppCreated = false;
	
	@Before
	public void setUp() throws Exception 
	{
		super.setUp();
		if (!_testAppCreated) { 
			createApplication("dynamicImport", new String[]{"dynamicImport.jar"});
			_testAppCreated = true;
		}
	}

	/*
	 * Install an .esa containing a bundle with a BundleActivator, and a 
	 * DynamicImport-Package on org.osgi.framework. This app should fail to 
	 * start unless we do something to stop the SubsystemResolverHook 
	 * from preventing this package from wiring. 
	 */
	@Test
	public void verifyThatDynamicImportNeedsHandling() throws Exception
	{
		
		System.out.println ("Into verifyThatDynamicImportNeedsHandling");
		
		Subsystem subsystem = installSubsystemFromFile ("dynamicImport.esa");
		try { 
			startSubsystem(subsystem);
			Bundle[] bundles = subsystem.getBundleContext().getBundles();
			for (Bundle b : bundles) { 
				System.out.println (b.getSymbolicName() + " -> " + b.getState());
			}
			fail ("dynamicImport.esa started when we didn't expect it to");
		} catch (SubsystemException sx) { 
			Throwable cause = sx.getCause();
			assertTrue("BundleException expected", cause instanceof BundleException);
		}
	}
/*	
	@Test
	public void testHello() throws Exception 
	{
		Subsystem subsystem = installSubsystemFromFile("hello.esa");
		try {
			subsystem.start();
			BundleContext bc = subsystem.getBundleContext();
			Hello h = getOsgiService(bc, Hello.class, null, DEFAULT_TIMEOUT);
			String message = h.saySomething();
			assertEquals ("Wrong message back", "something", message);
			subsystem.stop();
		}
		finally {
			uninstallSubsystemSilently(subsystem);
		}
	} */
	
	protected static Option[] updateOptions(Option[] options) 
	{
		Option[] helloOptions = options(
				mavenBundle("org.apache.aries.subsystem", "org.apache.aries.subsystem.itest.interfaces")
//				org.ops4j.pax.exam.container.def.PaxRunnerOptions.vmOption("-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"),
		);
		return combine(helloOptions, options);
	}
}

@@ -1343,163 +1343,187 @@
 org.
-osgi.framework. This app should fail to %0D%0A%09 * start unless we do something to stop the SubsystemResolverHook %0D%0A%09 * from preventing this package from wiring
+apache.aries.subsystem.itests.hello.api.%0D%0A%09 * This app should fail to start because we've not yet intervened to permit %0D%0A%09 * this dynamic package wiring requirement from being met
 . %0D%0A
@@ -2210,485 +2210,8 @@
 %09%7D%0D%0A
-/*%09%0D%0A%09@Test%0D%0A%09public void testHello() throws Exception %0D%0A%09%7B%0D%0A%09%09Subsystem subsystem = installSubsystemFromFile(%22hello.esa%22);%0D%0A%09%09try %7B%0D%0A%09%09%09subsystem.start();%0D%0A%09%09%09BundleContext bc = subsystem.getBundleContext();%0D%0A%09%09%09Hello h = getOsgiService(bc, Hello.class, null, DEFAULT_TIMEOUT);%0D%0A%09%09%09String message = h.saySomething();%0D%0A%09%09%09assertEquals (%22Wrong message back%22, %22something%22, message);%0D%0A%09%09%09subsystem.stop();%0D%0A%09%09%7D%0D%0A%09%09finally %7B%0D%0A%09%09%09uninstallSubsystemSilently(subsystem);%0D%0A%09%09%7D%0D%0A%09%7D */%0D%0A
 %09%0D%0A%09

Tidy up first test. Still investigating how to fix this.