1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.tools; |
17 | |
18 | import java.util.Arrays; |
19 | import java.util.Comparator; |
20 | |
21 | import junit.framework.TestCase; |
22 | |
23 | /** |
24 | * TestCase for NaturalCaseInsensitiveOrderComparator. |
25 | * |
26 | * @author Thomas Aglassinger |
27 | */ |
28 | public class NaturalCaseInsensitiveOrderComparatorTest extends TestCase |
29 | { |
30 | private StringTools stringTools; |
31 | |
32 | public NaturalCaseInsensitiveOrderComparatorTest(String arg0) { |
33 | super(arg0); |
34 | } |
35 | |
36 | /* |
37 | * @see TestCase#setUp() |
38 | */ |
39 | protected void setUp() |
40 | throws Exception { |
41 | super.setUp(); |
42 | TestTools.instance(); |
43 | stringTools = StringTools.instance(); |
44 | } |
45 | |
46 | public void testOrder() { |
47 | assertSorted(new String[]{"a", "b", "c"}, new String[]{"a", "b", "c"}); |
48 | assertSorted(new String[]{"a", "b", "c"}, new String[]{"a", "B", "c"}); |
49 | assertSorted(new String[]{"a", "b", "c"}, new String[]{"c", "b", "a"}); |
50 | assertSorted(new String[]{"a", "b", "c"}, new String[]{"C", "b", "A"}); |
51 | assertSorted( |
52 | new String[]{"v01c001", "v01c002", "v01c003"}, |
53 | new String[]{"v01c001", "v01c002", "v01c003"}); |
54 | assertSorted( |
55 | new String[]{"v01c001", "v01c002", "v01c003"}, |
56 | new String[]{"v01c003", "v01c001", "v01c002"}); |
57 | assertSorted( |
58 | new String[]{"v01c001", "V01c002", "v1c3", "v01c004", "v02c001"}, |
59 | new String[]{"v01c001", "V01c002", "v1c3", "v01c004", "v02c001"}); |
60 | } |
61 | |
62 | /* |
63 | * @see TestCase#tearDown() |
64 | */ |
65 | protected void tearDown() |
66 | throws Exception { |
67 | stringTools = null; |
68 | super.tearDown(); |
69 | } |
70 | |
71 | private void assertSorted(String[] expected, String[] actual) { |
72 | assert expected != null; |
73 | assert actual != null; |
74 | Comparator comparator = new NaturalCaseInsensitiveOrderComparator(); |
75 | |
76 | Arrays.sort(actual, comparator); |
77 | for (int i = 0; i < Math.max(expected.length, actual.length); i += 1) { |
78 | if (i == expected.length) { |
79 | assertTrue("no matching expected for actual at " + i + ": " + stringTools.sourced(actual[i]), false); |
80 | } |
81 | if (i == actual.length) { |
82 | assertTrue("no matching actual for expected at " + i + ": " + stringTools.sourced(expected[i]), false); |
83 | } |
84 | assertEquals(expected[i].toLowerCase(), actual[i].toLowerCase()); |
85 | } |
86 | } |
87 | |
88 | } |