Skip to content

Commit 16fd324

Browse files
committed
Added unit test for combo box and some ui changes.
1 parent 77e8d51 commit 16fd324

File tree

2 files changed

+98
-7
lines changed

2 files changed

+98
-7
lines changed

droid-swing-ui/src/main/java/uk/gov/nationalarchives/droid/gui/export/ExportDialog.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.LinkedList;
4747
import java.util.List;
4848
import java.util.stream.Collectors;
49+
import java.util.stream.Stream;
4950
import javax.swing.BorderFactory;
5051

5152
import javax.swing.ButtonGroup;
@@ -87,7 +88,7 @@ public class ExportDialog extends JDialog {
8788
private static final int CAPACITY = 128;
8889
private static final String EXPORT_TEMPLATE_FILE_EXTENSION = ".template";
8990

90-
private DroidMainFrame droidMain;
91+
private final DroidMainFrame droidMain;
9192
private DefaultTableModel tableModel;
9293
private List<ProfileWrapper> profilesRowData;
9394
private boolean approved;
@@ -205,7 +206,7 @@ public void setExportOptions(ExportOptions options) {
205206
* @return the profilesRowData
206207
*/
207208
public List<String> getSelectedProfileIds() {
208-
List<String> selectedProfiles = new ArrayList<String>();
209+
List<String> selectedProfiles = new ArrayList<>();
209210

210211
for (ProfileWrapper profileWrapper : profilesRowData) {
211212
if (profileWrapper.isSelected()) {
@@ -734,6 +735,8 @@ private void jButtonSetAllColumnsActionPerformed(ActionEvent evt) {//GEN-FIRST:e
734735
private void jCheckBoxUseTemplateStateChanged(ChangeEvent evt) {//GEN-FIRST:event_jCheckBoxUseTemplateStateChanged
735736
jPanelTemplateChoices.setVisible(jCheckBoxUseTemplate.isSelected());
736737
jPanelColumnChoices.setVisible(!jCheckBoxUseTemplate.isSelected());
738+
jButtonSetAllColumns.setEnabled(!jCheckBoxUseTemplate.isSelected());
739+
toggleColumnButton.setEnabled(!jCheckBoxUseTemplate.isSelected());
737740
}//GEN-LAST:event_jCheckBoxUseTemplateStateChanged
738741

739742
/**
@@ -884,11 +887,12 @@ private ComboBoxModel getOutputEncodings() {
884887
}
885888

886889
class ExportTemplateComboBoxItem implements Comparable{
887-
private String label;
888-
private Path templatePath;
890+
private final String label;
891+
private final Path templatePath;
889892

890893
ExportTemplateComboBoxItem(Path templateFilePath) {
891-
this.label = templateFilePath.getFileName().toString();
894+
String templateFileName = templateFilePath.getFileName().toString();
895+
this.label = templateFileName.substring(0, templateFileName.length() - EXPORT_TEMPLATE_FILE_EXTENSION.length());
892896
this.templatePath = templateFilePath;
893897
}
894898

@@ -917,8 +921,8 @@ public int compareTo(Object o) {
917921

918922
private ComboBoxModel getExportTemplatesModel() {
919923
List<ExportTemplateComboBoxItem> items = new LinkedList<>();
920-
try {
921-
List<Path> templates = Files.list(exportTemplatesFolder).collect(Collectors.toList());
924+
try (Stream<Path> fileStream = Files.list(exportTemplatesFolder)) {
925+
List<Path> templates = fileStream.collect(Collectors.toList());
922926
for (Path template : templates) {
923927
if (!Files.isDirectory(template) && (template.getFileName().toString().endsWith(EXPORT_TEMPLATE_FILE_EXTENSION))) {
924928
items.add(new ExportTemplateComboBoxItem(template));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2016, The National Archives <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following
7+
* conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of the The National Archives nor the
17+
* names of its contributors may be used to endorse or promote products
18+
* derived from this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package uk.gov.nationalarchives.droid.gui.util;
33+
34+
import org.junit.Test;
35+
36+
import java.util.Arrays;
37+
import java.util.List;
38+
39+
import static org.junit.Assert.assertEquals;
40+
41+
public class SortedComboBoxModelTest {
42+
@Test
43+
public void should_show_the_string_entries_in_sorted_order() {
44+
List<String> items = Arrays.asList("First Item", "0th item", "last item", "First item");
45+
SortedComboBoxModel<String> model = new SortedComboBoxModel<>(items);
46+
assertEquals("0th item", model.getElementAt(0));
47+
assertEquals("First Item", model.getElementAt(1));
48+
assertEquals("First item", model.getElementAt(2));
49+
assertEquals("last item", model.getElementAt(3));
50+
}
51+
52+
@Test
53+
public void should_show_the_numeric_entries_in_sorted_order() {
54+
List<Integer> items = Arrays.asList(12, 3, 4, 0);
55+
SortedComboBoxModel<Integer> model = new SortedComboBoxModel<>(items);
56+
assertEquals(0, model.getElementAt(0));
57+
assertEquals(3, model.getElementAt(1));
58+
assertEquals(4, model.getElementAt(2));
59+
assertEquals(12, model.getElementAt(3));
60+
}
61+
62+
@Test
63+
public void should_show_the_entries_based_on_custom_sorted_order_based_on_comparable_implemntation() {
64+
List<IntAsStringSortedItem> items = Arrays.asList(new IntAsStringSortedItem(12),
65+
new IntAsStringSortedItem(3),
66+
new IntAsStringSortedItem(4),
67+
new IntAsStringSortedItem(0));
68+
SortedComboBoxModel<IntAsStringSortedItem> model = new SortedComboBoxModel<>(items);
69+
assertEquals("0", ((IntAsStringSortedItem)model.getElementAt(0)).valAsString);
70+
assertEquals("12", ((IntAsStringSortedItem)model.getElementAt(1)).valAsString);
71+
assertEquals("3", ((IntAsStringSortedItem)model.getElementAt(2)).valAsString);
72+
assertEquals("4", ((IntAsStringSortedItem)model.getElementAt(3)).valAsString);
73+
}
74+
75+
static class IntAsStringSortedItem implements Comparable<Object> {
76+
private final String valAsString;
77+
78+
IntAsStringSortedItem(Integer someVal) {
79+
this.valAsString = someVal.toString();
80+
}
81+
@Override
82+
public int compareTo(Object o) {
83+
IntAsStringSortedItem that = (IntAsStringSortedItem) o;
84+
return this.valAsString.compareTo(that.valAsString);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)