5193 |
27 Nov 09 |
nicklas |
1 |
/* |
5215 |
15 Jan 10 |
jari |
$Id$ |
5193 |
27 Nov 09 |
nicklas |
3 |
|
5193 |
27 Nov 09 |
nicklas |
Copyright (C) 2009 Nicklas Nordborg |
5193 |
27 Nov 09 |
nicklas |
5 |
|
5193 |
27 Nov 09 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5193 |
27 Nov 09 |
nicklas |
Available at http://base.thep.lu.se/ |
5193 |
27 Nov 09 |
nicklas |
8 |
|
5193 |
27 Nov 09 |
nicklas |
BASE is free software; you can redistribute it and/or |
5193 |
27 Nov 09 |
nicklas |
modify it under the terms of the GNU General Public License |
5193 |
27 Nov 09 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5193 |
27 Nov 09 |
nicklas |
of the License, or (at your option) any later version. |
5193 |
27 Nov 09 |
nicklas |
13 |
|
5193 |
27 Nov 09 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5193 |
27 Nov 09 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5193 |
27 Nov 09 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5193 |
27 Nov 09 |
nicklas |
GNU General Public License for more details. |
5193 |
27 Nov 09 |
nicklas |
18 |
|
5193 |
27 Nov 09 |
nicklas |
You should have received a copy of the GNU General Public License |
5193 |
27 Nov 09 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5193 |
27 Nov 09 |
nicklas |
21 |
*/ |
5193 |
27 Nov 09 |
nicklas |
22 |
import java.io.File; |
5193 |
27 Nov 09 |
nicklas |
23 |
import java.io.FileInputStream; |
5194 |
04 Dec 09 |
nicklas |
24 |
import java.io.FileOutputStream; |
5193 |
27 Nov 09 |
nicklas |
25 |
import java.io.FileWriter; |
5194 |
04 Dec 09 |
nicklas |
26 |
import java.io.OutputStreamWriter; |
5193 |
27 Nov 09 |
nicklas |
27 |
import java.io.Writer; |
7714 |
21 May 19 |
nicklas |
28 |
import java.nio.charset.StandardCharsets; |
5193 |
27 Nov 09 |
nicklas |
29 |
import java.util.Arrays; |
5193 |
27 Nov 09 |
nicklas |
30 |
|
5193 |
27 Nov 09 |
nicklas |
31 |
import net.sf.basedb.core.BaseException; |
5193 |
27 Nov 09 |
nicklas |
32 |
import net.sf.basedb.util.Values; |
5194 |
04 Dec 09 |
nicklas |
33 |
import net.sf.basedb.util.bfs.AnnotationModel; |
5193 |
27 Nov 09 |
nicklas |
34 |
import net.sf.basedb.util.bfs.AnnotationParser; |
5193 |
27 Nov 09 |
nicklas |
35 |
import net.sf.basedb.util.bfs.AnnotationWriter; |
5194 |
04 Dec 09 |
nicklas |
36 |
import net.sf.basedb.util.bfs.DataParser; |
5194 |
04 Dec 09 |
nicklas |
37 |
import net.sf.basedb.util.bfs.DataWriter; |
5194 |
04 Dec 09 |
nicklas |
38 |
import net.sf.basedb.util.bfs.MatrixModel; |
5193 |
27 Nov 09 |
nicklas |
39 |
import net.sf.basedb.util.bfs.MetadataModel; |
5193 |
27 Nov 09 |
nicklas |
40 |
import net.sf.basedb.util.bfs.MetadataParser; |
5193 |
27 Nov 09 |
nicklas |
41 |
import net.sf.basedb.util.bfs.MetadataWriter; |
5193 |
27 Nov 09 |
nicklas |
42 |
import net.sf.basedb.util.bfs.MetadataModel.Section; |
5193 |
27 Nov 09 |
nicklas |
43 |
|
5193 |
27 Nov 09 |
nicklas |
44 |
public class TestBfs |
5193 |
27 Nov 09 |
nicklas |
45 |
{ |
5193 |
27 Nov 09 |
nicklas |
46 |
|
5193 |
27 Nov 09 |
nicklas |
47 |
static boolean ok = true; |
5193 |
27 Nov 09 |
nicklas |
48 |
public static void main(String[] args) |
5193 |
27 Nov 09 |
nicklas |
49 |
{ |
5193 |
27 Nov 09 |
nicklas |
50 |
TestUtil.checkArgs(args); |
5193 |
27 Nov 09 |
nicklas |
51 |
TestUtil.begin(); |
5193 |
27 Nov 09 |
nicklas |
52 |
ok = test_all(); |
5193 |
27 Nov 09 |
nicklas |
53 |
TestUtil.stop(); |
5193 |
27 Nov 09 |
nicklas |
54 |
} |
5193 |
27 Nov 09 |
nicklas |
55 |
|
5193 |
27 Nov 09 |
nicklas |
56 |
static boolean test_all() |
5193 |
27 Nov 09 |
nicklas |
57 |
{ |
5193 |
27 Nov 09 |
nicklas |
58 |
write("++Testing BFS format"); |
5193 |
27 Nov 09 |
nicklas |
59 |
|
5193 |
27 Nov 09 |
nicklas |
60 |
File metadata = test_create_temp_file("metadata"); |
5194 |
04 Dec 09 |
nicklas |
61 |
File metadata_copy = test_create_temp_file("metadata_copy"); |
5193 |
27 Nov 09 |
nicklas |
62 |
test_write_metadata(metadata); |
5194 |
04 Dec 09 |
nicklas |
63 |
test_read_metadata(metadata, metadata_copy); |
5194 |
04 Dec 09 |
nicklas |
64 |
test_read_metadata(metadata_copy, null); |
5193 |
27 Nov 09 |
nicklas |
65 |
|
5193 |
27 Nov 09 |
nicklas |
66 |
File annotations = test_create_temp_file("annotations"); |
5193 |
27 Nov 09 |
nicklas |
67 |
test_write_annotations(annotations); |
5193 |
27 Nov 09 |
nicklas |
68 |
test_read_annotations(annotations); |
5193 |
27 Nov 09 |
nicklas |
69 |
|
5194 |
04 Dec 09 |
nicklas |
70 |
File data = test_create_temp_file("data"); |
5194 |
04 Dec 09 |
nicklas |
71 |
test_write_data(data); |
5194 |
04 Dec 09 |
nicklas |
72 |
test_read_data(data); |
5194 |
04 Dec 09 |
nicklas |
73 |
|
5193 |
27 Nov 09 |
nicklas |
74 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
5193 |
27 Nov 09 |
nicklas |
75 |
|
5193 |
27 Nov 09 |
nicklas |
76 |
write("++Testing BFS format "+(ok ? "OK" : "Failed")+"\n"); |
5193 |
27 Nov 09 |
nicklas |
77 |
return ok; |
5193 |
27 Nov 09 |
nicklas |
78 |
} |
5193 |
27 Nov 09 |
nicklas |
79 |
|
5193 |
27 Nov 09 |
nicklas |
80 |
static File test_create_temp_file(String prefix) |
5193 |
27 Nov 09 |
nicklas |
81 |
{ |
5193 |
27 Nov 09 |
nicklas |
82 |
File f = null; |
5193 |
27 Nov 09 |
nicklas |
83 |
try |
5193 |
27 Nov 09 |
nicklas |
84 |
{ |
5193 |
27 Nov 09 |
nicklas |
85 |
f = File.createTempFile(prefix, ".bfs.txt"); |
5193 |
27 Nov 09 |
nicklas |
86 |
f.deleteOnExit(); |
5193 |
27 Nov 09 |
nicklas |
87 |
} |
5193 |
27 Nov 09 |
nicklas |
88 |
catch (Throwable t) |
5193 |
27 Nov 09 |
nicklas |
89 |
{ |
5193 |
27 Nov 09 |
nicklas |
90 |
write("--Create BFS " + prefix + " FAILED"); |
5193 |
27 Nov 09 |
nicklas |
91 |
t.printStackTrace(); |
5193 |
27 Nov 09 |
nicklas |
92 |
ok = false; |
5193 |
27 Nov 09 |
nicklas |
93 |
} |
5193 |
27 Nov 09 |
nicklas |
94 |
return f; |
5193 |
27 Nov 09 |
nicklas |
95 |
} |
5193 |
27 Nov 09 |
nicklas |
96 |
|
5193 |
27 Nov 09 |
nicklas |
97 |
static void test_write_metadata(File file) |
5193 |
27 Nov 09 |
nicklas |
98 |
{ |
5193 |
27 Nov 09 |
nicklas |
99 |
if (file == null) return; |
5193 |
27 Nov 09 |
nicklas |
100 |
MetadataWriter bfs = null; |
5193 |
27 Nov 09 |
nicklas |
101 |
try |
5193 |
27 Nov 09 |
nicklas |
102 |
{ |
5193 |
27 Nov 09 |
nicklas |
103 |
Writer out = new FileWriter(file); |
5194 |
04 Dec 09 |
nicklas |
104 |
bfs = new MetadataWriter(out); |
5194 |
04 Dec 09 |
nicklas |
105 |
bfs.setSubtype("test"); |
5193 |
27 Nov 09 |
nicklas |
106 |
|
5193 |
27 Nov 09 |
nicklas |
// Test comments and empty lines |
5193 |
27 Nov 09 |
nicklas |
108 |
bfs.bfsPrintComment("A comment line"); |
5193 |
27 Nov 09 |
nicklas |
109 |
bfs.bfsPrintEmptyLine(1); |
5193 |
27 Nov 09 |
nicklas |
110 |
bfs.bfsPrintComment("A multiline comment\nis automatically generated"); |
5193 |
27 Nov 09 |
nicklas |
111 |
bfs.bfsPrintEmptyLine(1); |
5193 |
27 Nov 09 |
nicklas |
112 |
|
5193 |
27 Nov 09 |
nicklas |
// First section, a duplicate key is not allowed |
5193 |
27 Nov 09 |
nicklas |
114 |
bfs.bfsPrintSection("one", true, true); |
5193 |
27 Nov 09 |
nicklas |
115 |
bfs.bfsPrintValue("first", "The first value"); |
5193 |
27 Nov 09 |
nicklas |
116 |
bfs.bfsPrintValue("second", "The second value"); |
5193 |
27 Nov 09 |
nicklas |
117 |
try |
5193 |
27 Nov 09 |
nicklas |
118 |
{ |
5193 |
27 Nov 09 |
nicklas |
119 |
bfs.bfsPrintValue("second", "Not allowed"); |
5193 |
27 Nov 09 |
nicklas |
120 |
throw new BaseException("BFSMetadataWriter did not detect duplicate value"); |
5193 |
27 Nov 09 |
nicklas |
121 |
} |
5193 |
27 Nov 09 |
nicklas |
122 |
catch (IllegalArgumentException ex) |
5193 |
27 Nov 09 |
nicklas |
123 |
{} // Expected |
5193 |
27 Nov 09 |
nicklas |
124 |
|
5193 |
27 Nov 09 |
nicklas |
// Numeric values and a vector |
5193 |
27 Nov 09 |
nicklas |
126 |
bfs.bfsPrintValue("float", 8.0); |
5193 |
27 Nov 09 |
nicklas |
127 |
bfs.bfsPrintValue("vector", 1.22, 2.59999, -1.0); |
5193 |
27 Nov 09 |
nicklas |
128 |
bfs.bfsPrintEmptyLine(1); |
5193 |
27 Nov 09 |
nicklas |
129 |
|
5193 |
27 Nov 09 |
nicklas |
// Second section allow duplicate keys |
5193 |
27 Nov 09 |
nicklas |
131 |
bfs.bfsPrintSection("two", true, false); |
5193 |
27 Nov 09 |
nicklas |
132 |
bfs.bfsPrintValue("float", 8.0); |
5193 |
27 Nov 09 |
nicklas |
133 |
bfs.bfsPrintValue("float", 9.0); |
5193 |
27 Nov 09 |
nicklas |
134 |
|
5193 |
27 Nov 09 |
nicklas |
// A duplicate section is not allowed |
5193 |
27 Nov 09 |
nicklas |
136 |
try |
5193 |
27 Nov 09 |
nicklas |
137 |
{ |
5193 |
27 Nov 09 |
nicklas |
138 |
bfs.bfsPrintSection("two", true, false); |
5193 |
27 Nov 09 |
nicklas |
139 |
throw new BaseException("BFSMetadataWriter did not detect duplicate section"); |
5193 |
27 Nov 09 |
nicklas |
140 |
} |
5193 |
27 Nov 09 |
nicklas |
141 |
catch (IllegalArgumentException ex) |
5193 |
27 Nov 09 |
nicklas |
142 |
{} // Expected |
5193 |
27 Nov 09 |
nicklas |
143 |
|
5193 |
27 Nov 09 |
nicklas |
// The 'files' section is a pre-defined section |
5193 |
27 Nov 09 |
nicklas |
145 |
bfs.bfsPrintSection("files", true, true); |
5193 |
27 Nov 09 |
nicklas |
146 |
bfs.bfsPrintValue("file-1", "abc123.txt"); |
5193 |
27 Nov 09 |
nicklas |
147 |
bfs.bfsPrintValue("file-2", "def456.png"); |
5193 |
27 Nov 09 |
nicklas |
148 |
|
5193 |
27 Nov 09 |
nicklas |
149 |
bfs.flush(); |
5193 |
27 Nov 09 |
nicklas |
150 |
bfs.close(); |
5193 |
27 Nov 09 |
nicklas |
151 |
write("--Test BFS Metadata writer OK"); |
5193 |
27 Nov 09 |
nicklas |
152 |
} |
5193 |
27 Nov 09 |
nicklas |
153 |
catch (Throwable ex) |
5193 |
27 Nov 09 |
nicklas |
154 |
{ |
5193 |
27 Nov 09 |
nicklas |
155 |
write("--Test BFS Metadata writer FAILED: " + file); |
5193 |
27 Nov 09 |
nicklas |
156 |
ex.printStackTrace(); |
5193 |
27 Nov 09 |
nicklas |
157 |
ok = false; |
5193 |
27 Nov 09 |
nicklas |
158 |
} |
5193 |
27 Nov 09 |
nicklas |
159 |
finally |
5193 |
27 Nov 09 |
nicklas |
160 |
{ |
5193 |
27 Nov 09 |
nicklas |
161 |
if (bfs != null) |
5193 |
27 Nov 09 |
nicklas |
162 |
{ |
5193 |
27 Nov 09 |
nicklas |
163 |
bfs.flush(); |
5193 |
27 Nov 09 |
nicklas |
164 |
bfs.close(); |
5193 |
27 Nov 09 |
nicklas |
165 |
} |
5193 |
27 Nov 09 |
nicklas |
166 |
} |
5193 |
27 Nov 09 |
nicklas |
167 |
} |
5193 |
27 Nov 09 |
nicklas |
168 |
|
5194 |
04 Dec 09 |
nicklas |
169 |
static void test_read_metadata(File file, File copyTo) |
5193 |
27 Nov 09 |
nicklas |
170 |
{ |
5193 |
27 Nov 09 |
nicklas |
171 |
if (file == null) return; |
5193 |
27 Nov 09 |
nicklas |
172 |
boolean log = !TestUtil.getSilent(); |
5193 |
27 Nov 09 |
nicklas |
173 |
if (log) write(" Parsing BFS Metadata: " + file); |
5193 |
27 Nov 09 |
nicklas |
174 |
try |
5193 |
27 Nov 09 |
nicklas |
175 |
{ |
5193 |
27 Nov 09 |
nicklas |
176 |
MetadataParser bfsParser = new MetadataParser(); |
5194 |
04 Dec 09 |
nicklas |
177 |
MetadataModel bfs = new MetadataModel(); |
5221 |
22 Jan 10 |
nicklas |
178 |
bfsParser.setInputStream(new FileInputStream(file)); |
5221 |
22 Jan 10 |
nicklas |
179 |
bfsParser.setFilename(file.getAbsolutePath()); |
5221 |
22 Jan 10 |
nicklas |
180 |
bfsParser.parse(bfs); |
5193 |
27 Nov 09 |
nicklas |
181 |
|
5193 |
27 Nov 09 |
nicklas |
182 |
if (log) |
5193 |
27 Nov 09 |
nicklas |
183 |
{ |
5193 |
27 Nov 09 |
nicklas |
184 |
write(" BFS Subtype: " + bfs.getSubtype()); |
5193 |
27 Nov 09 |
nicklas |
185 |
write(" Sections: " + bfs.getSectionCount()); |
5193 |
27 Nov 09 |
nicklas |
186 |
for (int i = 0; i < bfs.getSectionCount(); ++i) |
5193 |
27 Nov 09 |
nicklas |
187 |
{ |
5193 |
27 Nov 09 |
nicklas |
188 |
Section s = bfs.getSection(i); |
5193 |
27 Nov 09 |
nicklas |
189 |
write(" Section " + i + ": " + s.getName()); |
5193 |
27 Nov 09 |
nicklas |
190 |
for (int j = 0; j < s.getEntryCount(); ++j) |
5193 |
27 Nov 09 |
nicklas |
191 |
{ |
5193 |
27 Nov 09 |
nicklas |
192 |
write(" " + s.getKey(j) + "=" + s.getValue(j)); |
5193 |
27 Nov 09 |
nicklas |
193 |
} |
5193 |
27 Nov 09 |
nicklas |
194 |
} |
5193 |
27 Nov 09 |
nicklas |
195 |
} |
5193 |
27 Nov 09 |
nicklas |
196 |
if (!"test".equals(bfs.getSubtype())) |
5193 |
27 Nov 09 |
nicklas |
197 |
{ |
5193 |
27 Nov 09 |
nicklas |
198 |
throw new BaseException("Unexpected BFS subtype: " + bfs.getSubtype()); |
5193 |
27 Nov 09 |
nicklas |
199 |
} |
5194 |
04 Dec 09 |
nicklas |
200 |
|
5193 |
27 Nov 09 |
nicklas |
201 |
if (bfs.getSectionCount() != 3) |
5193 |
27 Nov 09 |
nicklas |
202 |
{ |
5193 |
27 Nov 09 |
nicklas |
203 |
throw new BaseException("Unexpected number of sections: " + bfs.getSectionCount()); |
5193 |
27 Nov 09 |
nicklas |
204 |
} |
5193 |
27 Nov 09 |
nicklas |
205 |
Section one = bfs.getSection(0); |
5193 |
27 Nov 09 |
nicklas |
206 |
if (!"one".equals(one.getName())) |
5193 |
27 Nov 09 |
nicklas |
207 |
{ |
5193 |
27 Nov 09 |
nicklas |
208 |
throw new BaseException("Unexpected name of first section: " + one.getName()); |
5193 |
27 Nov 09 |
nicklas |
209 |
} |
5193 |
27 Nov 09 |
nicklas |
210 |
if (one.getEntryCount() != 4) |
5193 |
27 Nov 09 |
nicklas |
211 |
{ |
5193 |
27 Nov 09 |
nicklas |
212 |
throw new BaseException("Unexpected number of values in section 'one': " + one.getEntryCount()); |
5193 |
27 Nov 09 |
nicklas |
213 |
} |
5193 |
27 Nov 09 |
nicklas |
214 |
if (!"first".equals(one.getKey(0))) |
5193 |
27 Nov 09 |
nicklas |
215 |
{ |
5193 |
27 Nov 09 |
nicklas |
216 |
throw new BaseException("Unexpected key in section 'one', index 0: " + one.getKey(0)); |
5193 |
27 Nov 09 |
nicklas |
217 |
} |
5193 |
27 Nov 09 |
nicklas |
218 |
if (!"The first value".equals(one.getValue(0))) |
5193 |
27 Nov 09 |
nicklas |
219 |
{ |
5193 |
27 Nov 09 |
nicklas |
220 |
throw new BaseException("Unexpected value in section 'one', index 0: " + one.getValue(0)); |
5193 |
27 Nov 09 |
nicklas |
221 |
} |
5193 |
27 Nov 09 |
nicklas |
222 |
if (!"The second value".equals(one.getValue("second"))) |
5193 |
27 Nov 09 |
nicklas |
223 |
{ |
5193 |
27 Nov 09 |
nicklas |
224 |
throw new BaseException("Unexpected value in section 'one', index 'second': " + one.getValue("second")); |
5193 |
27 Nov 09 |
nicklas |
225 |
} |
5193 |
27 Nov 09 |
nicklas |
226 |
String[] vector = one.getValues("vector"); |
5193 |
27 Nov 09 |
nicklas |
227 |
if (vector.length != 3) |
5193 |
27 Nov 09 |
nicklas |
228 |
{ |
5193 |
27 Nov 09 |
nicklas |
229 |
throw new BaseException("Unexpected value in section 'one', index 'vector': " + one.getValue("vector")); |
5193 |
27 Nov 09 |
nicklas |
230 |
} |
5193 |
27 Nov 09 |
nicklas |
231 |
|
5193 |
27 Nov 09 |
nicklas |
232 |
Section two = bfs.getSection("two"); |
5193 |
27 Nov 09 |
nicklas |
233 |
if (two == null) |
5193 |
27 Nov 09 |
nicklas |
234 |
{ |
5193 |
27 Nov 09 |
nicklas |
235 |
throw new BaseException("Can't find section: two"); |
5193 |
27 Nov 09 |
nicklas |
236 |
} |
5193 |
27 Nov 09 |
nicklas |
237 |
if (two.getEntryCount() != 2) |
5193 |
27 Nov 09 |
nicklas |
238 |
{ |
5193 |
27 Nov 09 |
nicklas |
239 |
throw new BaseException("Unexpected number of values in section 'two': " + two.getEntryCount()); |
5193 |
27 Nov 09 |
nicklas |
240 |
} |
5193 |
27 Nov 09 |
nicklas |
241 |
if (!"float".equals(two.getKey(0))) |
5193 |
27 Nov 09 |
nicklas |
242 |
{ |
5193 |
27 Nov 09 |
nicklas |
243 |
throw new BaseException("Unexpected key in section 'two', index 0: " + two.getKey(0)); |
5193 |
27 Nov 09 |
nicklas |
244 |
} |
5193 |
27 Nov 09 |
nicklas |
245 |
if (!"float".equals(two.getKey(1))) |
5193 |
27 Nov 09 |
nicklas |
246 |
{ |
5193 |
27 Nov 09 |
nicklas |
247 |
throw new BaseException("Unexpected key in section 'two', index 1: " + two.getKey(1)); |
5193 |
27 Nov 09 |
nicklas |
248 |
} |
5193 |
27 Nov 09 |
nicklas |
249 |
|
5193 |
27 Nov 09 |
nicklas |
250 |
if (bfs.getFileCount() != 2) |
5193 |
27 Nov 09 |
nicklas |
251 |
{ |
5193 |
27 Nov 09 |
nicklas |
252 |
throw new BaseException("Unexpected number of file: " + bfs.getFileCount()); |
5193 |
27 Nov 09 |
nicklas |
253 |
} |
5193 |
27 Nov 09 |
nicklas |
254 |
if (!"abc123.txt".equals(bfs.getFile(0))) |
5193 |
27 Nov 09 |
nicklas |
255 |
{ |
5193 |
27 Nov 09 |
nicklas |
256 |
throw new BaseException("Unexpected file, index 0: " + bfs.getFile(0)); |
5193 |
27 Nov 09 |
nicklas |
257 |
} |
5193 |
27 Nov 09 |
nicklas |
258 |
if (!"def456.png".equals(bfs.getFile("file-2"))) |
5193 |
27 Nov 09 |
nicklas |
259 |
{ |
5193 |
27 Nov 09 |
nicklas |
260 |
throw new BaseException("Unexpected file, index 'file-2': " + bfs.getFile("file-2")); |
5193 |
27 Nov 09 |
nicklas |
261 |
} |
5194 |
04 Dec 09 |
nicklas |
262 |
|
5194 |
04 Dec 09 |
nicklas |
263 |
if (copyTo != null) |
5194 |
04 Dec 09 |
nicklas |
264 |
{ |
7714 |
21 May 19 |
nicklas |
265 |
bfs.print(new OutputStreamWriter(new FileOutputStream(copyTo), StandardCharsets.UTF_8)); |
5194 |
04 Dec 09 |
nicklas |
266 |
} |
5194 |
04 Dec 09 |
nicklas |
267 |
|
5193 |
27 Nov 09 |
nicklas |
268 |
write("--Test BFS Metadata parser OK"); |
5193 |
27 Nov 09 |
nicklas |
269 |
} |
5193 |
27 Nov 09 |
nicklas |
270 |
catch (Throwable ex) |
5193 |
27 Nov 09 |
nicklas |
271 |
{ |
5193 |
27 Nov 09 |
nicklas |
272 |
write("--Test BFS Metadata parser FAILED: " + file); |
5193 |
27 Nov 09 |
nicklas |
273 |
ex.printStackTrace(); |
5193 |
27 Nov 09 |
nicklas |
274 |
ok = false; |
5193 |
27 Nov 09 |
nicklas |
275 |
} |
5193 |
27 Nov 09 |
nicklas |
276 |
} |
5193 |
27 Nov 09 |
nicklas |
277 |
|
5193 |
27 Nov 09 |
nicklas |
278 |
static void test_write_annotations(File file) |
5193 |
27 Nov 09 |
nicklas |
279 |
{ |
5193 |
27 Nov 09 |
nicklas |
280 |
if (file == null) return; |
5193 |
27 Nov 09 |
nicklas |
281 |
AnnotationWriter bfs = null; |
5193 |
27 Nov 09 |
nicklas |
282 |
try |
5193 |
27 Nov 09 |
nicklas |
283 |
{ |
5193 |
27 Nov 09 |
nicklas |
284 |
Writer out = new FileWriter(file); |
5193 |
27 Nov 09 |
nicklas |
285 |
bfs = new AnnotationWriter(out); |
5193 |
27 Nov 09 |
nicklas |
286 |
|
5193 |
27 Nov 09 |
nicklas |
// Invalid headers with duplicates |
5193 |
27 Nov 09 |
nicklas |
288 |
try |
5193 |
27 Nov 09 |
nicklas |
289 |
{ |
5193 |
27 Nov 09 |
nicklas |
290 |
bfs.bfsPrintHeaders("name", "description", "name"); |
5193 |
27 Nov 09 |
nicklas |
291 |
throw new BaseException("AnnotationWriter did not detect duplicate column name"); |
5193 |
27 Nov 09 |
nicklas |
292 |
} |
5193 |
27 Nov 09 |
nicklas |
293 |
catch (IllegalArgumentException ex) |
5193 |
27 Nov 09 |
nicklas |
294 |
{} // Expected |
5193 |
27 Nov 09 |
nicklas |
295 |
|
5193 |
27 Nov 09 |
nicklas |
// Valid headers |
5193 |
27 Nov 09 |
nicklas |
297 |
bfs.bfsPrintHeaders("name", "decription", "size"); |
5193 |
27 Nov 09 |
nicklas |
298 |
|
5193 |
27 Nov 09 |
nicklas |
// Two valid lines with data |
5193 |
27 Nov 09 |
nicklas |
300 |
bfs.bfsPrintData(1, "item-1", "The first item", 55.3); |
5193 |
27 Nov 09 |
nicklas |
301 |
bfs.bfsPrintData(2, "item-2", "The second item has a <\t> (tab) and <\n> (linebreak)", -44); |
5193 |
27 Nov 09 |
nicklas |
302 |
|
5193 |
27 Nov 09 |
nicklas |
// A duplicate id |
5193 |
27 Nov 09 |
nicklas |
304 |
try |
5193 |
27 Nov 09 |
nicklas |
305 |
{ |
5193 |
27 Nov 09 |
nicklas |
306 |
bfs.bfsPrintData(1, "duplicate", "", null); |
5193 |
27 Nov 09 |
nicklas |
307 |
throw new BaseException("AnnotationWriter did not detect duplicate row id"); |
5193 |
27 Nov 09 |
nicklas |
308 |
} |
5193 |
27 Nov 09 |
nicklas |
309 |
catch (IllegalArgumentException ex) |
5193 |
27 Nov 09 |
nicklas |
310 |
{} // Expected |
5193 |
27 Nov 09 |
nicklas |
311 |
|
5193 |
27 Nov 09 |
nicklas |
// Incorrect number of columns |
5193 |
27 Nov 09 |
nicklas |
313 |
try |
5193 |
27 Nov 09 |
nicklas |
314 |
{ |
5193 |
27 Nov 09 |
nicklas |
315 |
bfs.bfsPrintData(3, "item-3", "The third item is missing size"); |
5193 |
27 Nov 09 |
nicklas |
316 |
throw new BaseException("AnnotationWriter did not detect incorrect number of columns"); |
5193 |
27 Nov 09 |
nicklas |
317 |
} |
5193 |
27 Nov 09 |
nicklas |
318 |
catch (IllegalArgumentException ex) |
5193 |
27 Nov 09 |
nicklas |
319 |
{} // Expected |
5193 |
27 Nov 09 |
nicklas |
320 |
|
5193 |
27 Nov 09 |
nicklas |
321 |
bfs.flush(); |
5193 |
27 Nov 09 |
nicklas |
322 |
bfs.close(); |
5193 |
27 Nov 09 |
nicklas |
323 |
write("--Test BFS Annotation writer OK"); |
5193 |
27 Nov 09 |
nicklas |
324 |
} |
5193 |
27 Nov 09 |
nicklas |
325 |
catch (Throwable ex) |
5193 |
27 Nov 09 |
nicklas |
326 |
{ |
5193 |
27 Nov 09 |
nicklas |
327 |
write("--Test BFS Annotation writer FAILED: " + file); |
5193 |
27 Nov 09 |
nicklas |
328 |
ex.printStackTrace(); |
5193 |
27 Nov 09 |
nicklas |
329 |
ok = false; |
5193 |
27 Nov 09 |
nicklas |
330 |
} |
5193 |
27 Nov 09 |
nicklas |
331 |
finally |
5193 |
27 Nov 09 |
nicklas |
332 |
{ |
5193 |
27 Nov 09 |
nicklas |
333 |
if (bfs != null) |
5193 |
27 Nov 09 |
nicklas |
334 |
{ |
5193 |
27 Nov 09 |
nicklas |
335 |
bfs.flush(); |
5193 |
27 Nov 09 |
nicklas |
336 |
bfs.close(); |
5193 |
27 Nov 09 |
nicklas |
337 |
} |
5193 |
27 Nov 09 |
nicklas |
338 |
} |
5193 |
27 Nov 09 |
nicklas |
339 |
} |
5193 |
27 Nov 09 |
nicklas |
340 |
|
5193 |
27 Nov 09 |
nicklas |
341 |
static void test_read_annotations(File file) |
5193 |
27 Nov 09 |
nicklas |
342 |
{ |
5193 |
27 Nov 09 |
nicklas |
343 |
if (file == null) return; |
5193 |
27 Nov 09 |
nicklas |
344 |
final boolean log = !TestUtil.getSilent(); |
5193 |
27 Nov 09 |
nicklas |
345 |
if (log) write(" Parsing BFS Annotations: " + file); |
5193 |
27 Nov 09 |
nicklas |
346 |
try |
5193 |
27 Nov 09 |
nicklas |
347 |
{ |
5193 |
27 Nov 09 |
nicklas |
348 |
AnnotationParser bfsParser = new AnnotationParser(); |
5194 |
04 Dec 09 |
nicklas |
349 |
AnnotationModel bfs = new AnnotationModel(); |
5221 |
22 Jan 10 |
nicklas |
350 |
bfsParser.setInputStream(new FileInputStream(file)); |
5221 |
22 Jan 10 |
nicklas |
351 |
bfsParser.setFilename(file.getAbsolutePath()); |
5221 |
22 Jan 10 |
nicklas |
352 |
bfsParser.parse(bfs); |
5194 |
04 Dec 09 |
nicklas |
353 |
|
5194 |
04 Dec 09 |
nicklas |
354 |
String[] headers = bfs.getHeaders(); |
5194 |
04 Dec 09 |
nicklas |
355 |
int rowCount = bfs.getRowCount(); |
5194 |
04 Dec 09 |
nicklas |
356 |
int columnCount = bfs.getColumnCount(); |
5193 |
27 Nov 09 |
nicklas |
357 |
|
5194 |
04 Dec 09 |
nicklas |
358 |
if (log) |
5194 |
04 Dec 09 |
nicklas |
359 |
{ |
5194 |
04 Dec 09 |
nicklas |
360 |
write(" Rows: " + rowCount + "; columns: " + columnCount); |
5194 |
04 Dec 09 |
nicklas |
361 |
write(" Header: " + |
5194 |
04 Dec 09 |
nicklas |
362 |
Values.getString(Arrays.asList(headers), "\t", false)); |
5194 |
04 Dec 09 |
nicklas |
363 |
|
5194 |
04 Dec 09 |
nicklas |
364 |
for (int r = 0; r < rowCount; r++) |
5193 |
27 Nov 09 |
nicklas |
365 |
{ |
5194 |
04 Dec 09 |
nicklas |
366 |
int rowId = bfs.getRowId(r); |
5194 |
04 Dec 09 |
nicklas |
367 |
String[] row = bfs.getRowById(rowId); |
5194 |
04 Dec 09 |
nicklas |
368 |
write(" Line " + r + ": " + |
5194 |
04 Dec 09 |
nicklas |
369 |
Values.getString(Arrays.asList(row), "\t", false)); |
5194 |
04 Dec 09 |
nicklas |
370 |
if (row.length != columnCount) |
5193 |
27 Nov 09 |
nicklas |
371 |
{ |
5194 |
04 Dec 09 |
nicklas |
372 |
throw new BaseException("Expected " + columnCount + |
5194 |
04 Dec 09 |
nicklas |
373 |
" data columns, not " + row.length); |
5193 |
27 Nov 09 |
nicklas |
374 |
} |
5193 |
27 Nov 09 |
nicklas |
375 |
} |
5194 |
04 Dec 09 |
nicklas |
376 |
} |
5194 |
04 Dec 09 |
nicklas |
377 |
|
5194 |
04 Dec 09 |
nicklas |
378 |
if (columnCount != 4) |
5194 |
04 Dec 09 |
nicklas |
379 |
{ |
5194 |
04 Dec 09 |
nicklas |
380 |
throw new BaseException("Expected 4 header columns, not " + headers.length); |
5194 |
04 Dec 09 |
nicklas |
381 |
} |
5194 |
04 Dec 09 |
nicklas |
382 |
|
5194 |
04 Dec 09 |
nicklas |
383 |
if (rowCount != 2) |
5194 |
04 Dec 09 |
nicklas |
384 |
{ |
5194 |
04 Dec 09 |
nicklas |
385 |
throw new BaseException("Expected 2 lines with data, not " + rowCount); |
5194 |
04 Dec 09 |
nicklas |
386 |
} |
5194 |
04 Dec 09 |
nicklas |
387 |
|
5194 |
04 Dec 09 |
nicklas |
388 |
if (bfs.getRowId(0) != 1) |
5194 |
04 Dec 09 |
nicklas |
389 |
{ |
5194 |
04 Dec 09 |
nicklas |
390 |
throw new BaseException("Unexpected row id at line 0: " + bfs.getRowId(0)); |
5194 |
04 Dec 09 |
nicklas |
391 |
} |
5194 |
04 Dec 09 |
nicklas |
392 |
if (bfs.getRowIndex(2) != 1) |
5194 |
04 Dec 09 |
nicklas |
393 |
{ |
5194 |
04 Dec 09 |
nicklas |
394 |
throw new BaseException("Unexpected row indx for row id '2': " + bfs.getRowIndex(2)); |
5194 |
04 Dec 09 |
nicklas |
395 |
} |
5194 |
04 Dec 09 |
nicklas |
396 |
|
5193 |
27 Nov 09 |
nicklas |
397 |
write("--Test BFS Annotation parser OK"); |
5193 |
27 Nov 09 |
nicklas |
398 |
} |
5193 |
27 Nov 09 |
nicklas |
399 |
catch (Throwable ex) |
5193 |
27 Nov 09 |
nicklas |
400 |
{ |
5193 |
27 Nov 09 |
nicklas |
401 |
write("--Test BFS Annotation parser FAILED: " + file); |
5193 |
27 Nov 09 |
nicklas |
402 |
ex.printStackTrace(); |
5193 |
27 Nov 09 |
nicklas |
403 |
ok = false; |
5193 |
27 Nov 09 |
nicklas |
404 |
} |
5193 |
27 Nov 09 |
nicklas |
405 |
} |
5193 |
27 Nov 09 |
nicklas |
406 |
|
5194 |
04 Dec 09 |
nicklas |
407 |
static void test_write_data(File file) |
5194 |
04 Dec 09 |
nicklas |
408 |
{ |
5194 |
04 Dec 09 |
nicklas |
409 |
if (file == null) return; |
5194 |
04 Dec 09 |
nicklas |
410 |
DataWriter bfs = null; |
5194 |
04 Dec 09 |
nicklas |
411 |
try |
5194 |
04 Dec 09 |
nicklas |
412 |
{ |
5194 |
04 Dec 09 |
nicklas |
413 |
Writer out = new FileWriter(file); |
5194 |
04 Dec 09 |
nicklas |
414 |
bfs = new DataWriter(out); |
5194 |
04 Dec 09 |
nicklas |
415 |
|
5194 |
04 Dec 09 |
nicklas |
416 |
bfs.setColumnCount(4); |
5194 |
04 Dec 09 |
nicklas |
417 |
|
5194 |
04 Dec 09 |
nicklas |
418 |
|
5194 |
04 Dec 09 |
nicklas |
// Two valid lines with data |
5194 |
04 Dec 09 |
nicklas |
420 |
bfs.bfsPrintData(1, "item-1", "The first item", 55.3); |
5194 |
04 Dec 09 |
nicklas |
421 |
bfs.bfsPrintData(2, "item-2", "The second item has a <\t> (tab) and <\n> (linebreak)", -44); |
5194 |
04 Dec 09 |
nicklas |
422 |
|
5194 |
04 Dec 09 |
nicklas |
// Incorrect number of columns |
5194 |
04 Dec 09 |
nicklas |
424 |
try |
5194 |
04 Dec 09 |
nicklas |
425 |
{ |
5194 |
04 Dec 09 |
nicklas |
426 |
bfs.bfsPrintData(3, "item-3", "The third line is missing a column"); |
5194 |
04 Dec 09 |
nicklas |
427 |
throw new BaseException("DataWriter did not detect incorrect number of columns"); |
5194 |
04 Dec 09 |
nicklas |
428 |
} |
5194 |
04 Dec 09 |
nicklas |
429 |
catch (IllegalArgumentException ex) |
5194 |
04 Dec 09 |
nicklas |
430 |
{} // Expected |
5194 |
04 Dec 09 |
nicklas |
431 |
|
5194 |
04 Dec 09 |
nicklas |
432 |
bfs.flush(); |
5194 |
04 Dec 09 |
nicklas |
433 |
bfs.close(); |
5194 |
04 Dec 09 |
nicklas |
434 |
write("--Test BFS Data writer OK"); |
5194 |
04 Dec 09 |
nicklas |
435 |
} |
5194 |
04 Dec 09 |
nicklas |
436 |
catch (Throwable ex) |
5194 |
04 Dec 09 |
nicklas |
437 |
{ |
5194 |
04 Dec 09 |
nicklas |
438 |
write("--Test BFS Data writer FAILED: " + file); |
5194 |
04 Dec 09 |
nicklas |
439 |
ex.printStackTrace(); |
5194 |
04 Dec 09 |
nicklas |
440 |
ok = false; |
5194 |
04 Dec 09 |
nicklas |
441 |
} |
5194 |
04 Dec 09 |
nicklas |
442 |
finally |
5194 |
04 Dec 09 |
nicklas |
443 |
{ |
5194 |
04 Dec 09 |
nicklas |
444 |
if (bfs != null) |
5194 |
04 Dec 09 |
nicklas |
445 |
{ |
5194 |
04 Dec 09 |
nicklas |
446 |
bfs.flush(); |
5194 |
04 Dec 09 |
nicklas |
447 |
bfs.close(); |
5194 |
04 Dec 09 |
nicklas |
448 |
} |
5194 |
04 Dec 09 |
nicklas |
449 |
} |
5194 |
04 Dec 09 |
nicklas |
450 |
} |
5194 |
04 Dec 09 |
nicklas |
451 |
|
5194 |
04 Dec 09 |
nicklas |
452 |
static void test_read_data(File file) |
5194 |
04 Dec 09 |
nicklas |
453 |
{ |
5194 |
04 Dec 09 |
nicklas |
454 |
if (file == null) return; |
5194 |
04 Dec 09 |
nicklas |
455 |
final boolean log = !TestUtil.getSilent(); |
5194 |
04 Dec 09 |
nicklas |
456 |
if (log) write(" Parsing BFS Data: " + file); |
5194 |
04 Dec 09 |
nicklas |
457 |
try |
5194 |
04 Dec 09 |
nicklas |
458 |
{ |
5194 |
04 Dec 09 |
nicklas |
459 |
DataParser bfsParser = new DataParser(); |
5194 |
04 Dec 09 |
nicklas |
460 |
MatrixModel bfs = new MatrixModel(); |
5221 |
22 Jan 10 |
nicklas |
461 |
bfsParser.setInputStream(new FileInputStream(file)); |
5221 |
22 Jan 10 |
nicklas |
462 |
bfsParser.setFilename(file.getAbsolutePath()); |
5221 |
22 Jan 10 |
nicklas |
463 |
bfsParser.parse(bfs); |
5194 |
04 Dec 09 |
nicklas |
464 |
|
5194 |
04 Dec 09 |
nicklas |
465 |
int rowCount = bfs.getRowCount(); |
5194 |
04 Dec 09 |
nicklas |
466 |
int columnCount = bfs.getColumnCount(); |
5194 |
04 Dec 09 |
nicklas |
467 |
|
5194 |
04 Dec 09 |
nicklas |
468 |
if (log) |
5194 |
04 Dec 09 |
nicklas |
469 |
{ |
5194 |
04 Dec 09 |
nicklas |
470 |
write(" Rows: " + rowCount + "; columns: " + columnCount); |
5194 |
04 Dec 09 |
nicklas |
471 |
|
5194 |
04 Dec 09 |
nicklas |
472 |
for (int r = 0; r < rowCount; r++) |
5194 |
04 Dec 09 |
nicklas |
473 |
{ |
5194 |
04 Dec 09 |
nicklas |
474 |
String[] row = bfs.getRowByIndex(r); |
5194 |
04 Dec 09 |
nicklas |
475 |
write(" Line " + r + ": " + |
5194 |
04 Dec 09 |
nicklas |
476 |
Values.getString(Arrays.asList(row), "\t", false)); |
5194 |
04 Dec 09 |
nicklas |
477 |
if (row.length != columnCount) |
5194 |
04 Dec 09 |
nicklas |
478 |
{ |
5194 |
04 Dec 09 |
nicklas |
479 |
throw new BaseException("Expected " + columnCount + |
5194 |
04 Dec 09 |
nicklas |
480 |
" data columns, not " + row.length); |
5194 |
04 Dec 09 |
nicklas |
481 |
} |
5194 |
04 Dec 09 |
nicklas |
482 |
} |
5194 |
04 Dec 09 |
nicklas |
483 |
} |
5194 |
04 Dec 09 |
nicklas |
484 |
|
5194 |
04 Dec 09 |
nicklas |
485 |
if (columnCount != 4) |
5194 |
04 Dec 09 |
nicklas |
486 |
{ |
5194 |
04 Dec 09 |
nicklas |
487 |
throw new BaseException("Expected 4 data columns, not " + columnCount); |
5194 |
04 Dec 09 |
nicklas |
488 |
} |
5194 |
04 Dec 09 |
nicklas |
489 |
|
5194 |
04 Dec 09 |
nicklas |
490 |
if (rowCount != 2) |
5194 |
04 Dec 09 |
nicklas |
491 |
{ |
5194 |
04 Dec 09 |
nicklas |
492 |
throw new BaseException("Expected 2 lines with data, not " + rowCount); |
5194 |
04 Dec 09 |
nicklas |
493 |
} |
5194 |
04 Dec 09 |
nicklas |
494 |
|
5194 |
04 Dec 09 |
nicklas |
495 |
write("--Test BFS Data parser OK"); |
5194 |
04 Dec 09 |
nicklas |
496 |
} |
5194 |
04 Dec 09 |
nicklas |
497 |
catch (Throwable ex) |
5194 |
04 Dec 09 |
nicklas |
498 |
{ |
5194 |
04 Dec 09 |
nicklas |
499 |
write("--Test BFS Data parser FAILED: " + file); |
5194 |
04 Dec 09 |
nicklas |
500 |
ex.printStackTrace(); |
5194 |
04 Dec 09 |
nicklas |
501 |
ok = false; |
5194 |
04 Dec 09 |
nicklas |
502 |
} |
5194 |
04 Dec 09 |
nicklas |
503 |
} |
5194 |
04 Dec 09 |
nicklas |
504 |
|
5194 |
04 Dec 09 |
nicklas |
505 |
|
5193 |
27 Nov 09 |
nicklas |
506 |
static void write(String message) |
5193 |
27 Nov 09 |
nicklas |
507 |
{ |
5193 |
27 Nov 09 |
nicklas |
508 |
System.out.println(message); |
5193 |
27 Nov 09 |
nicklas |
509 |
} |
5193 |
27 Nov 09 |
nicklas |
510 |
|
5193 |
27 Nov 09 |
nicklas |
511 |
} |
5193 |
27 Nov 09 |
nicklas |
512 |
|