[jboss-cvs] JBossAS SVN: r99875 - in projects/ejb-book/trunk/chxx-interceptors/src: test/java/org/jboss/ejb3/examples/chxx/tuner and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Jan 25 02:14:20 EST 2010
Author: ALRubinger
Date: 2010-01-25 02:14:19 -0500 (Mon, 25 Jan 2010)
New Revision: 99875
Added:
projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/AuditedInvocation.java
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/CachingInterceptorUnitTestCase.java
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/Channel2RestrictorUnitTestCase.java
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/InterceptionIntegrationTest.java
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/MockInvocationContext.java
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/SecurityActions.java
Removed:
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/CachingInterceptorUnitTestCase.java
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/Channel2RestrictorUnitTestCase.java
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/InterceptionIntegrationTest.java
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/MockInvocationContext.java
projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/SecurityActions.java
Modified:
projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/CachingAuditor.java
projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/Channel2Restrictor.java
Log:
[EJBBOOK-16] Add @Resource injection into the Interceptors example
Added: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/AuditedInvocation.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/AuditedInvocation.java (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/AuditedInvocation.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -0,0 +1,89 @@
+/*
+ * 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.ejb3.examples.chxx.tuner;
+
+import java.security.Principal;
+
+import javax.interceptor.InvocationContext;
+
+/**
+ * Data object encapsulating the auditable properties behind an invocation
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class AuditedInvocation
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Invoked context
+ */
+ private final InvocationContext context;
+
+ /**
+ * Caller
+ */
+ private final Principal caller;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates a new instance
+ */
+ AuditedInvocation(final InvocationContext context, final Principal caller)
+ {
+ // Precondition checks
+ assert context != null : "context must be specified";
+ assert caller != null : "caller must be specified";
+
+ // Set
+ this.context = context;
+ this.caller = caller;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the context
+ */
+ public InvocationContext getContext()
+ {
+ return context;
+ }
+
+ /**
+ * @return the caller
+ */
+ public Principal getCaller()
+ {
+ return caller;
+ }
+
+}
Modified: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/CachingAuditor.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/CachingAuditor.java 2010-01-25 06:53:09 UTC (rev 99874)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/CachingAuditor.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -21,11 +21,14 @@
*/
package org.jboss.ejb3.examples.chxx.tuner;
+import java.security.Principal;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;
+import javax.annotation.Resource;
+import javax.ejb.EJBContext;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
@@ -61,38 +64,72 @@
* each bean instance is guaranteed to be used by only one thread at once, many bean instances
* may be executed concurrently.
*/
- private static final List<InvocationContext> invocations = new CopyOnWriteArrayList<InvocationContext>();
+ private static final List<AuditedInvocation> invocations = new CopyOnWriteArrayList<AuditedInvocation>();
//-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * The current EJB Context; will either be injected by the EJB Container or
+ * manually populated by unit tests
+ */
+ @Resource
+ EJBContext beanContext;
+
+ //-------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
- * Caches the intercepted {@link InvocationContext} such that
- * a test may obtain it later
+ * Caches the intercepted invocation in an auditable view such that
+ * it may later be obtained
*/
@AroundInvoke
- public Object audit(final InvocationContext context) throws Exception
+ public Object audit(final InvocationContext invocationContext) throws Exception
{
// Precondition checks
- assert context != null : "Context was not specified";
+ assert invocationContext != null : "Context was not specified";
+ // Obtain the caller
+ Principal caller;
+ try
+ {
+ caller = beanContext.getCallerPrincipal();
+ }
+ //TODO EJBTHREE-1996 Should not throw NPE
+ catch (final NullPointerException npe)
+ {
+ caller = new Principal()
+ {
+
+ @Override
+ public String getName()
+ {
+ return "Unauthenticated Caller";
+ }
+ };
+ }
+
+ // Create a new view
+ final AuditedInvocation audit = new AuditedInvocation(invocationContext, caller);
+
// Add the invocation to the cache
- invocations.add(context);
+ invocations.add(audit);
// Carry out the invocation, noting where we've intercepted before and after the call (around it)
try
{
// Log
- log.info("Intercepted: " + context);
+ log.info("Intercepted: " + invocationContext);
// Return
- return context.proceed();
+ return invocationContext.proceed();
}
finally
{
// Log
- log.info("Done with: " + context);
+ log.info("Done with: " + invocationContext);
}
}
@@ -105,7 +142,7 @@
* Returns a read-only view of the {@link InvocationContext}
* cached by this interceptor
*/
- public static List<InvocationContext> getInvocations()
+ public static List<AuditedInvocation> getInvocations()
{
// Copy on export
return Collections.unmodifiableList(invocations);
Modified: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/Channel2Restrictor.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/Channel2Restrictor.java 2010-01-25 06:53:09 UTC (rev 99874)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/tuner/Channel2Restrictor.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -61,8 +61,8 @@
/**
* Examines the specified request to determine if the caller is attempting
- * to obtain content for Channel 2. If so, will block the request with
- * {@link Channel2ClosedException}
+ * to obtain content for Channel 2. If so, and Channel 2 is currently closed,
+ * will block the request, instead throwing {@link Channel2ClosedException}
*/
@AroundInvoke
public Object checkAccessibility(final InvocationContext context) throws Exception
Copied: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/CachingInterceptorUnitTestCase.java (from rev 99871, projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/CachingInterceptorUnitTestCase.java)
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/CachingInterceptorUnitTestCase.java (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/CachingInterceptorUnitTestCase.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -0,0 +1,212 @@
+/*
+ * 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.ejb3.examples.chxx.tuner;
+
+import java.security.Identity;
+import java.security.Principal;
+import java.util.Properties;
+import java.util.logging.Logger;
+
+import javax.ejb.EJBContext;
+import javax.ejb.EJBHome;
+import javax.ejb.EJBLocalHome;
+import javax.ejb.TimerService;
+import javax.interceptor.InvocationContext;
+import javax.transaction.UserTransaction;
+
+import junit.framework.TestCase;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests to ensure that the {@link CachingAuditor}
+ * interceptor is working as expected outside the context
+ * of a full container.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class CachingInterceptorUnitTestCase
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(CachingInterceptorUnitTestCase.class.getName());
+
+ /**
+ * Name of the mock user
+ */
+ private static String NAME_PRINCIPAL = "Mock User";
+
+ /**
+ * Principal to return
+ */
+ private Principal PRINCIPAL = new Principal()
+ {
+
+ @Override
+ public String getName()
+ {
+ return NAME_PRINCIPAL;
+ }
+ };
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * The interceptor instance to test
+ */
+ private CachingAuditor interceptor;
+
+ //-------------------------------------------------------------------------------------||
+ // Lifecycle --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates the interceptor instance to be used in testing
+ */
+ @Before
+ public void createInterceptor()
+ {
+ interceptor = new CachingAuditor();
+ // Manually set the EJBContext to a mock view which only supports returning a principal
+ interceptor.beanContext = new EJBContext()
+ {
+
+ /**
+ * Exception to throw if we invoke any method aside from getCallerPrincipal
+ */
+ private UnsupportedOperationException UNSUPPORTED = new UnsupportedOperationException(
+ "Not supported in mock implementation");
+
+ @Override
+ public void setRollbackOnly() throws IllegalStateException
+ {
+ throw UNSUPPORTED;
+
+ }
+
+ @Override
+ public Object lookup(String arg0) throws IllegalArgumentException
+ {
+ throw UNSUPPORTED;
+ }
+
+ @Override
+ public boolean isCallerInRole(String arg0)
+ {
+ throw UNSUPPORTED;
+ }
+
+ @Override
+ @SuppressWarnings("deprecation")
+ public boolean isCallerInRole(Identity arg0)
+ {
+ throw UNSUPPORTED;
+ }
+
+ @Override
+ public UserTransaction getUserTransaction() throws IllegalStateException
+ {
+ throw UNSUPPORTED;
+ }
+
+ @Override
+ public TimerService getTimerService() throws IllegalStateException
+ {
+ throw UNSUPPORTED;
+ }
+
+ @Override
+ public boolean getRollbackOnly() throws IllegalStateException
+ {
+ throw UNSUPPORTED;
+ }
+
+ @Override
+ public Properties getEnvironment()
+ {
+ throw UNSUPPORTED;
+ }
+
+ @Override
+ public EJBLocalHome getEJBLocalHome()
+ {
+ throw UNSUPPORTED;
+ }
+
+ @Override
+ public EJBHome getEJBHome()
+ {
+ throw UNSUPPORTED;
+ }
+
+ @Override
+ public Principal getCallerPrincipal()
+ {
+ return PRINCIPAL;
+ }
+
+ @Override
+ @SuppressWarnings("deprecation")
+ public Identity getCallerIdentity()
+ {
+ throw UNSUPPORTED;
+ }
+ };
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Tests ------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Ensures that contexts passed through the interceptor are cached
+ */
+ @Test
+ public void testCache() throws Exception
+ {
+ // Ensure the cache is empty to start
+ TestCase.assertEquals("Cache should start empty", 0, CachingAuditor.getInvocations().size());
+
+ // Invoke
+ final InvocationContext invocation = new MockInvocationContext(TunerLocalBusiness.class.getMethods()[0],
+ new Object[]
+ {1});
+ interceptor.audit(invocation);
+
+ // Test our invocation was cached properly
+ TestCase.assertEquals("Cache should have the first invocation", 1, CachingAuditor.getInvocations().size());
+ final AuditedInvocation audit = CachingAuditor.getInvocations().get(0);
+ TestCase.assertEquals("Invocation cached was not the one that was invoked", invocation, audit.getContext());
+ TestCase.assertEquals("Invocation did not store the caller as expected", PRINCIPAL, audit.getCaller());
+ }
+
+}
Copied: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/Channel2RestrictorUnitTestCase.java (from rev 99871, projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/Channel2RestrictorUnitTestCase.java)
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/Channel2RestrictorUnitTestCase.java (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/Channel2RestrictorUnitTestCase.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -0,0 +1,141 @@
+/*
+ * 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.ejb3.examples.chxx.tuner;
+
+import java.lang.reflect.Method;
+import java.util.logging.Logger;
+
+import javax.interceptor.InvocationContext;
+
+import junit.framework.TestCase;
+
+import org.jboss.ejb3.examples.chxx.tuner.Channel2AccessPolicy;
+import org.jboss.ejb3.examples.chxx.tuner.Channel2ClosedException;
+import org.jboss.ejb3.examples.chxx.tuner.Channel2Restrictor;
+import org.jboss.ejb3.examples.chxx.tuner.TunerLocalBusiness;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests to ensure that the {@link Channel2Restrictor}
+ * interceptor is working as expected outside the context
+ * of a full container.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class Channel2RestrictorUnitTestCase
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(Channel2RestrictorUnitTestCase.class.getName());
+
+ /**
+ * Method to get channel content
+ */
+ private static final Method METHOD_GET_CHANNEL = TunerLocalBusiness.class.getMethods()[0];
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * The interceptor instance to test
+ */
+ private Channel2Restrictor interceptor;
+
+ //-------------------------------------------------------------------------------------||
+ // Lifecycle --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates the interceptor instance to be used in testing
+ */
+ @Before
+ public void createInterceptor()
+ {
+ interceptor = new Channel2Restrictor();
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Tests ------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Ensures requests for channel 2 are blocked when the channel's access is closed
+ */
+ @Test(expected = Channel2ClosedException.class)
+ public void requestsToChannel2Blocked() throws Exception
+ {
+ // Set the access policy to block
+ Channel2AccessPolicy.setChannel2Permitted(false);
+
+ // Invoke
+ final InvocationContext invocation = new MockInvocationContext(METHOD_GET_CHANNEL, new Object[]
+ {2});
+ interceptor.checkAccessibility(invocation);
+ }
+
+ /**
+ * Ensures requests for channel 2 are not blocked when the channel's access is open
+ */
+ @Test
+ public void requestsToChannel2NotBlocked() throws Exception
+ {
+ // Set the access policy to block
+ Channel2AccessPolicy.setChannel2Permitted(true);
+
+ // Invoke
+ final InvocationContext invocation = new MockInvocationContext(METHOD_GET_CHANNEL, new Object[]
+ {2});
+ try
+ {
+ interceptor.checkAccessibility(invocation);
+ }
+ catch (final Channel2ClosedException e)
+ {
+ TestCase.fail("Should not have been blocked with: " + e);
+ }
+ }
+
+ /**
+ * Ensures requests for channel 1 are not blocked channel 2's access is closed
+ */
+ @Test
+ public void requestsToChannel1NeverBlocked() throws Exception
+ {
+ // Set the access policy to block
+ Channel2AccessPolicy.setChannel2Permitted(false);
+
+ // Invoke
+ final InvocationContext invocation = new MockInvocationContext(METHOD_GET_CHANNEL, new Object[]
+ {1});
+ interceptor.checkAccessibility(invocation);
+ }
+
+}
Copied: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/InterceptionIntegrationTest.java (from rev 99871, projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/InterceptionIntegrationTest.java)
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/InterceptionIntegrationTest.java (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/InterceptionIntegrationTest.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -0,0 +1,322 @@
+/*
+ * 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.ejb3.examples.chxx.tuner;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.UndeclaredThrowableException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.interceptor.Interceptors;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import junit.framework.TestCase;
+
+import org.jboss.bootstrap.api.lifecycle.LifecycleState;
+import org.jboss.ejb3.examples.chxx.tuner.CachingAuditor;
+import org.jboss.ejb3.examples.chxx.tuner.Channel2AccessPolicy;
+import org.jboss.ejb3.examples.chxx.tuner.Channel2ClosedException;
+import org.jboss.ejb3.examples.chxx.tuner.Channel2Restrictor;
+import org.jboss.ejb3.examples.chxx.tuner.TunerBean;
+import org.jboss.ejb3.examples.chxx.tuner.TunerLocalBusiness;
+import org.jboss.embedded.api.server.JBossASEmbeddedServer;
+import org.jboss.embedded.api.server.JBossASEmbeddedServerFactory;
+import org.jboss.embedded.api.server.JBossHomeClassLoader;
+import org.jboss.shrinkwrap.api.Archives;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Integration test ensuring that an EJB with {@link Interceptors}
+ * declared are intercepted when invoked
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class InterceptionIntegrationTest
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(InterceptionIntegrationTest.class.getName());
+
+ /**
+ * The CL of the test as originally loaded
+ */
+ private static ClassLoader originalClassLoader;
+
+ /**
+ * The server instance
+ */
+ private static JBossASEmbeddedServer server;
+
+ /**
+ * Name of the system property for JBOSS_HOME
+ * @deprecated EJBBOOK-14
+ */
+ @Deprecated
+ private static final String NAME_SYSPROP_JBOSS_HOME = "jboss.home";
+
+ /**
+ * The JNDI Context
+ */
+ private static Context NAMING_CONTEXT;
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Archive representing the deployment
+ */
+ private JavaArchive deployment;
+
+ /**
+ * The bean to invoke upon
+ */
+ private TunerLocalBusiness bean;
+
+ //-------------------------------------------------------------------------------------||
+ // Lifecycle --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates and starts a new JBossAS Server Embedded within this JVM
+ */
+ //TODO EJBBOOK-15
+ @BeforeClass
+ public static void createAndStartJBossASAndSetNamingContext() throws Exception
+ {
+ // Get JBOSS_HOME
+ final URL jbossHome = getJBossHome();
+
+ // Get additional binaries which need CL visibility (ie. jboss-embedded-core,
+ // which is placed under "target/deps" by the build). These
+ // binaries are not presently available under $JBOSS_HOME
+ final Set<URL> additionalUrls = new HashSet<URL>();
+ final URL source = InterceptionIntegrationTest.class.getProtectionDomain().getCodeSource().getLocation();
+ final URL target = new URL(source, "..");
+ final URL additionalDeps = new URL(target, "deps");
+ final File deps = new File(additionalDeps.toURI());
+ TestCase.assertTrue("Dependencies location does not exist: " + deps, deps.exists());
+ TestCase.assertTrue("Dependencies location is not a directory: " + deps, deps.isDirectory());
+ for (final File child : deps.listFiles())
+ {
+ additionalUrls.add(child.toURI().toURL());
+ log.info("Booting with: " + child);
+ }
+
+ // Make the new ClassLoader
+ originalClassLoader = SecurityActions.getThreadContextClassLoader();
+ final ClassLoader jbossHomeClassLoader = JBossHomeClassLoader.newInstance(jbossHome, additionalUrls
+ .toArray(new URL[]
+ {}), originalClassLoader);
+
+ // Make Server
+ server = JBossASEmbeddedServerFactory.createServer(jbossHomeClassLoader);
+ log.info("Created: " + server);
+
+ // Start
+ log.info("Starting Server: " + server);
+
+ // Set TCCL
+ SecurityActions.setThreadContextClassLoader(jbossHomeClassLoader);
+
+ // Start the Server
+ server.start();
+
+ // Set Naming Context
+ NAMING_CONTEXT = new InitialContext();
+ }
+
+ /**
+ * Stops the Application Server
+ */
+ @AfterClass
+ public static void stopJBossAS() throws Exception
+ {
+ if (server != null && server.getState().equals(LifecycleState.STARTED))
+ {
+ try
+ {
+ server.shutdown();
+ }
+ finally
+ {
+ // Reset the TCCL
+ Thread.currentThread().setContextClassLoader(originalClassLoader);
+ }
+ }
+ }
+
+ /**
+ * Deploys the EJB into the server and looks up an invokable reference
+ * @throws Exception
+ */
+ @Before
+ public void deployAndGetBean() throws Exception
+ {
+
+ // Create the archive
+ deployment = Archives.create("echo.jar", JavaArchive.class).addClasses(TunerLocalBusiness.class, TunerBean.class,
+ CachingAuditor.class, Channel2Restrictor.class);
+
+ // Deploy
+ server.deploy(deployment);
+
+ // Lookup
+ bean = (TunerLocalBusiness) NAMING_CONTEXT.lookup(TunerLocalBusiness.JNDI_NAME);
+ }
+
+ /**
+ * Undeploys the EJB from the server
+ * @throws Exception
+ */
+ @After
+ public void undeploy() throws Exception
+ {
+ // If we've created the running server and deployed into it
+ if (deployment != null && server != null && server.getState().equals(LifecycleState.STARTED))
+ {
+ // Undeploy
+ server.undeploy(deployment);
+ }
+
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Tests ------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Ensures that invocation upon an EJB with {@link CachingAuditor} declared
+ * results in the interception of targeted methods
+ */
+ @Test
+ public void testCachingInterception() throws NamingException, IOException
+ {
+ // Ensure no invocations intercepted yet
+ TestCase.assertEquals("No invocations should have yet been intercepted", 0, CachingAuditor.getInvocations()
+ .size());
+
+ // Invoke
+ final int channel = 1;
+ final InputStream content = bean.getChannel(channel);
+
+ // Test the response is as expected
+ TestCase.assertEquals("Did not obtain expected response", channel, content.read());
+
+ // Test the invocation was intercepted
+ TestCase.assertEquals("The invocation should have been intercepted", 1, CachingAuditor.getInvocations().size());
+ }
+
+ /**
+ * Ensures that requests to obtain Channel 2 while restricted are blocked with {@link Channel2ClosedException}
+ */
+ @Test(expected = Channel2ClosedException.class)
+ public void testChannel2Restricted() throws Throwable
+ {
+ // Set the policy to block channel 2
+ Channel2AccessPolicy.setChannel2Permitted(false);
+
+ // Invoke
+ try
+ {
+ bean.getChannel(2);
+ }
+ // Expected
+ catch (final UndeclaredThrowableException ute)
+ {
+ throw ute.getCause();
+ }
+
+ // Fail if we reach here
+ TestCase.fail("Request should have been blocked");
+ }
+
+ /**
+ * Ensures that requests to obtain Channel 2 while open succeed
+ */
+ @Test
+ public void testChannel2Allowed() throws NamingException, IOException
+ {
+ // Set the policy to block channel 2
+ Channel2AccessPolicy.setChannel2Permitted(true);
+
+ // Invoke
+ final int channel = 2;
+ final InputStream stream = bean.getChannel(channel);
+
+ // Test
+ TestCase.assertEquals("Unexpected content obtained from channel " + channel, channel, stream.read());
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Obtains $JBOSS_HOME from the system property
+ *
+ * @deprecated EJBBOOK-14
+ * @return
+ */
+ @Deprecated
+ private static URL getJBossHome()
+ {
+ final String sysProp = NAME_SYSPROP_JBOSS_HOME;
+ final String jbossHomeString = SecurityActions.getSystemProperty(sysProp);
+ if (jbossHomeString == null)
+ {
+ throw new IllegalStateException("System property \"" + sysProp + "\" must be present in the environment");
+ }
+ final File jbossHomeFile = new File(jbossHomeString);
+ if (!jbossHomeFile.exists())
+ {
+ throw new IllegalStateException("JBOSS_HOME does not exist: " + jbossHomeFile.getAbsolutePath());
+ }
+ try
+ {
+ return jbossHomeFile.toURI().toURL();
+ }
+ catch (final MalformedURLException murle)
+ {
+ throw new RuntimeException("Could not get JBOSS_HOME", murle);
+ }
+ }
+}
Copied: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/MockInvocationContext.java (from rev 99871, projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/MockInvocationContext.java)
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/MockInvocationContext.java (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/MockInvocationContext.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -0,0 +1,120 @@
+/*
+ * 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.ejb3.examples.chxx.tuner;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import javax.interceptor.InvocationContext;
+
+/**
+ * {@link InvocationContext} implementation which throws {@link UnsupportedOperationException}
+ * for all required methods except {@link InvocationContext#proceed()}, which will always return null,
+ * {@link InvocationContext#getMethod()}, and {@link InvocationContext#getParameters()}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class MockInvocationContext implements InvocationContext
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Message used to denote that the operation is not supported
+ */
+ private static final String MSG_UNSUPPORTED = "Not supported in mock implementation";
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Method invoked
+ */
+ private final Method method;
+
+ /**
+ * Parameters in the request
+ */
+ private final Object[] params;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Constructs a new instance with the specified required arguments
+ * @param method
+ * @param params
+ */
+ MockInvocationContext(final Method method, final Object[] params)
+ {
+
+ assert method != null : "method must be specified";
+ assert params != null : "params must be specified";
+ this.method = method;
+ this.params = params;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ @Override
+ public Map<String, Object> getContextData()
+ {
+ throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+ }
+
+ @Override
+ public Method getMethod()
+ {
+ return method;
+ }
+
+ @Override
+ public Object[] getParameters()
+ {
+ return params;
+ }
+
+ @Override
+ public Object getTarget()
+ {
+ throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+ }
+
+ @Override
+ public Object proceed() throws Exception
+ {
+ return null;
+ }
+
+ @Override
+ public void setParameters(final Object[] arg0)
+ {
+ throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+ }
+}
Copied: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/SecurityActions.java (from rev 99871, projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/SecurityActions.java)
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/SecurityActions.java (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/SecurityActions.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -0,0 +1,154 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.ejb3.examples.chxx.tuner;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * Protected security actions not to leak outside this package
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class SecurityActions
+{
+
+ //-------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------||
+
+ /**
+ * No external instanciation
+ */
+ private SecurityActions()
+ {
+
+ }
+
+ //-------------------------------------------------------------------------------||
+ // Utility Methods --------------------------------------------------------------||
+ //-------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the Thread Context ClassLoader
+ */
+ static ClassLoader getThreadContextClassLoader()
+ {
+ return AccessController.doPrivileged(GetTcclAction.INSTANCE);
+ }
+
+ /**
+ * Sets the specified CL upon the current Thread's Context
+ *
+ * @param cl
+ * @throws IllegalArgumentException If the CL was null
+ */
+ static void setThreadContextClassLoader(final ClassLoader cl) throws IllegalArgumentException
+ {
+ if (cl == null)
+ {
+ throw new IllegalArgumentException("ClassLoader was null");
+ }
+
+ AccessController.doPrivileged(new PrivilegedAction<Void>()
+ {
+ public Void run()
+ {
+ Thread.currentThread().setContextClassLoader(cl);
+ return null;
+ };
+ });
+ }
+
+ /**
+ * Obtains the system property with the specified key
+ *
+ * @param key
+ * @return
+ * @throws IllegalArgumentException If the key is null
+ */
+ static String getSystemProperty(final String key) throws IllegalArgumentException
+ {
+ // Precondition check
+ if (key == null)
+ {
+ throw new IllegalArgumentException("key was null");
+ }
+
+ // Get sysprop
+ return AccessController.doPrivileged(new GetSystemPropertyAction(key));
+ }
+
+ //-------------------------------------------------------------------------------||
+ // Inner Classes ----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------||
+
+ /**
+ * {@link PrivilegedAction} action to obtain the TCCL
+ */
+ private enum GetTcclAction implements PrivilegedAction<ClassLoader> {
+ INSTANCE;
+
+ @Override
+ public ClassLoader run()
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ }
+
+ /**
+ * {@link PrivilegedAction} to access a system property
+ *
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ private static class GetSystemPropertyAction implements PrivilegedAction<String>
+ {
+
+ /**
+ * Name of the sysprop to get
+ */
+ private String sysPropName;
+
+ /**
+ * Creates a new instance capable of obtaining the specified system property by name
+ * @param sysPropName
+ */
+ public GetSystemPropertyAction(final String sysPropName)
+ {
+ this.sysPropName = sysPropName;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see java.security.PrivilegedAction#run()
+ */
+ @Override
+ public String run()
+ {
+ return System.getProperty(sysPropName);
+ }
+ }
+
+}
Deleted: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/CachingInterceptorUnitTestCase.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/CachingInterceptorUnitTestCase.java 2010-01-25 06:53:09 UTC (rev 99874)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/CachingInterceptorUnitTestCase.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -1,102 +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.ejb3.examples.chxx.tuner.test;
-
-import java.util.logging.Logger;
-
-import javax.interceptor.InvocationContext;
-
-import junit.framework.TestCase;
-
-import org.jboss.ejb3.examples.chxx.tuner.CachingAuditor;
-import org.jboss.ejb3.examples.chxx.tuner.TunerLocalBusiness;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * Tests to ensure that the {@link CachingAuditor}
- * interceptor is working as expected outside the context
- * of a full container.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-public class CachingInterceptorUnitTestCase
-{
-
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Logger
- */
- private static final Logger log = Logger.getLogger(CachingInterceptorUnitTestCase.class.getName());
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * The interceptor instance to test
- */
- private CachingAuditor interceptor;
-
- //-------------------------------------------------------------------------------------||
- // Lifecycle --------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Creates the interceptor instance to be used in testing
- */
- @Before
- public void createInterceptor()
- {
- interceptor = new CachingAuditor();
- }
-
- //-------------------------------------------------------------------------------------||
- // Tests ------------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Ensures that contexts passed through the interceptor are cached
- */
- @Test
- public void testCache() throws Exception
- {
- // Ensure the cache is empty to start
- TestCase.assertEquals("Cache should start empty", 0, CachingAuditor.getInvocations().size());
-
- // Invoke
- final InvocationContext invocation = new MockInvocationContext(TunerLocalBusiness.class.getMethods()[0],
- new Object[]
- {1});
- interceptor.audit(invocation);
-
- // Test our invocation was cached properly
- TestCase.assertEquals("Cache should have the first invocation", 1, CachingAuditor.getInvocations().size());
- TestCase.assertEquals("Invocation cached was not the one that was invoked", invocation, CachingAuditor
- .getInvocations().get(0));
- }
-
-}
Deleted: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/Channel2RestrictorUnitTestCase.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/Channel2RestrictorUnitTestCase.java 2010-01-25 06:53:09 UTC (rev 99874)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/Channel2RestrictorUnitTestCase.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -1,141 +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.ejb3.examples.chxx.tuner.test;
-
-import java.lang.reflect.Method;
-import java.util.logging.Logger;
-
-import javax.interceptor.InvocationContext;
-
-import junit.framework.TestCase;
-
-import org.jboss.ejb3.examples.chxx.tuner.Channel2AccessPolicy;
-import org.jboss.ejb3.examples.chxx.tuner.Channel2ClosedException;
-import org.jboss.ejb3.examples.chxx.tuner.Channel2Restrictor;
-import org.jboss.ejb3.examples.chxx.tuner.TunerLocalBusiness;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * Tests to ensure that the {@link Channel2Restrictor}
- * interceptor is working as expected outside the context
- * of a full container.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-public class Channel2RestrictorUnitTestCase
-{
-
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Logger
- */
- private static final Logger log = Logger.getLogger(Channel2RestrictorUnitTestCase.class.getName());
-
- /**
- * Method to get channel content
- */
- private static final Method METHOD_GET_CHANNEL = TunerLocalBusiness.class.getMethods()[0];
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * The interceptor instance to test
- */
- private Channel2Restrictor interceptor;
-
- //-------------------------------------------------------------------------------------||
- // Lifecycle --------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Creates the interceptor instance to be used in testing
- */
- @Before
- public void createInterceptor()
- {
- interceptor = new Channel2Restrictor();
- }
-
- //-------------------------------------------------------------------------------------||
- // Tests ------------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Ensures requests for channel 2 are blocked when the channel's access is closed
- */
- @Test(expected = Channel2ClosedException.class)
- public void requestsToChannel2Blocked() throws Exception
- {
- // Set the access policy to block
- Channel2AccessPolicy.setChannel2Permitted(false);
-
- // Invoke
- final InvocationContext invocation = new MockInvocationContext(METHOD_GET_CHANNEL, new Object[]
- {2});
- interceptor.checkAccessibility(invocation);
- }
-
- /**
- * Ensures requests for channel 2 are not blocked when the channel's access is open
- */
- @Test
- public void requestsToChannel2NotBlocked() throws Exception
- {
- // Set the access policy to block
- Channel2AccessPolicy.setChannel2Permitted(true);
-
- // Invoke
- final InvocationContext invocation = new MockInvocationContext(METHOD_GET_CHANNEL, new Object[]
- {2});
- try
- {
- interceptor.checkAccessibility(invocation);
- }
- catch (final Channel2ClosedException e)
- {
- TestCase.fail("Should not have been blocked with: " + e);
- }
- }
-
- /**
- * Ensures requests for channel 1 are not blocked channel 2's access is closed
- */
- @Test
- public void requestsToChannel1NeverBlocked() throws Exception
- {
- // Set the access policy to block
- Channel2AccessPolicy.setChannel2Permitted(false);
-
- // Invoke
- final InvocationContext invocation = new MockInvocationContext(METHOD_GET_CHANNEL, new Object[]
- {1});
- interceptor.checkAccessibility(invocation);
- }
-
-}
Deleted: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/InterceptionIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/InterceptionIntegrationTest.java 2010-01-25 06:53:09 UTC (rev 99874)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/InterceptionIntegrationTest.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -1,322 +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.ejb3.examples.chxx.tuner.test;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.reflect.UndeclaredThrowableException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.logging.Logger;
-
-import javax.interceptor.Interceptors;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import junit.framework.TestCase;
-
-import org.jboss.bootstrap.api.lifecycle.LifecycleState;
-import org.jboss.ejb3.examples.chxx.tuner.CachingAuditor;
-import org.jboss.ejb3.examples.chxx.tuner.Channel2AccessPolicy;
-import org.jboss.ejb3.examples.chxx.tuner.Channel2ClosedException;
-import org.jboss.ejb3.examples.chxx.tuner.Channel2Restrictor;
-import org.jboss.ejb3.examples.chxx.tuner.TunerBean;
-import org.jboss.ejb3.examples.chxx.tuner.TunerLocalBusiness;
-import org.jboss.embedded.api.server.JBossASEmbeddedServer;
-import org.jboss.embedded.api.server.JBossASEmbeddedServerFactory;
-import org.jboss.embedded.api.server.JBossHomeClassLoader;
-import org.jboss.shrinkwrap.api.Archives;
-import org.jboss.shrinkwrap.api.spec.JavaArchive;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-/**
- * Integration test ensuring that an EJB with {@link Interceptors}
- * declared are intercepted when invoked
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-public class InterceptionIntegrationTest
-{
-
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Logger
- */
- private static final Logger log = Logger.getLogger(InterceptionIntegrationTest.class.getName());
-
- /**
- * The CL of the test as originally loaded
- */
- private static ClassLoader originalClassLoader;
-
- /**
- * The server instance
- */
- private static JBossASEmbeddedServer server;
-
- /**
- * Name of the system property for JBOSS_HOME
- * @deprecated EJBBOOK-14
- */
- @Deprecated
- private static final String NAME_SYSPROP_JBOSS_HOME = "jboss.home";
-
- /**
- * The JNDI Context
- */
- private static Context NAMING_CONTEXT;
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Archive representing the deployment
- */
- private JavaArchive deployment;
-
- /**
- * The bean to invoke upon
- */
- private TunerLocalBusiness bean;
-
- //-------------------------------------------------------------------------------------||
- // Lifecycle --------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Creates and starts a new JBossAS Server Embedded within this JVM
- */
- //TODO EJBBOOK-15
- @BeforeClass
- public static void createAndStartJBossASAndSetNamingContext() throws Exception
- {
- // Get JBOSS_HOME
- final URL jbossHome = getJBossHome();
-
- // Get additional binaries which need CL visibility (ie. jboss-embedded-core,
- // which is placed under "target/deps" by the build). These
- // binaries are not presently available under $JBOSS_HOME
- final Set<URL> additionalUrls = new HashSet<URL>();
- final URL source = InterceptionIntegrationTest.class.getProtectionDomain().getCodeSource().getLocation();
- final URL target = new URL(source, "..");
- final URL additionalDeps = new URL(target, "deps");
- final File deps = new File(additionalDeps.toURI());
- TestCase.assertTrue("Dependencies location does not exist: " + deps, deps.exists());
- TestCase.assertTrue("Dependencies location is not a directory: " + deps, deps.isDirectory());
- for (final File child : deps.listFiles())
- {
- additionalUrls.add(child.toURI().toURL());
- log.info("Booting with: " + child);
- }
-
- // Make the new ClassLoader
- originalClassLoader = SecurityActions.getThreadContextClassLoader();
- final ClassLoader jbossHomeClassLoader = JBossHomeClassLoader.newInstance(jbossHome, additionalUrls
- .toArray(new URL[]
- {}), originalClassLoader);
-
- // Make Server
- server = JBossASEmbeddedServerFactory.createServer(jbossHomeClassLoader);
- log.info("Created: " + server);
-
- // Start
- log.info("Starting Server: " + server);
-
- // Set TCCL
- SecurityActions.setThreadContextClassLoader(jbossHomeClassLoader);
-
- // Start the Server
- server.start();
-
- // Set Naming Context
- NAMING_CONTEXT = new InitialContext();
- }
-
- /**
- * Stops the Application Server
- */
- @AfterClass
- public static void stopJBossAS() throws Exception
- {
- if (server != null && server.getState().equals(LifecycleState.STARTED))
- {
- try
- {
- server.shutdown();
- }
- finally
- {
- // Reset the TCCL
- Thread.currentThread().setContextClassLoader(originalClassLoader);
- }
- }
- }
-
- /**
- * Deploys the EJB into the server and looks up an invokable reference
- * @throws Exception
- */
- @Before
- public void deployAndGetBean() throws Exception
- {
-
- // Create the archive
- deployment = Archives.create("echo.jar", JavaArchive.class).addClasses(TunerLocalBusiness.class, TunerBean.class,
- CachingAuditor.class, Channel2Restrictor.class);
-
- // Deploy
- server.deploy(deployment);
-
- // Lookup
- bean = (TunerLocalBusiness) NAMING_CONTEXT.lookup(TunerLocalBusiness.JNDI_NAME);
- }
-
- /**
- * Undeploys the EJB from the server
- * @throws Exception
- */
- @After
- public void undeploy() throws Exception
- {
- // If we've created the running server and deployed into it
- if (deployment != null && server != null && server.getState().equals(LifecycleState.STARTED))
- {
- // Undeploy
- server.undeploy(deployment);
- }
-
- }
-
- //-------------------------------------------------------------------------------------||
- // Tests ------------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Ensures that invocation upon an EJB with {@link CachingAuditor} declared
- * results in the interception of targeted methods
- */
- @Test
- public void testCachingInterception() throws NamingException, IOException
- {
- // Ensure no invocations intercepted yet
- TestCase.assertEquals("No invocations should have yet been intercepted", 0, CachingAuditor.getInvocations()
- .size());
-
- // Invoke
- final int channel = 1;
- final InputStream content = bean.getChannel(channel);
-
- // Test the response is as expected
- TestCase.assertEquals("Did not obtain expected response", channel, content.read());
-
- // Test the invocation was intercepted
- TestCase.assertEquals("The invocation should have been intercepted", 1, CachingAuditor.getInvocations().size());
- }
-
- /**
- * Ensures that requests to obtain Channel 2 while restricted are blocked with {@link Channel2ClosedException}
- */
- @Test(expected = Channel2ClosedException.class)
- public void testChannel2Restricted() throws Throwable
- {
- // Set the policy to block channel 2
- Channel2AccessPolicy.setChannel2Permitted(false);
-
- // Invoke
- try
- {
- bean.getChannel(2);
- }
- // Expected
- catch (final UndeclaredThrowableException ute)
- {
- throw ute.getCause();
- }
-
- // Fail if we reach here
- TestCase.fail("Request should have been blocked");
- }
-
- /**
- * Ensures that requests to obtain Channel 2 while open succeed
- */
- @Test
- public void testChannel2Allowed() throws NamingException, IOException
- {
- // Set the policy to block channel 2
- Channel2AccessPolicy.setChannel2Permitted(true);
-
- // Invoke
- final int channel = 2;
- final InputStream stream = bean.getChannel(channel);
-
- // Test
- TestCase.assertEquals("Unexpected content obtained from channel " + channel, channel, stream.read());
- }
-
- //-------------------------------------------------------------------------------------||
- // Internal Helper Methods ------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Obtains $JBOSS_HOME from the system property
- *
- * @deprecated EJBBOOK-14
- * @return
- */
- @Deprecated
- private static URL getJBossHome()
- {
- final String sysProp = NAME_SYSPROP_JBOSS_HOME;
- final String jbossHomeString = SecurityActions.getSystemProperty(sysProp);
- if (jbossHomeString == null)
- {
- throw new IllegalStateException("System property \"" + sysProp + "\" must be present in the environment");
- }
- final File jbossHomeFile = new File(jbossHomeString);
- if (!jbossHomeFile.exists())
- {
- throw new IllegalStateException("JBOSS_HOME does not exist: " + jbossHomeFile.getAbsolutePath());
- }
- try
- {
- return jbossHomeFile.toURI().toURL();
- }
- catch (final MalformedURLException murle)
- {
- throw new RuntimeException("Could not get JBOSS_HOME", murle);
- }
- }
-}
Deleted: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/MockInvocationContext.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/MockInvocationContext.java 2010-01-25 06:53:09 UTC (rev 99874)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/MockInvocationContext.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -1,120 +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.ejb3.examples.chxx.tuner.test;
-
-import java.lang.reflect.Method;
-import java.util.Map;
-
-import javax.interceptor.InvocationContext;
-
-/**
- * {@link InvocationContext} implementation which throws {@link UnsupportedOperationException}
- * for all required methods except {@link InvocationContext#proceed()}, which will always return null,
- * {@link InvocationContext#getMethod()}, and {@link InvocationContext#getParameters()}.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-class MockInvocationContext implements InvocationContext
-{
-
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Message used to denote that the operation is not supported
- */
- private static final String MSG_UNSUPPORTED = "Not supported in mock implementation";
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Method invoked
- */
- private final Method method;
-
- /**
- * Parameters in the request
- */
- private final Object[] params;
-
- //-------------------------------------------------------------------------------------||
- // Constructor ------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Constructs a new instance with the specified required arguments
- * @param method
- * @param params
- */
- MockInvocationContext(final Method method, final Object[] params)
- {
-
- assert method != null : "method must be specified";
- assert params != null : "params must be specified";
- this.method = method;
- this.params = params;
- }
-
- //-------------------------------------------------------------------------------------||
- // Required Implementations -----------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- @Override
- public Map<String, Object> getContextData()
- {
- throw new UnsupportedOperationException(MSG_UNSUPPORTED);
- }
-
- @Override
- public Method getMethod()
- {
- return method;
- }
-
- @Override
- public Object[] getParameters()
- {
- return params;
- }
-
- @Override
- public Object getTarget()
- {
- throw new UnsupportedOperationException(MSG_UNSUPPORTED);
- }
-
- @Override
- public Object proceed() throws Exception
- {
- return null;
- }
-
- @Override
- public void setParameters(final Object[] arg0)
- {
- throw new UnsupportedOperationException(MSG_UNSUPPORTED);
- }
-}
Deleted: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/SecurityActions.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/SecurityActions.java 2010-01-25 06:53:09 UTC (rev 99874)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/tuner/test/SecurityActions.java 2010-01-25 07:14:19 UTC (rev 99875)
@@ -1,154 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, 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.ejb3.examples.chxx.tuner.test;
-
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-
-/**
- * Protected security actions not to leak outside this package
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-class SecurityActions
-{
-
- //-------------------------------------------------------------------------------||
- // Constructor ------------------------------------------------------------------||
- //-------------------------------------------------------------------------------||
-
- /**
- * No external instanciation
- */
- private SecurityActions()
- {
-
- }
-
- //-------------------------------------------------------------------------------||
- // Utility Methods --------------------------------------------------------------||
- //-------------------------------------------------------------------------------||
-
- /**
- * Obtains the Thread Context ClassLoader
- */
- static ClassLoader getThreadContextClassLoader()
- {
- return AccessController.doPrivileged(GetTcclAction.INSTANCE);
- }
-
- /**
- * Sets the specified CL upon the current Thread's Context
- *
- * @param cl
- * @throws IllegalArgumentException If the CL was null
- */
- static void setThreadContextClassLoader(final ClassLoader cl) throws IllegalArgumentException
- {
- if (cl == null)
- {
- throw new IllegalArgumentException("ClassLoader was null");
- }
-
- AccessController.doPrivileged(new PrivilegedAction<Void>()
- {
- public Void run()
- {
- Thread.currentThread().setContextClassLoader(cl);
- return null;
- };
- });
- }
-
- /**
- * Obtains the system property with the specified key
- *
- * @param key
- * @return
- * @throws IllegalArgumentException If the key is null
- */
- static String getSystemProperty(final String key) throws IllegalArgumentException
- {
- // Precondition check
- if (key == null)
- {
- throw new IllegalArgumentException("key was null");
- }
-
- // Get sysprop
- return AccessController.doPrivileged(new GetSystemPropertyAction(key));
- }
-
- //-------------------------------------------------------------------------------||
- // Inner Classes ----------------------------------------------------------------||
- //-------------------------------------------------------------------------------||
-
- /**
- * {@link PrivilegedAction} action to obtain the TCCL
- */
- private enum GetTcclAction implements PrivilegedAction<ClassLoader> {
- INSTANCE;
-
- @Override
- public ClassLoader run()
- {
- return Thread.currentThread().getContextClassLoader();
- }
- }
-
- /**
- * {@link PrivilegedAction} to access a system property
- *
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
- private static class GetSystemPropertyAction implements PrivilegedAction<String>
- {
-
- /**
- * Name of the sysprop to get
- */
- private String sysPropName;
-
- /**
- * Creates a new instance capable of obtaining the specified system property by name
- * @param sysPropName
- */
- public GetSystemPropertyAction(final String sysPropName)
- {
- this.sysPropName = sysPropName;
- }
-
- /**
- * {@inheritDoc}
- * @see java.security.PrivilegedAction#run()
- */
- @Override
- public String run()
- {
- return System.getProperty(sysPropName);
- }
- }
-
-}
More information about the jboss-cvs-commits
mailing list