Added README file
[metux-java.git] / tests / _test_util.java
blobc973c8aff5bd9b636d69147af4b10e4dbe339f5f
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;
17 import java.util.*;
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()
38 __normalize_strip_sr(
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"
43 __normalize_strip_sr(
44 "to///my/foo/./bar//././boo/",
45 "/opt//xcompiler/samma//sys-root//",
46 "to/my/foo/bar/boo"
48 __normalize_strip_sr(
49 "//path/to///my/foo/./bar//././boo",
50 "/opt//xcompiler/samma/sys-root",
51 "/path/to/my/foo/bar/boo"
53 __normalize_strip_sr(
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()
77 __normalize_add_sr(
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"
82 __normalize_add_sr(
83 "to///my/foo/./bar//././boo/",
84 "/opt//xcompiler/samma//sys-root//",
85 "to/my/foo/bar/boo"
87 __normalize_add_sr(
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"
92 __normalize_add_sr(
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"
97 __normalize_add_sr(
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()
128 __normalize(
129 "//path/to///my/foo/./bar//././boo/",
130 "/path/to/my/foo/bar/boo"
132 __normalize(
133 "to///my/foo/./bar//././boo/",
134 "to/my/foo/bar/boo"
136 __normalize(
137 "//path/to///my/foo/./bar//././boo",
138 "/path/to/my/foo/bar/boo"
140 __normalize(
141 "//path/to///my/foo/./bar//././boo///",
142 "/path/to/my/foo/bar/boo"
144 __replace_suffix(
145 "//path/to/my/foo.bar.boo",
146 "c",
147 "//path/to/my/foo.bar.c"
149 __replace_suffix(
150 "//path/to/my/foo.c",
151 "c",
152 "//path/to/my/foo.c"
154 __replace_suffix(
155 "//path/for/dummy.obj",
156 "in",
157 "//path/for/dummy.in"
159 __replace_suffix(
160 "huhu/allo",
161 "asm",
162 "huhu/allo.asm"
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}");
181 if (pos != 11)
183 System.err.println("java.lang.String class buggy: indexOf() doesnt find SUFFIX");
184 System.exit(1);
188 static boolean test_StrReplace_try(
189 String name,
190 String source,
191 String needle,
192 String replace,
193 String ok)
195 String res =
196 StrReplace.replace(needle, replace, source);
198 if (res.equals(ok))
200 System.out.println("StrReplace test "+name+" ... OK");
201 return true;
203 else
205 System.err.println("StrReplace test "+name+" ... FAILED !");
206 return false;
210 static void test_StrReplace()
212 boolean okay;
214 okay =
216 test_StrReplace_try(
217 "endswith",
218 "bummsfall"",
219 """,
220 "era666",
221 "bummsfallera666"
222 ) &&
223 test_StrReplace_try(
224 "multi-replace",
225 "//hello/world/////baa/",
226 "//",
227 "/",
228 "/hello/world/baa/"
229 ) &&
230 test_StrReplace_try(
231 "simple-replace",
232 "/install/config/styles/{STYLE}.cf{TERM_NORMAL}",
233 "{TERM_NORMAL}",
234 "FOO",
235 "/install/config/styles/{STYLE}.cfFOO"
236 ) &&
237 test_StrReplace_try(
238 "simple-replace",
239 "/install/config/styles/{STYLE}.cf",
240 "{STYLE}",
241 "system/homer",
242 "/install/config/styles/system/homer.cf"
246 if (!okay)
247 System.exit(1);
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");
275 System.exit(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");
283 System.exit(1);
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
293 try {
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
307 try {
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
321 try {
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
343 try {
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()
374 Depend dep;
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]);
389 if (!(
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");
436 if (!c.equals(""))
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()
464 String [] res;
466 res = PathNormalizer.split_filename("one.c");
467 if (res.length != 2)
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");
475 if (res.length != 2)
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.");
483 if (res.length != 2)
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()
492 throws Exception
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");
500 post.send();
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");
512 else
514 System.err.println("HTMLEntities.decode() FAILED");
515 System.err.println(" res=\""+res+"\"");
516 System.exit(1);
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");
526 else
527 throw new RuntimeException("VFile_normalize_filename failed test: out=\""+out+"\" okay=\""+okay+"\"");
530 static public void test_VFile()
531 throws Exception
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
542 test_StrReplace();
543 test_basename();
544 test_dirname();
545 test_PathNormalize();
546 test_PathNormalize_strip_sysroot();
547 test_PathNormalize_add_sysroot();
548 test_Version();
549 test_Exec();
550 test_Env_native();
551 test_Java_Regex();
552 test_shellvariabledef();
553 test_filename_split();
555 // test IPTool.getInterfaceAddress()
556 // IPTools.getInterfaceAddress("lo");
558 test_Depend();
559 test_SimpleSMTPClient();
560 test_HTMLEntities();
562 System.err.println("CWD="+FileOps.getcwd());
564 test_VFile();
566 // mkdir.mkdir_recursive("/home/briegel/download/zlib/huhu/123/");