Search in sources :

Example 1 with ExportMenuItemActionHandler

use of au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler in project constellation by constellation-app.

the class ExportMenuNGTest method verifyExportExcelAction.

/**
 * Verify that the passed event handler exports to Excel either the whole
 * table or just the selected rows.
 *
 * @param eventHandler the handler to test
 * @param expectedCopyOnlySelectedRows true if only the selected rows are
 *     expected to be exported, false otherwise
 * @param userCancelsRequest true if the user is meant to cancel the export when
 *     picking a file in the file chooser
 */
private void verifyExportExcelAction(final EventHandler<ActionEvent> eventHandler, final boolean userCancelsRequest, final boolean expectedExportOnlySelectedRows) throws InterruptedException, PluginException, ExecutionException {
    final ExportMenuItemActionHandler exportActionHandler = (ExportMenuItemActionHandler) eventHandler;
    final ExportMenuItemActionHandler spiedExportActionHandler = spy(exportActionHandler);
    final FileChooserBuilder exportFileChooser = mock(FileChooserBuilder.class);
    final File exportFile = userCancelsRequest ? null : new File("test.xlsx");
    final Optional<File> optionalExportFile = Optional.ofNullable(exportFile);
    doReturn(exportFileChooser).when(spiedExportActionHandler).getExportFileChooser();
    try (final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class);
        final MockedStatic<FileChooser> fileChooserMockedStatic = Mockito.mockStatic(FileChooser.class);
        final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
        // This is added so that the mocked static that we would otherwise be
        // trying to run in the fx thread is actually invoked properly
        platformMockedStatic.when(() -> Platform.runLater(any(Runnable.class))).thenAnswer(iom -> {
            ((Runnable) iom.getArgument(0)).run();
            return null;
        });
        fileChooserMockedStatic.when(() -> FileChooser.openSaveDialog(exportFileChooser)).thenReturn(CompletableFuture.completedFuture(optionalExportFile));
        final ActionEvent actionEvent = mock(ActionEvent.class);
        final Pagination pagination = mock(Pagination.class);
        final TableView<ObservableList<String>> tableView = mock(TableView.class);
        final int maxRowsPerPage = 42;
        final UserTablePreferences userTablePreferences = new UserTablePreferences();
        userTablePreferences.setMaxRowsPerPage(maxRowsPerPage);
        when(activeTableReference.getPagination()).thenReturn(pagination);
        when(table.getTableView()).thenReturn(tableView);
        when(activeTableReference.getUserTablePreferences()).thenReturn(userTablePreferences);
        final PluginExecution pluginExecution = mock(PluginExecution.class);
        pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(Plugin.class))).thenAnswer(mockitoInvocation -> {
            final ExportToExcelFilePlugin plugin = (ExportToExcelFilePlugin) mockitoInvocation.getArgument(0);
            if (exportFile != null) {
                assertEquals(exportFile.getAbsolutePath(), plugin.getFile().getAbsolutePath());
            } else {
                assertEquals(exportFile, plugin.getFile());
            }
            assertEquals(pagination, plugin.getPagination());
            assertEquals(tableView, plugin.getTable());
            assertEquals(42, plugin.getRowsPerPage());
            assertEquals(GRAPH_ID, plugin.getSheetName());
            assertEquals(expectedExportOnlySelectedRows, plugin.isSelectedOnly());
            return pluginExecution;
        });
        spiedExportActionHandler.handle(actionEvent);
        // Wait for the export job to complete
        spiedExportActionHandler.getLastExport().get();
        fileChooserMockedStatic.verify(() -> FileChooser.openSaveDialog(exportFileChooser));
        if (userCancelsRequest) {
            verifyNoInteractions(pluginExecution);
        } else {
            verify(pluginExecution).executeNow((Graph) null);
        }
        verify(actionEvent).consume();
    }
}
Also used : PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) Platform(javafx.application.Platform) ActionEvent(javafx.event.ActionEvent) ExportMenuItemActionHandler(au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler) ExportToExcelFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin) Pagination(javafx.scene.control.Pagination) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) ObservableList(javafx.collections.ObservableList) JFileChooser(javax.swing.JFileChooser) FileChooser(au.gov.asd.tac.constellation.utilities.gui.filechooser.FileChooser) FileChooserBuilder(org.openide.filesystems.FileChooserBuilder) File(java.io.File) ExportToExcelFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) ExportToCsvFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToCsvFilePlugin)

Example 2 with ExportMenuItemActionHandler

use of au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler in project constellation by constellation-app.

the class ExportMenuNGTest method exportFileChooser.

@Test
public void exportFileChooser() throws IOException {
    final String fileChooserTitle = "File Chooser Title";
    final String expectedFileExtension = ".json";
    final String fileChooserDescription = "File Filter Description";
    ExportMenuItemActionHandler handler = exportMenu.new ExportMenuItemActionHandler(fileChooserTitle, expectedFileExtension, fileChooserDescription, null);
    final JFileChooser fileChooser = handler.getExportFileChooser().createFileChooser();
    assertEquals(fileChooser.getDialogTitle(), fileChooserTitle);
    assertEquals(fileChooser.getChoosableFileFilters().length, 2);
    assertEquals(fileChooser.getChoosableFileFilters()[0].getDescription(), "All Files");
    assertEquals(fileChooser.getChoosableFileFilters()[1].getDescription(), fileChooserDescription);
    // File does not end with correct extension
    final File tmpFileInvalid = File.createTempFile("test", ".random");
    assertEquals(fileChooser.getChoosableFileFilters()[1].accept(tmpFileInvalid), false);
    // File does not exist
    assertEquals(fileChooser.getChoosableFileFilters()[1].accept(new File("/tmp/test" + expectedFileExtension)), false);
    // File is valid. Exists and ends with correct extension
    final File tmpFileValid = File.createTempFile("test", expectedFileExtension);
    assertEquals(fileChooser.getChoosableFileFilters()[1].accept(tmpFileValid), true);
    Files.deleteIfExists(tmpFileInvalid.toPath());
    Files.deleteIfExists(tmpFileValid.toPath());
}
Also used : JFileChooser(javax.swing.JFileChooser) ExportMenuItemActionHandler(au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler) File(java.io.File) Test(org.testng.annotations.Test)

Example 3 with ExportMenuItemActionHandler

use of au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler in project constellation by constellation-app.

the class ExportMenuNGTest method verifyExportCSVAction.

/**
 * Verify that the passed event handler exports to CSV either the whole
 * table or just the selected rows.
 *
 * @param eventHandler the handler to test
 * @param expectedCopyOnlySelectedRows true if only the selected rows are
 *     expected to be exported, false otherwise
 * @param userCancelsRequest true if the user is meant to cancel the export when
 *     picking a file in the file chooser
 */
private void verifyExportCSVAction(final EventHandler<ActionEvent> eventHandler, final boolean userCancelsRequest, final boolean expectedExportOnlySelectedRows) throws InterruptedException, PluginException, ExecutionException {
    final ExportMenuItemActionHandler exportActionHandler = (ExportMenuItemActionHandler) eventHandler;
    final ExportMenuItemActionHandler spiedExportActionHandler = spy(exportActionHandler);
    final FileChooserBuilder exportFileChooser = mock(FileChooserBuilder.class);
    final File exportFile = userCancelsRequest ? null : new File("test.csv");
    final Optional<File> optionalExportFile = Optional.ofNullable(exportFile);
    doReturn(exportFileChooser).when(spiedExportActionHandler).getExportFileChooser();
    try (final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class);
        final MockedStatic<FileChooser> fileChooserMockedStatic = Mockito.mockStatic(FileChooser.class);
        final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
        // This is added so that the mocked static that we would otherwise be
        // trying to run in the fx thread is actually invoked properly
        platformMockedStatic.when(() -> Platform.runLater(any(Runnable.class))).thenAnswer(iom -> {
            ((Runnable) iom.getArgument(0)).run();
            return null;
        });
        fileChooserMockedStatic.when(() -> FileChooser.openSaveDialog(exportFileChooser)).thenReturn(CompletableFuture.completedFuture(optionalExportFile));
        final ActionEvent actionEvent = mock(ActionEvent.class);
        final Pagination pagination = mock(Pagination.class);
        final TableView<ObservableList<String>> tableView = mock(TableView.class);
        when(activeTableReference.getPagination()).thenReturn(pagination);
        when(table.getTableView()).thenReturn(tableView);
        final PluginExecution pluginExecution = mock(PluginExecution.class);
        pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(Plugin.class))).thenAnswer(mockitoInvocation -> {
            final ExportToCsvFilePlugin plugin = (ExportToCsvFilePlugin) mockitoInvocation.getArgument(0);
            if (exportFile != null) {
                assertEquals(exportFile.getAbsolutePath(), plugin.getFile().getAbsolutePath());
            } else {
                assertEquals(exportFile, plugin.getFile());
            }
            assertEquals(pagination, plugin.getPagination());
            assertEquals(tableView, plugin.getTable());
            assertEquals(expectedExportOnlySelectedRows, plugin.isSelectedOnly());
            return pluginExecution;
        });
        spiedExportActionHandler.handle(actionEvent);
        // Wait for the export job to complete
        spiedExportActionHandler.getLastExport().get();
        fileChooserMockedStatic.verify(() -> FileChooser.openSaveDialog(exportFileChooser));
        if (userCancelsRequest) {
            verifyNoInteractions(pluginExecution);
        } else {
            verify(pluginExecution).executeNow((Graph) null);
        }
        verify(actionEvent).consume();
    }
}
Also used : PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) Platform(javafx.application.Platform) ActionEvent(javafx.event.ActionEvent) ExportMenuItemActionHandler(au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler) Pagination(javafx.scene.control.Pagination) ExportToCsvFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToCsvFilePlugin) ObservableList(javafx.collections.ObservableList) JFileChooser(javax.swing.JFileChooser) FileChooser(au.gov.asd.tac.constellation.utilities.gui.filechooser.FileChooser) FileChooserBuilder(org.openide.filesystems.FileChooserBuilder) File(java.io.File) ExportToExcelFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) ExportToCsvFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToCsvFilePlugin)

Aggregations

ExportMenuItemActionHandler (au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler)3 File (java.io.File)3 JFileChooser (javax.swing.JFileChooser)3 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)2 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)2 FileChooser (au.gov.asd.tac.constellation.utilities.gui.filechooser.FileChooser)2 ExportToCsvFilePlugin (au.gov.asd.tac.constellation.views.tableview.plugins.ExportToCsvFilePlugin)2 ExportToExcelFilePlugin (au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin)2 Platform (javafx.application.Platform)2 ObservableList (javafx.collections.ObservableList)2 ActionEvent (javafx.event.ActionEvent)2 Pagination (javafx.scene.control.Pagination)2 FileChooserBuilder (org.openide.filesystems.FileChooserBuilder)2 UserTablePreferences (au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences)1 Test (org.testng.annotations.Test)1