Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix embedded web test #24922

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions appserver/tests/embedded/utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,5 @@
<artifactId>internal-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2009, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,13 +17,12 @@

package org.glassfish.tests.embedded.utils;

import org.junit.Assert;
import org.glassfish.internal.embedded.LifecycleException;
import java.io.File;

import org.glassfish.internal.embedded.EmbeddedFileSystem;
import org.glassfish.internal.embedded.LifecycleException;
import org.glassfish.internal.embedded.Server;

import java.io.File;

public class EmbeddedServerUtils {

public static File getServerLocation() {
Expand All @@ -35,50 +35,52 @@ public static File getServerLocation() {
if (f.exists()) {
System.out.println("Using gf at " + f.getAbsolutePath());
} else {
System.out.println("GlassFish not found at " + f.getAbsolutePath());
Assert.assertTrue(f.exists());
if (!f.exists()) {
throw new IllegalStateException("GlassFish not found at " + f.getAbsolutePath());
}
}
return f;
}


public static File getDomainLocation(File serverLocation) {
return getDomainLocation(serverLocation, "domain1");
}

public static File getDomainLocation(File serverLocation, String domainName) {

// find the domain root.
File f = new File(serverLocation,"domains");
public static File getDomainLocation(File serverLocation, String domainName) {
// find the domain root.
File f = new File(serverLocation, "domains");
f = new File(f, domainName);
Assert.assertTrue(f.exists());
if (!f.exists()) {
throw new IllegalStateException("GlassFish domain not found at " + f.getAbsolutePath());
}
return f;
}


public static Server createServer(EmbeddedFileSystem fileSystem) throws Exception {
try {
Server.Builder builder = new Server.Builder("inplanted");
builder.embeddedFileSystem(fileSystem);
return builder.build();
} catch(Exception e) {
e.printStackTrace();
} catch (Exception e) {
if (fileSystem.autoDelete) {
fileSystem.preDestroy();
}
throw e;
}
}


public static void shutdownServer(Server server) throws Exception {
System.out.println("shutdown initiated");
if (server!=null) {
if (server != null) {
try {
server.stop();
} catch (LifecycleException e) {
e.printStackTrace();
throw e;
}
}


}

}
66 changes: 28 additions & 38 deletions appserver/tests/embedded/web/servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -32,57 +33,46 @@
<artifactId>servlet</artifactId>
<packaging>war</packaging>
<name>Simple embedded servlet project</name>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<!--argument>-Xdebug</argument>
<argument>-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009</argument-->
<argument>-classpath</argument>
<classpath/>
<argument>org.glassfish.tests.embedded.web.servlet.ServletMain</argument>
</arguments>
<classpathScope>test</classpathScope>
<includeProjectDependencies>true</includeProjectDependencies>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-shell</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.main.common</groupId>
<artifactId>internal-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.29</version>
<groupId>org.glassfish.main</groupId>
<artifactId>test-utils</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.tests.embedded</groupId>
<artifactId>utils</artifactId>
<version>${project.version}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.70.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package test;
package org.glassfish.tests.embedded.web.servlet;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -19,23 +24,16 @@

import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

@WebServlet(urlPatterns={"/hello"})
public class HelloWorld extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
PrintWriter pw = res.getWriter();
try {
pw.println("Hello World !<br>");
} catch(Exception e) {
e.printStackTrace();
}
pw.println("Hello World !<br>");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2009, 2018 Oracle 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
* 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.tests.embedded.web.servlet;


import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;

import org.glassfish.embeddable.GlassFish;
import org.glassfish.embeddable.GlassFishException;
import org.glassfish.embeddable.GlassFishProperties;
import org.glassfish.embeddable.GlassFishRuntime;
import org.glassfish.embeddable.archive.ScatteredArchive;
import org.glassfish.tests.utils.ServerUtils;


public class Application implements Closeable {

private static final int HTTP_PORT = ServerUtils.getFreePort();

private final GlassFish glassfish;
private final String name;

private Application(final GlassFish glassfish, final String name) {
this.glassfish = glassfish;
this.name = name;
}


public URL getEndpoint() {
try {
return new URI("http://localhost:" + HTTP_PORT + "/" + name + "/hello").toURL();
} catch (MalformedURLException | URISyntaxException e) {
throw new IllegalStateException(e);
}
}


@Override
public void close() throws IOException {
try {
System.out.println("Undeploying");
glassfish.getDeployer().undeploy(name);
System.out.println("Stopping the server !");
glassfish.dispose();
} catch (final GlassFishException e) {
throw new IllegalStateException(e);
}
}


public static Application start() throws IOException, GlassFishException {
GlassFishProperties props = new GlassFishProperties();
props.setPort("http-listener", HTTP_PORT);
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(props);
glassfish.start();

final File classes = Path.of(System.getProperty("basedir")).resolve("target").resolve("classes").toFile();
final ScatteredArchive war = new ScatteredArchive("hello", ScatteredArchive.Type.WAR);
war.addClassPath(classes);
System.out.println("War content: \n" + war);

final String name = glassfish.getDeployer().deploy(war.toURI());
return new Application(glassfish, name);
}
}
Loading