5188 |
09 Nov 09 |
nicklas |
1 |
import net.sf.basedb.util.encode.EncoderDecoder; |
5188 |
09 Nov 09 |
nicklas |
2 |
import net.sf.basedb.util.encode.TabCrLfEncoderDecoder; |
5188 |
09 Nov 09 |
nicklas |
3 |
import net.sf.basedb.util.encode.ToSpaceEncoderDecoder; |
5188 |
09 Nov 09 |
nicklas |
4 |
|
5188 |
09 Nov 09 |
nicklas |
5 |
/* |
5188 |
09 Nov 09 |
nicklas |
$Id $ |
5188 |
09 Nov 09 |
nicklas |
7 |
|
5188 |
09 Nov 09 |
nicklas |
Copyright (C) 2009 Nicklas Nordborg |
5188 |
09 Nov 09 |
nicklas |
9 |
|
5188 |
09 Nov 09 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5188 |
09 Nov 09 |
nicklas |
Available at http://base.thep.lu.se/ |
5188 |
09 Nov 09 |
nicklas |
12 |
|
5188 |
09 Nov 09 |
nicklas |
BASE is free software; you can redistribute it and/or |
5188 |
09 Nov 09 |
nicklas |
modify it under the terms of the GNU General Public License |
5188 |
09 Nov 09 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5188 |
09 Nov 09 |
nicklas |
of the License, or (at your option) any later version. |
5188 |
09 Nov 09 |
nicklas |
17 |
|
5188 |
09 Nov 09 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5188 |
09 Nov 09 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5188 |
09 Nov 09 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5188 |
09 Nov 09 |
nicklas |
GNU General Public License for more details. |
5188 |
09 Nov 09 |
nicklas |
22 |
|
5188 |
09 Nov 09 |
nicklas |
You should have received a copy of the GNU General Public License |
5188 |
09 Nov 09 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5188 |
09 Nov 09 |
nicklas |
25 |
*/ |
5188 |
09 Nov 09 |
nicklas |
26 |
|
5188 |
09 Nov 09 |
nicklas |
27 |
public class TestEncoderDecoder |
5188 |
09 Nov 09 |
nicklas |
28 |
{ |
5188 |
09 Nov 09 |
nicklas |
29 |
|
5188 |
09 Nov 09 |
nicklas |
30 |
static boolean ok = true; |
5188 |
09 Nov 09 |
nicklas |
31 |
public static void main(String[] args) |
5188 |
09 Nov 09 |
nicklas |
32 |
{ |
5188 |
09 Nov 09 |
nicklas |
33 |
TestUtil.checkArgs(args); |
5188 |
09 Nov 09 |
nicklas |
34 |
TestUtil.begin(); |
5188 |
09 Nov 09 |
nicklas |
35 |
ok = test_all(); |
5188 |
09 Nov 09 |
nicklas |
36 |
TestUtil.stop(); |
5188 |
09 Nov 09 |
nicklas |
37 |
} |
5188 |
09 Nov 09 |
nicklas |
38 |
|
5188 |
09 Nov 09 |
nicklas |
39 |
static boolean test_all() |
5188 |
09 Nov 09 |
nicklas |
40 |
{ |
5188 |
09 Nov 09 |
nicklas |
41 |
write("++Testing string encode/decode"); |
5193 |
27 Nov 09 |
nicklas |
42 |
EncoderDecoder tabCrLf = new TabCrLfEncoderDecoder(false); |
5188 |
09 Nov 09 |
nicklas |
43 |
test_code(tabCrLf, "nothingtocode", "nothingtocode"); |
5188 |
09 Nov 09 |
nicklas |
44 |
test_code(tabCrLf, "a\tb\nc\rd\\e", "a\\tb\\nc\\rd\\\\e"); |
5188 |
09 Nov 09 |
nicklas |
45 |
|
5188 |
09 Nov 09 |
nicklas |
46 |
EncoderDecoder space = new ToSpaceEncoderDecoder(); |
5188 |
09 Nov 09 |
nicklas |
47 |
test_code(space, "nothingtocode", "nothingtocode"); |
5188 |
09 Nov 09 |
nicklas |
48 |
test_code(space, "a\tb\nc\rd\\e", "a b c d\\e"); |
5188 |
09 Nov 09 |
nicklas |
49 |
|
5188 |
09 Nov 09 |
nicklas |
50 |
write("++Testing string encode/decode "+(ok ? "OK" : "Failed")+"\n"); |
5188 |
09 Nov 09 |
nicklas |
51 |
return ok; |
5188 |
09 Nov 09 |
nicklas |
52 |
} |
5188 |
09 Nov 09 |
nicklas |
53 |
|
5188 |
09 Nov 09 |
nicklas |
54 |
static void test_code(EncoderDecoder coder, String original, String encoded) |
5188 |
09 Nov 09 |
nicklas |
55 |
{ |
5188 |
09 Nov 09 |
nicklas |
56 |
try |
5188 |
09 Nov 09 |
nicklas |
57 |
{ |
5188 |
09 Nov 09 |
nicklas |
58 |
String tmp = coder.encode(original); |
5188 |
09 Nov 09 |
nicklas |
59 |
if (!tmp.equals(encoded)) |
5188 |
09 Nov 09 |
nicklas |
60 |
{ |
5188 |
09 Nov 09 |
nicklas |
61 |
throw new Exception("Encoding of '" + original + "' failed; got '" + |
5188 |
09 Nov 09 |
nicklas |
62 |
tmp + "'; expected '" + encoded + "'"); |
5188 |
09 Nov 09 |
nicklas |
63 |
} |
5188 |
09 Nov 09 |
nicklas |
64 |
if (coder.isSymmetrical()) |
5188 |
09 Nov 09 |
nicklas |
65 |
{ |
5188 |
09 Nov 09 |
nicklas |
66 |
tmp = coder.decode(encoded); |
5188 |
09 Nov 09 |
nicklas |
67 |
if (!tmp.equals(original)) |
5188 |
09 Nov 09 |
nicklas |
68 |
{ |
5188 |
09 Nov 09 |
nicklas |
69 |
throw new Exception("Decoding of '" + encoded + "' failed; got '" + |
5188 |
09 Nov 09 |
nicklas |
70 |
tmp + "'; expected '" + original + "'"); |
5188 |
09 Nov 09 |
nicklas |
71 |
} |
5188 |
09 Nov 09 |
nicklas |
72 |
} |
5188 |
09 Nov 09 |
nicklas |
73 |
write("--Encode/decode: "+original + "<-->" + encoded + " OK"); |
5188 |
09 Nov 09 |
nicklas |
74 |
} |
5188 |
09 Nov 09 |
nicklas |
75 |
catch (Throwable ex) |
5188 |
09 Nov 09 |
nicklas |
76 |
{ |
5188 |
09 Nov 09 |
nicklas |
77 |
ex.printStackTrace(); |
5188 |
09 Nov 09 |
nicklas |
78 |
ok = false; |
5188 |
09 Nov 09 |
nicklas |
79 |
} |
5188 |
09 Nov 09 |
nicklas |
80 |
} |
5188 |
09 Nov 09 |
nicklas |
81 |
|
5188 |
09 Nov 09 |
nicklas |
82 |
|
5188 |
09 Nov 09 |
nicklas |
83 |
|
5188 |
09 Nov 09 |
nicklas |
84 |
static void write(String message) |
5188 |
09 Nov 09 |
nicklas |
85 |
{ |
5188 |
09 Nov 09 |
nicklas |
86 |
System.out.println(message); |
5188 |
09 Nov 09 |
nicklas |
87 |
} |
5188 |
09 Nov 09 |
nicklas |
88 |
|
5188 |
09 Nov 09 |
nicklas |
89 |
} |
5188 |
09 Nov 09 |
nicklas |
90 |
|