Skip to content

Commit

Permalink
Merge pull request #25031 from dmatej/test-cleanup
Browse files Browse the repository at this point in the history
JtaDataSourceResourceRefTest cleanup + fixed exception message
  • Loading branch information
dmatej committed Jul 7, 2024
2 parents bf64966 + 4b5564f commit f422752
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.glassfish.main.itest.tools;

import static org.glassfish.main.itest.tools.TestUtilities.getConfigBoolean;

import jakarta.ws.rs.client.Client;

import java.io.File;
Expand Down Expand Up @@ -84,8 +82,9 @@ public class GlassFishTestEnvironment {
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",
getConfigBoolean("glassfish.suspend") ? "--suspend" : "--debug"), asadminOK());
isStartDomainSuspendEnabled() ? "--suspend" : "--debug"), asadminOK());
}


Expand Down Expand Up @@ -307,6 +306,12 @@ private static void changePassword() {
}


private static boolean isStartDomainSuspendEnabled() {
final String envValue = System.getenv("GLASSFISH_SUSPEND");
return envValue == null ? Boolean.getBoolean("glassfish.suspend") : Boolean.parseBoolean(envValue);
}


private static <T> T doIO(IOSupplier<T> action) {
try {
return action.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,4 @@ public static void delete(final File... files) throws IOException {
}
throw failures;
}

public static String getConfig(String key) {
final String NOT_ALPHA_NUM = "\\P{Alnum}";
final String envValue = System.getenv(key.replaceAll(NOT_ALPHA_NUM, "_"));
return envValue != null ? envValue : System.getProperty(key);
}

public static boolean getConfigBoolean(String key) {
return Boolean.parseBoolean(getConfig(key));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
-->
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0"
>
<persistence-unit name="resourceRefPU" transaction-type="JTA">
<jta-data-source>myDS</jta-data-source>
<jta-data-source>myDS</jta-data-source>
</persistence-unit>
</persistence>
</persistence>
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Contributors to the Eclipse Foundation. 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
-->
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="6.0"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd">
<resource-ref>
<description>Primary database</description>
<res-ref-name>myDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Contributors to the Eclipse Foundation. 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
-->
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="6.0"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
>
<resource-ref>
<description>Primary database</description>
<res-ref-name>myDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -18,94 +18,113 @@
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.file.Files;
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.test.app.persistence.resourceref.webapp.ResourceRefApplication;
import org.glassfish.main.test.app.persistence.resourceref.webapp.ResourceRefResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static java.lang.System.Logger.Level.WARNING;
import static java.lang.System.Logger.Level.INFO;
import static org.glassfish.main.itest.tools.GlassFishTestEnvironment.getAsadmin;
import static org.glassfish.main.itest.tools.GlassFishTestEnvironment.openConnection;
import static org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher.asadminOK;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.glassfish.main.itest.tools.asadmin.Asadmin;
import org.glassfish.main.itest.tools.asadmin.AsadminResult;
import org.glassfish.main.itest.tools.setup.DeploymentAware;
import org.glassfish.main.test.app.persistence.resourceref.webapp.ResourceRefApplication;
import org.glassfish.main.test.app.persistence.resourceref.webapp.ResourceRefResource;

/**
* Tests that JTA datasource in persistence.xml can be a resource reference
*/
public class JtaDataSourceResourceRefTest implements DeploymentAware {
public class JtaDataSourceResourceRefTest {

private static final Class<?> TEST_CLASS = JtaDataSourceResourceRefTest.class;
private static final System.Logger LOG = System.getLogger(JtaDataSourceResourceRefTest.class.getName());
private static final Package TEST_PACKAGE = JtaDataSourceResourceRefTest.class.getPackage();
private static final String APP_NAME = JtaDataSourceResourceRefTest.class.getSimpleName() + "WebApp";
private static final String CONTEXT_ROOT = "/";
private static final Asadmin ASADMIN = getAsadmin();

private static final Package TEST_PACKAGE = TEST_CLASS.getPackage();
private static String[] derbyPoolSettingsBackup;

private static final System.Logger LOG = System.getLogger(TEST_CLASS.getName());

private static final String APP_NAME = TEST_CLASS.getSimpleName() + "WebApp";

private static final String CONTEXT_ROOT = "/" + APP_NAME;
@BeforeAll
public static void deploy() throws Exception {
backupDerbyPoolSettings();
setDerbyPoolEmbededded();
final File warFile = createDeployment();
try {
AsadminResult result = ASADMIN.exec("deploy", "--contextroot", CONTEXT_ROOT, "--name", APP_NAME,
warFile.getAbsolutePath());
assertThat(result, asadminOK());
} finally {
TestUtilities.delete(warFile);
}
}

protected static final Asadmin ASADMIN = getAsadmin();

public System.Logger getLogger() {
return LOG;
@AfterAll
static void undeploy() {
AsadminResult result = ASADMIN.exec("undeploy", APP_NAME);
assertThat(result, asadminOK());
restoreDerbyPoolSettings();
}

@Test
public void testDeploy() throws IOException {
makeTheDefaultDataSourceEmbededded();
File warFile = createDeployment();
try {
assertThat(ASADMIN.exec("deploy", warFile.getAbsolutePath()), asadminOK());
} finally {
try {
Files.deleteIfExists(warFile.toPath());
} catch (IOException e) {
LOG.log(WARNING, "An error occurred while remove temp file " + warFile.getAbsolutePath(), e);
}
}

HttpURLConnection connection = openConnection(8080, CONTEXT_ROOT);
@Test
public void test() throws IOException {
final HttpURLConnection connection = openConnection(8080, CONTEXT_ROOT);
connection.setRequestMethod("GET");
try {
assertThat(connection.getResponseCode(), equalTo(200));
} finally {
connection.disconnect();
}
}

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());
}

assertThat(ASADMIN.exec("undeploy", APP_NAME), 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 File createDeployment() throws IOException {
WebArchive webArchive = ShrinkWrap.create(WebArchive.class)
private static File createDeployment() throws IOException {
final WebArchive webArchive = ShrinkWrap.create(WebArchive.class)
.addClass(ResourceRefResource.class)
.addClass(ResourceRefApplication.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource(TEST_PACKAGE, "web.xml", "web.xml")
.addAsResource(TEST_PACKAGE, "persistence.xml", "META-INF/persistence.xml");

return createDeploymentWar(webArchive, APP_NAME);
}

private static void makeTheDefaultDataSourceEmbededded() {
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=");
if (result.isError()) {
System.out.println("Failed to update the default datasource DerbyPool.");
} else {
System.out.println("The default datasource changed to embedded.");
}
LOG.log(INFO, webArchive.toString(true));
File tempFile = File.createTempFile(APP_NAME, ".war");
webArchive.as(ZipExporter.class).exportTo(tempFile, true);
return tempFile;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2007, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -93,15 +93,15 @@ public ClassLoader run() {
}
});

try {
Thread.currentThread().setContextClassLoader(HibernateValidator.class.getClassLoader());
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
ValidatorContext validatorContext = validatorFactory.usingContext();
validatorContext.messageInterpolator(new MessageInterpolatorImpl());
beanValidator = validatorContext.traversableResolver(TRAVERSABLE_RESOLVER).getValidator();
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
try {
Thread.currentThread().setContextClassLoader(HibernateValidator.class.getClassLoader());
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
ValidatorContext validatorContext = validatorFactory.usingContext();
validatorContext.messageInterpolator(new MessageInterpolatorImpl());
beanValidator = validatorContext.traversableResolver(TRAVERSABLE_RESOLVER).getValidator();
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}

// private final Validator beanValidator;
Expand Down Expand Up @@ -240,7 +240,7 @@ public synchronized void setter(ConfigModel.Property property, Object newValue,
bean.getLock().unlock();
throw new IllegalArgumentException(
"Keys cannot be duplicate. Old value of this key " +
"property, " + oldKeyValue + "will be retained");
"property, " + oldKeyValue + " will be retained");
}
}
}
Expand Down

0 comments on commit f422752

Please sign in to comment.