Skip to content

Commit

Permalink
[TEST] Added unit tests where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Burgess committed Nov 12, 2015
1 parent 782ed35 commit f6b72b8
Show file tree
Hide file tree
Showing 8 changed files with 682 additions and 33 deletions.
1 change: 1 addition & 0 deletions dbdialog/ivy.xml
Expand Up @@ -31,5 +31,6 @@
<dependency org="org.eclipse.swt" name="swt-linux-x86_64" rev="4.3.2" transitive="false" />

<dependency org="junit" name="junit" rev="4.7" conf="test->default" transitive="false"/>
<dependency org="org.mockito" name="mockito-all" rev="1.9.5" conf="test->default" transitive="false" />
</dependencies>
</ivy-module>
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com
* Copyright (C) 2002-2015 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
Expand Down Expand Up @@ -34,7 +34,7 @@ public class DatabaseConnectionDialog {

public static final String DIALOG_DEFINITION_FILE = "org/pentaho/ui/database/databasedialog.xul";

private Map<String, String> extendedClasses = new HashMap<String, String>();
protected Map<String, String> extendedClasses = new HashMap<>();

public DatabaseConnectionDialog() {
}
Expand Down
59 changes: 36 additions & 23 deletions dbdialog/src/org/pentaho/ui/database/event/DataHandler.java
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com
* Copyright (C) 2002-2015 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
Expand Down Expand Up @@ -75,19 +75,17 @@

/**
* Handles all manipulation of the DatabaseMeta, data retrieval from XUL DOM and rudimentary validation.
*
* <p/>
* TODO: 2. Needs to be abstracted away from the DatabaseMeta object, so other tools in the platform can use the dialog
* and their preferred database object. 3. Needs exception handling, string resourcing and logging
*
* @author gmoran
* @created Mar 19, 2008
*
*/
public class DataHandler extends AbstractXulEventHandler {

public static final SortedMap<String, DatabaseInterface> connectionMap =
new TreeMap<String, DatabaseInterface>();
public static final Map<String, String> connectionNametoID = new HashMap<String, String>();
public static final SortedMap<String, DatabaseInterface> connectionMap = new TreeMap<>();
public static final Map<String, String> connectionNametoID = new HashMap<>();

// The connectionMap allows us to keep track of the connection
// type we are working with and the correlating database interface
Expand All @@ -97,7 +95,7 @@ public class DataHandler extends AbstractXulEventHandler {

List<PluginInterface> plugins = registry.getPlugins( DatabasePluginType.class );

PluginTypeListener databaseTypeListener = new DatabaseTypeListener( registry ){
PluginTypeListener databaseTypeListener = new DatabaseTypeListener( registry ) {
public void databaseTypeAdded( String pluginName, DatabaseInterface databaseInterface ) {
connectionMap.put( pluginName, databaseInterface );
connectionNametoID.put( pluginName, databaseInterface.getPluginId() );
Expand Down Expand Up @@ -254,21 +252,24 @@ public void loadConnectionData() {
}
PluginRegistry registry = PluginRegistry.getInstance();
registry.addPluginListener( DatabasePluginType.class, new DatabaseTypeListener( registry ) {
@Override public void databaseTypeAdded( String pluginName, DatabaseInterface databaseInterface ) {
@Override
public void databaseTypeAdded( String pluginName, DatabaseInterface databaseInterface ) {
if ( keys.add( pluginName ) ) {
update();
}
}

@Override public void databaseTypeRemoved( String pluginName ) {
@Override
public void databaseTypeRemoved( String pluginName ) {
if ( keys.remove( pluginName ) ) {
update();
}
}

private void update() {
Display.getDefault().syncExec( new Runnable() {
@Override public void run() {
@Override
public void run() {
connectionBox.removeItems();
for ( String key : keys ) {
connectionBox.addItem( key );
Expand Down Expand Up @@ -391,7 +392,9 @@ public void editOptions( int index ) {

public void clearOptionsData() {
getControls();
optionsParameterTree.getRootChildren().removeAll();
if ( optionsParameterTree != null ) {
optionsParameterTree.getRootChildren().removeAll();
}
}

public void getOptionHelp() {
Expand Down Expand Up @@ -423,7 +426,7 @@ public void setDeckChildIndex() {

// if pooling selected, check the parameter validity before allowing
// a deck panel switch...
int originalSelection = dialogDeck.getSelectedIndex();
int originalSelection = ( dialogDeck == null ? -1 : dialogDeck.getSelectedIndex() );

boolean passed = true;
if ( originalSelection == 3 ) {
Expand Down Expand Up @@ -782,18 +785,20 @@ private void setInfo( DatabaseMeta meta ) {
getControls();

// Name:
connectionNameBox.setValue( meta.getDisplayName() );
if ( connectionNameBox != null ) {
connectionNameBox.setValue( meta.getDisplayName() );
}

PluginRegistry registry = PluginRegistry.getInstance();
PluginInterface dInterface = registry.getPlugin( DatabasePluginType.class, meta.getPluginId() );

// Connection type:
int index = new ArrayList<String>( connectionMap.keySet() ).indexOf( dInterface.getName() );
int index = ( dInterface == null ? -1 : new ArrayList<>( connectionMap.keySet() ).indexOf( dInterface.getName() ) );
if ( index >= 0 ) {
connectionBox.setSelectedIndex( index );
} else {
LogChannel.GENERAL.logError( "Unable to find database type "
+ dInterface.getName() + " in our connection map" );
+ ( dInterface == null ? "null" : dInterface.getName() ) + " in our connection map" );
}

// Access type:
Expand Down Expand Up @@ -894,7 +899,9 @@ private void traverseDomSetReadOnly( XulComponent component, boolean readonly )
private void setReadOnly( boolean readonly ) {
// set the readonly status of EVERYTHING!
traverseDomSetReadOnly( document.getRootElement(), readonly );
noticeLabel.setVisible( readonly );
if ( noticeLabel != null ) {
noticeLabel.setVisible( readonly );
}

if ( readonly ) {
// now turn back on the cancel and test buttons
Expand All @@ -909,7 +916,6 @@ private void setReadOnly( boolean readonly ) {
}

/**
*
* @return the list of parameters that were enabled, but had invalid return values (null or empty)
*/
private boolean checkPoolingParameters() {
Expand Down Expand Up @@ -1449,14 +1455,15 @@ public void handleUseSecurityCheckbox() {
}
}

private static abstract class DatabaseTypeListener implements PluginTypeListener {
protected abstract static class DatabaseTypeListener implements PluginTypeListener {
private final PluginRegistry registry;

public DatabaseTypeListener( PluginRegistry registry ) {
this.registry = registry;
}

@Override public void pluginAdded( Object serviceObject ) {
@Override
public void pluginAdded( Object serviceObject ) {
PluginInterface plugin = (PluginInterface) serviceObject;
String pluginName = plugin.getName();
try {
Expand All @@ -1465,24 +1472,30 @@ public DatabaseTypeListener( PluginRegistry registry ) {
databaseInterface.setName( pluginName );
databaseTypeAdded( pluginName, databaseInterface );
} catch ( KettleException e ) {
Throwable t = e;
if ( e.getCause() != null ) {
t = e.getCause();
}
System.out.println( "Could not create connection entry for "
+ pluginName + ". " + e.getCause().getClass().getName() );
+ pluginName + ". " + t.getClass().getName() );
LogChannel.GENERAL.logError( "Could not create connection entry for "
+ pluginName + ". " + e.getCause().getClass().getName() );
+ pluginName + ". " + t.getClass().getName() );
}
}

public abstract void databaseTypeAdded( String pluginName, DatabaseInterface databaseInterface );

@Override public void pluginRemoved( Object serviceObject ) {
@Override
public void pluginRemoved( Object serviceObject ) {
PluginInterface plugin = (PluginInterface) serviceObject;
String pluginName = plugin.getName();
databaseTypeRemoved( pluginName );
}

public abstract void databaseTypeRemoved( String pluginName );

@Override public void pluginChanged( Object serviceObject ) {
@Override
public void pluginChanged( Object serviceObject ) {
pluginRemoved( serviceObject );
pluginAdded( serviceObject );
}
Expand Down
19 changes: 11 additions & 8 deletions dbdialog/src/org/pentaho/ui/database/event/FragmentHandler.java
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com
* Copyright (C) 2002-2015 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
Expand Down Expand Up @@ -54,12 +54,12 @@ public class FragmentHandler extends AbstractXulEventHandler {
public FragmentHandler() {
}

private void loadDatabaseOptionsFragment( String fragmentUri ) throws XulException {
protected void loadDatabaseOptionsFragment( String fragmentUri ) throws XulException {

XulComponent groupElement = document.getElementById( "database-options-box" );
XulComponent parentElement = groupElement.getParent();

XulDomContainer fragmentContainer = null;
XulDomContainer fragmentContainer;

try {

Expand Down Expand Up @@ -152,16 +152,19 @@ public void refreshOptions() {

}

private String getFragment( DatabaseInterface database, String dbName, String extension, String defaultFragment ) {
protected String getFragment( DatabaseInterface database, String dbName, String extension, String defaultFragment ) {
String fragment;
String ext = ( extension == null ? "" : extension );
String databaseName = ( dbName == null ? "" : dbName );
String defaultFrag = ( defaultFragment == null ? "" : defaultFragment );
if ( database.getXulOverlayFile() != null ) {
fragment = packagePath.concat( database.getXulOverlayFile() ).concat( extension );
fragment = packagePath.concat( database.getXulOverlayFile() ).concat( ext );
} else {
fragment = packagePath.concat( dbName ).concat( extension );
fragment = packagePath.concat( databaseName ).concat( ext );
}
InputStream in = getClass().getClassLoader().getResourceAsStream( fragment.toLowerCase() );
if ( in == null ) {
fragment = packagePath.concat( defaultFragment );
fragment = packagePath.concat( defaultFrag );
}
return fragment;
}
Expand All @@ -173,7 +176,7 @@ public Object getData() {
public void setData( Object arg0 ) {
}

private void showMessage( String message ) {
protected void showMessage( String message ) {
try {
XulMessageBox box = (XulMessageBox) document.createElement( "messagebox" );
box.setMessage( message );
Expand Down
@@ -0,0 +1,56 @@
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.ui.database;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;


/**
* Unit tests for DatabaseConnectionDialog.
*/
public class DatabaseConnectionDialogTest {

DatabaseConnectionDialog dialog;

@Before
public void setUp() throws Exception {
dialog = new DatabaseConnectionDialog();
}

@Test
public void testRegisterClass() throws Exception {
assertTrue( dialog.extendedClasses.isEmpty() );
dialog.registerClass( "MyClass", "org.pentaho.test.MyClass" );
assertFalse( dialog.extendedClasses.isEmpty() );
assertEquals( "org.pentaho.test.MyClass", dialog.extendedClasses.get( "MyClass" ) );
}

@Test
public void testGetSwtInstance() throws Exception {

}
}
71 changes: 71 additions & 0 deletions dbdialog/test-src/org/pentaho/ui/database/MessagesTest.java
@@ -0,0 +1,71 @@
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.ui.database;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;


/**
* Unit tests for Messages.
*/
public class MessagesTest {

@Test
public void testGetBundle() throws Exception {
assertNotNull( Messages.getBundle() );
}

@Test
public void testGetString() throws Exception {
// These tests are meant for the en_US locale (or equivalent)
assertEquals( "Database Connection", Messages.getString( "DatabaseDialog.Shell.title" ) );
assertEquals( "!Not.A.Message!", Messages.getString( "Not.A.Message" ) );

// 1 param
assertEquals( "MyParam: JDBC options help", Messages.getString( "DatabaseDialog.JDBCOptions.Tab", "MyParam" ) );
assertEquals( "!Not.A.Message!", Messages.getString( "Not.A.Message", "Unused1" ) );
assertEquals( "!null!", Messages.getString( null, "Unused1" ) );

// 2 params
assertEquals( "MyParam: JDBC options help",
Messages.getString( "DatabaseDialog.JDBCOptions.Tab", "MyParam", "Unused" ) );
assertEquals( "!Not.A.Message!", Messages.getString( "Not.A.Message", "Unused1", "Unused2" ) );
assertEquals( "!null!", Messages.getString( null, null, null ) );

// 3 params
assertEquals( "MyParam: JDBC options help",
Messages.getString( "DatabaseDialog.JDBCOptions.Tab", "MyParam", "Unused2", "Unused3" ) );
assertEquals( "!Not.A.Message!", Messages.getString( "Not.A.Message", "Unused1", "Unused2", "Unused3" ) );
assertEquals( "!null!", Messages.getString( null, null, null, null ) );

// 4 params
assertEquals( "MyParam: JDBC options help",
Messages.getString( "DatabaseDialog.JDBCOptions.Tab", "MyParam", "Unused2", "Unused3", "Unused4" ) );
assertEquals( "!Not.A.Message!",
Messages.getString( "Not.A.Message", "Unused1", "Unused2", "Unused3", "Unused4" ) );
assertEquals( "!null!", Messages.getString( null, null, null, null, null ) );

}
}

0 comments on commit f6b72b8

Please sign in to comment.