diff --git a/pom.xml b/pom.xml index 42e0927..11f56fc 100644 --- a/pom.xml +++ b/pom.xml @@ -31,10 +31,11 @@ 3.12.0 + - junit - junit - 3.8.1 + org.junit + junit5-engine + 5.0.0-ALPHA test @@ -44,6 +45,31 @@ 0.10.2 + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + + + + org.mockito + mockito-core + 5.10.0 + test + + + + + org.easytesting + fest-swing + 1.2.1 + + + + diff --git a/src/main/java/org/nmpl/App.java b/src/main/java/org/nmpl/App.java index 2aad5f4..7ad3d33 100644 --- a/src/main/java/org/nmpl/App.java +++ b/src/main/java/org/nmpl/App.java @@ -15,7 +15,7 @@ public class App { try { manager = new DbExporter(dbName,username,pasword); } - catch (SQLException e) { + catch (Exception e) { throw new RuntimeException(e); } manager.setVisible(true); diff --git a/src/main/java/org/nmpl/DbExporter.java b/src/main/java/org/nmpl/DbExporter.java index 7b934f5..0ca0be8 100644 --- a/src/main/java/org/nmpl/DbExporter.java +++ b/src/main/java/org/nmpl/DbExporter.java @@ -9,8 +9,8 @@ import java.util.List; import java.util.Objects; public class DbExporter extends JFrame { - private final JComboBox exportFormatComboBox; - private final JList tableList; + protected final JComboBox exportFormatComboBox; + protected final JList tableList; private final DefaultListModel tableListModel; private final Connection connection; @@ -60,7 +60,7 @@ public class DbExporter extends JFrame { exportButton.addActionListener(e -> exportTable()); } - private void showTables() { + protected void showTables() { try { DatabaseMetaData metaData = connection.getMetaData(); String catalog = connection.getCatalog(); @@ -81,7 +81,7 @@ public class DbExporter extends JFrame { } } - private void exportTable() { + protected void exportTable() { String selectedTable = tableList.getSelectedValue(); if (selectedTable == null) { JOptionPane.showMessageDialog(this, "Please select a table to export."); diff --git a/src/main/java/org/nmpl/ExporterFactory.java b/src/main/java/org/nmpl/ExporterFactory.java index bc76f65..5852437 100644 --- a/src/main/java/org/nmpl/ExporterFactory.java +++ b/src/main/java/org/nmpl/ExporterFactory.java @@ -48,12 +48,16 @@ public class ExporterFactory { } } - public Exportable getExporter(String format) throws Exception { + public Exportable getExporter(String format) { String className = exporterClassNames.get(format); if (className == null) { throw new IllegalArgumentException("Unsupported export format: " + format); } - return (Exportable) Class.forName(className).getDeclaredConstructor(Connection.class).newInstance(connection); + try { + return (Exportable) Class.forName(className).getDeclaredConstructor(Connection.class).newInstance(connection); + } catch (Exception e) { + throw new RuntimeException("Failed to create exporter instance", e); + } } public List getSupportedFormats() { diff --git a/src/test/java/org/nmpl/AppTest.java b/src/test/java/org/nmpl/AppTest.java index 1e03cff..a8a62fb 100644 --- a/src/test/java/org/nmpl/AppTest.java +++ b/src/test/java/org/nmpl/AppTest.java @@ -1,38 +1,38 @@ package org.nmpl; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import org.fest.swing.fixture.FrameFixture; +import org.junit.jupiter.api.Test; +import javax.swing.*; +import java.sql.SQLException; -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); +import static org.fest.swing.core.matcher.JButtonMatcher.withText; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +class AppTest { + + private FrameFixture frame; + + @Test + void setUp() { + final String dbName = "testDb"; + final String username = "root"; + final String password = "password"; + SwingUtilities.invokeLater(() -> { + DbExporter app = null; + try { + app = new DbExporter(dbName, username, password); + } + catch (SQLException e) { + e.printStackTrace(); + } + finally{ + frame = new FrameFixture(app); + frame.show(); + frame.button(withText("Show Tables")).click(); + assertTrue(frame.table().rowCount() >= 2); // Assuming tables are populated + } + }); } - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } } diff --git a/src/test/java/org/nmpl/ExporterFactoryTest.java b/src/test/java/org/nmpl/ExporterFactoryTest.java new file mode 100644 index 0000000..6dae40b --- /dev/null +++ b/src/test/java/org/nmpl/ExporterFactoryTest.java @@ -0,0 +1,59 @@ +package org.nmpl; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.nmpl.exporters.XMLExporter; + +import java.sql.Connection; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + + +public class ExporterFactoryTest { + + private Connection connection; + + @Mock + private Connection mockConnection; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + + // Assuming you have a method in ExporterFactory to set the Connection + // You may need to modify ExporterFactory to accept Connection as a parameter + ExporterFactory exporterFactory = new ExporterFactory(mockConnection); + } + + @Test + void testGetExporter() { + ExporterFactory exporterFactory = new ExporterFactory(connection); + + try { + // Assuming 'XML' is a supported export format in your config.xml + Exportable exporter = exporterFactory.getExporter("XML"); + assertNotNull(exporter); + + System.out.println("Actual class of exporter: " + exporter.getClass().getName()); + assertTrue(exporter instanceof XMLExporter); // Adjust based on your actual exporter class + + } catch (Exception e) { + e.printStackTrace(); + fail("Exception occurred while getting exporter"); + } + } + + @Test + void testGetSupportedFormats() { + ExporterFactory exporterFactory = new ExporterFactory(connection); + + List supportedFormats = exporterFactory.getSupportedFormats(); + + assertNotNull(supportedFormats); + assertFalse(supportedFormats.isEmpty()); + assertTrue(supportedFormats.contains("XML")); // Adjust based on your actual supported formats + } +}