Initial Commit

This commit is contained in:
Amit Kumar Nandi 2024-02-18 02:40:02 +05:30
parent f1adc67dc2
commit fed3887aee
6 changed files with 131 additions and 42 deletions

32
pom.xml
View file

@ -31,10 +31,11 @@
<version>3.12.0</version> <!-- Use the latest version --> <version>3.12.0</version> <!-- Use the latest version -->
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit5-engine</artifactId>
<version>3.8.1</version> <version>5.0.0-ALPHA</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
@ -44,6 +45,31 @@
<version>0.10.2</version> <version>0.10.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.easytesting/fest-swing -->
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-swing</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies> </dependencies>

View file

@ -15,7 +15,7 @@ public class App {
try { try {
manager = new DbExporter(dbName,username,pasword); manager = new DbExporter(dbName,username,pasword);
} }
catch (SQLException e) { catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
manager.setVisible(true); manager.setVisible(true);

View file

@ -9,8 +9,8 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
public class DbExporter extends JFrame { public class DbExporter extends JFrame {
private final JComboBox<String> exportFormatComboBox; protected final JComboBox<String> exportFormatComboBox;
private final JList<String> tableList; protected final JList<String> tableList;
private final DefaultListModel<String> tableListModel; private final DefaultListModel<String> tableListModel;
private final Connection connection; private final Connection connection;
@ -60,7 +60,7 @@ public class DbExporter extends JFrame {
exportButton.addActionListener(e -> exportTable()); exportButton.addActionListener(e -> exportTable());
} }
private void showTables() { protected void showTables() {
try { try {
DatabaseMetaData metaData = connection.getMetaData(); DatabaseMetaData metaData = connection.getMetaData();
String catalog = connection.getCatalog(); String catalog = connection.getCatalog();
@ -81,7 +81,7 @@ public class DbExporter extends JFrame {
} }
} }
private void exportTable() { protected void exportTable() {
String selectedTable = tableList.getSelectedValue(); String selectedTable = tableList.getSelectedValue();
if (selectedTable == null) { if (selectedTable == null) {
JOptionPane.showMessageDialog(this, "Please select a table to export."); JOptionPane.showMessageDialog(this, "Please select a table to export.");

View file

@ -48,12 +48,16 @@ public class ExporterFactory {
} }
} }
public Exportable getExporter(String format) throws Exception { public Exportable getExporter(String format) {
String className = exporterClassNames.get(format); String className = exporterClassNames.get(format);
if (className == null) { if (className == null) {
throw new IllegalArgumentException("Unsupported export format: " + format); throw new IllegalArgumentException("Unsupported export format: " + format);
} }
try {
return (Exportable) Class.forName(className).getDeclaredConstructor(Connection.class).newInstance(connection); return (Exportable) Class.forName(className).getDeclaredConstructor(Connection.class).newInstance(connection);
} catch (Exception e) {
throw new RuntimeException("Failed to create exporter instance", e);
}
} }
public List<String> getSupportedFormats() { public List<String> getSupportedFormats() {

View file

@ -1,38 +1,38 @@
package org.nmpl; package org.nmpl;
import junit.framework.Test; import org.fest.swing.fixture.FrameFixture;
import junit.framework.TestCase; import org.junit.jupiter.api.Test;
import junit.framework.TestSuite; import javax.swing.*;
import java.sql.SQLException;
/** import static org.fest.swing.core.matcher.JButtonMatcher.withText;
* Unit test for simple App. import static org.junit.jupiter.api.Assertions.assertTrue;
*/
public class AppTest
extends TestCase class AppTest {
{
/** private FrameFixture frame;
* Create the test case
* @Test
* @param testName name of the test case void setUp() {
*/ final String dbName = "testDb";
public AppTest( String testName ) final String username = "root";
{ final String password = "password";
super( testName ); 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 );
}
} }

View file

@ -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<String> supportedFormats = exporterFactory.getSupportedFormats();
assertNotNull(supportedFormats);
assertFalse(supportedFormats.isEmpty());
assertTrue(supportedFormats.contains("XML")); // Adjust based on your actual supported formats
}
}