Skip to content

Commit

Permalink
SQLTraceListener: Use embedded Derby for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OndroMih committed Aug 17, 2024
1 parent cf2fa28 commit 212540a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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<String> 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.*");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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.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 @@ -59,6 +60,8 @@ public class SQLTraceListenerTest {

private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();

private static final DomainSettings DOMAIN_SETTINGS = new DomainSettings(ASADMIN);

@TempDir
private static File appLibDir;

Expand All @@ -80,6 +83,9 @@ public static void deployAll() throws IOException {
result = ASADMIN.exec("restart-domain");
assertThat(result, asadminOK());

DOMAIN_SETTINGS.backupDerbyPoolSettings();
DOMAIN_SETTINGS.setDerbyPoolEmbededded();

result = ASADMIN.exec("set", "resources.jdbc-connection-pool." + POOL_NAME
+ ".sql-trace-listeners=" + LastTraceSQLTraceListener.class.getName());
assertThat(result, asadminOK());
Expand All @@ -93,6 +99,7 @@ public static void deployAll() throws IOException {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.stream.Stream;

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.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 +51,12 @@ public class JtaDataSourceResourceRefTest {
private static final String CONTEXT_ROOT = "/";
private static final Asadmin ASADMIN = getAsadmin();

private static String[] derbyPoolSettingsBackup;
private static final DomainSettings DOMAIN_SETTINGS = new DomainSettings(ASADMIN);

@BeforeAll
public static void deploy() throws Exception {
backupDerbyPoolSettings();
setDerbyPoolEmbededded();
DOMAIN_SETTINGS.backupDerbyPoolSettings();
DOMAIN_SETTINGS.setDerbyPoolEmbededded();
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());
restoreDerbyPoolSettings();
DOMAIN_SETTINGS.restoreSettings();
}


Expand All @@ -87,34 +87,6 @@ public void test() throws IOException {
}
}

private static void backupDerbyPoolSettings() {
final AsadminResult result = ASADMIN.exec(5_000, "get", "resources.jdbc-connection-pool.DerbyPool.*");
// Exclude "command successful and .name which cannot be changed
derbyPoolSettingsBackup = Stream.of(result.getStdOut().split("\n"))
.filter(line -> line.contains("=") && !line.contains(".name=")).toArray(String[]::new);
}

private static void restoreDerbyPoolSettings() {
String[] args = new String[derbyPoolSettingsBackup.length + 1];
args[0] = "set";
for (int i = 1; i < args.length; i++) {
args[i] = derbyPoolSettingsBackup[i - 1];
}
final AsadminResult result = ASADMIN.exec(5_000, args);
assertThat(result, asadminOK());
}

/** Default is org.apache.derby.jdbc.ClientDataSource */
private static 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.*");
}

private static File createDeployment() throws IOException {
final WebArchive webArchive = ShrinkWrap.create(WebArchive.class)
.addClass(ResourceRefResource.class)
Expand Down

0 comments on commit 212540a

Please sign in to comment.