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

Classes in ScatteredArchive are processed only once. #24570

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
32 changes: 32 additions & 0 deletions appserver/tests/embedded/scatteredarchive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,47 @@
<groupId>org.glassfish.tests.embedded.basic</groupId>
<artifactId>scatteredarchive</artifactId>
<name>Scattered Archive Test</name>
<packaging>war</packaging>
<build>
<finalName>scatteredarchive</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-test-dependencies</id>
<phase>process-test-classes</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>test</includeScope>
<stripVersion>true</stripVersion>
<outputDirectory>${project.build.directory}/test-dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.main.common</groupId>
<artifactId>common-util</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ScatteredArchiveTestServlet extends HttpServlet {
protected void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws ServletException, IOException {
PrintWriter out = httpServletResponse.getWriter();
out.println("Hi from ScatteredArchiveTestServlet");
out.println("Hi from " + this.getClass().getSimpleName());
out.flush();
out.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<!--

Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.

Copyright (c) 2023 Contributors to the Eclipse Foundation.
Copyright (c) 1997, 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.
Expand All @@ -14,10 +15,13 @@
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->

<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd">
<sun-web-app>
<glassfish-web-app error-url="">
<context-root>/satest</context-root>
</sun-web-app>
<class-loader delegate="true"/>
<jsp-config>
<property name="keepgenerated" value="true">
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
</glassfish-web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.tests.embedded.scatteredarchive;

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.INFO;
import static java.util.stream.Collectors.toList;
import static org.glassfish.tests.embedded.scatteredarchive.contextInitialized.ContextInitializedTestServlet.LABEL_CONTEXT_INITIALIZED_COUNTER;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;

import com.sun.enterprise.util.io.FileUtils;
import org.junit.jupiter.api.Assertions;
import org.glassfish.embeddable.Deployer;
import org.glassfish.embeddable.GlassFish;
Expand All @@ -26,6 +34,7 @@
import org.glassfish.embeddable.archive.ScatteredEnterpriseArchive;
import org.glassfish.embeddable.web.HttpListener;
import org.glassfish.embeddable.web.WebContainer;
import org.glassfish.embeddable.GlassFishException;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
Expand All @@ -37,30 +46,48 @@
import java.net.URLConnection;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.glassfish.tests.embedded.scatteredarchive.contextInitialized.ContextInitializedTestServlet;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

/**
* @author bhavanishankar@dev.java.net
*/

public class ScatteredArchiveTest {

private static final String PROJECT_DIR = System.getProperty("project.directory");
System.Logger logger = System.getLogger(ScatteredArchiveTest.class.getName());

@Test
public void test() throws Exception {
GlassFish glassfish;
GlassFishRuntime gfRuntime;

@BeforeEach
public void startGlassFish() throws GlassFishException {
GlassFishProperties props = new GlassFishProperties();
props.setPort("http-listener", 8080);
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(props);
gfRuntime = GlassFishRuntime.bootstrap();
glassfish = gfRuntime.newGlassFish(props);
glassfish.start();
}

@AfterEach
public void stopGlassFish() throws GlassFishException {
glassfish.dispose();
gfRuntime.shutdown();
}

@Test
public void testDefaults() throws Exception {

Class<ScatteredArchiveTestServlet> servletClass = ScatteredArchiveTestServlet.class;
String testServletName = servletClass.getSimpleName();

// Test Scattered Web Archive
ScatteredArchive sa = new ScatteredArchive("scatteredarchive",
ScatteredArchive.Type.WAR, new File(PROJECT_DIR, "src/main/webapp"));
sa.addClassPath(new File(PROJECT_DIR, "target/classes"));
sa.addClassPath(new File(PROJECT_DIR, "src/main/resources"));
ScatteredArchive sa = createDefaultArchive("scatteredarchive");
URI warURI = sa.toURI();
printContents(warURI);

Expand All @@ -78,10 +105,10 @@ public void test() throws Exception {
webcontainer.addWebListener(listener);

get("http://localhost:9090/satest", "Hi, my name is Bhavani. What's yours?");
get("http://localhost:9090/satest/ScatteredArchiveTestServlet",
"Hi from ScatteredArchiveTestServlet");
get("http://localhost:8080/satest/ScatteredArchiveTestServlet",
"Hi from ScatteredArchiveTestServlet");
get("http://localhost:9090/satest/" + testServletName,
"Hi from " + testServletName);
get("http://localhost:8080/satest/" + testServletName,
"Hi from " + testServletName);

deployer.undeploy(appname);

Expand All @@ -108,42 +135,92 @@ public void test() throws Exception {
Assertions.assertEquals(appname, "sear");

get("http://localhost:9090/satest", "Hi, my name is Bhavani. What's yours?");
get("http://localhost:9090/satest/ScatteredArchiveTestServlet",
"Hi from ScatteredArchiveTestServlet");
get("http://localhost:8080/satest/ScatteredArchiveTestServlet",
"Hi from ScatteredArchiveTestServlet");
get("http://localhost:9090/satest/" + testServletName,
"Hi from " + testServletName);
get("http://localhost:8080/satest/" + testServletName,
"Hi from " + testServletName);

glassfish.dispose();
}

@Test
public void testContextInitialized() throws Exception {
Class<ContextInitializedTestServlet> servletClass = ContextInitializedTestServlet.class;
String ARCHIVE_NAME = servletClass.getSimpleName() + "Archive";

ScatteredArchive sa = createDefaultArchive(ARCHIVE_NAME);
String testClassesSubPath = servletClass.getPackageName().replace('.', File.separatorChar);
String additionalClassPath = "target/" + servletClass.getSimpleName() + "-classes/";

// copy test-specific classes and add them to the archive
FileUtils.copyTree(new File(PROJECT_DIR, "target/test-classes/" + testClassesSubPath),
new File(PROJECT_DIR, additionalClassPath + testClassesSubPath));
sa.addClassPath(new File(PROJECT_DIR, additionalClassPath));

// add some JAR files to the archive
sa.addClassPath(new File(PROJECT_DIR, "target/test-dependencies/hamcrest.jar"));
sa.addClassPath(new File(PROJECT_DIR, "target/test-dependencies/junit-jupiter-engine.jar"));

URI warURI = sa.toURI();
printContents(warURI);

// Deploy archive
Deployer deployer = glassfish.getDeployer();
String appname = deployer.deploy(warURI);
logger.log(INFO, "Deployed [" + appname + "]");
Assertions.assertEquals(appname, ARCHIVE_NAME);

get("http://localhost:8080/satest/" + ContextInitializedTestServlet.class.getSimpleName(),
LABEL_CONTEXT_INITIALIZED_COUNTER, "1");
}

private void get(String urlStr, String result) throws Exception {
URL url = new URL(urlStr);
private ScatteredArchive createDefaultArchive(String ARCHIVE_NAME) throws IOException {
// Test Scattered Web Archive
ScatteredArchive sa = new ScatteredArchive(ARCHIVE_NAME,
ScatteredArchive.Type.WAR, new File(PROJECT_DIR, "src/main/webapp"));
sa.addClassPath(new File(PROJECT_DIR, "target/classes"));
sa.addClassPath(new File(PROJECT_DIR, "src/main/resources"));
return sa;
}

private void get(String urlStr, String containingString) throws Exception {
List<String> inLines = getLinesFromUrl(new URL(urlStr));
MatcherAssert.assertThat("Output from servlet", inLines, hasItem(containsString(containingString)));
logger.log(INFO, "***** SUCCESS **** Found [" + containingString + "] in the response.*****");
}

private void get(String urlStr, String key, String value) throws Exception {
List<String> inLines = getLinesFromUrl(new URL(urlStr));
String result = key + ":" + value;
MatcherAssert.assertThat("Output from servlet", inLines, hasItem(is(result)));
logger.log(INFO, "***** SUCCESS **** Found [" + result + "] in the response.*****");
}

private List<String> getLinesFromUrl(URL url) throws Exception {
URLConnection yc = url.openConnection();
System.out.println("\nURLConnection [" + yc + "] : ");
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String line = null;
boolean found = false;
while ((line = in.readLine()) != null) {
System.out.println(line);
if (line.indexOf(result) != -1) {
found = true;
}
logger.log(DEBUG, "\nURLConnection [" + yc + "] : ");
try (BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()))) {
return in.lines().collect(toList());
}
Assertions.assertTrue(found);
System.out.println("\n***** SUCCESS **** Found [" + result + "] in the response.*****\n");
}

void printContents(URI jarURI) throws IOException {
JarFile jarfile = new JarFile(new File(jarURI));
System.out.println("\n\n[" + jarURI + "] contents : \n");
StringBuilder contents = new StringBuilder();
contents.append("[")
.append(jarURI)
.append("] contents : \n");
Enumeration<JarEntry> entries = jarfile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
System.out.println(entry.getSize() + "\t" + new Date(entry.getTime()) +
"\t" + entry.getName());
contents.append(entry.getSize())
.append("\t")
.append(new Date(entry.getTime()))
.append("\t")
.append(entry.getName())
.append("\n");
}
System.out.println();
logger.log(INFO, contents);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.tests.embedded.scatteredarchive.contextInitialized;

public class ApplicationStatus {

static int contextInitializedCounter = 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.tests.embedded.scatteredarchive.contextInitialized;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "ContextInitializedTestServlet",
urlPatterns = "/ContextInitializedTestServlet")
public class ContextInitializedTestServlet extends HttpServlet {

public static final String LABEL_CONTEXT_INITIALIZED_COUNTER = "Number of times context was initialized";

@Override
protected void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws ServletException, IOException {
try (PrintWriter out = httpServletResponse.getWriter()) {
out.println(LABEL_CONTEXT_INITIALIZED_COUNTER + ":" + ApplicationStatus.contextInitializedCounter);
}
}
}
Loading