Skip to content

Commit

Permalink
Improved backup for DerbyPool in tests
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <[email protected]>
  • Loading branch information
dmatej committed Oct 8, 2024
1 parent e0a5c64 commit 8521a2a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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
* <code>resources.jdbc-connection-pool.DerbyPool.</code>
*/
public static DomainPropertiesBackup backupDerbyPool() {
return new DomainPropertiesBackup("resources.jdbc-connection-pool.DerbyPool.*",
"resources.jdbc-connection-pool.DerbyPool.name");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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();
}


Expand Down

0 comments on commit 8521a2a

Please sign in to comment.