Skip to content

Commit

Permalink
Add embedded deployment tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Aug 30, 2023
1 parent c6bc621 commit f1e06d5
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 3 deletions.
18 changes: 18 additions & 0 deletions appserver/extras/embedded/tests/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2023 Contributors to the Eclipse Foundation.
Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -31,6 +32,11 @@
<name>GlassFish Embedded Modules - Tests</name>

<dependencies>
<dependency>
<groupId>jakarta.ejb</groupId>
<artifactId>jakarta.ejb-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
Expand All @@ -48,4 +54,16 @@
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<reuseForks>false</reuseForks>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.main.extras.embedded.test.app.ejb;

import jakarta.ejb.Stateless;

@Stateless
public class RemoteBean implements RemoteInterface {

@Override
public String getMessage() {
// Returns Java version
return Runtime.version().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.main.extras.embedded.test.app.ejb;

import jakarta.ejb.Remote;

@Remote
public interface RemoteInterface {

String getMessage();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Eclipse Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -14,10 +14,11 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.main.extras.embedded.all;
package org.glassfish.main.extras.embedded.test.all;

import java.io.File;
import java.net.URL;

import org.glassfish.embeddable.BootstrapProperties;
import org.glassfish.embeddable.Deployer;
import org.glassfish.embeddable.GlassFish;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.main.extras.embedded.test.all.deployment;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.glassfish.embeddable.BootstrapProperties;
import org.glassfish.embeddable.GlassFish;
import org.glassfish.embeddable.GlassFishException;
import org.glassfish.embeddable.GlassFishProperties;
import org.glassfish.embeddable.GlassFishRuntime;
import org.glassfish.main.extras.embedded.test.app.ejb.RemoteInterface;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static java.lang.String.format;
import static org.glassfish.tests.utils.ServerUtils.getFreePort;
import static org.glassfish.tests.utils.ServerUtils.runCommand;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class RemoteDeploymentTestBase {

private static final String LOOKUP_STRING =
"java:global/%s/RemoteBean!org.glassfish.main.extras.embedded.test.app.ejb.RemoteInterface";

protected GlassFish glassfish;

@BeforeEach
public void startup() throws GlassFishException {
GlassFishRuntime glassfishRuntime = GlassFishRuntime.bootstrap(new BootstrapProperties());
glassfish = glassfishRuntime.newGlassFish(new GlassFishProperties());
assertThat(glassfish.getStatus(), equalTo(GlassFish.Status.INIT));

glassfish.start();
while (glassfish.getStatus() == GlassFish.Status.STARTING) {
Thread.yield();
}
assertThat(glassfish.getStatus(), equalTo(GlassFish.Status.STARTED));

runCommand(
glassfish,
"set",
"configs.config.server-config.iiop-service.iiop-listener.orb-listener-1.port=" + getFreePort());
runCommand(
glassfish,
"set",
"configs.config.server-config.iiop-service.iiop-listener.SSL.port=" + getFreePort());
runCommand(
glassfish,
"set",
"configs.config.server-config.iiop-service.iiop-listener.SSL_MUTUALAUTH.port=" + getFreePort());
}

@AfterEach
public void shutdown() throws GlassFishException {
glassfish.stop();
while (glassfish.getStatus() == GlassFish.Status.STOPPING) {
Thread.yield();
}
assertThat(glassfish.getStatus(), equalTo(GlassFish.Status.STOPPED));

glassfish.dispose();
assertThat(glassfish.getStatus(), equalTo(GlassFish.Status.DISPOSED));
}

protected File createFileFor(Archive<?> archive, String appName) throws IOException {
File tempDir = Files.createTempDirectory(appName).toFile();
File appFile = new File(tempDir, appName + extensionFor(archive));
archive.as(ZipExporter.class).exportTo(appFile, true);
tempDir.deleteOnExit();
return appFile;
}

private String extensionFor(Archive<?> archive) {
String extension = ".jar";
if (archive instanceof WebArchive) {
extension = ".war";
} else if (archive instanceof EnterpriseArchive) {
extension = ".ear";
}
return extension;
}

protected String getResult(final String appName) throws NamingException {
InitialContext initialContext = new InitialContext();
RemoteInterface remoteBean = (RemoteInterface) initialContext.lookup(getLookupString(appName));
return remoteBean.getMessage();
}

protected String getResult(final String appName, final String moduleName) throws NamingException {
InitialContext initialContext = new InitialContext();
RemoteInterface remoteBean = (RemoteInterface) initialContext.lookup(getLookupString(appName, moduleName));
return remoteBean.getMessage();
}

private String getLookupString(final String appName) {
return format(LOOKUP_STRING, appName);
}

private String getLookupString(final String appName, final String moduleName) {
return format(LOOKUP_STRING, appName + "/" + moduleName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.main.extras.embedded.test.all.deployment.ear;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import javax.naming.NamingException;

import org.glassfish.embeddable.Deployer;
import org.glassfish.embeddable.GlassFishException;
import org.glassfish.main.extras.embedded.test.all.deployment.RemoteDeploymentTestBase;
import org.glassfish.main.extras.embedded.test.app.ejb.RemoteBean;
import org.glassfish.main.extras.embedded.test.app.ejb.RemoteInterface;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.Test;

import static java.lang.System.Logger.Level.INFO;
import static java.lang.System.Logger.Level.WARNING;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class RemoteEarDeploymentTest extends RemoteDeploymentTestBase {

private static final System.Logger LOG = System.getLogger(RemoteEarDeploymentTest.class.getName());

private static final String APP_NAME = "RemoteEar";

private static final String WAR_MODULE_NAME = "RemoteWar";

private static final String WAR_FILE_NAME = WAR_MODULE_NAME + ".war";

private static final String LIB_FILE_NAME = "RemoteLib.jar";

@Test
public void testDeployAndUndeployApplication() throws GlassFishException, IOException, NamingException {
Deployer deployer = glassfish.getDeployer();
File ejbFile = createDeployment();
try {
assertThat(deployer.deploy(ejbFile), equalTo(APP_NAME));
assertThat(getResult(APP_NAME, WAR_MODULE_NAME), equalTo(Runtime.version().toString()));
} finally {
try {
Files.deleteIfExists(ejbFile.toPath());
} catch (IOException e) {
LOG.log(WARNING, "An error occurred while remove temp file " + ejbFile.getAbsolutePath(), e);
}
deployer.undeploy(APP_NAME);
}
}

private File createDeployment() throws IOException {
JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class, LIB_FILE_NAME)
.addClass(RemoteInterface.class);

WebArchive webArchive = ShrinkWrap.create(WebArchive.class, WAR_FILE_NAME)
.addClass(RemoteBean.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

EnterpriseArchive enterpriseArchive = ShrinkWrap.create(EnterpriseArchive.class)
.addAsLibrary(javaArchive)
.addAsModule(webArchive);

LOG.log(INFO, enterpriseArchive.toString(true));

return createFileFor(enterpriseArchive, APP_NAME);
}
}
Loading

0 comments on commit f1e06d5

Please sign in to comment.