From 9ee4984a2db41e17e10ab098bbc0f1689ffcaf1b Mon Sep 17 00:00:00 2001
From: Frank Cornelis <info@e-contract.be>
Date: Fri, 24 Jan 2014 14:23:30 +0100
Subject: [PATCH] integration test for JACC PolicyContext

---
 .../security/jacc/context/HelloBean.java           | 15 +++++
 .../security/jacc/context/HelloServlet.java        | 31 +++++++++
 .../jacc/context/PolicyContextTestCase.java        | 76 ++++++++++++++++++++++
 .../integration/security/jacc/context/ejb-jar.xml  |  8 +++
 4 files changed, 130 insertions(+)
 create mode 100644 testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/HelloBean.java
 create mode 100644 testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/HelloServlet.java
 create mode 100644 testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/PolicyContextTestCase.java
 create mode 100644 testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/ejb-jar.xml

diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/HelloBean.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/HelloBean.java
new file mode 100644
index 0000000..d0fa395
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/HelloBean.java
@@ -0,0 +1,15 @@
+package org.jboss.as.test.integration.security.jacc.context;
+
+import javax.ejb.Stateless;
+import javax.security.jacc.PolicyContext;
+import javax.security.jacc.PolicyContextException;
+import javax.servlet.http.HttpServletRequest;
+
+@Stateless
+public class HelloBean {
+
+    public void getHttpServletRequest() throws PolicyContextException {
+        HttpServletRequest httpServletRequest = (HttpServletRequest) PolicyContext
+                .getContext("javax.servlet.http.HttpServletRequest");
+    }
+}
diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/HelloServlet.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/HelloServlet.java
new file mode 100644
index 0000000..0faa212
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/HelloServlet.java
@@ -0,0 +1,31 @@
+package org.jboss.as.test.integration.security.jacc.context;
+
+import java.io.IOException;
+
+import javax.ejb.EJB;
+import javax.security.jacc.PolicyContextException;
+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(urlPatterns = { HelloServlet.SERVLET_PATH })
+public class HelloServlet extends HttpServlet {
+
+    private static final long serialVersionUID = 1L;
+
+    public static final String SERVLET_PATH = "/test";
+
+    @EJB
+    private HelloBean helloBean;
+
+    @Override
+    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+        try {
+            this.helloBean.getHttpServletRequest();
+        } catch (PolicyContextException e) {
+            throw new ServletException("JACC error: " + e.getMessage(), e);
+        }
+    }
+}
diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/PolicyContextTestCase.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/PolicyContextTestCase.java
new file mode 100644
index 0000000..a0927d1
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/PolicyContextTestCase.java
@@ -0,0 +1,76 @@
+package org.jboss.as.test.integration.security.jacc.context;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.container.test.api.OperateOnDeployment;
+import org.jboss.arquillian.container.test.api.RunAsClient;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.logging.Logger;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(Arquillian.class)
+@RunAsClient
+public class PolicyContextTestCase {
+
+    private static Logger LOGGER = Logger.getLogger(PolicyContextTestCase.class);
+
+    @Deployment(name = "ear")
+    public static EnterpriseArchive earDeployment() {
+        LOGGER.info("Start EAR deployment");
+        final String earName = "ear-jacc-context";
+
+        final EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, earName + ".ear");
+        final JavaArchive jar = createJar(earName);
+        final WebArchive war = createWar(earName);
+        ear.addAsModule(war);
+        ear.addAsModule(jar);
+
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug(ear.toString(true));
+        }
+        return ear;
+    }
+
+    @Test
+    @OperateOnDeployment("ear")
+    public void testHttpServletRequest(@ArquillianResource URL webAppURL) throws Exception {
+        invoke(webAppURL);
+    }
+
+    private void invoke(final URL webAppURL) throws MalformedURLException, IOException {
+        final URL servletURL = new URL(webAppURL.toExternalForm().substring(0, webAppURL.toExternalForm().length() - 1) + ".war/" + HelloServlet.SERVLET_PATH.substring(1));
+        LOGGER.info("Testing JACC permissions: " + servletURL);
+        final InputStream is = servletURL.openStream();
+        try {
+
+        } finally {
+            is.close();
+        }
+    }
+
+    private static JavaArchive createJar(final String jarName) {
+        final JavaArchive jar = ShrinkWrap.create(JavaArchive.class, jarName + ".jar");
+        jar.addClasses(HelloBean.class);
+        jar.addAsManifestResource(PolicyContextTestCase.class.getPackage(), "ejb-jar.xml", "ejb-jar.xml");
+        return jar;
+    }
+
+    private static WebArchive createWar(final String warName) {
+        final WebArchive war = ShrinkWrap.create(WebArchive.class, warName + ".war");
+        war.addClass(HelloServlet.class);
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug(war.toString(true));
+        }
+        return war;
+    }
+}
diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/ejb-jar.xml b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/ejb-jar.xml
new file mode 100644
index 0000000..0fb77ab
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/jacc/context/ejb-jar.xml
@@ -0,0 +1,8 @@
+<?xml version="1.1" encoding="UTF-8"?>
+<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
+         version="3.1">
+
+</ejb-jar>
+
-- 
1.7.11.7

