Created
June 4, 2012 11:12
-
-
Save pires/2867767 to your computer and use it in GitHub Desktop.
Glassfish Embedded TestNG Bootstrap (EJB + WAR)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Testing environment configuration. | |
*/ | |
public final class SetupTestSuite { | |
public final static String PU_NAME = "qos-test"; | |
public final static int SERVER_PORT = 8181; | |
private final String JDBC_POOL = "MyPool"; | |
private final String JDBC_RESOURCE = "jdbc/memory/qos"; | |
private Server server; | |
/** | |
* This is meant to run before all tests are performed. | |
* | |
* @throws Throwable | |
*/ | |
@BeforeSuite | |
public final void setUp() throws Throwable { | |
Server.Builder builder = new Server.Builder("server"); | |
server = builder.build(); | |
server.createPort(SERVER_PORT); | |
// add the required containers | |
server.addContainer(ContainerBuilder.Type.jpa); | |
server.addContainer(ContainerBuilder.Type.ejb); | |
server.addContainer(ContainerBuilder.Type.web); | |
// Create the WAR file | |
List<URL> urls = new ArrayList<URL>(); | |
urls.add(new File("target/classes").toURI().toURL()); | |
ScatteredArchive.Builder saBuilder = new ScatteredArchive.Builder("qos", | |
urls); | |
ScatteredArchive archive = saBuilder.buildWar(); | |
// Deploy the WAR file | |
EmbeddedDeployer deployer = server.getDeployer(); | |
deployer.deploy(archive, null); | |
// Create a JDBC connection pool | |
ParameterMap params = new ParameterMap(); | |
params.add("datasourceclassname", | |
"org.apache.derby.jdbc.EmbeddedXADataSource"); | |
params.add("property", | |
"databaseName=memory\\:qos:connectionAttributes=\\;create\\=true"); | |
params.add("jdbc_connection_pool_id", JDBC_POOL); | |
executeCommand("create-jdbc-connection-pool", server, params); | |
System.out.println("JDBC Connection Pool successfully created."); | |
// Assert the JDBC connection pool | |
List<MessagePart> messageParts = executeCommand( | |
"list-jdbc-connection-pools", server); | |
assertInArray(messageParts, JDBC_POOL); | |
// Create a JDBC resource | |
System.out.println("Creating JDBC resource ..."); | |
ParameterMap params2 = new ParameterMap(); | |
params2.add("connectionpoolid", "MyPool"); | |
params2.add("jndi_name", JDBC_RESOURCE); | |
executeCommand("create-jdbc-resource", server, params2); | |
// Assert the JDBC resource | |
messageParts = executeCommand("list-jdbc-resources", server); | |
assertInArray(messageParts, JDBC_RESOURCE); | |
} | |
/** | |
* This is meant to run after all tests are performed. | |
* | |
* @throws | |
*/ | |
@AfterSuite | |
public final void tearDown() { | |
try { | |
server.stop(); | |
} catch (LifecycleException e) { | |
System.out.println("Oops, testbed teardown failed."); | |
} finally { | |
server = null; | |
} | |
} | |
private List<MessagePart> executeCommand(String command, Server server) | |
throws Throwable { | |
return executeCommand(command, server, null); | |
} | |
private List<MessagePart> executeCommand(String command, Server server, | |
ParameterMap params) throws Throwable { | |
CommandRunner runner = server.getHabitat() | |
.getComponent(CommandRunner.class); | |
ActionReport report = server.getHabitat().getComponent(ActionReport.class); | |
if (params == null) | |
runner.getCommandInvocation(command, report).execute(); | |
else | |
runner.getCommandInvocation(command, report).parameters(params).execute(); | |
if (report.hasFailures()) { | |
report.writeReport(System.out); | |
throw report.getFailureCause(); | |
} | |
return report.getTopMessagePart().getChildren(); | |
} | |
private void assertInArray(List<MessagePart> messageParts, String resource) | |
throws Throwable { | |
boolean found = false; | |
for (MessagePart part : messageParts) { | |
System.out.println(part.getMessage()); | |
} | |
for (MessagePart part : messageParts) { | |
if (part.getMessage().equals(resource)) { | |
found = true; | |
break; | |
} | |
// System.out.println(part.getMessage()); | |
} | |
if (!found) | |
throw new Throwable("\"" + resource + "\" resource not found"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment