Skip to content

Commit 07c0830

Browse files
committed
Code cleanup in embedded runnable tests
- Special matcher - Deleting temporary files - Removed LONG_RUNNING tag from monitorngtest - finishes around 2 seconds. Instead I tried to stabilize the host resolution which seems to make problems on windows (looks like JDK bug?) Signed-off-by: David Matějček <[email protected]>
1 parent c4e94f9 commit 07c0830

File tree

11 files changed

+334
-416
lines changed

11 files changed

+334
-416
lines changed

appserver/tests/application/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@
6161
<dependency>
6262
<groupId>org.jboss.shrinkwrap.resolver</groupId>
6363
<artifactId>shrinkwrap-resolver-impl-maven-archive</artifactId>
64-
<exclusions>
65-
<exclusion>
66-
<groupId>javax.*</groupId>
67-
<artifactId>*</artifactId>
68-
</exclusion>
69-
</exclusions>
7064
</dependency>
7165

7266
<dependency>

appserver/tests/embedded/pom.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,4 @@
9595
</plugins>
9696
</pluginManagement>
9797
</build>
98-
99-
<profiles>
100-
<profile>
101-
<activation>
102-
<property>
103-
<name>glassfish.test.excludedGroups</name>
104-
</property>
105-
</activation>
106-
<properties>
107-
<excludedGroups>${glassfish.test.excludedGroups}</excludedGroups>
108-
</properties>
109-
</profile>
110-
</profiles>
11198
</project>

appserver/tests/embedded/runnable/pom.xml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<artifactId>runnable</artifactId>
3030
<name>Tests for testing runnable GF Embedded</name>
3131
<dependencies>
32+
<dependency>
33+
<groupId>org.hamcrest</groupId>
34+
<artifactId>hamcrest</artifactId>
35+
</dependency>
3236
<dependency>
3337
<groupId>org.junit.jupiter</groupId>
3438
<artifactId>junit-jupiter-engine</artifactId>
@@ -37,12 +41,6 @@
3741
<groupId>org.junit.jupiter</groupId>
3842
<artifactId>junit-jupiter-params</artifactId>
3943
</dependency>
40-
<dependency>
41-
<groupId>org.jboss.shrinkwrap</groupId>
42-
<artifactId>shrinkwrap-depchain</artifactId>
43-
<version>1.2.6</version>
44-
<type>pom</type>
45-
</dependency>
4644
<dependency>
4745
<groupId>org.glassfish.main.extras</groupId>
4846
<artifactId>glassfish-embedded-all</artifactId>
@@ -57,9 +55,8 @@
5755
<version>${project.version}</version>
5856
</dependency>
5957
<dependency>
60-
<groupId>org.glassfish.main</groupId>
61-
<artifactId>glassfish-itest-tools</artifactId>
62-
<version>${project.version}</version>
58+
<groupId>org.jboss.shrinkwrap.resolver</groupId>
59+
<artifactId>shrinkwrap-resolver-impl-maven-archive</artifactId>
6360
<scope>test</scope>
6461
</dependency>
6562
</dependencies>

appserver/tests/embedded/runnable/src/test/java/org/glassfish/tests/embedded/runnable/AddLibraryTest.java

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2024, 2025 Contributors to the Eclipse Foundation.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -15,104 +15,89 @@
1515
*/
1616
package org.glassfish.tests.embedded.runnable;
1717

18-
import java.io.File;
19-
import java.util.Optional;
20-
import java.util.concurrent.TimeUnit;
21-
import java.util.logging.Logger;
18+
import java.lang.System.Logger;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
2221

2322
import org.glassfish.tests.embedded.runnable.TestArgumentProviders.GfEmbeddedJarNameProvider;
2423
import org.glassfish.tests.embedded.runnable.app.App;
2524
import org.glassfish.tests.embedded.runnable.library.MockExecutorService;
2625
import org.jboss.shrinkwrap.api.ShrinkWrap;
27-
import org.jboss.shrinkwrap.api.UnknownExtensionTypeException;
28-
import org.jboss.shrinkwrap.api.exporter.ArchiveExportException;
29-
import org.jboss.shrinkwrap.api.exporter.FileExistsException;
3026
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
3127
import org.jboss.shrinkwrap.api.spec.JavaArchive;
3228
import org.jboss.shrinkwrap.api.spec.WebArchive;
29+
import org.junit.jupiter.api.AfterEach;
30+
import org.junit.jupiter.api.io.TempDir;
3331
import org.junit.jupiter.params.ParameterizedTest;
3432
import org.junit.jupiter.params.provider.ArgumentsSource;
3533

36-
import static java.lang.System.err;
37-
import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.outputToStreamOfLines;
34+
import static java.lang.System.Logger.Level.INFO;
35+
import static java.util.concurrent.TimeUnit.SECONDS;
3836
import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.runGlassFishEmbedded;
39-
import static org.glassfish.tests.embedded.runnable.ShrinkwrapUtils.logArchiveContent;
37+
import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.stderrContains;
38+
import static org.hamcrest.MatcherAssert.assertThat;
39+
import static org.hamcrest.Matchers.not;
4040
import static org.junit.jupiter.api.Assertions.assertTrue;
4141

4242
/**
4343
* @author Ondro Mihalyi
4444
*/
4545
public class AddLibraryTest {
4646

47-
private static final Logger LOG = Logger.getLogger(AddLibraryTest.class.getName());
47+
private static final Logger LOG = System.getLogger(AddLibraryTest.class.getName());
48+
49+
@TempDir
50+
private Path tmpDir;
51+
private Path jarFile;
52+
private Path warFile;
53+
54+
@AfterEach
55+
void deleteFiles() throws Exception {
56+
if (jarFile != null) {
57+
Files.deleteIfExists(jarFile);
58+
}
59+
if (warFile != null) {
60+
Files.deleteIfExists(warFile);
61+
}
62+
}
4863

4964
@ParameterizedTest
5065
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
5166
void testAddLibraryForApp(String gfEmbeddedJarName) throws Exception {
52-
53-
File jarFile = null;
54-
File warFile = null;
55-
try {
56-
jarFile = testLibraryJavaArchive(jarFile);
57-
warFile = warArchiveThatDependsOnTestLibrary(warFile);
58-
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
59-
"add-library " + jarFile.getAbsolutePath(),
60-
warFile.getAbsolutePath()
61-
);
62-
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
63-
.filter(line -> line.contains("App initialized"))
64-
.findAny().isPresent(),
65-
"A log from deployed application is present");
66-
gfEmbeddedProcess
67-
.waitFor(30, TimeUnit.SECONDS);
68-
} finally {
69-
Optional.ofNullable(jarFile).ifPresent(File::delete);
70-
Optional.ofNullable(warFile).ifPresent(File::delete);
71-
}
67+
jarFile = testLibraryJavaArchive();
68+
warFile = warArchiveThatDependsOnTestLibrary();
69+
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName, "add-library " + jarFile,
70+
warFile.toString());
71+
assertThat(gfEmbeddedProcess.errorReader(), stderrContains("App initialized"));
72+
assertTrue(gfEmbeddedProcess.waitFor(30, SECONDS), "Process finished.");
7273
}
7374

7475
@ParameterizedTest
7576
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
7677
void testAddLibraryForGrizzlyExecutor(String gfEmbeddedJarName) throws Exception {
77-
78-
File jarFile = null;
79-
File warFile = null;
80-
try {
81-
jarFile = testLibraryJavaArchive(jarFile);
82-
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
83-
"add-library " + jarFile.getAbsolutePath(),
84-
"set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.classname=" + MockExecutorService.class.getName()
85-
);
86-
assertTrue(! outputToStreamOfLines(gfEmbeddedProcess)
87-
.peek(err::println)
88-
.filter(line -> line.contains("ClassNotFoundException"))
89-
.findAny().isPresent(),
90-
"ClassNotFoundException should not be thrown for " + MockExecutorService.class.getSimpleName());
91-
gfEmbeddedProcess
92-
.waitFor(30, TimeUnit.SECONDS);
93-
} finally {
94-
Optional.ofNullable(jarFile).ifPresent(File::delete);
95-
Optional.ofNullable(warFile).ifPresent(File::delete);
96-
}
78+
jarFile = testLibraryJavaArchive();
79+
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName, "add-library " + jarFile,
80+
"set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.classname="
81+
+ MockExecutorService.class.getName());
82+
assertThat(gfEmbeddedProcess.errorReader(), not(stderrContains("ClassNotFoundException")));
83+
assertTrue(gfEmbeddedProcess.waitFor(30, SECONDS), "Process finished.");
9784
}
9885

99-
private File testLibraryJavaArchive(File jarFile) throws FileExistsException, ArchiveExportException, UnknownExtensionTypeException, IllegalArgumentException {
86+
private Path testLibraryJavaArchive() throws Exception {
10087
String jarName = "testLibrary.jar";
101-
JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class, jarName)
102-
.addClass(MockExecutorService.class);
103-
jarFile = new File(jarName);
104-
javaArchive.as(ZipExporter.class).exportTo(jarFile);
105-
logArchiveContent(javaArchive, jarName, LOG::info);
88+
JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class, jarName).addClass(MockExecutorService.class);
89+
jarFile = tmpDir.resolve(jarName).toAbsolutePath();
90+
javaArchive.as(ZipExporter.class).exportTo(jarFile.toFile());
91+
LOG.log(INFO, () -> "Java library:\n" + javaArchive.toString(true));
10692
return jarFile;
10793
}
10894

109-
private File warArchiveThatDependsOnTestLibrary(File warFile) throws FileExistsException, ArchiveExportException, IllegalArgumentException {
95+
private Path warArchiveThatDependsOnTestLibrary() throws Exception {
11096
String warName = "testLibraryApp.war";
111-
WebArchive warArchive = ShrinkWrap.create(WebArchive.class, warName)
112-
.addPackage(App.class.getPackage());
113-
warFile = new File(warName);
114-
warArchive.as(ZipExporter.class).exportTo(warFile);
115-
logArchiveContent(warArchive, warName, LOG::info);
97+
WebArchive warArchive = ShrinkWrap.create(WebArchive.class, warName).addPackage(App.class.getPackage());
98+
warFile = tmpDir.resolve(warName).toAbsolutePath();
99+
warArchive.as(ZipExporter.class).exportTo(warFile.toFile());
100+
LOG.log(INFO, () -> "WAR:\n" + warArchive.toString(true));
116101
return warFile;
117102
}
118103

appserver/tests/embedded/runnable/src/test/java/org/glassfish/tests/embedded/runnable/ConfigFileTest.java

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,20 @@
1717

1818
import java.io.IOException;
1919
import java.nio.file.Files;
20-
import java.nio.file.Paths;
21-
import java.nio.file.StandardCopyOption;
22-
import java.util.List;
23-
import java.util.concurrent.TimeUnit;
24-
import java.util.concurrent.atomic.AtomicReference;
25-
import java.util.function.Predicate;
26-
import java.util.stream.Collectors;
20+
import java.nio.file.Path;
2721

2822
import org.glassfish.tests.embedded.runnable.TestArgumentProviders.GfEmbeddedJarNameProvider;
2923
import org.junit.jupiter.api.BeforeEach;
3024
import org.junit.jupiter.api.TestInfo;
25+
import org.junit.jupiter.api.io.TempDir;
3126
import org.junit.jupiter.params.ParameterizedTest;
3227
import org.junit.jupiter.params.provider.ArgumentsSource;
3328

34-
import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.outputToStreamOfLines;
29+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
30+
import static java.util.concurrent.TimeUnit.SECONDS;
3531
import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.runGlassFishEmbedded;
32+
import static org.glassfish.tests.embedded.runnable.GfEmbeddedUtils.stderrContains;
33+
import static org.hamcrest.MatcherAssert.assertThat;
3634
import static org.junit.jupiter.api.Assertions.assertTrue;
3735

3836
/**
@@ -41,84 +39,45 @@
4139
*/
4240
public class ConfigFileTest {
4341

44-
String propertiesFile;
42+
@TempDir
43+
private Path tmpDir;
44+
private Path propertiesFile;
4545

4646
@BeforeEach
4747
void init(TestInfo testInfo) throws IOException {
48-
propertiesFile = "glassfish-" + testInfo.getTestMethod().get().getName() + ".properties";
49-
Files.copy(getClass().getClassLoader().getResourceAsStream(testInfo.getTestClass().get().getSimpleName() + "/" + propertiesFile),
50-
Paths.get(propertiesFile),
51-
StandardCopyOption.REPLACE_EXISTING);
52-
48+
String fileName = "glassfish-" + testInfo.getTestMethod().get().getName() + ".properties";
49+
propertiesFile = tmpDir.resolve(fileName);
50+
Files.copy(getClass().getClassLoader().getResourceAsStream(
51+
testInfo.getTestClass().get().getSimpleName() + "/" + fileName), propertiesFile, REPLACE_EXISTING);
5352
}
5453

5554
@ParameterizedTest
5655
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
5756
void testExecuteCommandsInOrderDefinedInPropertiesFile(String gfEmbeddedJarName) throws Exception {
5857
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
59-
"--properties=" + propertiesFile,
60-
"get server.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size"
58+
"--properties=" + propertiesFile,
59+
"get server.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size"
6160
);
62-
assertTrue(outputToStreamOfLines(gfEmbeddedProcess)
63-
.filter(new TwoLineMatcher(
64-
line -> line.contains("Description: get"),
65-
line -> line.contains("server.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=200")
66-
))
67-
.findAny().isPresent(),
68-
"Message about max-thread-pool-size=200 set is found");
69-
gfEmbeddedProcess
70-
.waitFor(30, TimeUnit.SECONDS);
61+
assertThat(gfEmbeddedProcess.errorReader(),
62+
stderrContains("server.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=200"));
63+
assertTrue(gfEmbeddedProcess.waitFor(30, SECONDS), "Process finished.");
7164
}
7265

7366
@ParameterizedTest
7467
@ArgumentsSource(GfEmbeddedJarNameProvider.class)
7568
void testPropertyNamesVariants(String gfEmbeddedJarName) throws Exception {
7669
Process gfEmbeddedProcess = runGlassFishEmbedded(gfEmbeddedJarName,
77-
"--properties=" + propertiesFile,
78-
"get server.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size",
79-
"get resources.jdbc-connection-pool.DerbyPool.connection-leak-timeout-in-seconds",
80-
"get server.network-config.protocols.protocol.http-listener.http.max-connections",
81-
"get security-configurations.authorization-service.authorizationService.default"
82-
70+
"--properties=" + propertiesFile,
71+
"get server.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size",
72+
"get resources.jdbc-connection-pool.DerbyPool.connection-leak-timeout-in-seconds",
73+
"get server.network-config.protocols.protocol.http-listener.http.max-connections",
74+
"get security-configurations.authorization-service.authorizationService.default"
8375
);
84-
gfEmbeddedProcess
85-
.waitFor(30, TimeUnit.SECONDS);
86-
List<String> appliedPropertyLogs = outputToStreamOfLines(gfEmbeddedProcess)
87-
.filter(new TwoLineMatcher(
88-
line -> line.contains("Description: get"),
89-
line -> true
90-
))
91-
.collect(Collectors.toList());
92-
assertTrue(appliedPropertyLogs
93-
.get(0).contains("server.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=200"));
94-
assertTrue(appliedPropertyLogs
95-
.get(1).contains("resources.jdbc-connection-pool.DerbyPool.connection-leak-timeout-in-seconds=5"));
96-
assertTrue(appliedPropertyLogs
97-
.get(2).contains("server.network-config.protocols.protocol.http-listener.http.max-connections=10000"));
98-
assertTrue(appliedPropertyLogs
99-
.get(3).contains("security-configurations.authorization-service.authorizationService.default=false"));
100-
assertTrue(appliedPropertyLogs.size() == 4,"4 properties should be logged");
101-
}
102-
103-
private class TwoLineMatcher implements Predicate<String> {
104-
105-
AtomicReference<String> previousLine = new AtomicReference<>();
106-
Predicate<String> previousLinePredicate;
107-
Predicate<String> currentLinePredicate;
108-
109-
public TwoLineMatcher(Predicate<String> previousLinePredicate, Predicate<String> currentLinePredicate) {
110-
this.previousLinePredicate = previousLinePredicate;
111-
this.currentLinePredicate = currentLinePredicate;
112-
}
113-
114-
@Override
115-
public boolean test(String line) {
116-
boolean lineMatches = previousLine.get() != null
117-
&& previousLinePredicate.test(previousLine.get())
118-
&& currentLinePredicate.test(line);
119-
previousLine.set(line);
120-
return lineMatches;
121-
}
76+
assertThat(gfEmbeddedProcess.errorReader(),
77+
stderrContains("server.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=200",
78+
"resources.jdbc-connection-pool.DerbyPool.connection-leak-timeout-in-seconds=5",
79+
"server.network-config.protocols.protocol.http-listener.http.max-connections=10000",
80+
"security-configurations.authorization-service.authorizationService.default=false"));
81+
assertTrue(gfEmbeddedProcess.waitFor(30, SECONDS), "Process finished.");
12282
}
123-
12483
}

0 commit comments

Comments
 (0)