1 package be
.nikiroo
.utils
.test_code
;
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
;
12 import be
.nikiroo
.utils
.test
.TestCase
;
13 import be
.nikiroo
.utils
.test
.TestLauncher
;
15 class StringUtilsTest
extends TestLauncher
{
16 public StringUtilsTest(String
[] args
) {
17 super("StringUtils test", args
);
19 addTest(new TestCase("Time serialisation") {
21 public void test() throws Exception
{
22 for (long fullTime
: new Long
[] { 0l, 123456l, 123456000l,
23 new Date().getTime() }) {
24 // precise to the second, no more
25 long time
= (fullTime
/ 1000) * 1000;
27 String displayTime
= StringUtils
.fromTime(time
);
28 assertNotNull("The stringified time for " + time
29 + " should not be null", displayTime
);
30 assertEquals("The stringified time for " + time
31 + " should not be empty", false, displayTime
.trim()
34 assertEquals("The time " + time
35 + " should be loop-convertable", time
,
36 StringUtils
.toTime(displayTime
));
38 assertEquals("The time " + displayTime
39 + " should be loop-convertable", displayTime
,
40 StringUtils
.fromTime(StringUtils
41 .toTime(displayTime
)));
46 addTest(new TestCase("MD5") {
48 public void test() throws Exception
{
49 String mess
= "The String we got is not what 'md5sum' said it should heve been";
50 assertEquals(mess
, "34ded48fcff4221d644be9a37e1cb1d9",
51 StringUtils
.getMd5Hash("fanfan la tulipe"));
52 assertEquals(mess
, "7691b0cb74ed0f94b4d8cd858abe1165",
53 StringUtils
.getMd5Hash("je te do-o-o-o-o-o-nne"));
57 addTest(new TestCase("Padding") {
59 public void test() throws Exception
{
60 for (String data
: new String
[] { "fanfan", "la tulipe",
61 "1234567890", "12345678901234567890", "1", "" }) {
62 String result
= StringUtils
.padString(data
, -1);
63 assertEquals("A size of -1 is expected to produce a noop",
64 true, data
.equals(result
));
65 for (int size
: new Integer
[] { 0, 1, 5, 10, 40 }) {
66 result
= StringUtils
.padString(data
, size
);
68 "Padding a String at a certain size should give a String of the given size",
69 size
, result
.length());
71 "Padding a String should not change the content",
72 true, data
.trim().startsWith(result
.trim()));
74 result
= StringUtils
.padString(data
, size
, false, null);
76 "Padding a String without cutting should not shorten the String",
77 true, data
.length() <= result
.length());
79 "Padding a String without cutting should keep the whole content",
80 true, data
.trim().equals(result
.trim()));
82 result
= StringUtils
.padString(data
, size
, false,
84 if (size
> data
.length()) {
86 "Padding a String to the end should work as expected",
87 true, result
.endsWith(data
));
90 result
= StringUtils
.padString(data
, size
, false,
92 if (size
> data
.length()) {
93 String unspacedData
= data
.trim();
94 String unspacedResult
= result
.trim();
95 for (int i
= 0; i
< size
; i
++) {
96 unspacedData
= unspacedData
.replace(" ", " ");
97 unspacedResult
= unspacedResult
.replace(" ",
102 "Justified text trimmed with all spaces collapsed "
103 + "sould be identical to original text "
104 + "trimmed with all spaces collapsed",
105 unspacedData
, unspacedResult
);
108 result
= StringUtils
.padString(data
, size
, false,
110 if (size
> data
.length()) {
112 for (int i
= 0; i
< result
.length()
113 && result
.charAt(i
) == ' '; i
++) {
118 for (int i
= result
.length() - 1; i
>= 0
119 && result
.charAt(i
) == ' '; i
--) {
123 if (result
.trim().isEmpty()) {
125 if (before
> (2 * after
)) {
133 "Padding a String on center should work as expected",
134 result
.length(), before
+ data
.length()
137 "Padding a String on center should not uncenter the content",
138 true, Math
.abs(before
- after
) <= 1);
145 addTest(new TestCase("Justifying") {
147 public void test() throws Exception
{
148 Map
<String
, Map
<Integer
, Entry
<Alignment
, List
<String
>>>> source
= new HashMap
<String
, Map
<Integer
, Entry
<Alignment
, List
<String
>>>>();
149 addValue(source
, Alignment
.LEFT
, "testy", -1, "testy");
150 addValue(source
, Alignment
.RIGHT
, "testy", -1, "testy");
151 addValue(source
, Alignment
.CENTER
, "testy", -1, "testy");
152 addValue(source
, Alignment
.JUSTIFY
, "testy", -1, "testy");
153 addValue(source
, Alignment
.LEFT
, "testy", 5, "testy");
154 addValue(source
, Alignment
.LEFT
, "testy", 3, "te-", "sty");
155 addValue(source
, Alignment
.LEFT
,
156 "Un petit texte qui se mettra sur plusieurs lignes",
157 10, "Un petit", "texte qui", "se mettra", "sur",
158 "plusieurs", "lignes");
159 addValue(source
, Alignment
.LEFT
,
160 "Un petit texte qui se mettra sur plusieurs lignes", 7,
161 "Un", "petit", "texte", "qui se", "mettra", "sur",
162 "plusie-", "urs", "lignes");
163 addValue(source
, Alignment
.RIGHT
,
164 "Un petit texte qui se mettra sur plusieurs lignes", 7,
165 " Un", " petit", " texte", " qui se", " mettra",
166 " sur", "plusie-", " urs", " lignes");
167 addValue(source
, Alignment
.CENTER
,
168 "Un petit texte qui se mettra sur plusieurs lignes", 7,
169 " Un ", " petit ", " texte ", "qui se ", "mettra ",
170 " sur ", "plusie-", " urs ", "lignes ");
171 addValue(source
, Alignment
.JUSTIFY
,
172 "Un petit texte qui se mettra sur plusieurs lignes", 7,
173 "Un pet-", "it tex-", "te qui", "se met-", "tra sur",
174 "plusie-", "urs li-", "gnes");
175 addValue(source
, Alignment
.JUSTIFY
,
176 "Un petit texte qui se mettra sur plusieurs lignes",
177 14, "Un petit", "texte qui se",
178 "mettra sur", "plusieurs lig-", "nes");
179 addValue(source
, Alignment
.JUSTIFY
, "le dash-test", 9,
182 for (String data
: source
.keySet()) {
183 for (int size
: source
.get(data
).keySet()) {
184 Alignment align
= source
.get(data
).get(size
).getKey();
185 List
<String
> values
= source
.get(data
).get(size
)
188 List
<String
> result
= StringUtils
.justifyText(data
,
191 // System.out.println("[" + data + " (" + size + ")" +
193 // for (int i = 0; i < result.size(); i++) {
194 // String resultLine = result.get(i);
195 // System.out.println(i + ": " + resultLine);
197 // System.out.println("]");
199 assertEquals(values
, result
);
205 addTest(new TestCase("unhtml") {
207 public void test() throws Exception
{
208 Map
<String
, String
> data
= new HashMap
<String
, String
>();
209 data
.put("aa", "aa");
210 data
.put("test with spaces ", "test with spaces ");
211 data
.put("<a href='truc://target/'>link</a>", "link");
212 data
.put("<html>Digimon</html>", "Digimon");
216 for (Entry
<String
, String
> entry
: data
.entrySet()) {
217 String result
= StringUtils
.unhtml(entry
.getKey());
218 assertEquals("Result is not what we expected",
219 entry
.getValue(), result
);
224 addTest(new TestCase("zip64") {
226 public void test() throws Exception
{
227 String orig
= "test";
228 String zipped
= StringUtils
.zip64(orig
);
229 String unzipped
= StringUtils
.unzip64s(zipped
);
230 assertEquals(orig
, unzipped
);
234 addTest(new TestCase("format/toNumber simple") {
236 public void test() throws Exception
{
237 assertEquals(263l, StringUtils
.toNumber("263"));
238 assertEquals(21200l, StringUtils
.toNumber("21200"));
239 assertEquals(0l, StringUtils
.toNumber("0"));
240 assertEquals("263", StringUtils
.formatNumber(263l));
241 assertEquals("21 k", StringUtils
.formatNumber(21000l));
242 assertEquals("0", StringUtils
.formatNumber(0l));
246 addTest(new TestCase("format/toNumber not 000") {
248 public void test() throws Exception
{
249 assertEquals(263200l, StringUtils
.toNumber("263.2 k"));
250 assertEquals(42000l, StringUtils
.toNumber("42.0 k"));
251 assertEquals(12000000l, StringUtils
.toNumber("12 M"));
252 assertEquals(2000000000l, StringUtils
.toNumber("2 G"));
253 assertEquals("263 k", StringUtils
.formatNumber(263012l));
254 assertEquals("42 k", StringUtils
.formatNumber(42012l));
255 assertEquals("12 M", StringUtils
.formatNumber(12012121l));
256 assertEquals("7 G", StringUtils
.formatNumber(7364635928l));
260 addTest(new TestCase("format/toNumber decimals") {
262 public void test() throws Exception
{
263 assertEquals(263200l, StringUtils
.toNumber("263.2 k"));
264 assertEquals(1200l, StringUtils
.toNumber("1.2 k"));
265 assertEquals(42700000l, StringUtils
.toNumber("42.7 M"));
266 assertEquals(1220l, StringUtils
.toNumber("1.22 k"));
267 assertEquals(1432l, StringUtils
.toNumber("1.432 k"));
268 assertEquals(6938l, StringUtils
.toNumber("6.938 k"));
269 assertEquals("1.3 k", StringUtils
.formatNumber(1300l, 1));
270 assertEquals("263.2020 k", StringUtils
.formatNumber(263202l, 4));
271 assertEquals("1.26 k", StringUtils
.formatNumber(1267l, 2));
272 assertEquals("42.7 M", StringUtils
.formatNumber(42712121l, 1));
273 assertEquals("5.09 G", StringUtils
.formatNumber(5094837485l, 2));
278 static private void addValue(
279 Map
<String
, Map
<Integer
, Entry
<Alignment
, List
<String
>>>> source
,
280 final Alignment align
, String input
, int size
,
281 final String
... result
) {
282 if (!source
.containsKey(input
)) {
284 new HashMap
<Integer
, Entry
<Alignment
, List
<String
>>>());
287 source
.get(input
).put(size
, new Entry
<Alignment
, List
<String
>>() {
289 public Alignment
getKey() {
294 public List
<String
> getValue() {
295 return Arrays
.asList(result
);
299 public List
<String
> setValue(List
<String
> value
) {