PageRenderTime 41ms CodeModel.GetById 7ms RepoModel.GetById 0ms 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. package org.jboss.as.test.manualmode.ejb.client.outbound.connection;
  23. import org.jboss.arquillian.container.test.api.ContainerController;
  24. import org.jboss.arquillian.container.test.api.Deployer;
  25. import org.jboss.arquillian.container.test.api.Deployment;
  26. import org.jboss.arquillian.container.test.api.RunAsClient;
  27. import org.jboss.arquillian.container.test.api.TargetsContainer;
  28. import org.jboss.arquillian.junit.Arquillian;
  29. import org.jboss.arquillian.test.api.ArquillianResource;
  30. import org.jboss.ejb.client.ContextSelector;
  31. import org.jboss.ejb.client.EJBClientConfiguration;
  32. import org.jboss.ejb.client.EJBClientContext;
  33. import org.jboss.ejb.client.PropertiesBasedEJBClientConfiguration;
  34. import org.jboss.ejb.client.remoting.ConfigBasedEJBClientContextSelector;
  35. import org.jboss.logging.Logger;
  36. import org.jboss.shrinkwrap.api.Archive;
  37. import org.jboss.shrinkwrap.api.ShrinkWrap;
  38. import org.jboss.shrinkwrap.api.spec.JavaArchive;
  39. import org.junit.AfterClass;
  40. import org.junit.Assert;
  41. import org.junit.BeforeClass;
  42. import org.junit.Test;
  43. import org.junit.runner.RunWith;
  44. import javax.naming.Context;
  45. import javax.naming.InitialContext;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.util.Hashtable;
  49. import java.util.Properties;
  50. /**
  51. * Tests that if a deployment contains a jboss-ejb-client.xml pointing to a outbound connection to a server
  52. * which isn't yet up, then it doesn't fail the deployment. Instead it (re)connects whenever the server is ready and
  53. * available.
  54. *
  55. * @author Jaikiran Pai
  56. * @see https://issues.jboss.org/browse/AS7-3820 for details
  57. */
  58. @RunWith(Arquillian.class)
  59. @RunAsClient
  60. public class LazyOutboundConnectionReconnectTestCase {
  61. private static final Logger logger = Logger.getLogger(LazyOutboundConnectionReconnectTestCase.class);
  62. private static final String SERVER_ONE_MODULE_NAME = "server-one-module";
  63. private static final String SERVER_TWO_MODULE_NAME = "server-two-module";
  64. private static final String DEFAULT_JBOSSAS = "default-jbossas";
  65. private static final String JBOSSAS_WITH_REMOTE_OUTBOUND_CONNECTION = "jbossas-with-remote-outbound-connection";
  66. private static final String DEFAULT_AS_DEPLOYMENT = "default-jbossas-deployment";
  67. private static final String DEPLOYMENT_WITH_JBOSS_EJB_CLIENT_XML = "other-deployment";
  68. @ArquillianResource
  69. private ContainerController container;
  70. @ArquillianResource
  71. private Deployer deployer;
  72. private static Context context;
  73. private static ContextSelector<EJBClientContext> previousClientContextSelector;
  74. @BeforeClass
  75. public static void beforeClass() throws Exception {
  76. final Hashtable props = new Hashtable();
  77. props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
  78. context = new InitialContext(props);
  79. // setup the client context selector
  80. previousClientContextSelector = setupEJBClientContextSelector();
  81. }
  82. @AfterClass
  83. public static void afterClass() {
  84. if (previousClientContextSelector != null) {
  85. EJBClientContext.setSelector(previousClientContextSelector);
  86. }
  87. }
  88. @Deployment(name = DEFAULT_AS_DEPLOYMENT, managed = false, testable = false)
  89. @TargetsContainer(DEFAULT_JBOSSAS)
  90. public static Archive createContainer1Deployment() {
  91. final JavaArchive ejbJar = ShrinkWrap.create(JavaArchive.class, SERVER_TWO_MODULE_NAME + ".jar");
  92. ejbJar.addClasses(EchoOnServerTwo.class, RemoteEcho.class);
  93. return ejbJar;
  94. }
  95. @Deployment(name = DEPLOYMENT_WITH_JBOSS_EJB_CLIENT_XML, managed = false, testable = false)
  96. @TargetsContainer(JBOSSAS_WITH_REMOTE_OUTBOUND_CONNECTION)
  97. public static Archive createContainer2Deployment() {
  98. final JavaArchive ejbJar = ShrinkWrap.create(JavaArchive.class, SERVER_ONE_MODULE_NAME + ".jar");
  99. ejbJar.addClasses(EchoOnServerOne.class, RemoteEcho.class, IndependentBean.class);
  100. ejbJar.addAsManifestResource(EchoOnServerOne.class.getPackage(), "jboss-ejb-client.xml", "jboss-ejb-client.xml");
  101. return ejbJar;
  102. }
  103. /**
  104. * Start a server (A) which has a remote outbound connection to another server (B). Server (B) is down.
  105. * Deploy (X) to server A. X contains a jboss-ejb-client.xml pointing to server B (which is down). The deployment
  106. * must succeed. However invocations on beans which depend on server B should fail.
  107. * Then start server B and deploy Y to it. Invoke again on server A beans which depend on server B and this time
  108. * they should pass
  109. *
  110. * @throws Exception
  111. */
  112. @Test
  113. public void testRemoteServerStartsLate() throws Exception {
  114. // First start the server which has a remote-outbound-connection
  115. this.container.start(JBOSSAS_WITH_REMOTE_OUTBOUND_CONNECTION);
  116. boolean defaultContainerStarted = false;
  117. try {
  118. // deploy a deployment which contains jboss-ejb-client.xml that contains a EJB receiver pointing
  119. // to a server which hasn't yet started. Should succeed without throwing deployment error
  120. this.deployer.deploy(DEPLOYMENT_WITH_JBOSS_EJB_CLIENT_XML);
  121. // To make sure deployment succeeded and invocations are possible, call a independent bean
  122. final RemoteEcho independentBean = (RemoteEcho) context.lookup("ejb:/" + SERVER_ONE_MODULE_NAME + "//" + IndependentBean.class.getSimpleName() + "!" + RemoteEcho.class.getName());
  123. final String msg = "Hellooooo!";
  124. final String echoFromIndependentBean = independentBean.echo(msg);
  125. Assert.assertEquals("Unexpected echo from independent bean", msg, echoFromIndependentBean);
  126. // now try invoking the EJB (which calls a delegate bean on other server) on this server.
  127. // should fail with no EJB receivers, since the other server
  128. // which can handle the delegate bean invocation hasn't yet started.
  129. try {
  130. final RemoteEcho dependentBean = (RemoteEcho) context.lookup("ejb:/" + SERVER_ONE_MODULE_NAME + "//" + EchoOnServerOne.class.getSimpleName() + "!" + RemoteEcho.class.getName());
  131. final String echoBeforeOtherServerStart = dependentBean.echo(msg);
  132. Assert.fail("Invocation on bean when was expected to fail due to other server being down");
  133. } catch (Exception e) {
  134. // expected
  135. logger.info("Got the expected exception on invoking a bean when other server was down", e);
  136. }
  137. // now start the main server
  138. this.container.start(DEFAULT_JBOSSAS);
  139. defaultContainerStarted = true;
  140. // deploy to this container
  141. this.deployer.deploy(DEFAULT_AS_DEPLOYMENT);
  142. // now invoke the EJB (which had failed earlier)
  143. final RemoteEcho dependentBean = (RemoteEcho) context.lookup("ejb:/" + SERVER_ONE_MODULE_NAME + "//" + EchoOnServerOne.class.getSimpleName() + "!" + RemoteEcho.class.getName());
  144. final String echoAfterOtherServerStarted = dependentBean.echo(msg);
  145. Assert.assertEquals("Unexpected echo from bean", EchoOnServerTwo.ECHO_PREFIX + msg, echoAfterOtherServerStarted);
  146. } finally {
  147. try {
  148. this.deployer.undeploy(DEPLOYMENT_WITH_JBOSS_EJB_CLIENT_XML);
  149. this.container.stop(JBOSSAS_WITH_REMOTE_OUTBOUND_CONNECTION);
  150. } catch (Exception e) {
  151. logger.debug("Exception during container shutdown", e);
  152. }
  153. if (defaultContainerStarted) {
  154. try {
  155. this.deployer.undeploy(DEFAULT_AS_DEPLOYMENT);
  156. this.container.stop(DEFAULT_JBOSSAS);
  157. } catch (Exception e) {
  158. logger.debug("Exception during container shutdown", e);
  159. }
  160. }
  161. }
  162. }
  163. /**
  164. * Sets up the EJB client context to use a selector which processes and sets up EJB receivers
  165. * based on this testcase specific jboss-ejb-client.properties file
  166. *
  167. * @return
  168. * @throws java.io.IOException
  169. */
  170. private static ContextSelector<EJBClientContext> setupEJBClientContextSelector() throws IOException {
  171. // setup the selector
  172. final String clientPropertiesFile = "org/jboss/as/test/manualmode/ejb/client/outbound/connection/jboss-ejb-client.properties";
  173. final InputStream inputStream = LazyOutboundConnectionReconnectTestCase.class.getClassLoader().getResourceAsStream(clientPropertiesFile);
  174. if (inputStream == null) {
  175. throw new IllegalStateException("Could not find " + clientPropertiesFile + " in classpath");
  176. }
  177. final Properties properties = new Properties();
  178. properties.load(inputStream);
  179. final EJBClientConfiguration ejbClientConfiguration = new PropertiesBasedEJBClientConfiguration(properties);
  180. final ConfigBasedEJBClientContextSelector selector = new ConfigBasedEJBClientContextSelector(ejbClientConfiguration);
  181. return EJBClientContext.setSelector(selector);
  182. }
  183. }