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.Assert;
import org.junit.Before;
import org.junit.Ignore;
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/");
		Bundle[] bundles = bundleContext.getBundles();
		Bundle streamBundle = null; 
		for (Bundle bundle : bundles) {
			if ("AnnotatedServletTest".equalsIgnoreCase(bundle.getSymbolicName())) {
				streamBundle = bundle;
				break;
			}
		}
		if (streamBundle != null) {
			int state = streamBundle.getState();
			int i = 0;
			while (state != Bundle.ACTIVE && i < 50) {
				Thread.sleep(200);
				state = streamBundle.getState();
			}
			if (i == 50)
				Assert.fail("waiting for AnnotatedServletTest bundle took to long");
		}
	}

	@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
	@Ignore("As it happens this test sometimes causes the other test to fail, too.")
	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");
		
	}
}

@@ -483,16 +483,68 @@
 axExam;%0A
+import org.ops4j.pax.exam.testforge.WaitForService;%0A
 import o
@@ -1516,532 +1516,73 @@
 %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%09initServletListener(%22test%22);%0A%09%09%0A%09%09waitForServletListener(
 );%0A%09%09
-%7D
 %0A%09%7D%0A
@@ -1781,97 +1781,15 @@
  */%0A
+
 %09@Test%0A
-%09@Ignore(%22As it happens this test sometimes causes the other test to fail, too.%22)%0A
 %09pub

fix testing issue ...