From 16fd32400d0e061be7df32caf6bd6650b80518c0 Mon Sep 17 00:00:00 2001
From: sparkhi <parkhi@gmail.com>
Date: Wed, 13 Mar 2024 12:16:57 +0000
Subject: [PATCH] Added unit test for combo box and some ui changes.

---
 .../droid/gui/export/ExportDialog.java        | 18 ++--
 .../gui/util/SortedComboBoxModelTest.java     | 87 +++++++++++++++++++
 2 files changed, 98 insertions(+), 7 deletions(-)
 create mode 100644 droid-swing-ui/src/test/java/uk/gov/nationalarchives/droid/gui/util/SortedComboBoxModelTest.java

diff --git a/droid-swing-ui/src/main/java/uk/gov/nationalarchives/droid/gui/export/ExportDialog.java b/droid-swing-ui/src/main/java/uk/gov/nationalarchives/droid/gui/export/ExportDialog.java
index fc8a3cd72..139773350 100644
--- a/droid-swing-ui/src/main/java/uk/gov/nationalarchives/droid/gui/export/ExportDialog.java
+++ b/droid-swing-ui/src/main/java/uk/gov/nationalarchives/droid/gui/export/ExportDialog.java
@@ -46,6 +46,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import javax.swing.BorderFactory;
 
 import javax.swing.ButtonGroup;
@@ -87,7 +88,7 @@ public class ExportDialog extends JDialog {
     private static final int CAPACITY = 128;
     private static final String EXPORT_TEMPLATE_FILE_EXTENSION = ".template";
 
-    private DroidMainFrame droidMain;
+    private final DroidMainFrame droidMain;
     private DefaultTableModel tableModel;
     private List<ProfileWrapper> profilesRowData;
     private boolean approved;
@@ -205,7 +206,7 @@ public void setExportOptions(ExportOptions options) {
      * @return the profilesRowData
      */
     public List<String> getSelectedProfileIds() {
-        List<String> selectedProfiles = new ArrayList<String>();
+        List<String> selectedProfiles = new ArrayList<>();
         
         for (ProfileWrapper profileWrapper : profilesRowData) {
             if (profileWrapper.isSelected()) {
@@ -734,6 +735,8 @@ private void jButtonSetAllColumnsActionPerformed(ActionEvent evt) {//GEN-FIRST:e
     private void jCheckBoxUseTemplateStateChanged(ChangeEvent evt) {//GEN-FIRST:event_jCheckBoxUseTemplateStateChanged
         jPanelTemplateChoices.setVisible(jCheckBoxUseTemplate.isSelected());
         jPanelColumnChoices.setVisible(!jCheckBoxUseTemplate.isSelected());
+        jButtonSetAllColumns.setEnabled(!jCheckBoxUseTemplate.isSelected());
+        toggleColumnButton.setEnabled(!jCheckBoxUseTemplate.isSelected());
     }//GEN-LAST:event_jCheckBoxUseTemplateStateChanged
 
     /**
@@ -884,11 +887,12 @@ private ComboBoxModel getOutputEncodings() {
     }
 
     class ExportTemplateComboBoxItem implements Comparable{
-        private String label;
-        private Path templatePath;
+        private final String label;
+        private final Path templatePath;
 
         ExportTemplateComboBoxItem(Path templateFilePath) {
-            this.label = templateFilePath.getFileName().toString();
+            String templateFileName = templateFilePath.getFileName().toString();
+            this.label = templateFileName.substring(0, templateFileName.length() - EXPORT_TEMPLATE_FILE_EXTENSION.length());
             this.templatePath = templateFilePath;
         }
 
@@ -917,8 +921,8 @@ public int compareTo(Object o) {
 
     private ComboBoxModel getExportTemplatesModel() {
         List<ExportTemplateComboBoxItem> items = new LinkedList<>();
-        try {
-            List<Path> templates = Files.list(exportTemplatesFolder).collect(Collectors.toList());
+        try (Stream<Path> fileStream = Files.list(exportTemplatesFolder)) {
+            List<Path> templates = fileStream.collect(Collectors.toList());
             for (Path template : templates) {
                 if (!Files.isDirectory(template) && (template.getFileName().toString().endsWith(EXPORT_TEMPLATE_FILE_EXTENSION))) {
                     items.add(new ExportTemplateComboBoxItem(template));
diff --git a/droid-swing-ui/src/test/java/uk/gov/nationalarchives/droid/gui/util/SortedComboBoxModelTest.java b/droid-swing-ui/src/test/java/uk/gov/nationalarchives/droid/gui/util/SortedComboBoxModelTest.java
new file mode 100644
index 000000000..ab4428d78
--- /dev/null
+++ b/droid-swing-ui/src/test/java/uk/gov/nationalarchives/droid/gui/util/SortedComboBoxModelTest.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2016, The National Archives <pronom@nationalarchives.gov.uk>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ *  * Neither the name of the The National Archives nor the
+ *    names of its contributors may be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package uk.gov.nationalarchives.droid.gui.util;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class SortedComboBoxModelTest {
+    @Test
+    public void should_show_the_string_entries_in_sorted_order() {
+        List<String> items = Arrays.asList("First Item", "0th item", "last item", "First item");
+        SortedComboBoxModel<String> model = new SortedComboBoxModel<>(items);
+        assertEquals("0th item", model.getElementAt(0));
+        assertEquals("First Item", model.getElementAt(1));
+        assertEquals("First item", model.getElementAt(2));
+        assertEquals("last item", model.getElementAt(3));
+    }
+
+    @Test
+    public void should_show_the_numeric_entries_in_sorted_order() {
+        List<Integer> items = Arrays.asList(12, 3, 4, 0);
+        SortedComboBoxModel<Integer> model = new SortedComboBoxModel<>(items);
+        assertEquals(0, model.getElementAt(0));
+        assertEquals(3, model.getElementAt(1));
+        assertEquals(4, model.getElementAt(2));
+        assertEquals(12, model.getElementAt(3));
+    }
+
+    @Test
+    public void should_show_the_entries_based_on_custom_sorted_order_based_on_comparable_implemntation() {
+        List<IntAsStringSortedItem> items = Arrays.asList(new IntAsStringSortedItem(12),
+                new IntAsStringSortedItem(3),
+                new IntAsStringSortedItem(4),
+                new IntAsStringSortedItem(0));
+        SortedComboBoxModel<IntAsStringSortedItem> model = new SortedComboBoxModel<>(items);
+        assertEquals("0", ((IntAsStringSortedItem)model.getElementAt(0)).valAsString);
+        assertEquals("12", ((IntAsStringSortedItem)model.getElementAt(1)).valAsString);
+        assertEquals("3", ((IntAsStringSortedItem)model.getElementAt(2)).valAsString);
+        assertEquals("4", ((IntAsStringSortedItem)model.getElementAt(3)).valAsString);
+    }
+
+    static class IntAsStringSortedItem implements Comparable<Object> {
+        private final String valAsString;
+
+        IntAsStringSortedItem(Integer someVal) {
+            this.valAsString = someVal.toString();
+        }
+        @Override
+        public int compareTo(Object o) {
+            IntAsStringSortedItem that = (IntAsStringSortedItem) o;
+            return this.valAsString.compareTo(that.valAsString);
+        }
+    }
+}
\ No newline at end of file