diff --git a/appserver/tests/embedded/utils/pom.xml b/appserver/tests/embedded/utils/pom.xml index 840d25a4a0d..3b4e210b9d1 100644 --- a/appserver/tests/embedded/utils/pom.xml +++ b/appserver/tests/embedded/utils/pom.xml @@ -41,10 +41,5 @@ internal-api ${project.version} - - junit - junit - 4.13.2 - diff --git a/appserver/tests/embedded/utils/src/main/java/org/glassfish/tests/embedded/utils/EmbeddedServerUtils.java b/appserver/tests/embedded/utils/src/main/java/org/glassfish/tests/embedded/utils/EmbeddedServerUtils.java index 5d6cdf390dc..8c336259a17 100644 --- a/appserver/tests/embedded/utils/src/main/java/org/glassfish/tests/embedded/utils/EmbeddedServerUtils.java +++ b/appserver/tests/embedded/utils/src/main/java/org/glassfish/tests/embedded/utils/EmbeddedServerUtils.java @@ -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 @@ -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() { @@ -35,31 +35,36 @@ 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(); } @@ -67,18 +72,15 @@ public static Server createServer(EmbeddedFileSystem fileSystem) throws Exceptio } } + 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; } } - - } - } diff --git a/appserver/tests/embedded/web/servlet/pom.xml b/appserver/tests/embedded/web/servlet/pom.xml index 1c8b5ff744b..9c256ddfe01 100644 --- a/appserver/tests/embedded/web/servlet/pom.xml +++ b/appserver/tests/embedded/web/servlet/pom.xml @@ -18,8 +18,9 @@ --> - + 4.0.0 @@ -32,57 +33,46 @@ servlet war Simple embedded servlet project - - ${project.artifactId} - - - org.codehaus.mojo - exec-maven-plugin - 1.1.1 - - - test - - exec - - - - - java - - - -classpath - - org.glassfish.tests.embedded.web.servlet.ServletMain - - test - true - - - - + org.glassfish.main.extras glassfish-embedded-web test + + org.glassfish.main.extras + glassfish-embedded-shell + ${project.version} + + + org.glassfish.main.common + internal-api + ${project.version} + jakarta.servlet jakarta.servlet-api provided - net.sourceforge.htmlunit - htmlunit - 2.29 + org.glassfish.main + test-utils + ${project.version} test - org.glassfish.tests.embedded - utils - ${project.version} + org.junit.jupiter + junit-jupiter-engine + + + org.hamcrest + hamcrest + + + net.sourceforge.htmlunit + htmlunit + 2.70.0 test diff --git a/appserver/tests/embedded/web/servlet/src/main/java/test/HelloWorld.java b/appserver/tests/embedded/web/servlet/src/main/java/org/glassfish/tests/embedded/web/servlet/HelloWorld.java similarity index 72% rename from appserver/tests/embedded/web/servlet/src/main/java/test/HelloWorld.java rename to appserver/tests/embedded/web/servlet/src/main/java/org/glassfish/tests/embedded/web/servlet/HelloWorld.java index bb8f7d1281a..c48b3bdec53 100644 --- a/appserver/tests/embedded/web/servlet/src/main/java/test/HelloWorld.java +++ b/appserver/tests/embedded/web/servlet/src/main/java/org/glassfish/tests/embedded/web/servlet/HelloWorld.java @@ -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 @@ -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 !
"); - } catch(Exception e) { - e.printStackTrace(); - } + pw.println("Hello World !
"); } } diff --git a/appserver/tests/embedded/web/servlet/src/test/java/org/glassfish/tests/embedded/web/servlet/Application.java b/appserver/tests/embedded/web/servlet/src/test/java/org/glassfish/tests/embedded/web/servlet/Application.java new file mode 100644 index 00000000000..85f57dbc505 --- /dev/null +++ b/appserver/tests/embedded/web/servlet/src/test/java/org/glassfish/tests/embedded/web/servlet/Application.java @@ -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); + } +} diff --git a/appserver/tests/embedded/web/servlet/src/test/java/org/glassfish/tests/embedded/web/servlet/ServletMain.java b/appserver/tests/embedded/web/servlet/src/test/java/org/glassfish/tests/embedded/web/servlet/ServletMain.java deleted file mode 100644 index 3bfff8e9e31..00000000000 --- a/appserver/tests/embedded/web/servlet/src/test/java/org/glassfish/tests/embedded/web/servlet/ServletMain.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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 org.junit.*; -import org.junit.Assert; -import org.glassfish.internal.embedded.*; -import org.glassfish.api.deployment.*; - -import java.io.*; -import java.util.*; - -import com.gargoylesoftware.htmlunit.*; - -/** - * Created by IntelliJ IDEA. - * User: dochez - * Date: Nov 4, 2009 - * Time: 1:44:28 PM - * To change this template use File | Settings | File Templates. - */ -public class ServletMain { - - public static void main(String[] args) { - ServletMain test = new ServletMain(); - System.setProperty("basedir", System.getProperty("user.dir")); - try { - test.test(); - } catch (Exception e) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - } - } - - @Test - public void test() throws Exception { - - Server server = new Server.Builder("web").build(); - try { - File f = new File(System.getProperty("basedir")); - f = new File(f, "target"); - f = new File(f, "classes"); - ScatteredArchive.Builder builder = new ScatteredArchive.Builder("hello", f); - builder.addClassPath(f.toURI().toURL()); - builder.resources(f); - ScatteredArchive war = builder.buildWar(); - System.out.println("War content"); - Enumeration contents = war.entries(); - while(contents.hasMoreElements()) { - System.out.println(contents.nextElement()); - } - Port port = server.createPort(8882); - - server.addContainer(server.createConfig(ContainerBuilder.Type.web)); - DeployCommandParameters dp = new DeployCommandParameters(f); - String appName = server.getDeployer().deploy(war, dp); - - try (WebClient webClient = new WebClient()) { - Page page = webClient.getPage("http://localhost:8882/classes/hello"); - System.out.println("Got response " + page.getWebResponse().getContentAsString()); - Assert.assertTrue("Servlet returne wrong content", page.getWebResponse().getContentAsString().startsWith("Hello World")); - String hostName = System.getProperty("com.sun.aas.hostName"); - assert hostName!=null; - page = webClient.getPage("http://"+hostName+":8882/classes/hello"); - System.out.println("Got response " + page.getWebResponse().getContentAsString()); - Assert.assertTrue("Servlet returned wrong content", page.getWebResponse().getContentAsString().startsWith("Hello World")); - } finally { - System.out.println("Undeploying"); - server.getDeployer().undeploy(appName, null); - port.close(); - } - - } finally { - System.out.println("Stopping the server !"); - try { - server.stop(); - } catch (LifecycleException e) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - } - } - } -} diff --git a/appserver/tests/embedded/web/servlet/src/test/java/org/glassfish/tests/embedded/web/servlet/WebClientITest.java b/appserver/tests/embedded/web/servlet/src/test/java/org/glassfish/tests/embedded/web/servlet/WebClientITest.java new file mode 100644 index 00000000000..4ecb797f480 --- /dev/null +++ b/appserver/tests/embedded/web/servlet/src/test/java/org/glassfish/tests/embedded/web/servlet/WebClientITest.java @@ -0,0 +1,44 @@ +/* + * 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 com.gargoylesoftware.htmlunit.Page; +import com.gargoylesoftware.htmlunit.WebClient; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.startsWith; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class WebClientITest { + + @Test + public void test() throws Exception { + try (Application app = Application.start(); WebClient webClient = new WebClient()) { + Page page = webClient.getPage(app.getEndpoint()); + System.out.println("Got response " + page.getWebResponse().getContentAsString()); + assertThat(page.getWebResponse().getContentAsString(), startsWith("Hello World")); + String hostName = System.getProperty("com.sun.aas.hostName"); + assertNotNull(hostName); + page = webClient.getPage(app.getEndpoint().toExternalForm().replaceFirst("localhost", hostName)); + System.out.println("Got response " + page.getWebResponse().getContentAsString()); + assertThat(page.getWebResponse().getContentAsString(), startsWith("Hello World")); + } + } +} diff --git a/nucleus/common/internal-api/src/main/java/org/glassfish/internal/embedded/Server.java b/nucleus/common/internal-api/src/main/java/org/glassfish/internal/embedded/Server.java index 5c9636c32ce..1db6af68fa7 100644 --- a/nucleus/common/internal-api/src/main/java/org/glassfish/internal/embedded/Server.java +++ b/nucleus/common/internal-api/src/main/java/org/glassfish/internal/embedded/Server.java @@ -1,4 +1,5 @@ /* + * 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 @@ -16,19 +17,28 @@ package org.glassfish.internal.embedded; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; + import org.glassfish.api.container.Sniffer; -import org.glassfish.embeddable.*; +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.hk2.api.ServiceHandle; import org.glassfish.hk2.api.ServiceLocator; import org.glassfish.hk2.utilities.ServiceLocatorUtilities; import org.jvnet.hk2.annotations.Contract; -import java.io.File; -import java.io.IOException; -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; - /** @@ -159,12 +169,12 @@ private Container(EmbeddedContainer container) { } - private final static Map servers = new HashMap(); + private final static Map servers = new HashMap<>(); private final String serverName; private final ServiceHandle fileSystem; private final ServiceLocator habitat; - private final List containers = new ArrayList(); + private final List containers = new ArrayList<>(); private final GlassFish glassfish; private static GlassFishRuntime glassfishRuntime; @@ -241,7 +251,7 @@ private Server(Builder builder, Properties properties) { * @return list of the instanciated embedded instances. */ public static List getServerNames() { - List names = new ArrayList(); + List names = new ArrayList<>(); names.addAll(servers.keySet()); return names; } @@ -307,9 +317,10 @@ public synchronized void addContainer(final ContainerBuilder.Type type) { containers.add(new Container(new EmbeddedContainer() { - final List delegates = new ArrayList(); - final ArrayList sniffers = new ArrayList(); + final List delegates = new ArrayList<>(); + final ArrayList sniffers = new ArrayList<>(); + @Override public List getSniffers() { synchronized(sniffers) { if (sniffers.isEmpty()) { @@ -330,6 +341,7 @@ public List getSniffers() { return sniffers; } + @Override public void bind(Port port, String protocol) { for (Container delegate : delegates) { delegate.container.bind(port, protocol); @@ -343,8 +355,9 @@ private Container getContainerFor(final ContainerBuilder.Type type) { } else { return new Container(new EmbeddedContainer() { + @Override public List getSniffers() { - List sniffers = new ArrayList(); + List sniffers = new ArrayList<>(); Sniffer s = habitat.getService(Sniffer.class, type.toString()); if (s!=null) { sniffers.add(s); @@ -352,14 +365,17 @@ public List getSniffers() { return sniffers; } + @Override public void bind(Port port, String protocol) { } + @Override public void start() throws LifecycleException { } + @Override public void stop() throws LifecycleException { } @@ -368,6 +384,7 @@ public void stop() throws LifecycleException { } } + @Override public void start() throws LifecycleException { for (Container c : delegates) { if (!c.started) { @@ -377,6 +394,7 @@ public void start() throws LifecycleException { } } + @Override public void stop() throws LifecycleException { for (Container c : delegates) { if (c.started) { @@ -402,14 +420,14 @@ public void stop() throws LifecycleException { * creating the container from that configuration and finally adding the * container instance to the list of managed containers * - * @param info the configuration for the container + * @param info the configuration for the container. Must not be null. * @param type of the container * @return instance of the container * @throws IllegalStateException if the container is already started. */ public synchronized T addContainer(ContainerBuilder info) { T container = info.create(this); - if (container!=null && containers.add(new Container(container))) { + if (container != null && containers.add(new Container(container))) { return container; } return null; @@ -423,7 +441,7 @@ public synchronized T addContainer(ContainerBuilde * @return the containers list */ public Collection getContainers() { - ArrayList copy = new ArrayList(); + ArrayList copy = new ArrayList<>(); for (Container c : containers) { copy.add(c.container); }