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