Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.test; |
2 | ||
771c4ba4 | 3 | import java.util.Arrays; |
451f434b NR |
4 | import java.util.Date; |
5 | import java.util.HashMap; | |
6a493e05 | 6 | import java.util.List; |
451f434b NR |
7 | import java.util.Map; |
8 | import java.util.Map.Entry; | |
9 | ||
db31c358 | 10 | import be.nikiroo.utils.StringUtils; |
451f434b | 11 | import be.nikiroo.utils.StringUtils.Alignment; |
db31c358 NR |
12 | |
13 | class StringUtilsTest extends TestLauncher { | |
14 | public StringUtilsTest(String[] args) { | |
15 | super("StringUtils test", args); | |
16 | ||
451f434b NR |
17 | addTest(new TestCase("Time serialisation") { |
18 | @Override | |
19 | public void test() throws Exception { | |
20 | for (long fullTime : new Long[] { 0l, 123456l, 123456000l, | |
21 | new Date().getTime() }) { | |
22 | // precise to the second, no more | |
23 | long time = (fullTime / 1000) * 1000; | |
24 | ||
25 | String displayTime = StringUtils.fromTime(time); | |
26 | assertNotNull("The stringified time for " + time | |
27 | + " should not be null", displayTime); | |
28 | assertEquals("The stringified time for " + time | |
29 | + " should not be empty", false, displayTime.trim() | |
30 | .isEmpty()); | |
31 | ||
32 | assertEquals("The time " + time | |
33 | + " should be loop-convertable", time, | |
34 | StringUtils.toTime(displayTime)); | |
35 | ||
36 | assertEquals("The time " + displayTime | |
37 | + " should be loop-convertable", displayTime, | |
38 | StringUtils.fromTime(StringUtils | |
39 | .toTime(displayTime))); | |
40 | } | |
41 | } | |
42 | }); | |
43 | ||
44 | addTest(new TestCase("MD5") { | |
45 | @Override | |
46 | public void test() throws Exception { | |
47 | String mess = "The String we got is not what 'md5sum' said it should heve been"; | |
48 | assertEquals(mess, "34ded48fcff4221d644be9a37e1cb1d9", | |
49 | StringUtils.getMd5Hash("fanfan la tulipe")); | |
50 | assertEquals(mess, "7691b0cb74ed0f94b4d8cd858abe1165", | |
51 | StringUtils.getMd5Hash("je te do-o-o-o-o-o-nne")); | |
52 | } | |
53 | }); | |
54 | ||
55 | addTest(new TestCase("Padding") { | |
56 | @Override | |
57 | public void test() throws Exception { | |
58 | for (String data : new String[] { "fanfan", "la tulipe", | |
59 | "1234567890", "12345678901234567890", "1", "" }) { | |
60 | String result = StringUtils.padString(data, -1); | |
61 | assertEquals("A size of -1 is expected to produce a noop", | |
62 | true, data.equals(result)); | |
63 | for (int size : new Integer[] { 0, 1, 5, 10, 40 }) { | |
64 | result = StringUtils.padString(data, size); | |
65 | assertEquals( | |
66 | "Padding a String at a certain size should give a String of the given size", | |
67 | size, result.length()); | |
68 | assertEquals( | |
69 | "Padding a String should not change the content", | |
70 | true, data.trim().startsWith(result.trim())); | |
71 | ||
72 | result = StringUtils.padString(data, size, false, null); | |
73 | assertEquals( | |
74 | "Padding a String without cutting should not shorten the String", | |
75 | true, data.length() <= result.length()); | |
76 | assertEquals( | |
77 | "Padding a String without cutting should keep the whole content", | |
78 | true, data.trim().equals(result.trim())); | |
79 | ||
80 | result = StringUtils.padString(data, size, false, | |
cc3e7291 | 81 | Alignment.RIGHT); |
451f434b NR |
82 | if (size > data.length()) { |
83 | assertEquals( | |
84 | "Padding a String to the end should work as expected", | |
85 | true, result.endsWith(data)); | |
86 | } | |
87 | ||
88 | result = StringUtils.padString(data, size, false, | |
cc3e7291 NR |
89 | Alignment.JUSTIFY); |
90 | if (size > data.length()) { | |
91 | String unspacedData = data.trim(); | |
92 | String unspacedResult = result.trim(); | |
93 | for (int i = 0; i < size; i++) { | |
94 | unspacedData = unspacedData.replace(" ", " "); | |
95 | unspacedResult = unspacedResult.replace(" ", | |
96 | " "); | |
97 | } | |
98 | ||
99 | assertEquals( | |
100 | "Justified text trimmed with all spaces collapsed " | |
101 | + "sould be identical to original text " | |
102 | + "trimmed with all spaces collapsed", | |
103 | unspacedData, unspacedResult); | |
104 | } | |
105 | ||
106 | result = StringUtils.padString(data, size, false, | |
107 | Alignment.CENTER); | |
451f434b NR |
108 | if (size > data.length()) { |
109 | int before = 0; | |
110 | for (int i = 0; i < result.length() | |
111 | && result.charAt(i) == ' '; i++) { | |
112 | before++; | |
113 | } | |
114 | ||
115 | int after = 0; | |
116 | for (int i = result.length() - 1; i >= 0 | |
117 | && result.charAt(i) == ' '; i--) { | |
118 | after++; | |
119 | } | |
120 | ||
121 | if (result.trim().isEmpty()) { | |
122 | after = before / 2; | |
123 | if (before > (2 * after)) { | |
124 | before = after + 1; | |
125 | } else { | |
126 | before = after; | |
127 | } | |
128 | } | |
129 | ||
130 | assertEquals( | |
131 | "Padding a String on center should work as expected", | |
132 | result.length(), before + data.length() | |
133 | + after); | |
134 | assertEquals( | |
135 | "Padding a String on center should not uncenter the content", | |
136 | true, Math.abs(before - after) <= 1); | |
137 | } | |
138 | } | |
139 | } | |
140 | } | |
141 | }); | |
142 | ||
cc3e7291 NR |
143 | addTest(new TestCase("Justifying") { |
144 | @Override | |
145 | public void test() throws Exception { | |
771c4ba4 | 146 | Map<String, Map<Integer, Entry<Alignment, List<String>>>> source = new HashMap<String, Map<Integer, Entry<Alignment, List<String>>>>(); |
620f7329 NR |
147 | addValue(source, Alignment.LEFT, "testy", -1, "testy"); |
148 | addValue(source, Alignment.RIGHT, "testy", -1, "testy"); | |
149 | addValue(source, Alignment.CENTER, "testy", -1, "testy"); | |
150 | addValue(source, Alignment.JUSTIFY, "testy", -1, "testy"); | |
771c4ba4 NR |
151 | addValue(source, Alignment.LEFT, "testy", 5, "testy"); |
152 | addValue(source, Alignment.LEFT, "testy", 3, "te-", "sty"); | |
153 | addValue(source, Alignment.LEFT, | |
154 | "Un petit texte qui se mettra sur plusieurs lignes", | |
155 | 10, "Un petit", "texte qui", "se mettra", "sur", | |
156 | "plusieurs", "lignes"); | |
157 | addValue(source, Alignment.LEFT, | |
158 | "Un petit texte qui se mettra sur plusieurs lignes", 7, | |
159 | "Un", "petit", "texte", "qui se", "mettra", "sur", | |
160 | "plusie-", "urs", "lignes"); | |
161 | addValue(source, Alignment.RIGHT, | |
162 | "Un petit texte qui se mettra sur plusieurs lignes", 7, | |
163 | " Un", " petit", " texte", " qui se", " mettra", | |
164 | " sur", "plusie-", " urs", " lignes"); | |
165 | addValue(source, Alignment.CENTER, | |
166 | "Un petit texte qui se mettra sur plusieurs lignes", 7, | |
167 | " Un ", " petit ", " texte ", "qui se ", "mettra ", | |
168 | " sur ", "plusie-", " urs ", "lignes "); | |
169 | addValue(source, Alignment.JUSTIFY, | |
170 | "Un petit texte qui se mettra sur plusieurs lignes", 7, | |
620f7329 NR |
171 | "Un pet-", "it tex-", "te qui", "se met-", "tra sur", |
172 | "plusie-", "urs li-", "gnes"); | |
771c4ba4 NR |
173 | addValue(source, Alignment.JUSTIFY, |
174 | "Un petit texte qui se mettra sur plusieurs lignes", | |
175 | 14, "Un petit", "texte qui se", | |
176 | "mettra sur", "plusieurs lig-", "nes"); | |
177 | ||
771c4ba4 NR |
178 | for (String data : source.keySet()) { |
179 | for (int size : source.get(data).keySet()) { | |
180 | Alignment align = source.get(data).get(size).getKey(); | |
181 | List<String> values = source.get(data).get(size) | |
182 | .getValue(); | |
183 | ||
184 | List<String> result = StringUtils.justifyText(data, | |
185 | size, align); | |
186 | ||
620f7329 NR |
187 | // System.out.println("[" + data + " (" + size + ")" + |
188 | // "] -> ["); | |
189 | // for (int i = 0; i < result.size(); i++) { | |
190 | // String resultLine = result.get(i); | |
191 | // System.out.println(i + ": " + resultLine); | |
192 | // } | |
193 | // System.out.println("]"); | |
6a493e05 | 194 | |
620f7329 | 195 | assertEquals(values, result); |
771c4ba4 | 196 | } |
cc3e7291 NR |
197 | } |
198 | } | |
199 | }); | |
200 | ||
451f434b NR |
201 | addTest(new TestCase("unhtml") { |
202 | @Override | |
203 | public void test() throws Exception { | |
204 | Map<String, String> data = new HashMap<String, String>(); | |
205 | data.put("aa", "aa"); | |
206 | data.put("test with spaces ", "test with spaces "); | |
207 | data.put("<a href='truc://target/'>link</a>", "link"); | |
208 | data.put("<html>Digimon</html>", "Digimon"); | |
209 | data.put("", ""); | |
210 | data.put(" ", " "); | |
211 | ||
212 | for (Entry<String, String> entry : data.entrySet()) { | |
213 | String result = StringUtils.unhtml(entry.getKey()); | |
214 | assertEquals("Result is not what we expected", | |
215 | entry.getValue(), result); | |
216 | } | |
217 | } | |
218 | }); | |
219 | ||
db31c358 NR |
220 | addTest(new TestCase("zip64") { |
221 | @Override | |
222 | public void test() throws Exception { | |
223 | String orig = "test"; | |
224 | String zipped = StringUtils.zip64(orig); | |
225 | String unzipped = StringUtils.unzip64(zipped); | |
226 | assertEquals(orig, unzipped); | |
227 | } | |
228 | }); | |
229 | } | |
771c4ba4 NR |
230 | |
231 | static private void addValue( | |
232 | Map<String, Map<Integer, Entry<Alignment, List<String>>>> source, | |
233 | final Alignment align, String input, int size, | |
234 | final String... result) { | |
235 | if (!source.containsKey(input)) { | |
236 | source.put(input, | |
237 | new HashMap<Integer, Entry<Alignment, List<String>>>()); | |
238 | } | |
239 | ||
240 | source.get(input).put(size, new Entry<Alignment, List<String>>() { | |
241 | @Override | |
242 | public Alignment getKey() { | |
243 | return align; | |
244 | } | |
245 | ||
246 | @Override | |
247 | public List<String> getValue() { | |
248 | return Arrays.asList(result); | |
249 | } | |
250 | ||
251 | @Override | |
252 | public List<String> setValue(List<String> value) { | |
253 | return null; | |
254 | } | |
255 | }); | |
256 | } | |
db31c358 | 257 | } |