1 package be
.nikiroo
.utils
.test
;
4 import java
.util
.HashMap
;
6 import java
.util
.Map
.Entry
;
8 import be
.nikiroo
.utils
.StringUtils
;
9 import be
.nikiroo
.utils
.StringUtils
.Alignment
;
11 class StringUtilsTest
extends TestLauncher
{
12 public StringUtilsTest(String
[] args
) {
13 super("StringUtils test", args
);
15 addTest(new TestCase("Time serialisation") {
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;
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()
30 assertEquals("The time " + time
31 + " should be loop-convertable", time
,
32 StringUtils
.toTime(displayTime
));
34 assertEquals("The time " + displayTime
35 + " should be loop-convertable", displayTime
,
36 StringUtils
.fromTime(StringUtils
37 .toTime(displayTime
)));
42 addTest(new TestCase("MD5") {
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"));
53 addTest(new TestCase("Padding") {
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
);
64 "Padding a String at a certain size should give a String of the given size",
65 size
, result
.length());
67 "Padding a String should not change the content",
68 true, data
.trim().startsWith(result
.trim()));
70 result
= StringUtils
.padString(data
, size
, false, null);
72 "Padding a String without cutting should not shorten the String",
73 true, data
.length() <= result
.length());
75 "Padding a String without cutting should keep the whole content",
76 true, data
.trim().equals(result
.trim()));
78 result
= StringUtils
.padString(data
, size
, false,
80 if (size
> data
.length()) {
82 "Padding a String to the end should work as expected",
83 true, result
.endsWith(data
));
86 result
= StringUtils
.padString(data
, size
, false,
88 if (size
> data
.length()) {
90 for (int i
= 0; i
< result
.length()
91 && result
.charAt(i
) == ' '; i
++) {
96 for (int i
= result
.length() - 1; i
>= 0
97 && result
.charAt(i
) == ' '; i
--) {
101 if (result
.trim().isEmpty()) {
103 if (before
> (2 * after
)) {
111 "Padding a String on center should work as expected",
112 result
.length(), before
+ data
.length()
115 "Padding a String on center should not uncenter the content",
116 true, Math
.abs(before
- after
) <= 1);
123 addTest(new TestCase("unhtml") {
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");
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
);
142 addTest(new TestCase("zip64") {
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
);