package org.ops4j.pax.web.itest.jetty;

import static org.ops4j.pax.exam.CoreOptions.streamBundle;
import static org.ops4j.pax.exam.OptionUtils.combine;
import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.web.itest.base.support.AnnotatedTestServlet;
import org.ops4j.pax.web.service.WebContainerConstants;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;


/**
 * @author Achim Nierbeck (anierbeck)
 * @since Dec 30, 2012
 */
@RunWith(PaxExam.class)
public class ServletAnnotatedIntegrationTest extends ITestBase {

	@Configuration
	public static Option[] configure() {
		return combine(configureJetty(), 
				streamBundle(bundle()
		                .add(AnnotatedTestServlet.class)
		                .set(Constants.BUNDLE_SYMBOLICNAME, "AnnotatedServletTest")
		                .set(WebContainerConstants.CONTEXT_PATH_KEY, "/annotatedTest")
		                .set(Constants.IMPORT_PACKAGE, "javax.servlet")
		                .set(Constants.DYNAMICIMPORT_PACKAGE, "*")
		                .build()));
	}

	@Before
	public void setUp() throws 	Exception {
		waitForServer("http://127.0.0.1:8181/");
	}

	@After
	public void tearDown() throws BundleException {
	}

	/**
	 * You will get a list of bundles installed by default plus your testcase,
	 * wrapped into a bundle called pax-exam-probe
	 */
	@Test
	public void listBundles() {
		for (Bundle b : bundleContext.getBundles()) {
			System.out.println("Bundle " + b.getBundleId() + " : "
					+ b.getSymbolicName());
		}

	}

	@Test
	public void testBundle1() throws Exception {

		testWebPath("http://127.0.0.1:8181/annotatedTest/test", "TEST OK");
		
	}
}

@@ -253,19 +253,69 @@
 g.junit.
-Bef
+Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Ign
 ore;%0Aimp
@@ -1460,16 +1460,541 @@
 181/%22);%0A
+%09%09Bundle%5B%5D bundles = bundleContext.getBundles();%0A%09%09Bundle streamBundle = null; %0A%09%09for (Bundle bundle : bundles) %7B%0A%09%09%09if (%22AnnotatedServletTest%22.equalsIgnoreCase(bundle.getSymbolicName())) %7B%0A%09%09%09%09streamBundle = bundle;%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (streamBundle != null) %7B%0A%09%09%09int state = streamBundle.getState();%0A%09%09%09int i = 0;%0A%09%09%09while (state != Bundle.ACTIVE && i %3C 50) %7B%0A%09%09%09%09Thread.sleep(200);%0A%09%09%09%09state = streamBundle.getState();%0A%09%09%09%7D%0A%09%09%09if (i == 50)%0A%09%09%09%09Assert.fail(%22waiting for AnnotatedServletTest bundle took to long%22);%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Af
@@ -2183,32 +2183,114 @@
 obe%0A%09 */%0A%09@Test%0A
+%09@Ignore(%22As it happens this test sometimes causes the other test to fail, too.%22)%0A
 %09public void lis

test failed every now and then