274e5e881bf7bcf676bdbabda8ff6abe0b6f7e14
[nikiroo-utils.git] / src / be / nikiroo / utils / test / StringUtilsTest.java
1 package be.nikiroo.utils.test;
2
3 import java.util.Date;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Map.Entry;
7
8 import be.nikiroo.utils.StringUtils;
9 import be.nikiroo.utils.StringUtils.Alignment;
10
11 class StringUtilsTest extends TestLauncher {
12 public StringUtilsTest(String[] args) {
13 super("StringUtils test", args);
14
15 addTest(new TestCase("Time serialisation") {
16 @Override
17 public void test() throws Exception {
18 for (long fullTime : new Long[] { 0l, 123456l, 123456000l,
19 new Date().getTime() }) {
20 // precise to the second, no more
21 long time = (fullTime / 1000) * 1000;
22
23 String displayTime = StringUtils.fromTime(time);
24 assertNotNull("The stringified time for " + time
25 + " should not be null", displayTime);
26 assertEquals("The stringified time for " + time
27 + " should not be empty", false, displayTime.trim()
28 .isEmpty());
29
30 assertEquals("The time " + time
31 + " should be loop-convertable", time,
32 StringUtils.toTime(displayTime));
33
34 assertEquals("The time " + displayTime
35 + " should be loop-convertable", displayTime,
36 StringUtils.fromTime(StringUtils
37 .toTime(displayTime)));
38 }
39 }
40 });
41
42 addTest(new TestCase("MD5") {
43 @Override
44 public void test() throws Exception {
45 String mess = "The String we got is not what 'md5sum' said it should heve been";
46 assertEquals(mess, "34ded48fcff4221d644be9a37e1cb1d9",
47 StringUtils.getMd5Hash("fanfan la tulipe"));
48 assertEquals(mess, "7691b0cb74ed0f94b4d8cd858abe1165",
49 StringUtils.getMd5Hash("je te do-o-o-o-o-o-nne"));
50 }
51 });
52
53 addTest(new TestCase("Padding") {
54 @Override
55 public void test() throws Exception {
56 for (String data : new String[] { "fanfan", "la tulipe",
57 "1234567890", "12345678901234567890", "1", "" }) {
58 String result = StringUtils.padString(data, -1);
59 assertEquals("A size of -1 is expected to produce a noop",
60 true, data.equals(result));
61 for (int size : new Integer[] { 0, 1, 5, 10, 40 }) {
62 result = StringUtils.padString(data, size);
63 assertEquals(
64 "Padding a String at a certain size should give a String of the given size",
65 size, result.length());
66 assertEquals(
67 "Padding a String should not change the content",
68 true, data.trim().startsWith(result.trim()));
69
70 result = StringUtils.padString(data, size, false, null);
71 assertEquals(
72 "Padding a String without cutting should not shorten the String",
73 true, data.length() <= result.length());
74 assertEquals(
75 "Padding a String without cutting should keep the whole content",
76 true, data.trim().equals(result.trim()));
77
78 result = StringUtils.padString(data, size, false,
79 Alignment.RIGHT);
80 if (size > data.length()) {
81 assertEquals(
82 "Padding a String to the end should work as expected",
83 true, result.endsWith(data));
84 }
85
86 result = StringUtils.padString(data, size, false,
87 Alignment.JUSTIFY);
88 if (size > data.length()) {
89 String unspacedData = data.trim();
90 String unspacedResult = result.trim();
91 for (int i = 0; i < size; i++) {
92 unspacedData = unspacedData.replace(" ", " ");
93 unspacedResult = unspacedResult.replace(" ",
94 " ");
95 }
96
97 assertEquals(
98 "Justified text trimmed with all spaces collapsed "
99 + "sould be identical to original text "
100 + "trimmed with all spaces collapsed",
101 unspacedData, unspacedResult);
102 }
103
104 result = StringUtils.padString(data, size, false,
105 Alignment.CENTER);
106 if (size > data.length()) {
107 int before = 0;
108 for (int i = 0; i < result.length()
109 && result.charAt(i) == ' '; i++) {
110 before++;
111 }
112
113 int after = 0;
114 for (int i = result.length() - 1; i >= 0
115 && result.charAt(i) == ' '; i--) {
116 after++;
117 }
118
119 if (result.trim().isEmpty()) {
120 after = before / 2;
121 if (before > (2 * after)) {
122 before = after + 1;
123 } else {
124 before = after;
125 }
126 }
127
128 assertEquals(
129 "Padding a String on center should work as expected",
130 result.length(), before + data.length()
131 + after);
132 assertEquals(
133 "Padding a String on center should not uncenter the content",
134 true, Math.abs(before - after) <= 1);
135 }
136 }
137 }
138 }
139 });
140
141 addTest(new TestCase("Justifying") {
142 @Override
143 public void test() throws Exception {
144 for (String data : new String[] {}) {
145 // TODO: test it!
146 // String result = StringUtils.justifyText(data, 5,
147 // StringUtils.Alignment.LEFT);
148 }
149 }
150 });
151
152 addTest(new TestCase("unhtml") {
153 @Override
154 public void test() throws Exception {
155 Map<String, String> data = new HashMap<String, String>();
156 data.put("aa", "aa");
157 data.put("test with spaces ", "test with spaces ");
158 data.put("<a href='truc://target/'>link</a>", "link");
159 data.put("<html>Digimon</html>", "Digimon");
160 data.put("", "");
161 data.put(" ", " ");
162
163 for (Entry<String, String> entry : data.entrySet()) {
164 String result = StringUtils.unhtml(entry.getKey());
165 assertEquals("Result is not what we expected",
166 entry.getValue(), result);
167 }
168 }
169 });
170
171 addTest(new TestCase("zip64") {
172 @Override
173 public void test() throws Exception {
174 String orig = "test";
175 String zipped = StringUtils.zip64(orig);
176 String unzipped = StringUtils.unzip64(zipped);
177 assertEquals(orig, unzipped);
178 }
179 });
180 }
181 }