452b9061185f9838b2145fb74c7e533bb99d0c11
1 package be
.nikiroo
.utils
.test
;
4 import java
.util
.HashMap
;
7 import java
.util
.Map
.Entry
;
9 import be
.nikiroo
.utils
.StringUtils
;
10 import be
.nikiroo
.utils
.StringUtils
.Alignment
;
12 class StringUtilsTest
extends TestLauncher
{
13 public StringUtilsTest(String
[] args
) {
14 super("StringUtils test", args
);
16 addTest(new TestCase("Time serialisation") {
18 public void test() throws Exception
{
19 for (long fullTime
: new Long
[] { 0l, 123456l, 123456000l,
20 new Date().getTime() }) {
21 // precise to the second, no more
22 long time
= (fullTime
/ 1000) * 1000;
24 String displayTime
= StringUtils
.fromTime(time
);
25 assertNotNull("The stringified time for " + time
26 + " should not be null", displayTime
);
27 assertEquals("The stringified time for " + time
28 + " should not be empty", false, displayTime
.trim()
31 assertEquals("The time " + time
32 + " should be loop-convertable", time
,
33 StringUtils
.toTime(displayTime
));
35 assertEquals("The time " + displayTime
36 + " should be loop-convertable", displayTime
,
37 StringUtils
.fromTime(StringUtils
38 .toTime(displayTime
)));
43 addTest(new TestCase("MD5") {
45 public void test() throws Exception
{
46 String mess
= "The String we got is not what 'md5sum' said it should heve been";
47 assertEquals(mess
, "34ded48fcff4221d644be9a37e1cb1d9",
48 StringUtils
.getMd5Hash("fanfan la tulipe"));
49 assertEquals(mess
, "7691b0cb74ed0f94b4d8cd858abe1165",
50 StringUtils
.getMd5Hash("je te do-o-o-o-o-o-nne"));
54 addTest(new TestCase("Padding") {
56 public void test() throws Exception
{
57 for (String data
: new String
[] { "fanfan", "la tulipe",
58 "1234567890", "12345678901234567890", "1", "" }) {
59 String result
= StringUtils
.padString(data
, -1);
60 assertEquals("A size of -1 is expected to produce a noop",
61 true, data
.equals(result
));
62 for (int size
: new Integer
[] { 0, 1, 5, 10, 40 }) {
63 result
= StringUtils
.padString(data
, size
);
65 "Padding a String at a certain size should give a String of the given size",
66 size
, result
.length());
68 "Padding a String should not change the content",
69 true, data
.trim().startsWith(result
.trim()));
71 result
= StringUtils
.padString(data
, size
, false, null);
73 "Padding a String without cutting should not shorten the String",
74 true, data
.length() <= result
.length());
76 "Padding a String without cutting should keep the whole content",
77 true, data
.trim().equals(result
.trim()));
79 result
= StringUtils
.padString(data
, size
, false,
81 if (size
> data
.length()) {
83 "Padding a String to the end should work as expected",
84 true, result
.endsWith(data
));
87 result
= StringUtils
.padString(data
, size
, false,
89 if (size
> data
.length()) {
90 String unspacedData
= data
.trim();
91 String unspacedResult
= result
.trim();
92 for (int i
= 0; i
< size
; i
++) {
93 unspacedData
= unspacedData
.replace(" ", " ");
94 unspacedResult
= unspacedResult
.replace(" ",
99 "Justified text trimmed with all spaces collapsed "
100 + "sould be identical to original text "
101 + "trimmed with all spaces collapsed",
102 unspacedData
, unspacedResult
);
105 result
= StringUtils
.padString(data
, size
, false,
107 if (size
> data
.length()) {
109 for (int i
= 0; i
< result
.length()
110 && result
.charAt(i
) == ' '; i
++) {
115 for (int i
= result
.length() - 1; i
>= 0
116 && result
.charAt(i
) == ' '; i
--) {
120 if (result
.trim().isEmpty()) {
122 if (before
> (2 * after
)) {
130 "Padding a String on center should work as expected",
131 result
.length(), before
+ data
.length()
134 "Padding a String on center should not uncenter the content",
135 true, Math
.abs(before
- after
) <= 1);
142 addTest(new TestCase("Justifying") {
144 public void test() throws Exception
{
145 for (String data
: new String
[] { "test",
146 "let's test some words", "" }) {
148 for (String word
: data
.split((" "))) {
149 total
+= word
.replace("-", "").replace(" ", "")
152 List
<String
> result
= StringUtils
.justifyText(data
, 5,
153 StringUtils
.Alignment
.LEFT
);
155 System
.out
.println("["+data
+"] -> [");
158 for (String resultLine
: result
) {
159 System
.out
.println(resultLine
);
160 for (String word
: resultLine
.split((" "))) {
161 totalResult
+= word
.replace("-", "")
162 .replace(" ", "").length();
165 System
.out
.println("]");
168 "The number of letters ('-' not included) should be identical before and after",
174 addTest(new TestCase("unhtml") {
176 public void test() throws Exception
{
177 Map
<String
, String
> data
= new HashMap
<String
, String
>();
178 data
.put("aa", "aa");
179 data
.put("test with spaces ", "test with spaces ");
180 data
.put("<a href='truc://target/'>link</a>", "link");
181 data
.put("<html>Digimon</html>", "Digimon");
185 for (Entry
<String
, String
> entry
: data
.entrySet()) {
186 String result
= StringUtils
.unhtml(entry
.getKey());
187 assertEquals("Result is not what we expected",
188 entry
.getValue(), result
);
193 addTest(new TestCase("zip64") {
195 public void test() throws Exception
{
196 String orig
= "test";
197 String zipped
= StringUtils
.zip64(orig
);
198 String unzipped
= StringUtils
.unzip64(zipped
);
199 assertEquals(orig
, unzipped
);