From e0a5c642e0ad7879bf5f8dd2717de605a519f5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mat=C4=9Bj=C4=8Dek?= Date: Sun, 6 Oct 2024 13:56:51 +0200 Subject: [PATCH 1/2] Tests - terse can be a feature of Asadmin and can be default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - because in most cases we process results programatically, but sometimes we parse human readable output too. Signed-off-by: David Matějček --- .../itest/tools/GlassFishTestEnvironment.java | 13 ++++++++++-- .../main/itest/tools/asadmin/Asadmin.java | 21 +++++++++++++++++-- .../main/admin/test/AsadminVersionITest.java | 4 ++-- .../main/admin/test/ClusterITest.java | 2 +- .../main/admin/test/OSGiCommandsITest.java | 4 ++-- .../test/progress/DetachAttachITest.java | 6 +++--- .../admin/test/progress/JobTestExtension.java | 2 +- .../progress/ProgressStatusBasicITest.java | 16 ++++++-------- .../progress/ProgressStatusComplexITest.java | 2 +- .../progress/ProgressStatusFailITest.java | 4 ++-- .../progress/ProgressStatusSpecialITest.java | 4 ++-- .../defaultp2r/DefaultP2RAuthTest.java | 5 +++-- 12 files changed, 53 insertions(+), 30 deletions(-) diff --git a/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/GlassFishTestEnvironment.java b/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/GlassFishTestEnvironment.java index 211e40acf98..50881910a5c 100644 --- a/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/GlassFishTestEnvironment.java +++ b/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/GlassFishTestEnvironment.java @@ -84,7 +84,7 @@ public class GlassFishTestEnvironment { // AS_START_TIMEOUT for the detection that "the server is running!" // START_DOMAIN_TIMEOUT for us waiting for the end of the asadmin start-domain process. asadmin.withEnv("AS_START_TIMEOUT", Integer.toString(ASADMIN_START_DOMAIN_TIMEOUT - 5000)); - } + } // This is the absolutely first start - if it fails, all other starts will fail too. // Note: --suspend implicitly enables --debug assertThat(asadmin.exec(ASADMIN_START_DOMAIN_TIMEOUT, "start-domain", @@ -96,10 +96,18 @@ public class GlassFishTestEnvironment { * @return {@link Asadmin} command api for tests. */ public static Asadmin getAsadmin() { - return new Asadmin(ASADMIN, ADMIN_USER, PASSWORD_FILE); + return getAsadmin(true); } + /** + * @param terse true means suitable and minimized for easy parsing. + * @return {@link Asadmin} command api for tests. + */ + public static Asadmin getAsadmin(boolean terse) { + return new Asadmin(ASADMIN, ADMIN_USER, PASSWORD_FILE, terse); + } + /** * @return {@link Asadmin} command api for tests. */ @@ -107,6 +115,7 @@ public static StartServ getStartServ() { return new StartServ(STARTSERV); } + /** * @return {@link Asadmin} command api for tests. */ diff --git a/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/Asadmin.java b/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/Asadmin.java index dca148adbcb..d28ea0c8f33 100644 --- a/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/Asadmin.java +++ b/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/Asadmin.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023 Eclipse Foundation and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024 Eclipse Foundation 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 @@ -57,6 +57,7 @@ public class Asadmin { private final File asadmin; private final String adminUser; private final File adminPasswordFile; + private final boolean terse; private final Map environment = new HashMap<>(); @@ -68,9 +69,23 @@ public class Asadmin { * @param adminPasswordFile - a file containing admin's password set as AS_ADMIN_PASSWORD=... */ public Asadmin(final File asadmin, final String adminUser, final File adminPasswordFile) { + this(asadmin, adminUser, adminPasswordFile, false); + } + + + /** + * Creates a stateless instance of the tool. + * + * @param asadmin - executable file + * @param adminUser - username authorized to use the domain + * @param adminPasswordFile - a file containing admin's password set as AS_ADMIN_PASSWORD=... + * @param terse - to produce output, minimized and suitable for parsing. + */ + public Asadmin(final File asadmin, final String adminUser, final File adminPasswordFile, final boolean terse) { this.asadmin = asadmin; this.adminUser = adminUser; this.adminPasswordFile = adminPasswordFile; + this.terse = terse; } @@ -167,8 +182,10 @@ private AsadminResult exec(final int timeout, final boolean detachedAndTerse, fi command.add("--passwordfile"); command.add(adminPasswordFile.getAbsolutePath()); if (detachedAndTerse) { - command.add("--terse"); + command.add("--terse=true"); command.add("--detach"); + } else { + command.add("--terse=" + terse); } command.addAll(parameters); diff --git a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/AsadminVersionITest.java b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/AsadminVersionITest.java index 2b5d1e87487..206a33feb2b 100644 --- a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/AsadminVersionITest.java +++ b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/AsadminVersionITest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Eclipse Foundation and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2024 Eclipse Foundation 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 @@ -38,7 +38,7 @@ @TestMethodOrder(OrderAnnotation.class) public class AsadminVersionITest { - private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); + private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(false); @AfterAll public static void startDomainAgain() { diff --git a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/ClusterITest.java b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/ClusterITest.java index 2b52632f027..05afbd82f74 100644 --- a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/ClusterITest.java +++ b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/ClusterITest.java @@ -59,7 +59,7 @@ public class ClusterITest { private static final String INSTANCE_NAME_2 = "eein2"; private static final String URL1 = "http://localhost:" + PORT1; private static final String URL2 = "http://localhost:" + PORT2; - private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); + private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(false); private static final AtomicBoolean INSTANCES_REACHABLE = new AtomicBoolean(); private static final AtomicBoolean APP_DEPLOYED = new AtomicBoolean(); diff --git a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/OSGiCommandsITest.java b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/OSGiCommandsITest.java index 10c2a22e9d4..a774acf7ff7 100644 --- a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/OSGiCommandsITest.java +++ b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/OSGiCommandsITest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation * Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the @@ -47,7 +47,7 @@ */ public class OSGiCommandsITest { - private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); + private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(true); @BeforeAll public static void waitOsgiReady() throws Exception { diff --git a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/DetachAttachITest.java b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/DetachAttachITest.java index 9e966644552..b6506a85fb6 100644 --- a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/DetachAttachITest.java +++ b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/DetachAttachITest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation * Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the @@ -52,7 +52,7 @@ @ExtendWith(JobTestExtension.class) public class DetachAttachITest { private static final Logger LOG = Logger.getLogger(DetachAttachITest.class.getName()); - private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); + private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(false); @Test @@ -69,7 +69,7 @@ public void uptimePeriodically() throws Exception { } Thread.sleep(1000L); { - AsadminResult result = ASADMIN.exec("--terse", "attach", id); + AsadminResult result = GlassFishTestEnvironment.getAsadmin(true).exec("attach", id); assertThat(result, asadminOK()); assertTrue(result.getStdOut().contains("uptime")); } diff --git a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/JobTestExtension.java b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/JobTestExtension.java index dcb2af9178e..b7333bdc12e 100644 --- a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/JobTestExtension.java +++ b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/JobTestExtension.java @@ -41,7 +41,7 @@ public class JobTestExtension implements BeforeAllCallback, AfterAllCallback { private static final String ORIG_INITIAL_DELAY = "origInitialDelay"; private static final String ORIG_RETENTION_PERIOD = "origRetentionPeriod"; - private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); + private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(false); @Override public void beforeAll(ExtensionContext context) throws Exception { diff --git a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusBasicITest.java b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusBasicITest.java index e2d2e99e15a..35e1fcb0a67 100644 --- a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusBasicITest.java +++ b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusBasicITest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation * Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the @@ -19,13 +19,12 @@ import java.util.List; -import org.glassfish.main.itest.tools.GlassFishTestEnvironment; -import org.glassfish.main.itest.tools.asadmin.Asadmin; import org.glassfish.main.itest.tools.asadmin.AsadminResult; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import static org.glassfish.main.admin.test.progress.ProgressMessage.isIncreasing; +import static org.glassfish.main.itest.tools.GlassFishTestEnvironment.getAsadmin; import static org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher.asadminOK; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasSize; @@ -39,12 +38,9 @@ @ExtendWith(JobTestExtension.class) public class ProgressStatusBasicITest { - private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); - - @Test public void simple() { - AsadminResult result = ASADMIN.exec("progress-simple"); + AsadminResult result = getAsadmin(false).exec("progress-simple"); assertThat(result, asadminOK()); List prgs = ProgressMessage.grepProgressMessages(result.getStdOut()); assertThat(prgs, isIncreasing()); @@ -57,7 +53,7 @@ public void simple() { @Test public void simpleNoTotal() { - AsadminResult result = ASADMIN.exec("progress-simple", "--nototalsteps"); + AsadminResult result = getAsadmin(false).exec("progress-simple", "--nototalsteps"); assertThat(result, asadminOK()); List prgs = ProgressMessage.grepProgressMessages(result.getStdOut()); assertThat(prgs, isIncreasing()); @@ -73,7 +69,7 @@ public void simpleNoTotal() { @Test public void simpleSpecInAnnotation() { - AsadminResult result = ASADMIN.exec("progress-full-annotated"); + AsadminResult result = getAsadmin(false).exec("progress-full-annotated"); assertThat(result, asadminOK()); List prgs = ProgressMessage.grepProgressMessages(result.getStdOut()); assertThat(prgs, hasSize(12)); @@ -87,7 +83,7 @@ public void simpleSpecInAnnotation() { @Test public void simpleTerse() { - AsadminResult result = ASADMIN.exec("--terse", "progress-simple"); + AsadminResult result = getAsadmin(true).exec("progress-simple"); assertThat(result, asadminOK()); List prgs = ProgressMessage.grepProgressMessages(result.getStdOut()); assertThat(prgs, hasSize(0)); diff --git a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusComplexITest.java b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusComplexITest.java index 75be23615ea..942678654b9 100644 --- a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusComplexITest.java +++ b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusComplexITest.java @@ -44,7 +44,7 @@ @ExtendWith(JobTestExtension.class) public class ProgressStatusComplexITest { - private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); + private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(false); @Test diff --git a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusFailITest.java b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusFailITest.java index 2ac0d5a6fe4..4d0b0d3d9f6 100644 --- a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusFailITest.java +++ b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusFailITest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation * Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the @@ -37,7 +37,7 @@ @ExtendWith(JobTestExtension.class) public class ProgressStatusFailITest { - private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); + private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(false); @Test public void failDuringExecution() { diff --git a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusSpecialITest.java b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusSpecialITest.java index fb213bc78c7..ac932f72a11 100644 --- a/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusSpecialITest.java +++ b/appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/progress/ProgressStatusSpecialITest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation * Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the @@ -40,7 +40,7 @@ @ExtendWith(JobTestExtension.class) public class ProgressStatusSpecialITest { - private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); + private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(false); @Test diff --git a/appserver/tests/application/src/test/java/org/glassfish/main/test/app/security/defaultp2r/DefaultP2RAuthTest.java b/appserver/tests/application/src/test/java/org/glassfish/main/test/app/security/defaultp2r/DefaultP2RAuthTest.java index 85c63d5095f..52c2773f00c 100644 --- a/appserver/tests/application/src/test/java/org/glassfish/main/test/app/security/defaultp2r/DefaultP2RAuthTest.java +++ b/appserver/tests/application/src/test/java/org/glassfish/main/test/app/security/defaultp2r/DefaultP2RAuthTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Eclipse Foundation and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2024 Eclipse Foundation 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 @@ -65,7 +65,8 @@ public class DefaultP2RAuthTest { @BeforeAll public static void prepareDeployment() { - AsadminResult result = ASADMIN.exec("--terse", "get", SERVER_CFG_PROPERTY); + AsadminResult result = ASADMIN.exec("get", SERVER_CFG_PROPERTY); + assertThat(result, asadminOK()); orignalCfgValue = result.getStdOut().replaceFirst(SERVER_CFG_PROPERTY + "=", "").trim(); createFileUser(FILE_REALM_NAME, USER_NAME, USER_PASSWORD, "mygroup"); From 8521a2abe41363eddc4c859e786a96ac973e7505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mat=C4=9Bj=C4=8Dek?= Date: Sun, 6 Oct 2024 20:18:40 +0200 Subject: [PATCH 2/2] Improved backup for DerbyPool in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: David Matějček --- .../itest/tools/GlassFishTestEnvironment.java | 13 +++ .../tools/asadmin/DomainPropertiesBackup.java | 82 +++++++++++++++++++ .../itest/tools/asadmin/DomainSettings.java | 75 ----------------- .../app/connpool/SQLTraceListenerTest.java | 53 +++++------- .../JtaDataSourceResourceRefTest.java | 10 +-- 5 files changed, 120 insertions(+), 113 deletions(-) create mode 100644 appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/DomainPropertiesBackup.java delete mode 100644 appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/DomainSettings.java diff --git a/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/GlassFishTestEnvironment.java b/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/GlassFishTestEnvironment.java index 50881910a5c..207dce95d66 100644 --- a/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/GlassFishTestEnvironment.java +++ b/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/GlassFishTestEnvironment.java @@ -53,6 +53,7 @@ * after tests are finished. * * @author David Matejcek + * @author Ondro Mihalyi */ public class GlassFishTestEnvironment { private static final Logger LOG = Logger.getLogger(GlassFishTestEnvironment.class.getName()); @@ -267,6 +268,18 @@ public static void deleteJobsFile() { } + /** Default is org.apache.derby.jdbc.ClientDataSource */ + public static void switchDerbyPoolToEmbededded() { + final AsadminResult result = getAsadmin(true).exec(5_000, "set", + "resources.jdbc-connection-pool.DerbyPool.datasource-classname=org.apache.derby.jdbc.EmbeddedDataSource", + "resources.jdbc-connection-pool.DerbyPool.property.PortNumber=", + "resources.jdbc-connection-pool.DerbyPool.property.serverName=", + "resources.jdbc-connection-pool.DerbyPool.property.URL="); + assertThat(result, asadminOK()); + // Just to see the result in log. + assertThat(getAsadmin(true).exec(5_000, "get", "resources.jdbc-connection-pool.DerbyPool.*"), asadminOK()); + } + /** * Useful for a heuristic inside Eclipse and other environments. * diff --git a/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/DomainPropertiesBackup.java b/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/DomainPropertiesBackup.java new file mode 100644 index 00000000000..77cfd656df4 --- /dev/null +++ b/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/DomainPropertiesBackup.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2024 Eclipse Foundation 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.main.itest.tools.asadmin; + +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.glassfish.main.itest.tools.GlassFishTestEnvironment.getAsadmin; +import static org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher.asadminOK; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Tool to simplify properties backup so you can symmetrically set some subset of properties before + * and after the test + * + * @author Ondro Mihalyi + * @author David Matejcek + */ +public class DomainPropertiesBackup { + + private final List settingsBackup; + + /** + * Creates the backup by calling asadmin get keyFilter. + * From retrieved list excludes entries with the specified key. + * + * @param keyFilter + * @param excludedKeys + */ + public DomainPropertiesBackup(String keyFilter, String... excludedKeys) { + final AsadminResult result = getAsadmin(true).exec(5_000, "get", keyFilter); + assertThat(result, asadminOK()); + Predicate inclusionFilter = s -> { + for (String key : excludedKeys) { + if (s.startsWith(key + '=')) { + return false; + } + } + return true; + }; + settingsBackup = Stream.of(result.getStdOut().split("\n")).filter(inclusionFilter).collect(Collectors.toList()); + } + + /** + * Restore properties from the backup by calling asadmin set. + */ + public void restore() { + String[] args = new String[settingsBackup.size() + 1]; + args[0] = "set"; + for (int i = 1; i < args.length; i++) { + args[i] = settingsBackup.get(i - 1); + } + final AsadminResult result = getAsadmin(true).exec(5_000, args); + assertThat(result, asadminOK()); + } + + + /** + * @return backup for keys starting by + * resources.jdbc-connection-pool.DerbyPool. + */ + public static DomainPropertiesBackup backupDerbyPool() { + return new DomainPropertiesBackup("resources.jdbc-connection-pool.DerbyPool.*", + "resources.jdbc-connection-pool.DerbyPool.name"); + } +} diff --git a/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/DomainSettings.java b/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/DomainSettings.java deleted file mode 100644 index 04c00d60eb9..00000000000 --- a/appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/asadmin/DomainSettings.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (c) 2024 Eclipse Foundation 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.main.itest.tools.asadmin; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Stream; - -import static org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher.asadminOK; -import static org.hamcrest.MatcherAssert.assertThat; - -/** - * - * @author Ondro Mihalyi - */ -public class DomainSettings { - - private final Asadmin asadmin; - List settingsBackup = new ArrayList<>(); - - public DomainSettings(Asadmin asadmin) { - this.asadmin = asadmin; - } - - public void backupSettings(String getKey) { - final AsadminResult result = asadmin.exec(5_000, "get", getKey); - Stream.of(result.getStdOut().split("\n")) - // Exclude "command successful - .filter(line -> line.contains("=")) - // Exclude .name for connection pools which cannot be changed - .filter(line -> !line.startsWith("resources.jdbc-connection-pool") || !line.contains(".name=")) - .forEach(settingsBackup::add); - } - - public void restoreSettings() { - String[] args = new String[settingsBackup.size() + 1]; - args[0] = "set"; - for (int i = 1; i < args.length; i++) { - args[i] = settingsBackup.get(i - 1); - } - settingsBackup.clear(); - final AsadminResult result = asadmin.exec(5_000, args); - assertThat(result, asadminOK()); - } - - public void backupDerbyPoolSettings() { - backupSettings("resources.jdbc-connection-pool.DerbyPool.*"); - } - - /** Default is org.apache.derby.jdbc.ClientDataSource */ - public void setDerbyPoolEmbededded() { - final AsadminResult result = asadmin.exec(5_000, "set", - "resources.jdbc-connection-pool.DerbyPool.datasource-classname=org.apache.derby.jdbc.EmbeddedDataSource", - "resources.jdbc-connection-pool.DerbyPool.property.PortNumber=", - "resources.jdbc-connection-pool.DerbyPool.property.serverName=", - "resources.jdbc-connection-pool.DerbyPool.property.URL="); - assertThat(result, asadminOK()); - asadmin.exec(5_000, "get", "resources.jdbc-connection-pool.DerbyPool.*"); - } - - -} diff --git a/appserver/tests/application/src/test/java/org/glassfish/main/test/app/connpool/SQLTraceListenerTest.java b/appserver/tests/application/src/test/java/org/glassfish/main/test/app/connpool/SQLTraceListenerTest.java index 2107cd797d9..5b237548ac7 100644 --- a/appserver/tests/application/src/test/java/org/glassfish/main/test/app/connpool/SQLTraceListenerTest.java +++ b/appserver/tests/application/src/test/java/org/glassfish/main/test/app/connpool/SQLTraceListenerTest.java @@ -22,8 +22,7 @@ import org.glassfish.common.util.HttpParser; import org.glassfish.main.itest.tools.GlassFishTestEnvironment; import org.glassfish.main.itest.tools.asadmin.Asadmin; -import org.glassfish.main.itest.tools.asadmin.AsadminResult; -import org.glassfish.main.itest.tools.asadmin.DomainSettings; +import org.glassfish.main.itest.tools.asadmin.DomainPropertiesBackup; import org.glassfish.main.test.app.connpool.lib.LastTraceSQLTraceListener; import org.glassfish.main.test.app.connpool.webapp.Employee; import org.glassfish.main.test.app.connpool.webapp.SqlListenerApplication; @@ -56,7 +55,7 @@ public class SQLTraceListenerTest { private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); - private static final DomainSettings DOMAIN_SETTINGS = new DomainSettings(ASADMIN); + private static final DomainPropertiesBackup DERBYPOOL_BACKUP = DomainPropertiesBackup.backupDerbyPool(); @TempDir private static File appLibDir; @@ -66,39 +65,31 @@ public class SQLTraceListenerTest { @BeforeAll public static void deployAll() throws IOException { - AsadminResult result; - File webApp = createWebApp(); - File lib = createSqlTraceListenerLib(); - result = ASADMIN.exec("add-library", lib.getAbsolutePath()); - assertThat(result, asadminOK()); // add-library shouldn't require a restart anymore + assertThat(ASADMIN.exec("add-library", lib.getAbsolutePath()), asadminOK()); - DOMAIN_SETTINGS.backupDerbyPoolSettings(); - DOMAIN_SETTINGS.setDerbyPoolEmbededded(); + GlassFishTestEnvironment.switchDerbyPoolToEmbededded(); - result = ASADMIN.exec("set", "resources.jdbc-connection-pool." + POOL_NAME - + ".sql-trace-listeners=" + LastTraceSQLTraceListener.class.getName()); - assertThat(result, asadminOK()); + assertThat(ASADMIN.exec("set", "resources.jdbc-connection-pool." + POOL_NAME + ".sql-trace-listeners=" + + LastTraceSQLTraceListener.class.getName()), asadminOK()); - result = ASADMIN.exec("deploy", - "--contextroot", "/" + WEBAPP_NAME, - "--name", WEBAPP_NAME, - webApp.getAbsolutePath()); - assertThat(result, asadminOK()); + assertThat( + ASADMIN.exec("deploy", "--contextroot", "/" + WEBAPP_NAME, "--name", WEBAPP_NAME, webApp.getAbsolutePath()), + asadminOK()); } @AfterAll public static void undeployAll() { - DOMAIN_SETTINGS.restoreSettings(); assertAll( () -> assertThat(ASADMIN.exec("undeploy", WEBAPP_NAME), asadminOK()), () -> assertThat(ASADMIN.exec("set", "resources.jdbc-connection-pool." + POOL_NAME + ".sql-trace-listeners="), asadminOK()), () -> assertThat(ASADMIN.exec("remove-library", LIB_FILE_NAME), asadminOK()) ); + DERBYPOOL_BACKUP.restore(); } @Test @@ -111,20 +102,16 @@ private void assertValidTraceRecordReceived(String contextRoot, String endpoint) HttpURLConnection connection = openConnection(8080, "/" + contextRoot + "/" + endpoint); connection.setRequestMethod("GET"); try { - try { - assertThat(connection.getResponseCode(), equalTo(200)); - } catch (AssertionError e) { - throw new AssertionError(HttpParser.readResponseErrorStream(connection), e); - } + assertThat(connection.getResponseCode(), equalTo(200)); + } catch (AssertionError e) { + throw new AssertionError(HttpParser.readResponseErrorStream(connection), e); } finally { connection.disconnect(); } } - private static File createSqlTraceListenerLib() throws IOException { - JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class) - .addClasses(LastTraceSQLTraceListener.class); - + private static File createSqlTraceListenerLib() { + JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class).addClasses(LastTraceSQLTraceListener.class); LOG.log(INFO, javaArchive.toString(true)); File appLib = new File(appLibDir, LIB_FILE_NAME); @@ -134,11 +121,11 @@ private static File createSqlTraceListenerLib() throws IOException { private static File createWebApp() { WebArchive webArchive = ShrinkWrap.create(WebArchive.class) - .addClass(SqlListenerApplication.class) - .addClass(SqlListenerEndpoint.class) - .addClass(Employee.class) - .addAsResource(SqlListenerApplication.class.getPackage(), "/META-INF/persistence.xml", "/META-INF/persistence.xml") - .addAsResource(SqlListenerApplication.class.getPackage(), "/META-INF/load.sql", "/META-INF/load.sql"); + .addClass(SqlListenerApplication.class) + .addClass(SqlListenerEndpoint.class) + .addClass(Employee.class) + .addAsResource(SqlListenerApplication.class.getPackage(), "/META-INF/persistence.xml", "/META-INF/persistence.xml") + .addAsResource(SqlListenerApplication.class.getPackage(), "/META-INF/load.sql", "/META-INF/load.sql"); LOG.log(INFO, webArchive.toString(true)); diff --git a/appserver/tests/application/src/test/java/org/glassfish/main/test/app/persistence/resourceref/JtaDataSourceResourceRefTest.java b/appserver/tests/application/src/test/java/org/glassfish/main/test/app/persistence/resourceref/JtaDataSourceResourceRefTest.java index a2c4e54f6ee..d104e7a4907 100644 --- a/appserver/tests/application/src/test/java/org/glassfish/main/test/app/persistence/resourceref/JtaDataSourceResourceRefTest.java +++ b/appserver/tests/application/src/test/java/org/glassfish/main/test/app/persistence/resourceref/JtaDataSourceResourceRefTest.java @@ -19,10 +19,11 @@ import java.io.IOException; import java.net.HttpURLConnection; +import org.glassfish.main.itest.tools.GlassFishTestEnvironment; import org.glassfish.main.itest.tools.TestUtilities; import org.glassfish.main.itest.tools.asadmin.Asadmin; import org.glassfish.main.itest.tools.asadmin.AsadminResult; -import org.glassfish.main.itest.tools.asadmin.DomainSettings; +import org.glassfish.main.itest.tools.asadmin.DomainPropertiesBackup; import org.glassfish.main.test.app.persistence.resourceref.webapp.ResourceRefApplication; import org.glassfish.main.test.app.persistence.resourceref.webapp.ResourceRefResource; import org.jboss.shrinkwrap.api.ShrinkWrap; @@ -51,12 +52,11 @@ public class JtaDataSourceResourceRefTest { private static final String CONTEXT_ROOT = "/"; private static final Asadmin ASADMIN = getAsadmin(); - private static final DomainSettings DOMAIN_SETTINGS = new DomainSettings(ASADMIN); + private static final DomainPropertiesBackup DERBYPOOL_BACKUP = DomainPropertiesBackup.backupDerbyPool(); @BeforeAll public static void deploy() throws Exception { - DOMAIN_SETTINGS.backupDerbyPoolSettings(); - DOMAIN_SETTINGS.setDerbyPoolEmbededded(); + GlassFishTestEnvironment.switchDerbyPoolToEmbededded(); final File warFile = createDeployment(); try { AsadminResult result = ASADMIN.exec("deploy", "--contextroot", CONTEXT_ROOT, "--name", APP_NAME, @@ -72,7 +72,7 @@ public static void deploy() throws Exception { static void undeploy() { AsadminResult result = ASADMIN.exec("undeploy", APP_NAME); assertThat(result, asadminOK()); - DOMAIN_SETTINGS.restoreSettings(); + DERBYPOOL_BACKUP.restore(); }