PageRenderTime 65ms CodeModel.GetById 16ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/testsuite/integration/manualmode/src/test/java/org/jboss/as/test/manualmode/ejb/client/outbound/connection/LazyOutboundConnectionReconnectTestCase.java

#
Java | 206 lines | 125 code | 23 blank | 58 comment | 5 complexity | 7048e3a2354399cd1b0ed93101f2f0e5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source.
  3 * Copyright 2012, Red Hat, Inc., and individual contributors
  4 * as indicated by the @author tags. See the copyright.txt file in the
  5 * distribution for a full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.jboss.as.test.manualmode.ejb.client.outbound.connection;
 24
 25import org.jboss.arquillian.container.test.api.ContainerController;
 26import org.jboss.arquillian.container.test.api.Deployer;
 27import org.jboss.arquillian.container.test.api.Deployment;
 28import org.jboss.arquillian.container.test.api.RunAsClient;
 29import org.jboss.arquillian.container.test.api.TargetsContainer;
 30import org.jboss.arquillian.junit.Arquillian;
 31import org.jboss.arquillian.test.api.ArquillianResource;
 32import org.jboss.ejb.client.ContextSelector;
 33import org.jboss.ejb.client.EJBClientConfiguration;
 34import org.jboss.ejb.client.EJBClientContext;
 35import org.jboss.ejb.client.PropertiesBasedEJBClientConfiguration;
 36import org.jboss.ejb.client.remoting.ConfigBasedEJBClientContextSelector;
 37import org.jboss.logging.Logger;
 38import org.jboss.shrinkwrap.api.Archive;
 39import org.jboss.shrinkwrap.api.ShrinkWrap;
 40import org.jboss.shrinkwrap.api.spec.JavaArchive;
 41import org.junit.AfterClass;
 42import org.junit.Assert;
 43import org.junit.BeforeClass;
 44import org.junit.Test;
 45import org.junit.runner.RunWith;
 46
 47import javax.naming.Context;
 48import javax.naming.InitialContext;
 49import java.io.IOException;
 50import java.io.InputStream;
 51import java.util.Hashtable;
 52import java.util.Properties;
 53
 54/**
 55 * Tests that if a deployment contains a jboss-ejb-client.xml pointing to a outbound connection to a server
 56 * which isn't yet up, then it doesn't fail the deployment. Instead it (re)connects whenever the server is ready and
 57 * available.
 58 *
 59 * @author Jaikiran Pai
 60 * @see https://issues.jboss.org/browse/AS7-3820 for details
 61 */
 62@RunWith(Arquillian.class)
 63@RunAsClient
 64public class LazyOutboundConnectionReconnectTestCase {
 65
 66    private static final Logger logger = Logger.getLogger(LazyOutboundConnectionReconnectTestCase.class);
 67
 68    private static final String SERVER_ONE_MODULE_NAME = "server-one-module";
 69    private static final String SERVER_TWO_MODULE_NAME = "server-two-module";
 70
 71    private static final String DEFAULT_JBOSSAS = "default-jbossas";
 72    private static final String JBOSSAS_WITH_REMOTE_OUTBOUND_CONNECTION = "jbossas-with-remote-outbound-connection";
 73
 74    private static final String DEFAULT_AS_DEPLOYMENT = "default-jbossas-deployment";
 75    private static final String DEPLOYMENT_WITH_JBOSS_EJB_CLIENT_XML = "other-deployment";
 76
 77    @ArquillianResource
 78    private ContainerController container;
 79
 80    @ArquillianResource
 81    private Deployer deployer;
 82
 83    private static Context context;
 84    private static ContextSelector<EJBClientContext> previousClientContextSelector;
 85
 86    @BeforeClass
 87    public static void beforeClass() throws Exception {
 88        final Hashtable props = new Hashtable();
 89        props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
 90        context = new InitialContext(props);
 91        // setup the client context selector
 92        previousClientContextSelector = setupEJBClientContextSelector();
 93
 94    }
 95
 96    @AfterClass
 97    public static void afterClass() {
 98        if (previousClientContextSelector != null) {
 99            EJBClientContext.setSelector(previousClientContextSelector);
100        }
101    }
102
103    @Deployment(name = DEFAULT_AS_DEPLOYMENT, managed = false, testable = false)
104    @TargetsContainer(DEFAULT_JBOSSAS)
105    public static Archive createContainer1Deployment() {
106        final JavaArchive ejbJar = ShrinkWrap.create(JavaArchive.class, SERVER_TWO_MODULE_NAME + ".jar");
107        ejbJar.addClasses(EchoOnServerTwo.class, RemoteEcho.class);
108        return ejbJar;
109    }
110
111    @Deployment(name = DEPLOYMENT_WITH_JBOSS_EJB_CLIENT_XML, managed = false, testable = false)
112    @TargetsContainer(JBOSSAS_WITH_REMOTE_OUTBOUND_CONNECTION)
113    public static Archive createContainer2Deployment() {
114        final JavaArchive ejbJar = ShrinkWrap.create(JavaArchive.class, SERVER_ONE_MODULE_NAME + ".jar");
115        ejbJar.addClasses(EchoOnServerOne.class, RemoteEcho.class, IndependentBean.class);
116        ejbJar.addAsManifestResource(EchoOnServerOne.class.getPackage(), "jboss-ejb-client.xml", "jboss-ejb-client.xml");
117        return ejbJar;
118    }
119
120    /**
121     * Start a server (A) which has a remote outbound connection to another server (B). Server (B) is down.
122     * Deploy (X) to server A. X contains a jboss-ejb-client.xml pointing to server B (which is down). The deployment
123     * must succeed. However invocations on beans which depend on server B should fail.
124     * Then start server B and deploy Y to it. Invoke again on server A beans which depend on server B and this time
125     * they should pass
126     *
127     * @throws Exception
128     */
129    @Test
130    public void testRemoteServerStartsLate() throws Exception {
131        // First start the server which has a remote-outbound-connection
132        this.container.start(JBOSSAS_WITH_REMOTE_OUTBOUND_CONNECTION);
133        boolean defaultContainerStarted = false;
134        try {
135            // deploy a deployment which contains jboss-ejb-client.xml that contains a EJB receiver pointing
136            // to a server which hasn't yet started. Should succeed without throwing deployment error
137            this.deployer.deploy(DEPLOYMENT_WITH_JBOSS_EJB_CLIENT_XML);
138            // To make sure deployment succeeded and invocations are possible, call a independent bean
139            final RemoteEcho independentBean = (RemoteEcho) context.lookup("ejb:/" + SERVER_ONE_MODULE_NAME + "//" + IndependentBean.class.getSimpleName() + "!" + RemoteEcho.class.getName());
140            final String msg = "Hellooooo!";
141            final String echoFromIndependentBean = independentBean.echo(msg);
142            Assert.assertEquals("Unexpected echo from independent bean", msg, echoFromIndependentBean);
143
144            // now try invoking the EJB (which calls a delegate bean on other server) on this server. 
145            // should fail with no EJB receivers, since the other server
146            // which can handle the delegate bean invocation hasn't yet started.
147            try {
148                final RemoteEcho dependentBean = (RemoteEcho) context.lookup("ejb:/" + SERVER_ONE_MODULE_NAME + "//" + EchoOnServerOne.class.getSimpleName() + "!" + RemoteEcho.class.getName());
149                final String echoBeforeOtherServerStart = dependentBean.echo(msg);
150                Assert.fail("Invocation on bean when was expected to fail due to other server being down");
151            } catch (Exception e) {
152                // expected 
153                logger.info("Got the expected exception on invoking a bean when other server was down", e);
154            }
155            // now start the main server
156            this.container.start(DEFAULT_JBOSSAS);
157            defaultContainerStarted = true;
158            // deploy to this container
159            this.deployer.deploy(DEFAULT_AS_DEPLOYMENT);
160
161            // now invoke the EJB (which had failed earlier)
162            final RemoteEcho dependentBean = (RemoteEcho) context.lookup("ejb:/" + SERVER_ONE_MODULE_NAME + "//" + EchoOnServerOne.class.getSimpleName() + "!" + RemoteEcho.class.getName());
163            final String echoAfterOtherServerStarted = dependentBean.echo(msg);
164            Assert.assertEquals("Unexpected echo from bean", EchoOnServerTwo.ECHO_PREFIX + msg, echoAfterOtherServerStarted);
165
166        } finally {
167            try {
168                this.deployer.undeploy(DEPLOYMENT_WITH_JBOSS_EJB_CLIENT_XML);
169                this.container.stop(JBOSSAS_WITH_REMOTE_OUTBOUND_CONNECTION);
170            } catch (Exception e) {
171                logger.debug("Exception during container shutdown", e);
172            }
173            if (defaultContainerStarted) {
174                try {
175                    this.deployer.undeploy(DEFAULT_AS_DEPLOYMENT);
176                    this.container.stop(DEFAULT_JBOSSAS);
177                } catch (Exception e) {
178                    logger.debug("Exception during container shutdown", e);
179                }
180            }
181        }
182
183    }
184
185    /**
186     * Sets up the EJB client context to use a selector which processes and sets up EJB receivers
187     * based on this testcase specific jboss-ejb-client.properties file
188     *
189     * @return
190     * @throws java.io.IOException
191     */
192    private static ContextSelector<EJBClientContext> setupEJBClientContextSelector() throws IOException {
193        // setup the selector
194        final String clientPropertiesFile = "org/jboss/as/test/manualmode/ejb/client/outbound/connection/jboss-ejb-client.properties";
195        final InputStream inputStream = LazyOutboundConnectionReconnectTestCase.class.getClassLoader().getResourceAsStream(clientPropertiesFile);
196        if (inputStream == null) {
197            throw new IllegalStateException("Could not find " + clientPropertiesFile + " in classpath");
198        }
199        final Properties properties = new Properties();
200        properties.load(inputStream);
201        final EJBClientConfiguration ejbClientConfiguration = new PropertiesBasedEJBClientConfiguration(properties);
202        final ConfigBasedEJBClientContextSelector selector = new ConfigBasedEJBClientContextSelector(ejbClientConfiguration);
203
204        return EJBClientContext.setSelector(selector);
205    }
206}