Skip to content
Permalink
Browse files

[WFLY-10798] Test cases for Datasource classes using correct TCCL

  • Loading branch information
spyrkob committed Apr 24, 2019
1 parent 766193d commit 09fe081a07469f5f9b2f440fe96e78ec5983d23d
@@ -0,0 +1,216 @@
package org.jboss.as.test.integration.jca.classloading;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.as.arquillian.api.ServerSetupTask;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.as.test.integration.jca.datasource.Datasource;
import org.jboss.as.test.shared.ServerReload;
import org.jboss.as.test.shared.ServerSnapshot;
import org.jboss.dmr.ModelNode;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.Statement;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;

public abstract class AbstractDataSourceClassloadingTestCase {

public static JavaArchive getDeployment() {
JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "driver.jar");
jar.addClass(ClassloadingDataSource.class);
jar.addClass(ClassloadingDriver.class);
jar.addClass(TestConnection.class);
jar.addClass(ClassloadingXADataSource.class);
jar.addClass(TestXAConnection.class);
jar.addAsServiceProvider(Driver.class, ClassloadingDriver.class);
return jar;
}

@Deployment
public static JavaArchive getTesterDeployment() {
JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "tester.jar");
jar.addClass(AbstractDataSourceClassloadingTestCase.class);
jar.addClass(DataSourceTcclDatasourceClassTestCase.class);
return jar;
}

@Resource(mappedName = "java:jboss/datasources/TestDS")
private DataSource ds;

public static class Setup implements ServerSetupTask {

private AutoCloseable snapshot;
private String classNamePropertyName;
private String driverClass;

protected Setup(String classNamePropertyName, String driverClass) {
this.classNamePropertyName = classNamePropertyName;
this.driverClass = driverClass;
}

@Override
public void setup(ManagementClient managementClient, String s) throws Exception {
snapshot = ServerSnapshot.takeSnapshot(managementClient);

setupModule();
setupDriver(managementClient, classNamePropertyName, driverClass);
setupDs(managementClient, "TestDS", false);
ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient(), 50000);
}

@Override
public void tearDown(ManagementClient managementClient, String s) throws Exception {
snapshot.close();
File testModuleRoot = new File(getModulePath(), "org/jboss/test/testDriver");
if (testModuleRoot.exists()) {
deleteRecursively(testModuleRoot);
}
ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient(), 50000);
}

private void setupModule() throws IOException {
File testModuleRoot = new File(getModulePath(), "org/jboss/test/testDriver");
File file = new File(testModuleRoot, "main");
if (file.exists()) {
deleteRecursively(file);
}
if (!file.mkdirs()) {
// TODO handle
}

try(FileOutputStream jarFile = new FileOutputStream(new File(file, "module.xml"));
PrintWriter pw = new PrintWriter(jarFile)) {
pw.println("<module name=\"org.jboss.test.testDriver\" xmlns=\"urn:jboss:module:1.8\">\n" +
" <resources>\n" +
" <resource-root path=\"testDriver.jar\"/>\n" +
" </resources>\n" +
"\n" +
" <dependencies>\n" +
" <module name=\"javax.sql.api\"/>\n" +
" <module name=\"sun.jdk\"/>\n" +
" <module name=\"javax.orb.api\"/>\n" +
" <module name=\"java.logging\"/>\n" +
" </dependencies>\n" +
"</module>");
}


JavaArchive deployment = getDeployment();
try(FileOutputStream jarFile = new FileOutputStream(new File(file, "testDriver.jar"))) {
deployment.as(ZipExporter.class).exportTo(jarFile);
jarFile.flush();
}
}

private static void deleteRecursively(File file) {
if (file.exists()) {
if (file.isDirectory()) {
for (String name : file.list()) {
deleteRecursively(new File(file, name));
}
}
file.delete();
}
}

private static File getModulePath() {
String modulePath = System.getProperty("module.path", null);
if (modulePath == null) {
String jbossHome = System.getProperty("jboss.home", null);
if (jbossHome == null) {
throw new IllegalStateException("Neither -Dmodule.path nor -Djboss.home were set");
}
modulePath = jbossHome + File.separatorChar + "modules";
} else {
modulePath = modulePath.split(File.pathSeparator)[0];
}
File moduleDir = new File(modulePath);
if (!moduleDir.exists()) {
throw new IllegalStateException("Determined module path does not exist");
}
if (!moduleDir.isDirectory()) {
throw new IllegalStateException("Determined module path is not a dir");
}
return moduleDir;
}

private void setupDriver(ManagementClient managementClient, String classNamePropertyName, String driverClass) throws Exception {
ModelNode address = new ModelNode();
address.add("subsystem", "datasources");
address.add("jdbc-driver", "test");

ModelNode operation = new ModelNode();
operation.get(OP).set(ADD);
operation.get(OP_ADDR).set(address);
operation.get("driver-module-name").set("org.jboss.test.testDriver");
operation.get("driver-name").set("test");
operation.get("driver-datasource-class-name").set("org.jboss.as.test.integration.jca.classloading.ClassloadingDataSource");
operation.get(classNamePropertyName).set(driverClass);
managementClient.getControllerClient().execute(operation);
}

protected void setupDs(ManagementClient managementClient, String dsName, boolean jta) throws Exception {
Datasource ds = Datasource.Builder(dsName).build();
ModelNode address = new ModelNode();
address.add("subsystem", "datasources");
address.add("data-source", dsName);

ModelNode operation = new ModelNode();
operation.get(OP).set(ADD);
operation.get(OP_ADDR).set(address);
operation.get("jndi-name").set(ds.getJndiName());
operation.get("use-java-context").set("true");
operation.get("driver-name").set("test");
operation.get("enabled").set("true");
operation.get("user-name").set(ds.getUserName());
operation.get("password").set(ds.getPassword());
operation.get("jta").set(jta);
operation.get("use-ccm").set("true");
operation.get("connection-url").set("jdbc:foo:bar");
managementClient.getControllerClient().execute(operation);
}
}

@Test
public void testGetConnection() throws Exception {
Assert.assertNotNull(ds);
ds.getConnection();
ds.getConnection("", "");
}

@Test
public void testCreateStatement() throws Exception {
Connection connection = ds.getConnection();
connection.createStatement();
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}

@Test
public void testPrepareStatement() throws Exception {
Connection connection = ds.getConnection();
connection.prepareStatement("");
connection.prepareStatement("", new int[]{});
connection.prepareStatement("", new String[]{});
connection.prepareStatement("", Statement.RETURN_GENERATED_KEYS);
connection.prepareStatement("", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
connection.prepareStatement("", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}


}
@@ -0,0 +1,69 @@
package org.jboss.as.test.integration.jca.classloading;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

public class ClassloadingDataSource implements DataSource {

public ClassloadingDataSource() {
try {
Class.forName(TestConnection.class.getName(), true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

@Override
public Connection getConnection() throws SQLException {
try {
Class.forName(TestConnection.class.getName(), true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw new SQLException(e);
}
return new TestConnection();
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
return getConnection();
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}

@Override
public void setLogWriter(PrintWriter out) throws SQLException {

}

@Override
public void setLoginTimeout(int seconds) throws SQLException {

}

@Override
public int getLoginTimeout() throws SQLException {
return 0;
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
@@ -0,0 +1,62 @@
package org.jboss.as.test.integration.jca.classloading;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;

public class ClassloadingDriver implements Driver {

public ClassloadingDriver() {
try {
Class.forName(TestConnection.class.getName(), true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

@Override
public Connection connect(String url, Properties info) throws SQLException {

try {
Class.forName(TestConnection.class.getName(), true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw new SQLException(e);
}

return new TestConnection();
}

@Override
public boolean acceptsURL(String url) throws SQLException {
return true;
}

@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return new DriverPropertyInfo[0];
}

@Override
public int getMajorVersion() {
return 0;
}

@Override
public int getMinorVersion() {
return 0;
}

@Override
public boolean jdbcCompliant() {
return false;
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}

0 comments on commit 09fe081

Please sign in to comment.
You can’t perform that action at this time.