Text justification: tests (WIP) + code (WIP)
[nikiroo-utils.git] / src / be / nikiroo / utils / test / StringUtilsTest.java
1 package be.nikiroo.utils.test;
2
3 import java.util.Arrays;
4 import java.util.Date;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Map.Entry;
9
10 import be.nikiroo.utils.StringUtils;
11 import be.nikiroo.utils.StringUtils.Alignment;
12
13 class StringUtilsTest extends TestLauncher {
14 public StringUtilsTest(String[] args) {
15 super("StringUtils test", args);
16
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,
81 Alignment.RIGHT);
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,
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);
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
143 addTest(new TestCase("Justifying") {
144 @Override
145 public void test() throws Exception {
146 Map<String, Map<Integer, Entry<Alignment, List<String>>>> source = new HashMap<String, Map<Integer, Entry<Alignment, List<String>>>>();
147 addValue(source, Alignment.LEFT, "testy", 5, "testy");
148 addValue(source, Alignment.LEFT, "testy", 3, "te-", "sty");
149 addValue(source, Alignment.LEFT,
150 "Un petit texte qui se mettra sur plusieurs lignes",
151 10, "Un petit", "texte qui", "se mettra", "sur",
152 "plusieurs", "lignes");
153 addValue(source, Alignment.LEFT,
154 "Un petit texte qui se mettra sur plusieurs lignes", 7,
155 "Un", "petit", "texte", "qui se", "mettra", "sur",
156 "plusie-", "urs", "lignes");
157 addValue(source, Alignment.RIGHT,
158 "Un petit texte qui se mettra sur plusieurs lignes", 7,
159 " Un", " petit", " texte", " qui se", " mettra",
160 " sur", "plusie-", " urs", " lignes");
161 addValue(source, Alignment.CENTER,
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.JUSTIFY,
166 "Un petit texte qui se mettra sur plusieurs lignes", 7,
167 "Un", "petit", "texte", "qui se", "mettra", "sur",
168 "plusie-", "urs", "lignes");
169 addValue(source, Alignment.JUSTIFY,
170 "Un petit texte qui se mettra sur plusieurs lignes",
171 14, "Un petit", "texte qui se",
172 "mettra sur", "plusieurs lig-", "nes");
173
174 System.out.println();
175 for (String data : source.keySet()) {
176 for (int size : source.get(data).keySet()) {
177 Alignment align = source.get(data).get(size).getKey();
178 List<String> values = source.get(data).get(size)
179 .getValue();
180
181 List<String> result = StringUtils.justifyText(data,
182 size, align);
183
184 System.out.println("[" + data + " (" + size + ")"
185 + "] -> [");
186 for (int i = 0; i < result.size(); i++) {
187 String resultLine = result.get(i);
188 System.out.println(i + ": " + resultLine);
189 }
190 System.out.println("]");
191
192 for (int i = 0; i < result.size() && i < values.size(); i++) {
193 assertEquals("The line " + i + " is not correct",
194 values.get(i), result.get(i));
195 }
196 }
197 }
198 System.out.println();
199 }
200 });
201
202 addTest(new TestCase("unhtml") {
203 @Override
204 public void test() throws Exception {
205 Map<String, String> data = new HashMap<String, String>();
206 data.put("aa", "aa");
207 data.put("test with spaces ", "test with spaces ");
208 data.put("<a href='truc://target/'>link</a>", "link");
209 data.put("<html>Digimon</html>", "Digimon");
210 data.put("", "");
211 data.put(" ", " ");
212
213 for (Entry<String, String> entry : data.entrySet()) {
214 String result = StringUtils.unhtml(entry.getKey());
215 assertEquals("Result is not what we expected",
216 entry.getValue(), result);
217 }
218 }
219 });
220
221 addTest(new TestCase("zip64") {
222 @Override
223 public void test() throws Exception {
224 String orig = "test";
225 String zipped = StringUtils.zip64(orig);
226 String unzipped = StringUtils.unzip64(zipped);
227 assertEquals(orig, unzipped);
228 }
229 });
230 }
231
232 static private void addValue(
233 Map<String, Map<Integer, Entry<Alignment, List<String>>>> source,
234 final Alignment align, String input, int size,
235 final String... result) {
236 if (!source.containsKey(input)) {
237 source.put(input,
238 new HashMap<Integer, Entry<Alignment, List<String>>>());
239 }
240
241 source.get(input).put(size, new Entry<Alignment, List<String>>() {
242 @Override
243 public Alignment getKey() {
244 return align;
245 }
246
247 @Override
248 public List<String> getValue() {
249 return Arrays.asList(result);
250 }
251
252 @Override
253 public List<String> setValue(List<String> value) {
254 return null;
255 }
256 });
257 }
258 }