2 import org
.de
.metux
.util
.StrReplace
;
3 import org
.de
.metux
.util
.Environment
;
4 //import org.de.metux.util.PathNormalize;
5 import org
.de
.metux
.util
.PathNormalizer
;
6 import org
.de
.metux
.util
.Version
;
7 import org
.de
.metux
.util
.ShellVariableDef
;
8 import org
.de
.metux
.util
.Exec
;
9 import org
.de
.metux
.util
.Depend
;
10 //import org.de.metux.util.IPTools;
11 import org
.de
.metux
.util
.SimpleSMTPClient
;
12 import org
.de
.metux
.util
.HTMLEntities
;
13 import org
.de
.metux
.util
.VFile
;
14 import org
.de
.metux
.util
.mkdir
;
15 import org
.de
.metux
.util
.FileOps
;
18 import java
.util
.regex
.*;
20 public class _test_util
22 static void __normalize_strip_sr(String src
, String sysroot
, String dest
)
24 // String out = PathNormalize.normalize_strip_sysroot(src,sysroot);
25 // if (!out.equals(dest))
26 // throw new RuntimeException(
27 // "Testing PathNormalize.normalize() failed: \n"+
28 // "source=\""+src+"\"\n"+
29 // "sysroot=\""+sysroot+"\"\n"+
30 // "out=\""+out+"\"\n"+
31 // "dest=\""+dest+"\"");
33 // System.out.println("PathNormalize(\""+src+"\") OK");
36 static void test_PathNormalize_strip_sysroot()
39 "/opt/xcompiler//samma/sys-root//path/to///my/foo/./bar//././boo/",
40 "/opt//xcompiler/samma/sys-root/",
41 "/path/to/my/foo/bar/boo"
44 "to///my/foo/./bar//././boo/",
45 "/opt//xcompiler/samma//sys-root//",
49 "//path/to///my/foo/./bar//././boo",
50 "/opt//xcompiler/samma/sys-root",
51 "/path/to/my/foo/bar/boo"
54 "//opt/xcompiler/samma/sys-root//path/to///my/foo/./bar//././boo///",
55 "/opt//xcompiler/samma/sys-root",
56 "/path/to/my/foo/bar/boo"
61 static void __normalize_add_sr(String src
, String sysroot
, String dest
)
63 // String out = PathNormalize.normalize_add_sysroot(src,sysroot);
64 // if (!out.equals(dest))
65 // throw new RuntimeException(
66 // "Testing PathNormalize.normalize() failed: \n"+
67 // "source=\""+src+"\"\n"+
68 // "sysroot=\""+sysroot+"\"\n"+
69 // "out=\""+out+"\"\n"+
70 // "dest=\""+dest+"\"");
72 // System.out.println("PathNormalize(\""+src+"\") OK");
75 static void test_PathNormalize_add_sysroot()
78 "/opt/xcompiler//samma/sys-root//path/to///my/foo/./bar//././boo/",
79 "/opt//xcompiler/samma/sys-root/",
80 "/opt/xcompiler/samma/sys-root/path/to/my/foo/bar/boo"
83 "to///my/foo/./bar//././boo/",
84 "/opt//xcompiler/samma//sys-root//",
88 "//path/to///my/foo/./bar//././boo",
89 "/opt//xcompiler/samma/sys-root",
90 "/opt/xcompiler/samma/sys-root/path/to/my/foo/bar/boo"
93 "//path/to///my/foo/./bar//././boo",
94 "/opt//xcompiler/samma/sys-root//./",
95 "/opt/xcompiler/samma/sys-root/path/to/my/foo/bar/boo"
98 "//opt/xcompiler/samma/sys-root//path/to///my/foo/./bar//././boo///",
99 "/opt/./xcompiler/samma/sys-root",
100 "/opt/xcompiler/samma/sys-root/path/to/my/foo/bar/boo"
105 static void __normalize(String src
, String mask
)
107 // String out = PathNormalize.normalize(src);
108 // if (!out.equals(mask))
109 // throw new RuntimeException(
110 // "Testing PathNormalize.normalize() failed with \""+
111 // src+"\" -- is \""+out+"\" -- should be \""+mask+"\"");
113 // System.out.println("PathNormalize(\""+src+"\") OK");
116 static void __replace_suffix(String filename
, String suffix
, String mask
)
118 String out
= PathNormalizer
.replace_suffix(filename
,suffix
);
119 if (!out
.equals(mask
))
120 throw new RuntimeException(
121 "Testing PathNormalizer.replace_suffix() failed with \""+
122 filename
+"\" / \""+suffix
+"\" -- is \""+out
+"\" -- should be \""+mask
+"\"");
123 System
.out
.println("PathNormalizer.replace_suffix(\""+filename
+"\",\""+suffix
+"\")");
126 static void test_PathNormalize()
129 "//path/to///my/foo/./bar//././boo/",
130 "/path/to/my/foo/bar/boo"
133 "to///my/foo/./bar//././boo/",
137 "//path/to///my/foo/./bar//././boo",
138 "/path/to/my/foo/bar/boo"
141 "//path/to///my/foo/./bar//././boo///",
142 "/path/to/my/foo/bar/boo"
145 "//path/to/my/foo.bar.boo",
147 "//path/to/my/foo.bar.c"
150 "//path/to/my/foo.c",
155 "//path/for/dummy.obj",
157 "//path/for/dummy.in"
166 static void test_Java_Regex()
168 if (!Pattern
.matches("a*b", "aaaaab"))
169 throw new RuntimeException("regex test FAILED");
171 System
.out
.println("regex test OK");
175 /* testing StrReplace::replace */
176 static void test_StrReplace_1()
178 String knollo
= "HELLO WORLD{TERM}";
179 int pos
= knollo
.indexOf("{TERM}");
183 System
.err
.println("java.lang.String class buggy: indexOf() doesnt find SUFFIX");
188 static boolean test_StrReplace_try(
196 StrReplace
.replace(needle
, replace
, source
);
200 System
.out
.println("StrReplace test "+name
+" ... OK");
205 System
.err
.println("StrReplace test "+name
+" ... FAILED !");
210 static void test_StrReplace()
225 "//hello/world/////baa/",
232 "/install/config/styles/{STYLE}.cf{TERM_NORMAL}",
235 "/install/config/styles/{STYLE}.cfFOO"
239 "/install/config/styles/{STYLE}.cf",
242 "/install/config/styles/system/homer.cf"
249 System
.out
.println("selftest: StrReplace.replace() ... passed");
252 static public void test_Env()
254 Environment env
= new Environment();
255 System
.out
.println("ENV: HOME="+env
.getProperty("HOME"));
256 System
.out
.println("ENV: LOGNAME="+env
.getProperty("LOGNAME"));
257 System
.out
.println("ENV: LS_COLORS="+env
.getProperty("LS_COLORS"));
260 static public void test_Env_native()
262 System
.out
.println("sysENV: HOME="+System
.getenv("HOME"));
263 System
.out
.println("sysENV: LOGNAME="+System
.getenv("LOGNAME"));
264 System
.out
.println("sysENV: LS_COLORS="+System
.getenv("LS_COLORS"));
267 static public void test_Version()
269 Version ver
= new Version ( "1.2.3.4" );
271 if ((ver
.digits
[0]!=1)||(ver
.digits
[1]!=2)||
272 (ver
.digits
[2]!=3)||(ver
.digits
[3]!=4))
274 System
.err
.println("Version class failed test #1");
278 System
.out
.println("toString() => "+ver
.toString());
280 if (!ver
.toString().equals("1.2.3.4"))
282 System
.err
.println("Version class failed test #2");
286 System
.out
.println("Version class test passed.");
289 static void test_shellvariabledef()
290 throws ShellVariableDef
.XParseFailed
, ShellVariableDef
.XEmpty
292 // test several cases for XEmpty
294 new ShellVariableDef(null);
295 throw new RuntimeException("error in ShellVariablesDef: calling with null should throw XEmpty");
297 catch (ShellVariableDef
.XEmpty e
)
299 System
.out
.println("ShellVariableDef: case 1 okay");
301 catch (ShellVariableDef
.XParseFailed e
)
303 throw new RuntimeException("error in ShellVariablesDef: calling with null string brought parse error: "+e
);
306 // test several cases for XEmpty
308 new ShellVariableDef(" # adsfaasdf ");
309 throw new RuntimeException("error in ShellVariablesDef: calling with empty shuld throw XEmpty");
311 catch (ShellVariableDef
.XEmpty e
)
313 System
.out
.println("ShellVariableDef: case 2 okay");
315 catch (ShellVariableDef
.XParseFailed e
)
317 throw new RuntimeException("error in ShellVariablesDef: calling with empty string brought parse error: "+e
);
320 // try a fully conform string
322 ShellVariableDef shvar
=
323 new ShellVariableDef("foo='furunkel' # hello world");
324 if (shvar
.name
==null)
325 throw new RuntimeException("case 3 error: name is null");
326 if (shvar
.value
==null)
327 throw new RuntimeException("case 3 error: value is null");
328 if (!shvar
.name
.equals("foo"))
329 throw new RuntimeException("case 3 error: name is not \"foo\" -- \""+shvar
.name
+"\"");
330 if (!shvar
.value
.equals("furunkel"))
331 throw new RuntimeException("case 3 error: value is not \"furunkel\" -- \""+shvar
.value
+"\"");
333 catch (ShellVariableDef
.XParseFailed e
)
335 throw new RuntimeException("case 3 error: parse failed: "+e
);
337 catch (ShellVariableDef
.XEmpty e
)
339 throw new RuntimeException("case 3 error: parse failed: "+e
);
342 // try a string without quotes (ie. numeric or boolean
344 ShellVariableDef shvar
=
345 new ShellVariableDef("foo=yes # hello world");
346 if (shvar
.name
==null)
347 throw new RuntimeException("case 4 error: name is null");
348 if (shvar
.value
==null)
349 throw new RuntimeException("case 4 error: value is null");
350 if (!shvar
.name
.equals("foo"))
351 throw new RuntimeException("case 4 error: name is not \"foo\" -- \""+shvar
.name
+"\"");
352 if (!shvar
.value
.equals("yes"))
353 throw new RuntimeException("case 4 error: value is not \"yes\" -- \""+shvar
.value
+"\"");
355 catch (ShellVariableDef
.XParseFailed e
)
357 throw new RuntimeException("case 4 error: parse failed: "+e
);
359 catch (ShellVariableDef
.XEmpty e
)
361 throw new RuntimeException("case 4 error: parse failed: "+e
);
364 System
.out
.println("ShellVariableDef passed cases 1..4");
367 static void test_Exec()
369 Exec
.exec_command("ls /opt");
372 static void test_Depend()
376 /* --- test case 1 --- */
377 dep
= new Depend("knollo[foo][bar][blubb]");
378 if (!(dep
.package_name
.equals("knollo")))
379 throw new RuntimeException("Depend{} case 1 failed (pkgname)");
381 if (dep
.features
==null)
382 throw new RuntimeException("Depend{} case 1 failed. no features");
384 System
.out
.println("NUM_FEATURES: "+dep
.features
.length
);
385 System
.out
.println("FEAT0: "+dep
.features
[0]);
386 System
.out
.println("FEAT1: "+dep
.features
[1]);
387 System
.out
.println("FEAT2: "+dep
.features
[2]);
390 (dep
.features
.length
==3) &&
391 dep
.features
[0].equals("foo") &&
392 dep
.features
[1].equals("bar") &&
393 dep
.features
[2].equals("blubb")
395 throw new RuntimeException("Depend{} case 1 failed (features)");
397 /* --- test case 2 --- */
399 dep
= new Depend("furunkel(>=1.2.3.0)");
400 if (!dep
.package_name
.equals("furunkel"))
401 throw new RuntimeException("Depend{} case 2 failed (name) \""+dep
.package_name
+"\"");
403 if (!dep
.version
.toString().equals("1.2.3.0"))
404 throw new RuntimeException("Depend{} case 2 failed (version)");
406 /* --- test case 3 --- */
407 dep
= new Depend("albert(>=7.1.6.3)[foo][bratzel]");
408 if ((!dep
.package_name
.equals("albert")) ||
409 (!dep
.version
.toString().equals("7.1.6.3")) ||
410 (dep
.features
.length
!=2) ||
411 (!dep
.features
[0].equals("foo")) ||
412 (!dep
.features
[1].equals("bratzel"))
414 throw new RuntimeException("Depend{} case 3 failed" );
416 /* --- test case 4 --- */
417 dep
= new Depend("hutzbutz");
418 if ((!dep
.package_name
.equals("hutzbutz")) ||
419 (!dep
.version
.toString().equals("0.0.0.0")) ||
420 (dep
.features
!= null))
421 throw new RuntimeException("Depend{} case 4 failed");
424 static public void test_basename()
426 System
.err
.println("Testing PathNormalizer::basename()");
428 String a
= PathNormalizer
.basename("test123.so");
429 String b
= PathNormalizer
.basename("/foo/bar.txt");
430 String c
= PathNormalizer
.basename("/knollo//");
432 if (!a
.equals("test123.so"))
433 throw new RuntimeException("test basename() #1 failed");
434 if (!b
.equals("bar.txt"))
435 throw new RuntimeException("test basename() #2 failed");
437 throw new RuntimeException("test basename() #3 failed");
440 static public void test_dirname()
442 System
.out
.println("Testing PathNormalizer::dirname()");
444 String dirname1
= PathNormalizer
.dirname("/foo/bar/boo");
445 String dirname2
= PathNormalizer
.dirname("bumm.bumm.blahh");
446 String dirname3
= PathNormalizer
.dirname("");
447 String dirname4
= PathNormalizer
.dirname(null);
448 String dirname5
= PathNormalizer
.dirname("test/knollo.la");
450 if (!dirname1
.equals("/foo/bar"))
451 throw new RuntimeException("test_dirname() #1 failed \""+dirname1
+"\"");
452 if (!dirname2
.equals(""))
453 throw new RuntimeException("test_dirname() #2 failed \""+dirname2
+"\"");
454 if (!dirname3
.equals(""))
455 throw new RuntimeException("test_dirname() #3 failed \""+dirname3
+"\"");
456 if (!dirname4
.equals(""))
457 throw new RuntimeException("test_dirname() #4 failed \""+dirname4
+"\"");
458 if (!dirname5
.equals("test"))
459 throw new RuntimeException("test_dirname() #5 failed \""+dirname5
+"\"");
462 static public void test_filename_split()
466 res
= PathNormalizer
.split_filename("one.c");
468 throw new RuntimeException("test_filename_split() #1.0 failed: return size should be 2, not "+res
.length
);
469 if (!res
[0].equals("one"))
470 throw new RuntimeException("test_filename_split() #1.1 failed: \""+res
[0]+"\"");
471 if (!res
[1].equals(".c"))
472 throw new RuntimeException("test_filename_split() #1.2 failed: \""+res
[1]+"\"");
474 res
= PathNormalizer
.split_filename("boo");
476 throw new RuntimeException("test_filename_split() #2.0 failed: return size should be 2, not "+res
.length
);
477 if (!res
[0].equals("boo"))
478 throw new RuntimeException("test_filename_split() #2.2 failed: \""+res
[0]+"\"");
479 if (!res
[1].equals(""))
480 throw new RuntimeException("test_filename_split() #2.3 failed: \""+res
[1]+"\"");
482 res
= PathNormalizer
.split_filename("aha.");
484 throw new RuntimeException("test_filename_split() #3.0 failed: return size should be 2, not "+res
.length
);
485 if (!res
[0].equals("aha"))
486 throw new RuntimeException("test_filename_split() #3.2 failed: \""+res
[0]+"\"");
487 if (!res
[1].equals("."))
488 throw new RuntimeException("test_filename_split() #3.3 failed: \""+res
[1]+"\"");
491 static public void test_SimpleSMTPClient()
494 System
.out
.println("Testing mail ....");
495 SimpleSMTPClient post
= new SimpleSMTPClient();
496 post
.setTo("weigelt@metux.de");
497 post
.setFrom("classlib@metux.de");
498 post
.setSubject("classlib regress test");
499 post
.setBody("Hello World huhuh");
501 System
.out
.println("Done");
504 static public void test_HTMLEntities()
506 String res
= HTMLEntities
.decode("test 213 "");
508 if (res
.equals("test 213 \""))
510 System
.err
.println("HTMLEntities.decode() OK");
514 System
.err
.println("HTMLEntities.decode() FAILED");
515 System
.err
.println(" res=\""+res
+"\"");
520 static public void test_VFile_normalize_filename(String test
, String okay
)
522 System
.err
.println("Testing VFile.normalize_filename(\""+test
+"\")");
523 String out
= VFile
.normalize_filename(test
);
524 if (out
.equals(okay
))
525 System
.err
.println(" --> OK");
527 throw new RuntimeException("VFile_normalize_filename failed test: out=\""+out
+"\" okay=\""+okay
+"\"");
530 static public void test_VFile()
533 test_VFile_normalize_filename("etc//./passwd", "etc/passwd");
534 test_VFile_normalize_filename("/etc///passwd/", "/etc/passwd/");
535 test_VFile_normalize_filename("/../etc//passwd", "/../etc/passwd");
536 test_VFile_normalize_filename("/etc/../passwd", "/passwd");
537 test_VFile_normalize_filename("etc/../passwd", "passwd");
540 static public void main(String args
[]) throws Exception
545 test_PathNormalize();
546 test_PathNormalize_strip_sysroot();
547 test_PathNormalize_add_sysroot();
552 test_shellvariabledef();
553 test_filename_split();
555 // test IPTool.getInterfaceAddress()
556 // IPTools.getInterfaceAddress("lo");
559 test_SimpleSMTPClient();
562 System
.err
.println("CWD="+FileOps
.getcwd());
566 // mkdir.mkdir_recursive("/home/briegel/download/zlib/huhu/123/");