new tests (wip) for from/to number
[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", -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");
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,
171 "Un pet-", "it tex-", "te qui", "se met-", "tra sur",
172 "plusie-", "urs li-", "gnes");
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 addValue(source, Alignment.JUSTIFY, "le dash-test", 9,
178 "le dash-", "test");
179
180 for (String data : source.keySet()) {
181 for (int size : source.get(data).keySet()) {
182 Alignment align = source.get(data).get(size).getKey();
183 List<String> values = source.get(data).get(size)
184 .getValue();
185
186 List<String> result = StringUtils.justifyText(data,
187 size, align);
188
189 // System.out.println("[" + data + " (" + size + ")" +
190 // "] -> [");
191 // for (int i = 0; i < result.size(); i++) {
192 // String resultLine = result.get(i);
193 // System.out.println(i + ": " + resultLine);
194 // }
195 // System.out.println("]");
196
197 assertEquals(values, result);
198 }
199 }
200 }
201 });
202
203 addTest(new TestCase("unhtml") {
204 @Override
205 public void test() throws Exception {
206 Map<String, String> data = new HashMap<String, String>();
207 data.put("aa", "aa");
208 data.put("test with spaces ", "test with spaces ");
209 data.put("<a href='truc://target/'>link</a>", "link");
210 data.put("<html>Digimon</html>", "Digimon");
211 data.put("", "");
212 data.put(" ", " ");
213
214 for (Entry<String, String> entry : data.entrySet()) {
215 String result = StringUtils.unhtml(entry.getKey());
216 assertEquals("Result is not what we expected",
217 entry.getValue(), result);
218 }
219 }
220 });
221
222 addTest(new TestCase("zip64") {
223 @Override
224 public void test() throws Exception {
225 String orig = "test";
226 String zipped = StringUtils.base64(orig, true);
227 String unzipped = StringUtils.unbase64s(zipped, true);
228 assertEquals(orig, unzipped);
229 }
230 });
231
232 addTest(new TestCase("from/toNumber") {
233 @Override
234 public void test() throws Exception {
235 assertEquals(StringUtils.toNumber("263"), 263l);
236 assertEquals(StringUtils.toNumber("21200"), 21200l);
237 assertEquals(StringUtils.toNumber("0"), 0l);
238 assertEquals(StringUtils.formatNumber(263l), "263");
239 assertEquals(StringUtils.formatNumber(21000l), "21k");
240 assertEquals(StringUtils.formatNumber(0l), "0");
241
242 assertEquals(StringUtils.formatNumber(1287l, false), "1287");
243 assertEquals(StringUtils.formatNumber(6056l, false), "6k");
244
245 assertEquals(StringUtils.toNumber("263k"), 263000l);
246 assertEquals(StringUtils.toNumber("42k"), 42000l);
247 assertEquals(StringUtils.toNumber("12M"), 12000000l);
248 assertEquals(StringUtils.formatNumber(263012l), "263k");
249 assertEquals(StringUtils.formatNumber(42012l), "42k");
250 assertEquals(StringUtils.formatNumber(12012121212l), "12M");
251
252 assertEquals(StringUtils.toNumber("263.2k"), 263200l);
253 assertEquals(StringUtils.toNumber("1.2k"), 1200l);
254 assertEquals(StringUtils.toNumber("42.7M"), 42700000000l);
255 assertEquals(StringUtils.formatNumber(263202l), "263.2k");
256 assertEquals(StringUtils.formatNumber(1267l), "1.2k");
257 assertEquals(StringUtils.formatNumber(42712121212l), "42.7M");
258 }
259 });
260 }
261
262 static private void addValue(
263 Map<String, Map<Integer, Entry<Alignment, List<String>>>> source,
264 final Alignment align, String input, int size,
265 final String... result) {
266 if (!source.containsKey(input)) {
267 source.put(input,
268 new HashMap<Integer, Entry<Alignment, List<String>>>());
269 }
270
271 source.get(input).put(size, new Entry<Alignment, List<String>>() {
272 @Override
273 public Alignment getKey() {
274 return align;
275 }
276
277 @Override
278 public List<String> getValue() {
279 return Arrays.asList(result);
280 }
281
282 @Override
283 public List<String> setValue(List<String> value) {
284 return null;
285 }
286 });
287 }
288 }