Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests in ide/db module #1437

Merged
merged 1 commit into from Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -96,6 +96,7 @@ matrix:
ide/css.editor
ide/css.lib
ide/css.model
ide/db
ide/db.dataview
ide/db.sql.editor
ide/docker.api
Expand Down
Expand Up @@ -19,13 +19,11 @@

package org.netbeans.modules.db.explorer;

import java.io.File;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.Driver;
Expand Down Expand Up @@ -199,7 +197,7 @@ public void testGetDriverPriority() throws Exception {
}

public void testJDBCDriverCached() throws Exception {
JDBCDriver drv = createDummyJDBCDriver(getDataDir());
JDBCDriver drv = createJDBCDriver();
Driver driver1 = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv);
Driver driver2 = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv);
assertSame(driver1.getClass(), driver2.getClass());
Expand All @@ -219,11 +217,6 @@ private static JDBCDriver createJDBCDriver() {
return JDBCDriver.create("test_driver", "DbDriverManagerTest Driver", "org.netbeans.modules.db.explorer.DbDriverManagerTest$DriverImpl", new URL[] { url });
}

private static JDBCDriver createDummyJDBCDriver(File dataDir) throws MalformedURLException {
URL url = dataDir.toURL();
return JDBCDriver.create("test_driver", "DbDriverManagerTest DummyDriver", "DummyDriver", new URL[] { url });
}

public static final class DriverImpl implements Driver {

public static final String DEFAULT_URL = "jdbc:DbDriverManagerTest";
Expand Down
Expand Up @@ -24,42 +24,44 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.netbeans.api.db.explorer.*;
import org.netbeans.modules.db.test.Util;
import org.netbeans.modules.db.explorer.DatabaseConnection;
import org.netbeans.modules.db.test.DBTestBase;
import org.netbeans.modules.db.test.Util;
import org.openide.WizardDescriptor;

public class AddConnectionWizardTest extends DBTestBase {

private static final Logger LOG = Logger.getLogger(AddConnectionWizardTest.class.getName());

public AddConnectionWizardTest(String testName) {
super(testName);
}

@Override
protected void setUp() throws Exception {
super.setUp();
Util.clearConnections();
}

public AddConnectionWizardTest(String testName) {
super(testName);
Util.deleteDriverFiles();
}

public void testAddConnectionWizdardReturnsNullOnCancel() throws Exception {
Util.clearConnections();
Util.deleteDriverFiles();

final CountDownLatch finalLock = new CountDownLatch(1);
final JDBCDriver driver = Util.createDummyDriver();
final org.netbeans.modules.db.explorer.DatabaseConnection dummyConnection
= new org.netbeans.modules.db.explorer.DatabaseConnection(
driver.getName(),
driver.getClassName(),
"database", "schema", "user", "password", true);
final DatabaseConnection dummyConnection = new DatabaseConnection(driver.getName(),
driver.getClassName(), "database", "schema", "user", "password", true);

final DatabaseConnection[] result = new DatabaseConnection[1];
final DatabaseConnection[] result = new DatabaseConnection[]{dummyConnection};

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
AddConnectionWizard wiz = reflectiveConstruct(AddConnectionWizard.class,
new Class[] {JDBCDriver.class, String.class, String.class, String.class},
new Object[] {driver, "jdbc:mysql://dummy", "dummy", "dummy"});
Expand All @@ -70,8 +72,11 @@ public void run() {
wd.doCancelClick();

result[0] = reflectiveCall(AddConnectionWizard.class, wiz, "getResult", new Class[0], new Object[0]);

} catch (Throwable throwable) {
LOG.log(Level.SEVERE, throwable.getLocalizedMessage(), throwable);
} finally {
finalLock.countDown();
}
}
});

Expand All @@ -83,22 +88,17 @@ public void run() {


public void testAddConnectionWizdardReturnsNonNullOnFinish() throws Exception {
Util.clearConnections();
Util.deleteDriverFiles();

final CountDownLatch finalLock = new CountDownLatch(1);
final JDBCDriver driver = Util.createDummyDriver();
final org.netbeans.modules.db.explorer.DatabaseConnection dummyConnection
= new org.netbeans.modules.db.explorer.DatabaseConnection(
driver.getName(),
driver.getClassName(),
"database", "schema", "user", "password", true);
final DatabaseConnection dummyConnection = new DatabaseConnection(driver.getName(),
driver.getClassName(), "database", "schema", "user", "password", true);

final DatabaseConnection[] result = new DatabaseConnection[1];

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
AddConnectionWizard wiz = reflectiveConstruct(AddConnectionWizard.class,
new Class[] {JDBCDriver.class, String.class, String.class, String.class},
new Object[] {driver, "jdbc:mysql://dummy", "dummy", "dummy"});
Expand All @@ -109,8 +109,11 @@ public void run() {
wd.doFinishClick();

result[0] = reflectiveCall(AddConnectionWizard.class, wiz, "getResult", new Class[0], new Object[0]);

} catch (Throwable throwable) {
LOG.log(Level.SEVERE, throwable.getLocalizedMessage(), throwable);
} finally {
finalLock.countDown();
}
}
});

Expand Down Expand Up @@ -140,16 +143,6 @@ private <T> T reflectiveFieldGet(Class clazz, Object instance, String fieldname)
}
}

private <T> T reflectiveFieldSet(Class clazz, Object instance, String fieldname) {
try {
Field f = clazz.getDeclaredField(fieldname);
f.setAccessible(true);
return (T) f.get(instance);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}

private <T> T reflectiveCall(Class clazz, Object instance, String name, Class[] signature, Object[] params) {
try {
Method m = clazz.getDeclaredMethod(name, signature);
Expand Down
Expand Up @@ -678,7 +678,7 @@ private void getProperties() throws Exception {
}
username = System.getProperty(USERNAME_PROPERTY, "DBTESTS");
password = System.getProperty(PASSWORD_PROPERTY, "DBTESTS");
driverJar = System.getProperty(DRIVER_JARPATH_PROPERTY, "nball:///db/external/derby-10.2.2.0.jar");
driverJar = System.getProperty(DRIVER_JARPATH_PROPERTY, "nball:///ide/db/external/derby-10.2.2.0.jar");

driverJar = convertPath(driverJar);
}
Expand Down