Skip to content

Commit

Permalink
Fix tests in ide/db module
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorespert committed Aug 18, 2019
1 parent 87ae192 commit 2d1c44d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 43 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 2d1c44d

Please sign in to comment.