1 package be
.nikiroo
.utils
.test
;
3 import java
.util
.Arrays
;
5 import java
.util
.HashMap
;
8 import java
.util
.Map
.Entry
;
10 import be
.nikiroo
.utils
.StringUtils
;
11 import be
.nikiroo
.utils
.StringUtils
.Alignment
;
13 class StringUtilsTest
extends TestLauncher
{
14 public StringUtilsTest(String
[] args
) {
15 super("StringUtils test", args
);
17 addTest(new TestCase("Time serialisation") {
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;
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()
32 assertEquals("The time " + time
33 + " should be loop-convertable", time
,
34 StringUtils
.toTime(displayTime
));
36 assertEquals("The time " + displayTime
37 + " should be loop-convertable", displayTime
,
38 StringUtils
.fromTime(StringUtils
39 .toTime(displayTime
)));
44 addTest(new TestCase("MD5") {
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"));
55 addTest(new TestCase("Padding") {
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
);
66 "Padding a String at a certain size should give a String of the given size",
67 size
, result
.length());
69 "Padding a String should not change the content",
70 true, data
.trim().startsWith(result
.trim()));
72 result
= StringUtils
.padString(data
, size
, false, null);
74 "Padding a String without cutting should not shorten the String",
75 true, data
.length() <= result
.length());
77 "Padding a String without cutting should keep the whole content",
78 true, data
.trim().equals(result
.trim()));
80 result
= StringUtils
.padString(data
, size
, false,
82 if (size
> data
.length()) {
84 "Padding a String to the end should work as expected",
85 true, result
.endsWith(data
));
88 result
= StringUtils
.padString(data
, size
, false,
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(" ",
100 "Justified text trimmed with all spaces collapsed "
101 + "sould be identical to original text "
102 + "trimmed with all spaces collapsed",
103 unspacedData
, unspacedResult
);
106 result
= StringUtils
.padString(data
, size
, false,
108 if (size
> data
.length()) {
110 for (int i
= 0; i
< result
.length()
111 && result
.charAt(i
) == ' '; i
++) {
116 for (int i
= result
.length() - 1; i
>= 0
117 && result
.charAt(i
) == ' '; i
--) {
121 if (result
.trim().isEmpty()) {
123 if (before
> (2 * after
)) {
131 "Padding a String on center should work as expected",
132 result
.length(), before
+ data
.length()
135 "Padding a String on center should not uncenter the content",
136 true, Math
.abs(before
- after
) <= 1);
143 addTest(new TestCase("Justifying") {
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,
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
)
186 List
<String
> result
= StringUtils
.justifyText(data
,
189 // System.out.println("[" + data + " (" + size + ")" +
191 // for (int i = 0; i < result.size(); i++) {
192 // String resultLine = result.get(i);
193 // System.out.println(i + ": " + resultLine);
195 // System.out.println("]");
197 assertEquals(values
, result
);
203 addTest(new TestCase("unhtml") {
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");
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
);
222 addTest(new TestCase("zip64") {
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
);
232 addTest(new TestCase("from/toNumber") {
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");
242 assertEquals(StringUtils
.formatNumber(1287l, false), "1287");
243 assertEquals(StringUtils
.formatNumber(6056l, false), "6k");
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");
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");
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
)) {
268 new HashMap
<Integer
, Entry
<Alignment
, List
<String
>>>());
271 source
.get(input
).put(size
, new Entry
<Alignment
, List
<String
>>() {
273 public Alignment
getKey() {
278 public List
<String
> getValue() {
279 return Arrays
.asList(result
);
283 public List
<String
> setValue(List
<String
> value
) {