Permalink
Browse files

JSFUNIT-274 Add a Arquillian based JSFUnit TestEnricher SPI impl

- Remove the need for CDI to handle Injection
- Split support in Arquillian and CDI
- Add CDI JSFUnit support if WebArchive is CDI enabled as a workaround for ARQ-405
- Move User facing API from org.jboss.jsfunit.cdi to org.jboss.jsfunit.api
- Add @JSFUnitResource injection point as a counter part to @Inject for non CDI enrichment

git-svn-id: https://svn.jboss.org/repos/jsfunit/trunk@572 992343fe-802f-0410-85da-d31fbad5f0a6
  • Loading branch information...
1 parent bf9f20f commit fc5ef0f11a512c5479040305e5b881d192599187 @aslakknutsen aslakknutsen committed Apr 5, 2011
Showing with 829 additions and 273 deletions.
  1. +2 −2 ...arquillian/hellojsf/src/test/java/org/jboss/jsfunit/example/hellojsf/BasicAuthenticationTest.java
  2. +9 −15 examples-arquillian/hellojsf/src/test/java/org/jboss/jsfunit/example/hellojsf/FacadeAPITest.java
  3. +2 −2 ...-arquillian/hellojsf/src/test/java/org/jboss/jsfunit/example/hellojsf/FormAuthenticationTest.java
  4. +5 −7 examples-arquillian/hellojsf/src/test/java/org/jboss/jsfunit/example/hellojsf/NoCDITest.java
  5. +1 −1 gettingstarted/src/test/java/org/jboss/jsfunit/example/hellojsf/HelloJSFTest.java
  6. +1 −1 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/{cdi → api}/BasicAuthentication.java
  7. +1 −1 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/{cdi → api}/Browser.java
  8. +1 −1 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/{cdi → api}/BrowserVersion.java
  9. +1 −1 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/{cdi → api}/Cookies.java
  10. +1 −1 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/{cdi → api}/FormAuthentication.java
  11. +1 −1 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/{cdi → api}/InitialPage.java
  12. +46 −0 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/api/InitialRequest.java
  13. +6 −6 ...t-arquillian/src/main/java/org/jboss/jsfunit/{cdi/InitialRequest.java → api/JSFUnitResource.java}
  14. +1 −1 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/{cdi → api}/Proxy.java
  15. +16 −4 ...an/src/main/java/org/jboss/jsfunit/arquillian/{ → client}/JSFUnitApplicationArchiveProcessor.java
  16. +8 −3 ...nit-arquillian/src/main/java/org/jboss/jsfunit/arquillian/{ → client}/JSFUnitArchiveAppender.java
  17. +1 −1 ...lian/src/main/java/org/jboss/jsfunit/arquillian/{ → container}/JSFUnitCleanupTestTreadFilter.java
  18. +290 −0 ...sfunit-arquillian/src/main/java/org/jboss/jsfunit/arquillian/container/JSFUnitSessionFactory.java
  19. +114 −0 ...-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/arquillian/container/JSFUnitTestEnricher.java
  20. +249 −0 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/arquillian/container/SecurityActions.java
  21. +0 −220 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/cdi/JSFSessionFactory.java
  22. +70 −0 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/cdi/JSFUnitCDIProducer.java
  23. 0 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/shrinkwrap/MavenArtifactResolver.java
  24. +0 −1 ...uillian/src/main/resources/META-INF/services/org.jboss.arquillian.spi.ApplicationArchiveProcessor
  25. +0 −1 ...arquillian/src/main/resources/META-INF/services/org.jboss.arquillian.spi.AuxiliaryArchiveAppender
  26. +1 −1 ...esources/META-INF/services/org.jboss.arquillian.spi.client.deployment.ApplicationArchiveProcessor
  27. +1 −1 ...n/resources/META-INF/services/org.jboss.arquillian.spi.client.deployment.AuxiliaryArchiveAppender
  28. +1 −1 jboss-jsfunit-arquillian/src/main/resources/arquillian/web-fragment.xml
View
4 ...an/hellojsf/src/test/java/org/jboss/jsfunit/example/hellojsf/BasicAuthenticationTest.java
@@ -26,8 +26,8 @@
import junit.framework.Assert;
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.jsfunit.cdi.InitialPage;
-import org.jboss.jsfunit.cdi.BasicAuthentication;
+import org.jboss.jsfunit.api.BasicAuthentication;
+import org.jboss.jsfunit.api.InitialPage;
import org.jboss.jsfunit.jsfsession.JSFClientSession;
import org.jboss.jsfunit.jsfsession.JSFServerSession;
import org.jboss.shrinkwrap.api.spec.WebArchive;
View
24 ...s-arquillian/hellojsf/src/test/java/org/jboss/jsfunit/example/hellojsf/FacadeAPITest.java
@@ -40,12 +40,12 @@
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.jsfunit.cdi.Browser;
-import org.jboss.jsfunit.cdi.BrowserVersion;
-import org.jboss.jsfunit.cdi.Cookies;
-import org.jboss.jsfunit.cdi.InitialPage;
-import org.jboss.jsfunit.cdi.InitialRequest;
-import org.jboss.jsfunit.cdi.Proxy;
+import org.jboss.jsfunit.api.Browser;
+import org.jboss.jsfunit.api.BrowserVersion;
+import org.jboss.jsfunit.api.Cookies;
+import org.jboss.jsfunit.api.InitialPage;
+import org.jboss.jsfunit.api.InitialRequest;
+import org.jboss.jsfunit.api.Proxy;
import org.jboss.jsfunit.jsfsession.ComponentIDNotFoundException;
import org.jboss.jsfunit.jsfsession.JSFClientSession;
import org.jboss.jsfunit.jsfsession.JSFServerSession;
@@ -78,15 +78,9 @@
@Deployment
public static WebArchive createDeployment() {
WebArchive war =
- ShrinkWrap.create(WebArchive.class, "test.war")
+ ShrinkWrap.create(WebArchive.class)
.setWebXML(new File("src/main/webapp/WEB-INF/web.xml"))
.addPackage(Package.getPackage("org.jboss.jsfunit.example.hellojsf"))
- /* .addClasses(org.slf4j.impl.Log4jLoggerAdapter.class,
- org.slf4j.impl.Log4jLoggerFactory.class,
- org.slf4j.impl.Log4jMDCAdapter.class,
- org.slf4j.impl.StaticLoggerBinder.class,
- org.slf4j.impl.StaticMarkerBinder.class,
- org.slf4j.impl.StaticMDCBinder.class) */
.addAsWebResource(new File("src/main/webapp", "index.xhtml"))
.addAsWebResource(new File("src/main/webapp", "finalgreeting.xhtml"))
@@ -390,7 +384,7 @@ public void testSelectManyListbox(JSFClientSession client, JSFServerSession serv
HtmlSelectManyListbox listBox = (HtmlSelectManyListbox)server.findComponent("Weekdays");
Object[] selectedValues = listBox.getSelectedValues();
Assert.assertEquals(3, selectedValues.length);
- List listOfValues = Arrays.asList(selectedValues);
+ List<Object> listOfValues = Arrays.asList(selectedValues);
Assert.assertTrue(listOfValues.contains("Monday"));
Assert.assertFalse(listOfValues.contains("Tuesday"));
Assert.assertTrue(listOfValues.contains("Wednesday"));
@@ -416,7 +410,7 @@ public void testSelectManyListboxWithItemList(JSFClientSession client, JSFServer
HtmlSelectManyListbox listBox = (HtmlSelectManyListbox)server.findComponent("WeekdaysUsingItemList");
Object[] selectedValues = listBox.getSelectedValues();
Assert.assertEquals(3, selectedValues.length);
- List listOfValues = Arrays.asList(selectedValues);
+ List<Object> listOfValues = Arrays.asList(selectedValues);
Assert.assertTrue(listOfValues.contains("Monday"));
Assert.assertFalse(listOfValues.contains("Tuesday"));
Assert.assertTrue(listOfValues.contains("Wednesday"));
View
4 ...ian/hellojsf/src/test/java/org/jboss/jsfunit/example/hellojsf/FormAuthenticationTest.java
@@ -27,8 +27,8 @@
import junit.framework.Assert;
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.jsfunit.cdi.InitialPage;
-import org.jboss.jsfunit.cdi.FormAuthentication;
+import org.jboss.jsfunit.api.FormAuthentication;
+import org.jboss.jsfunit.api.InitialPage;
import org.jboss.jsfunit.jsfsession.JSFClientSession;
import org.jboss.jsfunit.jsfsession.JSFServerSession;
import org.jboss.shrinkwrap.api.spec.WebArchive;
View
12 examples-arquillian/hellojsf/src/test/java/org/jboss/jsfunit/example/hellojsf/NoCDITest.java
@@ -23,11 +23,13 @@
package org.jboss.jsfunit.example.hellojsf;
import java.io.IOException;
+
import junit.framework.Assert;
+
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.jsfunit.api.InitialPage;
import org.jboss.jsfunit.jsfsession.JSFServerSession;
-import org.jboss.jsfunit.jsfsession.JSFSession;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -45,17 +47,13 @@
@Deployment
public static WebArchive createDeployment() {
- // The following line will work around JSFUNIT-269
- //Class clazz = org.jboss.jsfunit.cdi.InitialPage.class;
return FacadeAPITest.createDeployment();
}
@Test
- public void testGetCurrentViewId() throws IOException
+ @InitialPage("/index.faces")
+ public void testGetCurrentViewId(JSFServerSession server) throws IOException
{
- JSFSession jsfSession = new JSFSession("/index.faces");
- JSFServerSession server = jsfSession.getJSFServerSession();
-
// Test navigation to initial viewID
Assert.assertEquals("/index.xhtml", server.getCurrentViewID());
Assert.assertEquals(server.getCurrentViewID(), server.getFacesContext().getViewRoot().getViewId());
View
2 gettingstarted/src/test/java/org/jboss/jsfunit/example/hellojsf/HelloJSFTest.java
@@ -31,7 +31,7 @@
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.jsfunit.cdi.InitialPage;
+import org.jboss.jsfunit.api.InitialPage;
import org.jboss.jsfunit.jsfsession.JSFClientSession;
import org.jboss.jsfunit.jsfsession.JSFServerSession;
import org.jboss.shrinkwrap.api.ShrinkWrap;
View
2 ...boss/jsfunit/cdi/BasicAuthentication.java → ...boss/jsfunit/api/BasicAuthentication.java
@@ -20,7 +20,7 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
-package org.jboss.jsfunit.cdi;
+package org.jboss.jsfunit.api;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
View
2 ...n/java/org/jboss/jsfunit/cdi/Browser.java → ...n/java/org/jboss/jsfunit/api/Browser.java
@@ -20,7 +20,7 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
-package org.jboss.jsfunit.cdi;
+package org.jboss.jsfunit.api;
import com.gargoylesoftware.htmlunit.BrowserVersion;
View
2 ...org/jboss/jsfunit/cdi/BrowserVersion.java → ...org/jboss/jsfunit/api/BrowserVersion.java
@@ -20,7 +20,7 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
-package org.jboss.jsfunit.cdi;
+package org.jboss.jsfunit.api;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
View
2 ...n/java/org/jboss/jsfunit/cdi/Cookies.java → ...n/java/org/jboss/jsfunit/api/Cookies.java
@@ -20,7 +20,7 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
-package org.jboss.jsfunit.cdi;
+package org.jboss.jsfunit.api;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
View
2 ...jboss/jsfunit/cdi/FormAuthentication.java → ...jboss/jsfunit/api/FormAuthentication.java
@@ -20,7 +20,7 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
-package org.jboss.jsfunit.cdi;
+package org.jboss.jsfunit.api;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
View
2 ...va/org/jboss/jsfunit/cdi/InitialPage.java → ...va/org/jboss/jsfunit/api/InitialPage.java
@@ -20,7 +20,7 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
-package org.jboss.jsfunit.cdi;
+package org.jboss.jsfunit.api;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
View
46 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/api/InitialRequest.java
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.jsfunit.api;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import org.jboss.jsfunit.framework.InitialRequestStrategy;
+
+/**
+ * Annotation for specifying the HtmlUnit browser emulation.
+ *
+ * @author Stan Silvert
+ */
+@Target({METHOD, TYPE})
+@Documented
+@Retention(RUNTIME)
+public @interface InitialRequest
+{
+ public Class<? extends InitialRequestStrategy> value();
+}
View
12 ...org/jboss/jsfunit/cdi/InitialRequest.java → ...rg/jboss/jsfunit/api/JSFUnitResource.java
@@ -20,7 +20,7 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
-package org.jboss.jsfunit.cdi;
+package org.jboss.jsfunit.api;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
@@ -29,13 +29,13 @@
import static java.lang.annotation.ElementType.*;
/**
- * Annotation for specifying the HtmlUnit browser emulation.
+ * Annotation to describe a Arquillian Field injection point. Not needed for @Test method parameters.
*
- * @author Stan Silvert
+ * @author <a href="mailto:aslak@conduct.no">Aslak Knutsen</a>
*/
-@Target({METHOD, TYPE})
+@Target({PARAMETER, FIELD})
@Documented
@Retention(RUNTIME)
-public @interface InitialRequest {
- public Class value();
+public @interface JSFUnitResource
+{
}
View
2 ...ain/java/org/jboss/jsfunit/cdi/Proxy.java → ...ain/java/org/jboss/jsfunit/api/Proxy.java
@@ -20,7 +20,7 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
-package org.jboss.jsfunit.cdi;
+package org.jboss.jsfunit.api;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
View
20 ...n/JSFUnitApplicationArchiveProcessor.java → ...t/JSFUnitApplicationArchiveProcessor.java
@@ -20,26 +20,38 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
-package org.jboss.jsfunit.arquillian;
+package org.jboss.jsfunit.arquillian.client;
import org.jboss.arquillian.spi.TestClass;
import org.jboss.arquillian.spi.client.deployment.ApplicationArchiveProcessor;
+import org.jboss.jsfunit.cdi.JSFUnitCDIProducer;
import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.spec.WebArchive;
/**
* This adds CDI classes directly to WEB-INF/classes. They must be there for
* tests to use them.
*
* @author ssilvert
+ * @author <a href="mailto:aslak@conduct.no">Aslak Knutsen</a>
*/
+// TODO: this can be removed when ARQ-405 is fixed. We don't really need to support both CDI and Arq
public class JSFUnitApplicationArchiveProcessor implements ApplicationArchiveProcessor
{
-
+ /**
+ * If this is a CDI WebArchive, add the {@link JSFUnitCDIProducer} to the users deployment so it will be picked up by CDI
+ */
public void process(Archive<?> archive, TestClass tc)
{
- WebArchive webArchive = (WebArchive)archive;
- webArchive.addPackage(org.jboss.jsfunit.cdi.InitialPage.class.getPackage());
+ if(WebArchive.class.isInstance(archive))
+ {
+ WebArchive webArchive = WebArchive.class.cast(archive);
+ if(webArchive.contains(ArchivePaths.create("WEB-INF/beans.xml")))
+ {
+ webArchive.addClass(JSFUnitCDIProducer.class);
+ }
+ }
}
}
View
11 ...it/arquillian/JSFUnitArchiveAppender.java → ...illian/client/JSFUnitArchiveAppender.java
@@ -14,11 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.jboss.jsfunit.arquillian;
+package org.jboss.jsfunit.arquillian.client;
import java.net.URL;
+import org.jboss.arquillian.spi.TestEnricher;
import org.jboss.arquillian.spi.client.deployment.AuxiliaryArchiveAppender;
+import org.jboss.jsfunit.arquillian.container.JSFUnitCleanupTestTreadFilter;
+import org.jboss.jsfunit.arquillian.container.JSFUnitTestEnricher;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
@@ -46,12 +49,13 @@
org.jboss.jsfunit.framework.WebClientSpec.class.getPackage(),
org.jboss.jsfunit.context.JSFUnitFacesContext.class.getPackage(),
org.jboss.jsfunit.seam.SeamUtil.class.getPackage(),
+ org.jboss.jsfunit.api.JSFUnitResource.class.getPackage(), // Arquillian JSFunit API
+ org.jboss.jsfunit.arquillian.container.JSFUnitTestEnricher.class.getPackage(), // Support package for incontainer enrichment
org.apache.http.HttpEntity.class.getPackage(), // HTTPClient
com.gargoylesoftware.htmlunit.BrowserVersion.class.getPackage(),
org.apache.commons.codec.Decoder.class.getPackage(),
org.apache.commons.io.IOUtils.class.getPackage(),
org.apache.commons.lang.StringUtils.class.getPackage(),
-
net.sourceforge.htmlunit.corejs.javascript.EvaluatorException.class.getPackage(),
org.w3c.css.sac.CSSException.class.getPackage(),
com.steadystate.css.dom.CSSOMObject.class.getPackage(),
@@ -65,7 +69,8 @@
.addAsResource("com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.xml")
.addAsResource("com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.xsd")
.addAsManifestResource(jsfunitFacesConfigXml(), "faces-config.xml")
- .addAsManifestResource("arquillian/web-fragment.xml", "web-fragment.xml");
+ .addAsManifestResource("arquillian/web-fragment.xml", "web-fragment.xml")
+ .addAsServiceProvider(TestEnricher.class, JSFUnitTestEnricher.class);
}
private URL jsfunitFacesConfigXml()
View
2 ...illian/JSFUnitCleanupTestTreadFilter.java → ...tainer/JSFUnitCleanupTestTreadFilter.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.jboss.jsfunit.arquillian;
+package org.jboss.jsfunit.arquillian.container;
import java.io.IOException;
View
290 ...rquillian/src/main/java/org/jboss/jsfunit/arquillian/container/JSFUnitSessionFactory.java
@@ -0,0 +1,290 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
+ * as indicated by the @authors tag. All rights reserved.
+ * 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.jsfunit.arquillian.container;
+
+import java.io.IOException;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import javax.servlet.http.HttpSession;
+
+import org.jboss.jsfunit.api.BasicAuthentication;
+import org.jboss.jsfunit.api.Browser;
+import org.jboss.jsfunit.api.BrowserVersion;
+import org.jboss.jsfunit.api.Cookies;
+import org.jboss.jsfunit.api.FormAuthentication;
+import org.jboss.jsfunit.api.InitialPage;
+import org.jboss.jsfunit.api.InitialRequest;
+import org.jboss.jsfunit.api.Proxy;
+import org.jboss.jsfunit.framework.BasicAuthenticationStrategy;
+import org.jboss.jsfunit.framework.FormAuthenticationStrategy;
+import org.jboss.jsfunit.framework.InitialRequestStrategy;
+import org.jboss.jsfunit.framework.WebClientSpec;
+import org.jboss.jsfunit.framework.WebConversationFactory;
+import org.jboss.jsfunit.jsfsession.JSFClientSession;
+import org.jboss.jsfunit.jsfsession.JSFServerSession;
+import org.jboss.jsfunit.jsfsession.JSFSession;
+
+/**
+ * JSFUnitSessionFactory
+ *
+ * @author Stan Silvert
+ * @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class JSFUnitSessionFactory
+{
+ private static final String JSFSESSION_FIELD_INJECTED = JSFUnitTestEnricher.class.getName() + ".JSFSESSION_FIELD_INJECTED";
+
+ private static final String JSFSESSION_METHOD_INJECTED = JSFUnitTestEnricher.class.getName() + ".JSFSESSION_METHOD_INJECTED";
+
+ //-------------------------------------------------------------------------------------||
+ // Internal API -----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ public static JSFClientSession getJSFClientSession(AnnotatedElement injectionPoint) throws IOException
+ {
+ return findJSFSession(injectionPoint).getJSFClientSession();
+ }
+
+ public static JSFServerSession getJSFServerSession(AnnotatedElement injectionPoint) throws IOException
+ {
+ return findJSFSession(injectionPoint).getJSFServerSession();
+ }
+
+ public static JSFSession findJSFSession(AnnotatedElement injectionPoint) throws IOException
+ {
+ JSFUnitSessionFactory factory = new JSFUnitSessionFactory();
+ ElementType injectionType = factory.getElementType(injectionPoint);
+ HttpSession httpSession = WebConversationFactory.getSessionFromThreadLocal();
+ JSFSession jsfSession = null;
+ if (injectionType == ElementType.FIELD)
+ {
+ jsfSession = (JSFSession) httpSession.getAttribute(JSFSESSION_FIELD_INJECTED);
+ }
+ if (injectionType == ElementType.METHOD)
+ {
+ jsfSession = (JSFSession) httpSession.getAttribute(JSFSESSION_METHOD_INJECTED);
+ }
+
+ if (jsfSession != null)
+ {
+ return jsfSession;
+ }
+ jsfSession = factory.createJSFSession(injectionPoint, injectionType, httpSession);
+ return jsfSession;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helpers -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ public JSFUnitSessionFactory()
+ {
+ }
+
+ private JSFSession createJSFSession(AnnotatedElement injectionPoint, ElementType injectionType, HttpSession httpSession)
+ throws IOException
+ {
+ JSFSession jsfSession = new JSFSession(createWebClientSpec(injectionPoint, injectionType));
+
+ if (injectionType == ElementType.FIELD)
+ {
+ httpSession.setAttribute(JSFSESSION_FIELD_INJECTED, jsfSession);
+ }
+ if (injectionType == ElementType.METHOD)
+ {
+ httpSession.setAttribute(JSFSESSION_METHOD_INJECTED, jsfSession);
+ }
+
+ return jsfSession;
+ }
+
+ private WebClientSpec createWebClientSpec(AnnotatedElement injectionPoint, ElementType injectionType)
+ {
+ InitialPage initialPage = getAnnotation(InitialPage.class, injectionPoint, injectionType);
+ if (initialPage == null)
+ {
+ throw new IllegalArgumentException(
+ "@InitialPage required for injection. Specify at class level for field injection or at method level for method injection.");
+ }
+
+ Browser browser = null;
+ BrowserVersion browserAnno = getAnnotation(BrowserVersion.class, injectionPoint, injectionType);
+ if (browserAnno == null)
+ {
+ browser = Browser.DEFAULT;
+ }
+ if (browserAnno != null)
+ {
+ browser = browserAnno.value();
+ }
+
+ String host = null;
+ int port = 0;
+
+ Proxy proxyAnno = getAnnotation(Proxy.class, injectionPoint, injectionType);
+ if (proxyAnno != null)
+ {
+ host = proxyAnno.host();
+ port = proxyAnno.port();
+ }
+
+ WebClientSpec wcSpec = new WebClientSpec(initialPage.value(), browser.getVersion(), host, port);
+
+ BasicAuthentication basicAuthAnno = getAnnotation(BasicAuthentication.class, injectionPoint, injectionType);
+ FormAuthentication formAuthAnno = getAnnotation(FormAuthentication.class, injectionPoint, injectionType);
+ InitialRequest initReqAnno = getAnnotation(InitialRequest.class, injectionPoint, injectionType);
+
+ validateOneNonNullInitialRequest(injectionType, basicAuthAnno, formAuthAnno, initReqAnno);
+
+ if (basicAuthAnno != null)
+ {
+ String userName = basicAuthAnno.userName();
+ String password = basicAuthAnno.password();
+ wcSpec.setInitialRequestStrategy(new BasicAuthenticationStrategy(userName, password));
+ }
+
+ if (formAuthAnno != null)
+ {
+ String userName = formAuthAnno.userName();
+ String password = formAuthAnno.password();
+ String submitComponent = formAuthAnno.submitComponent();
+ String userNameComponent = formAuthAnno.userNameComponent();
+ String passwordComponent = formAuthAnno.passwordComponent();
+ wcSpec.setInitialRequestStrategy(
+ new FormAuthenticationStrategy(
+ userName, password, submitComponent, userNameComponent, passwordComponent));
+ }
+
+ if (initReqAnno != null)
+ {
+ Class<? extends InitialRequestStrategy> clazz = initReqAnno.value();
+ InitialRequestStrategy initReq = newInstance(clazz);
+ wcSpec.setInitialRequestStrategy(initReq);
+ }
+
+ setCookies(wcSpec, injectionPoint, injectionType);
+
+ return wcSpec;
+ }
+
+ private void setCookies(WebClientSpec wcSpec, AnnotatedElement injectionPoint, ElementType injectionType)
+ {
+ Cookies cookiesAnno = getAnnotation(Cookies.class, injectionPoint, injectionType);
+ if (cookiesAnno == null)
+ {
+ return;
+ }
+
+ String[] names = cookiesAnno.names();
+ String[] values = cookiesAnno.values();
+ if (names.length != values.length)
+ {
+ throw new IllegalArgumentException(
+ "'names' and 'values' must have equal number of elements in @Cookies annotation.");
+ }
+
+ for (int i = 0; i < names.length; i++)
+ {
+ wcSpec.addCookie(names[i], values[i]);
+ }
+ }
+
+ /**
+ * Make sure that only one of the InitialRequest annotations was used.
+ *
+ * @throws IllegalArgumentException if more than one is null.
+ */
+ private void validateOneNonNullInitialRequest(ElementType injectionType, Annotation... initRequests)
+ {
+ boolean found = false;
+ for (Annotation anno : initRequests)
+ {
+ if ((anno != null) && found)
+ {
+ if (injectionType == ElementType.METHOD)
+ {
+ throw new IllegalArgumentException(
+ "Only one of @BasicAuthentication, @FormAuthentication, or @InitialRequest is allowed per method.");
+ }
+ if (injectionType == ElementType.FIELD)
+ {
+ throw new IllegalArgumentException(
+ "Only one of @BasicAuthentication, @FormAuthentication, or @InitialRequest is allowed at the class level.");
+ }
+ }
+ if (anno != null)
+ {
+ found = true;
+ }
+ }
+ }
+
+ private ElementType getElementType(AnnotatedElement injectionPoint)
+ {
+ if (injectionPoint instanceof Method)
+ {
+ return ElementType.METHOD;
+ }
+ if (injectionPoint instanceof Field)
+ {
+ return ElementType.FIELD;
+ }
+
+ // should not really be possible unless the JSFUnitResource changes
+ throw new IllegalArgumentException(
+ "JSFUnit artifacts are only injectable at method parameters or field. Found InjectionPoint Member "
+ + injectionPoint);
+ }
+
+ private <T extends Annotation> T getAnnotation(Class<T> annoClass, AnnotatedElement injectionPoint, ElementType injectionType)
+ {
+ if (injectionType == ElementType.METHOD)
+ {
+ return injectionPoint.getAnnotation(annoClass);
+ }
+
+ if (injectionType == ElementType.FIELD)
+ {
+ Class<?> clazz = ((Field)injectionPoint).getDeclaringClass();
+ return clazz.getAnnotation(annoClass);
+ }
+
+ return null;
+ }
+
+ private static <T> T newInstance(Class<T> clazz)
+ {
+ try
+ {
+ return clazz.newInstance();
+ }
+ catch (InstantiationException e)
+ {
+ throw new IllegalArgumentException(e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new IllegalArgumentException(e);
+ }
+ }
+}
View
114 ...-arquillian/src/main/java/org/jboss/jsfunit/arquillian/container/JSFUnitTestEnricher.java
@@ -0,0 +1,114 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
+ * as indicated by the @authors tag. All rights reserved.
+ * 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.jsfunit.arquillian.container;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.List;
+
+import org.jboss.arquillian.spi.TestEnricher;
+import org.jboss.jsfunit.api.JSFUnitResource;
+import org.jboss.jsfunit.jsfsession.JSFClientSession;
+import org.jboss.jsfunit.jsfsession.JSFServerSession;
+import org.jboss.jsfunit.jsfsession.JSFSession;
+
+/**
+ * JSFUnitTestEnricher
+ *
+ * @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class JSFUnitTestEnricher implements TestEnricher
+{
+ //-------------------------------------------------------------------------------------||
+ // Contract - TestEnricher ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ @Override
+ public void enrich(Object testCase)
+ {
+ List<Field> annotatedFields = SecurityActions.getFieldsWithAnnotation(testCase.getClass(), JSFUnitResource.class);
+
+ try
+ {
+ for (Field field : annotatedFields)
+ {
+ Object value = null;
+
+ if(field.getType() == JSFSession.class)
+ {
+ value = JSFUnitSessionFactory.findJSFSession(field);
+ }
+ else if(field.getType() == JSFClientSession.class)
+ {
+ value = JSFUnitSessionFactory.getJSFClientSession(field);
+ }
+ else if(field.getType() == JSFServerSession.class)
+ {
+ value = JSFUnitSessionFactory.getJSFServerSession(field);
+ }
+
+ field.set(testCase, value);
+ }
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not inject members", e);
+ }
+ }
+
+ @Override
+ public Object[] resolve(Method method)
+ {
+ Object[] values = new Object[method.getParameterTypes().length];
+
+ try
+ {
+ Class<?>[] parameterTypes = method.getParameterTypes();
+ for(int i = 0; i < parameterTypes.length; i++)
+ {
+ if(values[i] != null)
+ {
+ continue; // someone else has set this value, possible the CDIEnricher
+ }
+ Class<?> parameterType = parameterTypes[i];
+
+ Object value = null;
+ if(parameterType == JSFSession.class)
+ {
+ value = JSFUnitSessionFactory.findJSFSession(method);
+ }
+ else if(parameterType == JSFClientSession.class)
+ {
+ value = JSFUnitSessionFactory.getJSFClientSession(method);
+ }
+ else if(parameterType == JSFServerSession.class)
+ {
+ value = JSFUnitSessionFactory.getJSFServerSession(method);
+ }
+ values[i] = value;
+ }
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not inject method parameters", e);
+ }
+ return values;
+ }
+
+}
View
249 ...unit-arquillian/src/main/java/org/jboss/jsfunit/arquillian/container/SecurityActions.java
@@ -0,0 +1,249 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, 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.jsfunit.arquillian.container;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SecurityActions
+ *
+ * A set of privileged actions that are not to leak out
+ * of this package
+ *
+ * @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+final class SecurityActions
+{
+
+ //-------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------||
+
+ /**
+ * No instantiation
+ */
+ private SecurityActions()
+ {
+ throw new UnsupportedOperationException("No instantiation");
+ }
+
+ //-------------------------------------------------------------------------------||
+ // Utility Methods --------------------------------------------------------------||
+ //-------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the Thread Context ClassLoader
+ */
+ static ClassLoader getThreadContextClassLoader()
+ {
+ return AccessController.doPrivileged(GetTcclAction.INSTANCE);
+ }
+
+ /**
+ * Obtains the Constructor specified from the given Class and argument types
+ * @param clazz
+ * @param argumentTypes
+ * @return
+ * @throws NoSuchMethodException
+ */
+ static Constructor<?> getConstructor(final Class<?> clazz, final Class<?>... argumentTypes)
+ throws NoSuchMethodException
+ {
+ try
+ {
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<?>>()
+ {
+ public Constructor<?> run() throws NoSuchMethodException
+ {
+ return clazz.getConstructor(argumentTypes);
+ }
+ });
+ }
+ // Unwrap
+ catch (final PrivilegedActionException pae)
+ {
+ final Throwable t = pae.getCause();
+ // Rethrow
+ if (t instanceof NoSuchMethodException)
+ {
+ throw (NoSuchMethodException) t;
+ }
+ else
+ {
+ // No other checked Exception thrown by Class.getConstructor
+ try
+ {
+ throw (RuntimeException) t;
+ }
+ // Just in case we've really messed up
+ catch (final ClassCastException cce)
+ {
+ throw new RuntimeException("Obtained unchecked Exception; this code should never be reached", t);
+ }
+ }
+ }
+ }
+
+ /**
+ * Create a new instance by finding a constructor that matches the argumentTypes signature
+ * using the arguments for instantiation.
+ *
+ * @param className Full classname of class to create
+ * @param argumentTypes The constructor argument types
+ * @param arguments The constructor arguments
+ * @return a new instance
+ * @throws IllegalArgumentException if className, argumentTypes, or arguments are null
+ * @throws RuntimeException if any exceptions during creation
+ * @author <a href="mailto:aslak@conduct.no">Aslak Knutsen</a>
+ * @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
+ */
+ static <T> T newInstance(final String className, final Class<?>[] argumentTypes, final Object[] arguments,
+ final Class<T> expectedType)
+ {
+ if (className == null)
+ {
+ throw new IllegalArgumentException("ClassName must be specified");
+ }
+ if (argumentTypes == null)
+ {
+ throw new IllegalArgumentException("ArgumentTypes must be specified. Use empty array if no arguments");
+ }
+ if (arguments == null)
+ {
+ throw new IllegalArgumentException("Arguments must be specified. Use empty array if no arguments");
+ }
+ final Object obj;
+ try
+ {
+ final ClassLoader tccl = getThreadContextClassLoader();
+ final Class<?> implClass = Class.forName(className, false, tccl);
+ Constructor<?> constructor = getConstructor(implClass, argumentTypes);
+ obj = constructor.newInstance(arguments);
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not create new instance of " + className
+ + ", missing package from classpath?", e);
+ }
+
+ // Cast
+ try
+ {
+ return expectedType.cast(obj);
+ }
+ catch (final ClassCastException cce)
+ {
+ // Reconstruct so we get some useful information
+ throw new ClassCastException("Incorrect expected type, " + expectedType.getName() + ", defined for "
+ + obj.getClass().getName());
+ }
+ }
+
+ static boolean isClassPresent(String name)
+ {
+ try
+ {
+ ClassLoader classLoader = getThreadContextClassLoader();
+ classLoader.loadClass(name);
+ return true;
+ }
+ catch (ClassNotFoundException e)
+ {
+ return false;
+ }
+ }
+
+ static List<Field> getFieldsWithAnnotation(final Class<?> source, final Class<? extends Annotation> annotationClass)
+ {
+ List<Field> declaredAccessableFields = AccessController.doPrivileged(new PrivilegedAction<List<Field>>()
+ {
+ public List<Field> run()
+ {
+ List<Field> foundFields = new ArrayList<Field>();
+ Class<?> nextSource = source;
+ while (nextSource != Object.class) {
+ for(Field field : nextSource.getDeclaredFields())
+ {
+ if(field.isAnnotationPresent(annotationClass))
+ {
+ if(!field.isAccessible())
+ {
+ field.setAccessible(true);
+ }
+ foundFields.add(field);
+ }
+ }
+ nextSource = nextSource.getSuperclass();
+ }
+ return foundFields;
+ }
+ });
+ return declaredAccessableFields;
+ }
+
+ static List<Method> getMethodsWithAnnotation(final Class<?> source, final Class<? extends Annotation> annotationClass)
+ {
+ List<Method> declaredAccessableMethods = AccessController.doPrivileged(new PrivilegedAction<List<Method>>()
+ {
+ public List<Method> run()
+ {
+ List<Method> foundMethods = new ArrayList<Method>();
+ for(Method method : source.getDeclaredMethods())
+ {
+ if(method.isAnnotationPresent(annotationClass))
+ {
+ if(!method.isAccessible())
+ {
+ method.setAccessible(true);
+ }
+ foundMethods.add(method);
+ }
+ }
+ return foundMethods;
+ }
+ });
+ return declaredAccessableMethods;
+ }
+
+ //-------------------------------------------------------------------------------||
+ // Inner Classes ----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------||
+
+ /**
+ * Single instance to get the TCCL
+ */
+ private enum GetTcclAction implements PrivilegedAction<ClassLoader> {
+ INSTANCE;
+
+ public ClassLoader run()
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+
+ }
+
+}
View
220 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/cdi/JSFSessionFactory.java
@@ -1,220 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2010, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.jsfunit.cdi;
-
-import java.io.IOException;
-import java.lang.annotation.Annotation;
-import java.lang.annotation.ElementType;
-import java.lang.reflect.Field;
-import java.lang.reflect.Member;
-import java.lang.reflect.Method;
-import javax.enterprise.inject.Produces;
-import javax.enterprise.inject.spi.InjectionPoint;
-import javax.servlet.http.HttpSession;
-import org.jboss.jsfunit.framework.BasicAuthenticationStrategy;
-import org.jboss.jsfunit.framework.Environment;
-import org.jboss.jsfunit.framework.FormAuthenticationStrategy;
-import org.jboss.jsfunit.framework.InitialRequestStrategy;
-import org.jboss.jsfunit.framework.WebConversationFactory;
-import org.jboss.jsfunit.framework.WebClientSpec;
-import org.jboss.jsfunit.jsfsession.JSFClientSession;
-import org.jboss.jsfunit.jsfsession.JSFServerSession;
-import org.jboss.jsfunit.jsfsession.JSFSession;
-
-/**
- * This class is called by CDI to create the JSFSession, JSFClientSession, and
- * JSFServerSession. JSFUnit annotations are used to describe how the JSFSession
- * will be created.
- *
- * It will store a copy of JSFSession in the HttpSession for the active test.
- *
- * @author Stan Silvert
- */
-public class JSFSessionFactory {
- public static final String JSFSESSION_FIELD_INJECTED = JSFSessionFactory.class.getName() + ".JSFSESSION_FIELD_INJECTED";
- public static final String JSFSESSION_METHOD_INJECTED = JSFSessionFactory.class.getName() + ".JSFSESSION_METHOD_INJECTED";
-
- public JSFSessionFactory()
- {
- }
-
- private Annotation getAnnotation(Class annoClass,
- InjectionPoint injectionPoint,
- ElementType injectionType)
- {
- if (injectionType == ElementType.METHOD )
- {
- Method method = (Method)injectionPoint.getMember();
- return method.getAnnotation(annoClass);
- }
-
- if (injectionType == ElementType.FIELD)
- {
- Class clazz = injectionPoint.getMember().getDeclaringClass();
- return clazz.getAnnotation(annoClass);
- }
-
- return null;
- }
-
- WebClientSpec createWebClientSpec(InjectionPoint injectionPoint,
- ElementType injectionType)
- {
- Annotation initialPage = getAnnotation(InitialPage.class, injectionPoint, injectionType);
- if (initialPage == null) throw new IllegalArgumentException("@InitialPage required for injection. Specify at class level for field injection or at method level for method injection.");
-
- Browser browser = null;
- Annotation browserAnno = getAnnotation(org.jboss.jsfunit.cdi.BrowserVersion.class, injectionPoint, injectionType);
- if (browserAnno == null) browser = Browser.DEFAULT;
- if (browserAnno != null) browser = ((org.jboss.jsfunit.cdi.BrowserVersion)browserAnno).value();
-
- String host = null;
- int port = 0;
-
- Proxy proxyAnno = (Proxy)getAnnotation(Proxy.class, injectionPoint, injectionType);
- if (proxyAnno != null)
- {
- host = proxyAnno.host();
- port = proxyAnno.port();
- }
-
- WebClientSpec wcSpec = new WebClientSpec(((InitialPage)initialPage).value(), browser.getVersion(), host, port);
-
- BasicAuthentication basicAuthAnno = (BasicAuthentication)getAnnotation(BasicAuthentication.class, injectionPoint, injectionType);
- FormAuthentication formAuthAnno = (FormAuthentication)getAnnotation(FormAuthentication.class, injectionPoint, injectionType);
- InitialRequest initReqAnno = (InitialRequest)getAnnotation(InitialRequest.class, injectionPoint, injectionType);
- validateOneNonNullInitialRequest(injectionType, basicAuthAnno, formAuthAnno, initReqAnno);
-
- if (basicAuthAnno != null)
- {
- String userName = basicAuthAnno.userName();
- String password = basicAuthAnno.password();
- wcSpec.setInitialRequestStrategy(new BasicAuthenticationStrategy(userName, password));
- }
-
- if (formAuthAnno != null)
- {
- String userName = formAuthAnno.userName();
- String password = formAuthAnno.password();
- String submitComponent = formAuthAnno.submitComponent();
- String userNameComponent = formAuthAnno.userNameComponent();
- String passwordComponent = formAuthAnno.passwordComponent();
- wcSpec.setInitialRequestStrategy(new FormAuthenticationStrategy(userName, password, submitComponent, userNameComponent, passwordComponent));
- }
-
- if (initReqAnno != null)
- {
- Class clazz = initReqAnno.value();
- InitialRequestStrategy initReq = (InitialRequestStrategy)Environment.newInstance(clazz);
- wcSpec.setInitialRequestStrategy(initReq);
- }
-
- setCookies(wcSpec, injectionPoint, injectionType);
-
- return wcSpec;
- }
-
- private void setCookies(WebClientSpec wcSpec,
- InjectionPoint injectionPoint,
- ElementType injectionType)
- {
- Cookies cookiesAnno = (Cookies)getAnnotation(Cookies.class, injectionPoint, injectionType);
- if (cookiesAnno == null) return;
-
- String[] names = cookiesAnno.names();
- String[] values = cookiesAnno.values();
- if (names.length != values.length) throw new IllegalArgumentException("'names' and 'values' must have equal number of elements in @Cookies annotation.");
-
- for (int i=0; i < names.length; i++)
- {
- wcSpec.addCookie(names[i], values[i]);
- }
- }
-
- /**
- * Make sure that only one of the InitialRequest annotations was used.
- *
- * @throws IllegalArgumentException if more than one is null.
- */
- private void validateOneNonNullInitialRequest(ElementType injectionType, Annotation... initRequests)
- {
- boolean found = false;
- for (Annotation anno : initRequests)
- {
- if ((anno != null) && found)
- {
- if (injectionType == ElementType.METHOD) throw new IllegalArgumentException("Only one of @BasicAuthentication, @FormAuthentication, or @InitialRequest is allowed per method.");
- if (injectionType == ElementType.FIELD) throw new IllegalArgumentException("Only one of @BasicAuthentication, @FormAuthentication, or @InitialRequest is allowed at the class level.");
- }
- if (anno != null) found = true;
- }
- }
-
- JSFSession createJSFSession(InjectionPoint injectionPoint,
- ElementType injectionType,
- HttpSession httpSession) throws IOException
- {
- JSFSession jsfSession = new JSFSession(createWebClientSpec(injectionPoint, injectionType));
-
- if (injectionType == ElementType.FIELD) httpSession.setAttribute(JSFSESSION_FIELD_INJECTED, jsfSession);
- if (injectionType == ElementType.METHOD) httpSession.setAttribute(JSFSESSION_METHOD_INJECTED, jsfSession);
-
- return jsfSession;
- }
-
- @Produces
- JSFSession findJSFSession(InjectionPoint injectionPoint) throws IOException
- {
- ElementType injectionType = getElementType(injectionPoint);
- HttpSession httpSession = WebConversationFactory.getSessionFromThreadLocal();
- JSFSession jsfSession = null;
- if (injectionType == ElementType.FIELD) jsfSession = (JSFSession)httpSession.getAttribute(JSFSESSION_FIELD_INJECTED);
- if (injectionType == ElementType.METHOD) jsfSession = (JSFSession)httpSession.getAttribute(JSFSESSION_METHOD_INJECTED);
-
- if (jsfSession != null) return jsfSession;
- jsfSession = createJSFSession(injectionPoint, injectionType, httpSession);
- return jsfSession;
- }
-
- @Produces
- JSFClientSession getJSFClientSession(InjectionPoint injectionPoint) throws IOException
- {
- return findJSFSession(injectionPoint).getJSFClientSession();
- }
-
- @Produces
- JSFServerSession getJSFServerSession(InjectionPoint injectionPoint) throws IOException
- {
- return findJSFSession(injectionPoint).getJSFServerSession();
- }
-
- private ElementType getElementType(InjectionPoint injectionPoint)
- {
- Member member = injectionPoint.getMember();
- if (member instanceof Method) return ElementType.METHOD;
- if (member instanceof Field) return ElementType.FIELD;
-
- throw new IllegalArgumentException("JSFUnit artifacts are only injectable at method or field. Found InjectionPoint Member "
- + injectionPoint.getMember());
- }
-}
View
70 jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/cdi/JSFUnitCDIProducer.java
@@ -0,0 +1,70 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
+ * as indicated by the @authors tag. All rights reserved.
+ * 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.jsfunit.cdi;
+
+import java.io.IOException;
+import java.lang.reflect.AnnotatedElement;
+
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.InjectionPoint;
+
+import org.jboss.jsfunit.arquillian.container.JSFUnitSessionFactory;
+import org.jboss.jsfunit.jsfsession.JSFClientSession;
+import org.jboss.jsfunit.jsfsession.JSFServerSession;
+import org.jboss.jsfunit.jsfsession.JSFSession;
+
+/**
+ * A CDI enabled producer for creating {@link JSFSession}, {@link JSFClientSession} and {@link JSFServerSession}.
+ *
+ * @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+// TODO: The CDI API can be removed when ARQ-405 is fixed. We don't really need to support both CDI and Arq
+public class JSFUnitCDIProducer
+{
+ //-------------------------------------------------------------------------------------||
+ // CDI API ----------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ @Produces
+ static JSFClientSession getJSFClientSession(InjectionPoint injectionPoint) throws IOException
+ {
+ return JSFUnitSessionFactory.getJSFClientSession(convertInjectionPoint(injectionPoint));
+ }
+
+ @Produces
+ static JSFServerSession getJSFServerSession(InjectionPoint injectionPoint) throws IOException
+ {
+ return JSFUnitSessionFactory.getJSFServerSession(convertInjectionPoint(injectionPoint));
+ }
+
+ @Produces
+ static JSFSession findJSFSession(InjectionPoint injectionPoint) throws IOException
+ {
+ return JSFUnitSessionFactory.findJSFSession(convertInjectionPoint(injectionPoint));
+ }
+
+ private static AnnotatedElement convertInjectionPoint(InjectionPoint injectionPoint)
+ {
+ if(injectionPoint.getMember() instanceof AnnotatedElement)
+ {
+ return (AnnotatedElement)injectionPoint.getMember();
+ }
+ throw new RuntimeException("Illegal injection point: " + injectionPoint.getMember());
+ }
+}
View
0 ...-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/shrinkwrap/MavenArtifactResolver.java
No changes.
View
1 ...src/main/resources/META-INF/services/org.jboss.arquillian.spi.ApplicationArchiveProcessor
@@ -1 +0,0 @@
-org.jboss.jsfunit.arquillian.JSFUnitApplicationArchiveProcessor
View
1 ...an/src/main/resources/META-INF/services/org.jboss.arquillian.spi.AuxiliaryArchiveAppender
@@ -1 +0,0 @@
-org.jboss.jsfunit.arquillian.JSFUnitArchiveAppender
View
2 .../META-INF/services/org.jboss.arquillian.spi.client.deployment.ApplicationArchiveProcessor
@@ -1 +1 @@
-org.jboss.jsfunit.arquillian.JSFUnitApplicationArchiveProcessor
+org.jboss.jsfunit.arquillian.client.JSFUnitApplicationArchiveProcessor
View
2 ...ces/META-INF/services/org.jboss.arquillian.spi.client.deployment.AuxiliaryArchiveAppender
@@ -1 +1 @@
-org.jboss.jsfunit.arquillian.JSFUnitArchiveAppender
+org.jboss.jsfunit.arquillian.client.JSFUnitArchiveAppender
View
2 jboss-jsfunit-arquillian/src/main/resources/arquillian/web-fragment.xml
@@ -5,7 +5,7 @@
<filter>
<filter-name>JSFUnitCleanupTestTreadFilter</filter-name>
- <filter-class>org.jboss.jsfunit.arquillian.JSFUnitCleanupTestTreadFilter</filter-class>
+ <filter-class>org.jboss.jsfunit.arquillian.container.JSFUnitCleanupTestTreadFilter</filter-class>
</filter>
<filter>

0 comments on commit fc5ef0f

Please sign in to comment.