|
| 1 | +package org.bugreport.ejb.test; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +import javax.naming.InitialContext; |
| 6 | +import javax.naming.NamingException; |
| 7 | + |
| 8 | +import org.apache.logging.log4j.LogManager; |
| 9 | +import org.apache.logging.log4j.Logger; |
| 10 | +import org.glassfish.embeddable.CommandResult; |
| 11 | +import org.glassfish.embeddable.CommandRunner; |
| 12 | +import org.glassfish.embeddable.GlassFishException; |
| 13 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 14 | +import org.jboss.arquillian.junit.Arquillian; |
| 15 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 16 | +import org.jboss.shrinkwrap.api.asset.EmptyAsset; |
| 17 | +import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; |
| 18 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.BeforeClass; |
| 22 | +import org.junit.Rule; |
| 23 | +import org.junit.rules.TestName; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author David Matějček |
| 28 | + */ |
| 29 | +@RunWith(Arquillian.class) |
| 30 | +public abstract class ArquillianDaoUnitTest { |
| 31 | + |
| 32 | + private static final Logger LOG = LogManager.getLogger(ArquillianDaoUnitTest.class); |
| 33 | + |
| 34 | + private static boolean containerInitialized; |
| 35 | + |
| 36 | + private long start; |
| 37 | + @Rule |
| 38 | + public final TestName name = new TestName(); |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * Only to mark the class initialization in logs |
| 43 | + */ |
| 44 | + @BeforeClass |
| 45 | + public static void initContainer() { |
| 46 | + LOG.info("initContainer()"); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + @Before |
| 51 | + public void before() { |
| 52 | + LOG.info("before(). Test name: {}", name.getMethodName()); |
| 53 | + this.start = System.currentTimeMillis(); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + @After |
| 58 | + public void after() { |
| 59 | + LOG.info("after(). Test name: {}, test time: {} ms", name.getMethodName(), System.currentTimeMillis() - this.start); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + /** |
| 64 | + * Initializes the deployment unit. |
| 65 | + * |
| 66 | + * @return {@link EnterpriseArchive} to deploy to the container. |
| 67 | + * @throws Exception exception |
| 68 | + */ |
| 69 | + @Deployment |
| 70 | + public static JavaArchive getArchiveToDeploy() throws Exception { |
| 71 | + if (!containerInitialized) { |
| 72 | + initEnvironment(); |
| 73 | + containerInitialized = true; |
| 74 | + } |
| 75 | + |
| 76 | + final JavaArchive ejbModule = ShrinkWrap.create(JavaArchive.class).addPackages(true, "org.bugreport.ejb") |
| 77 | + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");// |
| 78 | + |
| 79 | + LOG.info(ejbModule.toString(true)); |
| 80 | + return ejbModule; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + private static void initEnvironment() { |
| 85 | + LOG.debug("initEnvironment()"); |
| 86 | + LOG.debug("System properties:\n {}", System.getProperties()); |
| 87 | + |
| 88 | + try { |
| 89 | + runCommand("list-jdbc-connection-pools", "--echo=true", "--terse=true"); |
| 90 | + |
| 91 | + runCommand("set", "configs.config.server-config.jms-service.type=DISABLED"); |
| 92 | + runCommand("set", "configs.config.server-config.admin-service.das-config.deploy-xml-validation=none"); |
| 93 | + runCommand("set", "configs.config.server-config.iiop-service.iiop-listener.orb-listener-1.port=17300"); |
| 94 | + runCommand("set", "configs.config.server-config.iiop-service.iiop-listener.SSL.port=17301"); |
| 95 | + runCommand("set", "configs.config.server-config.iiop-service.iiop-listener.SSL_MUTUALAUTH.port=17302"); |
| 96 | + } catch (final Exception e) { |
| 97 | + throw new IllegalStateException("Cannot initialize the container!", e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + /** |
| 103 | + * Execute the command with parameters and return a result. |
| 104 | + * |
| 105 | + * @param command |
| 106 | + * @param parameters |
| 107 | + * @return result of the command |
| 108 | + * @throws GlassFishException - cannot communicate with the instance |
| 109 | + * @throws IllegalStateException - invalid parameters or command |
| 110 | + */ |
| 111 | + private static CommandResult runCommand(final String command, final String... parameters) throws GlassFishException { |
| 112 | + LOG.debug("runCommand(command={}, parameters={})", command, parameters); |
| 113 | + |
| 114 | + final CommandRunner runner; |
| 115 | + try { |
| 116 | + final InitialContext ctx = new InitialContext(); |
| 117 | + runner = (CommandRunner) ctx.lookup(CommandRunner.class.getCanonicalName()); |
| 118 | + Objects.requireNonNull(runner, "No command runner instance found in initial context!"); |
| 119 | + } catch (final NamingException e) { |
| 120 | + throw new IllegalStateException("Cannot run command " + command, e); |
| 121 | + } |
| 122 | + |
| 123 | + final CommandResult result = runner.run(command, parameters); |
| 124 | + checkCommandResult(command, result); |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + private static void checkCommandResult(final String cmd, final CommandResult result) { |
| 130 | + LOG.info("Command: {}\n Result.status:\n {}\n Result.out:\n {}\n Result.failCause:\n {}\n", cmd, |
| 131 | + result.getExitStatus(), result.getOutput(), result.getFailureCause()); |
| 132 | + |
| 133 | + if (result.getExitStatus().ordinal() != 0) { |
| 134 | + throw new IllegalStateException("Command '" + cmd + "' was unsuccessful: " + result.getOutput(), |
| 135 | + result.getFailureCause()); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments