/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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.jboss.weld.tests.contexts.session.weld1155;

import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.weld.tests.category.Integration;
import org.junit.After;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import com.gargoylesoftware.htmlunit.TextPage;
import com.gargoylesoftware.htmlunit.WebClient;

/**
 * @see https://issues.jboss.org/browse/WELD-1155
 *
 * @author Jozef Hartinger
 *
 */
@RunWith(Arquillian.class)
@Category(Integration.class)
public class SessionScopedProducerTest {

    @ArquillianResource
    private volatile URL url;

    private final WebClient client = new WebClient();
    
    private final ExecutorService executor = Executors.newCachedThreadPool();

    @Deployment(testable = false)
    public static Archive<?> getDeployment() {
        return ShrinkWrap.create(WebArchive.class).addClasses(Producer.class, Product.class, TestServlet.class, SessionScopedBean.class)
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }

    @Test
    public void test() throws Exception {
        client.getPage(url.toString() + "/initial");
        client.setThrowExceptionOnFailingStatusCode(false);

        List<Callable<Void>> requests = new LinkedList<Callable<Void>>();
        requests.add(new ConcurrentRequest());
        requests.add(new ConcurrentRequest());

        for (Future<Void> result : executor.invokeAll(requests)) {
            result.get();
        }
    }

    @After
    public void shutdown() {
        executor.shutdownNow();
    }

    private class ConcurrentRequest implements Callable<Void> {

        public Void call() throws Exception {
            TextPage page = client.getPage(url);
            if (page.getWebResponse().getStatusCode() == 500) {
                throw new RuntimeException(page.getContent());
            }
            return null;
        }
    }
}

@@ -895,24 +895,46 @@
 .util.List;%0A
+import java.util.Set;%0A
 import java.
@@ -1080,16 +1080,66 @@
 uture;%0A%0A
+import com.gargoylesoftware.htmlunit.util.Cookie;%0A
 import o
@@ -1695,20 +1695,16 @@
 tmlunit.
-Text
 Page;%0Aim
@@ -2048,20 +2048,16 @@
 ient();%0A
-    
 %0A    pri
@@ -2537,24 +2537,46 @@
 %0A       
+ Set%3CCookie%3E cookies =
  client.
 setThrow
@@ -2571,50 +2571,38 @@
 ent.
-setThrowExceptionOnFailingStatusCode(false
+getCookieManager().getCookies(
 );%0A%0A
@@ -2714,24 +2714,31 @@
 rentRequest(
+cookies
 ));%0A        
@@ -2772,16 +2772,23 @@
 Request(
+cookies
 ));%0A%0A   
@@ -3034,24 +3034,353 @@
 le%3CVoid%3E %7B%0A%0A
+        private final WebClient client;%0A%0A        public ConcurrentRequest(Set%3CCookie%3E cookies) %7B%0A            client = new WebClient();%0A            client.setThrowExceptionOnFailingStatusCode(false);%0A            for (Cookie cookie : cookies) %7B%0A                client.getCookieManager().addCookie(cookie);%0A            %7D%0A        %7D%0A%0A
         publ
@@ -3429,12 +3429,8 @@
     
-Text
 Page
@@ -3577,15 +3577,40 @@
 .get
-Content
+WebResponse().getContentAsString
 ());

Fix SessionScopedProducerTest (mkouba pointed out that WebClient is not threadsafe)