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.apache.aries.subsystem.itests.hello.api.
	 * This app should fail to start because we've not yet intervened to permit 
	 * this dynamic package wiring requirement from being met. 
	 */
	@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);
		}
	}
	
	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);
	}
}

@@ -344,32 +344,80 @@
 x.exam.Option;%0D%0A
+import org.ops4j.pax.exam.junit.Configuration;%0D%0A
 import org.ops4j
@@ -2262,97 +2262,76 @@
 %09%0D%0A%09
-protected static Option%5B%5D updateOptions(Option%5B%5D options) %0D%0A%09%7B%0D%0A%09%09Option%5B%5D helloOptions =
+@Configuration%0D%0A%09public static Option%5B%5D extraConfig() %0D%0A%09%7B%0D%0A%09%09return
  opt
@@ -2566,23 +2566,22 @@
 ess=
-5005
+7777
 %22)
-,
 %0D%0A%09%09);%0D%0A
 %09%09re
@@ -2580,50 +2580,8 @@
 );%0D%0A
-%09%09return combine(helloOptions, options);%0D%0A
 %09%7D%0D%0A

Fix test setup logic.