Skip to content

Commit 888483a

Browse files
committed
First test.
1 parent fd47eae commit 888483a

File tree

6 files changed

+216
-0
lines changed

6 files changed

+216
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
*
3+
*/
4+
package org.bugreport.ejb;
5+
6+
import javax.inject.Named;
7+
8+
@Named
9+
public class CdiBean {
10+
11+
public String hello() {
12+
return "hello!";
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
4+
version="1.2" bean-discovery-mode="annotated">
5+
</beans>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
*
3+
*/
4+
package org.bugreport.ejb;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
import javax.inject.Inject;
9+
10+
import org.bugreport.ejb.test.ArquillianDaoUnitTest;
11+
import org.junit.Test;
12+
13+
/**
14+
* @author David Matějček
15+
*/
16+
public class BugReportTest extends ArquillianDaoUnitTest {
17+
18+
@Inject
19+
private CdiBean bean;
20+
21+
@Test
22+
public void test() {
23+
final String response = bean.hello();
24+
assertEquals("hello!", response);
25+
}
26+
27+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://jboss.org/schema/arquillian"
3+
xsi:schemaLocation="http://jboss.org/schema/arquillian
4+
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
5+
<engine>
6+
<property name="deploymentExportPath">target/deployment</property>
7+
</engine>
8+
<container qualifier="glassfish-embedded" default="true">
9+
<configuration>
10+
<property name="bindHttpPort">58888</property>
11+
<property name="bindHttpsPort">58889</property>
12+
</configuration>
13+
</container>
14+
</arquillian>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
status = info
3+
dest = err
4+
name = PropertiesConfig
5+
#log4j2.debug = true
6+
7+
filter.threshold.type = ThresholdFilter
8+
filter.threshold.level = trace
9+
10+
appender.console.type = Console
11+
appender.console.name = STDOUT
12+
appender.console.layout.type = PatternLayout
13+
appender.console.layout.pattern = %d{HH:mm:ss,SSS} %-5p [%t] %25c{3}: %m%n
14+
appender.console.filter.threshold.type = ThresholdFilter
15+
appender.console.filter.threshold.level = trace
16+
17+
rootLogger.level = debug
18+
rootLogger.appenderRef.stdout.ref = STDOUT

0 commit comments

Comments
 (0)