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 junit.framework.TestCase; |
19 | import org.apache.commons.logging.Log; |
20 | import org.apache.commons.logging.LogFactory; |
21 | |
22 | /** |
23 | * TestCase for NumberedName. |
24 | * |
25 | * @author Thomas Aglassinger |
26 | */ |
27 | public class NumberedNameTest extends TestCase |
28 | { |
29 | private static Log logger = LogFactory.getLog(NumberedNameTest.class); |
30 | private StringTools stringTools; |
31 | |
32 | protected void setUp() |
33 | throws Exception { |
34 | super.setUp(); |
35 | stringTools = StringTools.instance(); |
36 | } |
37 | |
38 | public void testApiDocumentationExamples() { |
39 | testNumberedName("images/hugo00title.png", "images/", "hugo00title", "", ".png"); |
40 | testNumberedName("images/hugo12.png", "images/", "hugo", "12", ".png"); |
41 | testNumberedName("images/hugo12-13.png", "images/", "hugo", "12-13", ".png"); |
42 | } |
43 | |
44 | public void testBrokenNumberedName() { |
45 | testBrokenNumberedName("n"); |
46 | testBrokenNumberedName("n/"); |
47 | } |
48 | |
49 | public void testDoublePages() { |
50 | testNumberedName("p/n1-2.s", "p/", "n", "1-2", ".s"); |
51 | testNumberedName("p/n1+2.s", "p/", "n", "1+2", ".s"); |
52 | testNumberedName("p/n1_2.s", "p/", "n", "1_2", ".s"); |
53 | testNumberedName("p/n2-1.s", "p/", "n", "2-1", ".s"); |
54 | |
55 | testNumberedName("p/n1 2.s", "p/", "n1 ", "2", ".s"); |
56 | testNumberedName("p/n4+2.s", "p/", "n4+", "2", ".s"); |
57 | } |
58 | |
59 | public void testSplitComicImageName() { |
60 | // Abbreviations used here: p=path, n=name-prefix, s=suffix. Note that it is important to |
61 | // prefer single-letter test data because they make it easier to find off-by-one errors and |
62 | // ArrayIndexOutOfBoundsExceptions. |
63 | testNumberedName("p/n1.s", "p/", "n", "1", ".s"); |
64 | testNumberedName("path/name0023.suffix", "path/", "name", "0023", ".suffix"); |
65 | testNumberedName("p/r/s/t/n1.s", "p/r/s/t/", "n", "1", ".s"); |
66 | testNumberedName("n1.s", "", "n", "1", ".s"); |
67 | testNumberedName(".s", "", "", "", ".s"); |
68 | testNumberedName("n.", "", "n", "", "."); |
69 | testNumberedName(".", "", "", "", "."); |
70 | } |
71 | |
72 | public void testUsesPotentiallyMoronicNumbering() { |
73 | testUsesPotentiallyMoronicNumbering("p/n1.s", false); |
74 | testUsesPotentiallyMoronicNumbering("p/n01.s", true); |
75 | testUsesPotentiallyMoronicNumbering("p/n0910.s", true); |
76 | testUsesPotentiallyMoronicNumbering("p/n0911.s", false); |
77 | testUsesPotentiallyMoronicNumbering("p/n09+10.s", false); |
78 | testUsesPotentiallyMoronicNumbering(".s", false); |
79 | |
80 | // Explaination: prefix="n9+", page="10" |
81 | testUsesPotentiallyMoronicNumbering("p/n9+10.s", true); |
82 | } |
83 | |
84 | protected void tearDown() |
85 | throws Exception { |
86 | stringTools = null; |
87 | super.tearDown(); |
88 | } |
89 | |
90 | private void testBrokenNumberedName(String fileName) { |
91 | try { |
92 | new NumberedName(fileName); |
93 | fail("file name must result in IllegalArgumentException: " |
94 | + stringTools.sourced(fileName)); |
95 | } catch (IllegalArgumentException error) { |
96 | if (logger.isDebugEnabled()) { |
97 | logger.debug("ignoring expected exception", error); |
98 | } |
99 | } |
100 | } |
101 | |
102 | private void testNumberedName( |
103 | String source, String expectedPath, String expectedPrefix, String expectedPage, String expectedSuffix) { |
104 | assert source != null; |
105 | assert expectedPath != null; |
106 | assert expectedPrefix != null; |
107 | assert expectedPage != null; |
108 | assert expectedSuffix != null; |
109 | NumberedName actual = new NumberedName(source); |
110 | |
111 | assertEquals("path", expectedPath, actual.getPath()); |
112 | assertEquals("prefix", expectedPrefix, actual.getPrefix()); |
113 | assertEquals("page", expectedPage, actual.getPage()); |
114 | assertEquals("suffix", expectedSuffix, actual.getSuffix()); |
115 | } |
116 | |
117 | private void testUsesPotentiallyMoronicNumbering(String source, boolean expected) { |
118 | assert source != null; |
119 | NumberedName numberedName = new NumberedName(source); |
120 | |
121 | assertEquals("usesPotentiallyMoronicNumbering for " + stringTools.sourced(source) |
122 | + " yielding " + stringTools.sourced(numberedName.getPage()), |
123 | expected, numberedName.usesPotentiallyMoronicNumbering()); |
124 | } |
125 | } |