Skip to content

Commit

Permalink
WELD-1155 Testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting authored and stuartwdouglas committed Aug 1, 2012
1 parent e25839f commit ce75082
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 0 deletions.
@@ -0,0 +1,44 @@
/*
* 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.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.Bean;

public class Producer {

private static final CountDownLatch latch = new CountDownLatch(2);

@Produces
@SessionScoped
public static Product produceTrickyProduct(Bean<Product> bean) {
try {
latch.countDown();
if (latch.await(5, TimeUnit.SECONDS)) {
throw new IllegalStateException("Producer method " + bean.toString() + " invoked twice for the same session.");
} else {
return new Product();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,30 @@
/*
* 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.io.Serializable;

import javax.enterprise.inject.Veto;

@Veto
public class Product implements Serializable {

private static final long serialVersionUID = 6628439341707781817L;

public void ping() {
}
}
@@ -0,0 +1,42 @@
/*
* 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.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;

@SessionScoped
public class SessionScopedBean implements Serializable {

private static final long serialVersionUID = -3019516240186020733L;

private static final AtomicInteger ids = new AtomicInteger();

private volatile int id;

@PostConstruct
public void init() {
this.id = ids.incrementAndGet();
}

public int getId() {
return id;
}
}
@@ -0,0 +1,97 @@
/*
* 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.Ignore;
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)
@Ignore("WELD-1155")
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;
}
}
}
@@ -0,0 +1,63 @@
/*
* 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.io.IOException;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/")
public class TestServlet extends HttpServlet {

private static final long serialVersionUID = -2993633416781607366L;

@Inject
private Product product;

@Inject
private SessionScopedBean bean;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain");

// this forces session creation
// we reuse the session in the next request

if (!req.getRequestURI().endsWith("initial")) {
try {
product.ping();
} catch (Exception e) {
resp.setStatus(500);
resp.getWriter().println(e.getMessage());
return;
}
}
// this check is not necessary but just in case...
if (bean.getId() > 1) {
resp.setStatus(500);
resp.getWriter().print("Bean id > 1");
return;
}
resp.getWriter().println("Here you are: " + bean.getId());
}
}

0 comments on commit ce75082

Please sign in to comment.