2104 |
23 Mar 06 |
nicklas |
1 |
/* |
2104 |
23 Mar 06 |
nicklas |
$Id$ |
2104 |
23 Mar 06 |
nicklas |
3 |
|
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
2104 |
23 Mar 06 |
nicklas |
5 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
2104 |
23 Mar 06 |
nicklas |
8 |
|
2104 |
23 Mar 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
2104 |
23 Mar 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4480 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2104 |
23 Mar 06 |
nicklas |
of the License, or (at your option) any later version. |
2104 |
23 Mar 06 |
nicklas |
13 |
|
2104 |
23 Mar 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
2104 |
23 Mar 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2104 |
23 Mar 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2104 |
23 Mar 06 |
nicklas |
GNU General Public License for more details. |
2104 |
23 Mar 06 |
nicklas |
18 |
|
2104 |
23 Mar 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2104 |
23 Mar 06 |
nicklas |
21 |
*/ |
2104 |
23 Mar 06 |
nicklas |
22 |
import net.sf.basedb.core.*; |
2104 |
23 Mar 06 |
nicklas |
23 |
|
2104 |
23 Mar 06 |
nicklas |
24 |
import java.util.Arrays; |
2104 |
23 Mar 06 |
nicklas |
25 |
import java.util.Date; |
5340 |
10 May 10 |
nicklas |
26 |
import java.util.Set; |
2104 |
23 Mar 06 |
nicklas |
27 |
|
2104 |
23 Mar 06 |
nicklas |
28 |
public class TestFormula |
2104 |
23 Mar 06 |
nicklas |
29 |
{ |
2104 |
23 Mar 06 |
nicklas |
30 |
|
2104 |
23 Mar 06 |
nicklas |
31 |
static boolean ok = true; |
2104 |
23 Mar 06 |
nicklas |
32 |
public static void main(String[] args) |
2104 |
23 Mar 06 |
nicklas |
33 |
{ |
2104 |
23 Mar 06 |
nicklas |
34 |
TestUtil.checkArgs(args); |
2104 |
23 Mar 06 |
nicklas |
35 |
TestUtil.begin(); |
2104 |
23 Mar 06 |
nicklas |
36 |
ok = test_all(); |
2104 |
23 Mar 06 |
nicklas |
37 |
TestUtil.stop(); |
2104 |
23 Mar 06 |
nicklas |
38 |
} |
2104 |
23 Mar 06 |
nicklas |
39 |
|
2104 |
23 Mar 06 |
nicklas |
40 |
static boolean test_all() |
2104 |
23 Mar 06 |
nicklas |
41 |
{ |
2104 |
23 Mar 06 |
nicklas |
42 |
write("++Testing formulas"); |
2104 |
23 Mar 06 |
nicklas |
43 |
write_header(); |
2104 |
23 Mar 06 |
nicklas |
// Standard tests: create, load, list |
4232 |
17 Apr 08 |
nicklas |
45 |
int id = test_create("add", Formula.Type.COLUMN_EXPRESSION, |
4232 |
17 Apr 08 |
nicklas |
46 |
"genepix", new String[] { "ch(1) + ch(2)" }, true); |
4232 |
17 Apr 08 |
nicklas |
47 |
int id2 = test_create("raw fg mean", Formula.Type.INTENSITY_EXPRESSION, |
4232 |
17 Apr 08 |
nicklas |
48 |
"genepix", new String[] { "raw('ch1FgMean')", "raw('ch2FgMean')" }, false); |
2104 |
23 Mar 06 |
nicklas |
49 |
test_load(id); |
2104 |
23 Mar 06 |
nicklas |
50 |
test_list(-1); |
2104 |
23 Mar 06 |
nicklas |
51 |
|
2104 |
23 Mar 06 |
nicklas |
// Standard test: Delete |
2104 |
23 Mar 06 |
nicklas |
53 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
2104 |
23 Mar 06 |
nicklas |
54 |
test_delete(id); |
2104 |
23 Mar 06 |
nicklas |
55 |
test_delete(id2); |
2104 |
23 Mar 06 |
nicklas |
56 |
write("++Testing formulas "+(ok ? "OK" : "Failed")+"\n"); |
2104 |
23 Mar 06 |
nicklas |
57 |
return ok; |
2104 |
23 Mar 06 |
nicklas |
58 |
} |
2104 |
23 Mar 06 |
nicklas |
59 |
|
4232 |
17 Apr 08 |
nicklas |
60 |
static int test_create(String name, Formula.Type formulaType, String rawDataType, String[] formulas, boolean setAll) |
2104 |
23 Mar 06 |
nicklas |
61 |
{ |
2104 |
23 Mar 06 |
nicklas |
62 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.FORMULA)) return 0; |
2104 |
23 Mar 06 |
nicklas |
63 |
int id = 0; |
2104 |
23 Mar 06 |
nicklas |
64 |
DbControl dc = null; |
2104 |
23 Mar 06 |
nicklas |
65 |
try |
2104 |
23 Mar 06 |
nicklas |
66 |
{ |
2104 |
23 Mar 06 |
nicklas |
67 |
dc = TestUtil.getDbControl(); |
2104 |
23 Mar 06 |
nicklas |
68 |
Formula f = Formula.getNew(dc); |
2104 |
23 Mar 06 |
nicklas |
69 |
if (setAll) |
2104 |
23 Mar 06 |
nicklas |
70 |
{ |
2104 |
23 Mar 06 |
nicklas |
71 |
f.setName("Test formula"); |
2104 |
23 Mar 06 |
nicklas |
72 |
f.setDescription("Added at "+new Date()); |
2104 |
23 Mar 06 |
nicklas |
73 |
f.setChannels(2); |
2104 |
23 Mar 06 |
nicklas |
74 |
f.setParser(Formula.Parser.JEP); |
5319 |
20 Apr 10 |
nicklas |
75 |
Coloring c = f.getColoring(); |
5319 |
20 Apr 10 |
nicklas |
76 |
c.setLogarithmic(true); |
5319 |
20 Apr 10 |
nicklas |
77 |
c.setMidValue(0f); |
5319 |
20 Apr 10 |
nicklas |
78 |
c.setMinValue(-10f); |
5319 |
20 Apr 10 |
nicklas |
79 |
c.setMaxValue(10f); |
5319 |
20 Apr 10 |
nicklas |
80 |
c.setUsingColors(true); |
2104 |
23 Mar 06 |
nicklas |
81 |
} |
4232 |
17 Apr 08 |
nicklas |
82 |
if (name != null) f.setName(name); |
2104 |
23 Mar 06 |
nicklas |
83 |
f.setFormulaType(formulaType); |
2104 |
23 Mar 06 |
nicklas |
84 |
f.setRawDataType(RawDataTypes.getRawDataType(rawDataType)); |
2104 |
23 Mar 06 |
nicklas |
85 |
f.setFormulas(Arrays.asList(formulas)); |
2104 |
23 Mar 06 |
nicklas |
86 |
dc.saveItem(f); |
2104 |
23 Mar 06 |
nicklas |
87 |
dc.commit(); |
2104 |
23 Mar 06 |
nicklas |
88 |
id = f.getId(); |
2104 |
23 Mar 06 |
nicklas |
89 |
write_item(0, f); |
2104 |
23 Mar 06 |
nicklas |
90 |
write("--Create formula OK"); |
2104 |
23 Mar 06 |
nicklas |
91 |
} |
2104 |
23 Mar 06 |
nicklas |
92 |
catch (Throwable ex) |
2104 |
23 Mar 06 |
nicklas |
93 |
{ |
2104 |
23 Mar 06 |
nicklas |
94 |
write("--Create formula FAILED"); |
2104 |
23 Mar 06 |
nicklas |
95 |
ex.printStackTrace(); |
2104 |
23 Mar 06 |
nicklas |
96 |
ok = false; |
2104 |
23 Mar 06 |
nicklas |
97 |
} |
2104 |
23 Mar 06 |
nicklas |
98 |
finally |
2104 |
23 Mar 06 |
nicklas |
99 |
{ |
2104 |
23 Mar 06 |
nicklas |
100 |
if (dc != null) dc.close(); |
2104 |
23 Mar 06 |
nicklas |
101 |
} |
2104 |
23 Mar 06 |
nicklas |
102 |
return id; |
2104 |
23 Mar 06 |
nicklas |
103 |
} |
2104 |
23 Mar 06 |
nicklas |
104 |
|
2104 |
23 Mar 06 |
nicklas |
105 |
static void test_load(int id) |
2104 |
23 Mar 06 |
nicklas |
106 |
{ |
2104 |
23 Mar 06 |
nicklas |
107 |
if (id == 0) return; |
2104 |
23 Mar 06 |
nicklas |
108 |
DbControl dc = null; |
2104 |
23 Mar 06 |
nicklas |
109 |
try |
2104 |
23 Mar 06 |
nicklas |
110 |
{ |
2104 |
23 Mar 06 |
nicklas |
111 |
dc = TestUtil.getDbControl(); |
2104 |
23 Mar 06 |
nicklas |
112 |
Formula f = Formula.getById(dc, id); |
2104 |
23 Mar 06 |
nicklas |
113 |
write_item(0, f); |
2104 |
23 Mar 06 |
nicklas |
114 |
write("--Load formula OK"); |
2104 |
23 Mar 06 |
nicklas |
115 |
} |
2104 |
23 Mar 06 |
nicklas |
116 |
catch (Throwable ex) |
2104 |
23 Mar 06 |
nicklas |
117 |
{ |
2104 |
23 Mar 06 |
nicklas |
118 |
write("--Load formula FAILED"); |
2104 |
23 Mar 06 |
nicklas |
119 |
ex.printStackTrace(); |
2104 |
23 Mar 06 |
nicklas |
120 |
ok = false; |
2104 |
23 Mar 06 |
nicklas |
121 |
} |
2104 |
23 Mar 06 |
nicklas |
122 |
finally |
2104 |
23 Mar 06 |
nicklas |
123 |
{ |
2104 |
23 Mar 06 |
nicklas |
124 |
if (dc != null) dc.close(); |
2104 |
23 Mar 06 |
nicklas |
125 |
} |
2104 |
23 Mar 06 |
nicklas |
126 |
} |
2104 |
23 Mar 06 |
nicklas |
127 |
|
2104 |
23 Mar 06 |
nicklas |
128 |
static void test_list(int expectedResults) |
2104 |
23 Mar 06 |
nicklas |
129 |
{ |
2104 |
23 Mar 06 |
nicklas |
130 |
DbControl dc = null; |
2104 |
23 Mar 06 |
nicklas |
131 |
try |
2104 |
23 Mar 06 |
nicklas |
132 |
{ |
2104 |
23 Mar 06 |
nicklas |
133 |
dc = TestUtil.getDbControl(); |
2104 |
23 Mar 06 |
nicklas |
134 |
ItemResultList<Formula> l = Formula.getQuery().list(dc); |
2104 |
23 Mar 06 |
nicklas |
135 |
for (int i = 0; i<l.size(); i++) |
2104 |
23 Mar 06 |
nicklas |
136 |
{ |
2104 |
23 Mar 06 |
nicklas |
137 |
write_item(i, l.get(i)); |
2104 |
23 Mar 06 |
nicklas |
138 |
} |
2104 |
23 Mar 06 |
nicklas |
139 |
if (expectedResults >= 0 && expectedResults != l.size()) |
2104 |
23 Mar 06 |
nicklas |
140 |
{ |
2104 |
23 Mar 06 |
nicklas |
141 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
2104 |
23 Mar 06 |
nicklas |
142 |
} |
2104 |
23 Mar 06 |
nicklas |
143 |
write("--List formulas OK ("+l.size()+")"); |
2104 |
23 Mar 06 |
nicklas |
144 |
} |
2104 |
23 Mar 06 |
nicklas |
145 |
catch (Throwable ex) |
2104 |
23 Mar 06 |
nicklas |
146 |
{ |
2104 |
23 Mar 06 |
nicklas |
147 |
write("--List formulas FAILED"); |
2104 |
23 Mar 06 |
nicklas |
148 |
ex.printStackTrace(); |
2104 |
23 Mar 06 |
nicklas |
149 |
ok = false; |
2104 |
23 Mar 06 |
nicklas |
150 |
} |
2104 |
23 Mar 06 |
nicklas |
151 |
finally |
2104 |
23 Mar 06 |
nicklas |
152 |
{ |
2104 |
23 Mar 06 |
nicklas |
153 |
if (dc != null) dc.close(); |
2104 |
23 Mar 06 |
nicklas |
154 |
} |
2104 |
23 Mar 06 |
nicklas |
155 |
} |
2104 |
23 Mar 06 |
nicklas |
156 |
|
2104 |
23 Mar 06 |
nicklas |
157 |
static void test_delete(int id) |
2104 |
23 Mar 06 |
nicklas |
158 |
{ |
2104 |
23 Mar 06 |
nicklas |
159 |
if (id == 0) return; |
2104 |
23 Mar 06 |
nicklas |
160 |
DbControl dc = null; |
2104 |
23 Mar 06 |
nicklas |
161 |
try |
2104 |
23 Mar 06 |
nicklas |
162 |
{ |
2104 |
23 Mar 06 |
nicklas |
163 |
dc = TestUtil.getDbControl(); |
2104 |
23 Mar 06 |
nicklas |
164 |
Formula f = Formula.getById(dc, id); |
2104 |
23 Mar 06 |
nicklas |
165 |
dc.deleteItem(f); |
5340 |
10 May 10 |
nicklas |
166 |
Set<ItemProxy> using = f.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
167 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
168 |
{ |
5340 |
10 May 10 |
nicklas |
169 |
throw new BaseException(f + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
170 |
} |
2104 |
23 Mar 06 |
nicklas |
171 |
dc.commit(); |
2104 |
23 Mar 06 |
nicklas |
172 |
write("--Delete formula OK"); |
2104 |
23 Mar 06 |
nicklas |
173 |
} |
2104 |
23 Mar 06 |
nicklas |
174 |
catch (Throwable ex) |
2104 |
23 Mar 06 |
nicklas |
175 |
{ |
2104 |
23 Mar 06 |
nicklas |
176 |
write("--Delete formula FAILED"); |
2104 |
23 Mar 06 |
nicklas |
177 |
ex.printStackTrace(); |
2104 |
23 Mar 06 |
nicklas |
178 |
ok = false; |
2104 |
23 Mar 06 |
nicklas |
179 |
} |
2104 |
23 Mar 06 |
nicklas |
180 |
finally |
2104 |
23 Mar 06 |
nicklas |
181 |
{ |
2104 |
23 Mar 06 |
nicklas |
182 |
if (dc != null) dc.close(); |
2104 |
23 Mar 06 |
nicklas |
183 |
} |
2104 |
23 Mar 06 |
nicklas |
184 |
} |
2104 |
23 Mar 06 |
nicklas |
185 |
|
2104 |
23 Mar 06 |
nicklas |
186 |
static void write_header() |
2104 |
23 Mar 06 |
nicklas |
187 |
{ |
2104 |
23 Mar 06 |
nicklas |
188 |
if (!TestUtil.getSilent()) |
2104 |
23 Mar 06 |
nicklas |
189 |
{ |
2104 |
23 Mar 06 |
nicklas |
190 |
write(" \tID \tName \tDescription\tType\tParser\tRaw data type\tChannels\tFormulas"); |
2104 |
23 Mar 06 |
nicklas |
191 |
write("-- \t-- \t--------- \t-----------\t----\t------\t-------------\t--------\t--------"); |
2104 |
23 Mar 06 |
nicklas |
192 |
} |
2104 |
23 Mar 06 |
nicklas |
193 |
} |
2104 |
23 Mar 06 |
nicklas |
194 |
static void write_item(int i, Formula f) |
2104 |
23 Mar 06 |
nicklas |
195 |
throws BaseException |
2104 |
23 Mar 06 |
nicklas |
196 |
{ |
2104 |
23 Mar 06 |
nicklas |
197 |
if (!TestUtil.getSilent()) |
2104 |
23 Mar 06 |
nicklas |
198 |
{ |
2104 |
23 Mar 06 |
nicklas |
199 |
System.out.println(i+":\t"+f.getId()+"\t"+f.getName()+"\t"+f.getDescription()+"\t"+ |
2104 |
23 Mar 06 |
nicklas |
200 |
f.getFormulaType()+"\t"+f.getParser()+"\t"+f.getRawDataType()+"\t"+f.getChannels()+"\t"+ |
2104 |
23 Mar 06 |
nicklas |
201 |
f.getFormulas() |
2104 |
23 Mar 06 |
nicklas |
202 |
); |
2104 |
23 Mar 06 |
nicklas |
203 |
} |
2104 |
23 Mar 06 |
nicklas |
204 |
} |
2104 |
23 Mar 06 |
nicklas |
205 |
static void write(String message) |
2104 |
23 Mar 06 |
nicklas |
206 |
{ |
2104 |
23 Mar 06 |
nicklas |
207 |
System.out.println(message); |
2104 |
23 Mar 06 |
nicklas |
208 |
} |
2104 |
23 Mar 06 |
nicklas |
209 |
} |