|
1 | 1 | /* |
2 | | - * Copyright (c) 2024 Contributors to the Eclipse Foundation. |
| 2 | + * Copyright (c) 2024, 2025 Contributors to the Eclipse Foundation. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials are made available under the |
5 | 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
|
15 | 15 | */ |
16 | 16 | package org.glassfish.tests.embedded.runnable; |
17 | 17 |
|
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; |
22 | 21 |
|
23 | 22 | import org.glassfish.tests.embedded.runnable.TestArgumentProviders.GfEmbeddedJarNameProvider; |
24 | 23 | import org.glassfish.tests.embedded.runnable.app.App; |
25 | 24 | import org.glassfish.tests.embedded.runnable.library.MockExecutorService; |
26 | 25 | 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; |
30 | 26 | import org.jboss.shrinkwrap.api.exporter.ZipExporter; |
31 | 27 | import org.jboss.shrinkwrap.api.spec.JavaArchive; |
32 | 28 | import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 29 | +import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.api.io.TempDir; |
33 | 31 | import org.junit.jupiter.params.ParameterizedTest; |
34 | 32 | import org.junit.jupiter.params.provider.ArgumentsSource; |
35 | 33 |
|
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; |
38 | 36 | 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; |
40 | 40 | import static org.junit.jupiter.api.Assertions.assertTrue; |
41 | 41 |
|
42 | 42 | /** |
43 | 43 | * @author Ondro Mihalyi |
44 | 44 | */ |
45 | 45 | public class AddLibraryTest { |
46 | 46 |
|
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 | + } |
48 | 63 |
|
49 | 64 | @ParameterizedTest |
50 | 65 | @ArgumentsSource(GfEmbeddedJarNameProvider.class) |
51 | 66 | 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."); |
72 | 73 | } |
73 | 74 |
|
74 | 75 | @ParameterizedTest |
75 | 76 | @ArgumentsSource(GfEmbeddedJarNameProvider.class) |
76 | 77 | 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."); |
97 | 84 | } |
98 | 85 |
|
99 | | - private File testLibraryJavaArchive(File jarFile) throws FileExistsException, ArchiveExportException, UnknownExtensionTypeException, IllegalArgumentException { |
| 86 | + private Path testLibraryJavaArchive() throws Exception { |
100 | 87 | 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)); |
106 | 92 | return jarFile; |
107 | 93 | } |
108 | 94 |
|
109 | | - private File warArchiveThatDependsOnTestLibrary(File warFile) throws FileExistsException, ArchiveExportException, IllegalArgumentException { |
| 95 | + private Path warArchiveThatDependsOnTestLibrary() throws Exception { |
110 | 96 | 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)); |
116 | 101 | return warFile; |
117 | 102 | } |
118 | 103 |
|
|
0 commit comments