Auto cache cleaning + better err in ImageUtilsAnd
[nikiroo-utils.git] / src / be / nikiroo / utils / test / StringUtilsTest.java
CommitLineData
db31c358
NR
1package be.nikiroo.utils.test;
2
451f434b
NR
3import java.util.Date;
4import java.util.HashMap;
5import java.util.Map;
6import java.util.Map.Entry;
7
db31c358 8import be.nikiroo.utils.StringUtils;
451f434b 9import be.nikiroo.utils.StringUtils.Alignment;
db31c358
NR
10
11class StringUtilsTest extends TestLauncher {
12 public StringUtilsTest(String[] args) {
13 super("StringUtils test", args);
14
451f434b
NR
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.End);
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.Center);
88 if (size > data.length()) {
89 int before = 0;
90 for (int i = 0; i < result.length()
91 && result.charAt(i) == ' '; i++) {
92 before++;
93 }
94
95 int after = 0;
96 for (int i = result.length() - 1; i >= 0
97 && result.charAt(i) == ' '; i--) {
98 after++;
99 }
100
101 if (result.trim().isEmpty()) {
102 after = before / 2;
103 if (before > (2 * after)) {
104 before = after + 1;
105 } else {
106 before = after;
107 }
108 }
109
110 assertEquals(
111 "Padding a String on center should work as expected",
112 result.length(), before + data.length()
113 + after);
114 assertEquals(
115 "Padding a String on center should not uncenter the content",
116 true, Math.abs(before - after) <= 1);
117 }
118 }
119 }
120 }
121 });
122
123 addTest(new TestCase("unhtml") {
124 @Override
125 public void test() throws Exception {
126 Map<String, String> data = new HashMap<String, String>();
127 data.put("aa", "aa");
128 data.put("test with spaces ", "test with spaces ");
129 data.put("<a href='truc://target/'>link</a>", "link");
130 data.put("<html>Digimon</html>", "Digimon");
131 data.put("", "");
132 data.put(" ", " ");
133
134 for (Entry<String, String> entry : data.entrySet()) {
135 String result = StringUtils.unhtml(entry.getKey());
136 assertEquals("Result is not what we expected",
137 entry.getValue(), result);
138 }
139 }
140 });
141
db31c358
NR
142 addTest(new TestCase("zip64") {
143 @Override
144 public void test() throws Exception {
145 String orig = "test";
146 String zipped = StringUtils.zip64(orig);
147 String unzipped = StringUtils.unzip64(zipped);
148 assertEquals(orig, unzipped);
149 }
150 });
151 }
152}