5293 |
30 Mar 10 |
nicklas |
1 |
/* |
5293 |
30 Mar 10 |
nicklas |
$Id$ |
5293 |
30 Mar 10 |
nicklas |
3 |
|
5293 |
30 Mar 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5293 |
30 Mar 10 |
nicklas |
5 |
|
5293 |
30 Mar 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5293 |
30 Mar 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5293 |
30 Mar 10 |
nicklas |
8 |
|
5293 |
30 Mar 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5293 |
30 Mar 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5293 |
30 Mar 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5293 |
30 Mar 10 |
nicklas |
of the License, or (at your option) any later version. |
5293 |
30 Mar 10 |
nicklas |
13 |
|
5293 |
30 Mar 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5293 |
30 Mar 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5293 |
30 Mar 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5293 |
30 Mar 10 |
nicklas |
GNU General Public License for more details. |
5293 |
30 Mar 10 |
nicklas |
18 |
|
5293 |
30 Mar 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5293 |
30 Mar 10 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5293 |
30 Mar 10 |
nicklas |
21 |
*/ |
5293 |
30 Mar 10 |
nicklas |
22 |
|
5306 |
12 Apr 10 |
nicklas |
23 |
import java.util.Collection; |
5293 |
30 Mar 10 |
nicklas |
24 |
import java.util.Date; |
5293 |
30 Mar 10 |
nicklas |
25 |
|
5306 |
12 Apr 10 |
nicklas |
26 |
import net.sf.basedb.core.BaseException; |
5293 |
30 Mar 10 |
nicklas |
27 |
import net.sf.basedb.core.BasicItem; |
5293 |
30 Mar 10 |
nicklas |
28 |
import net.sf.basedb.core.DbControl; |
5652 |
10 Jun 11 |
nicklas |
29 |
import net.sf.basedb.core.DerivedBioAssay; |
5641 |
25 May 11 |
nicklas |
30 |
import net.sf.basedb.core.Extract; |
5293 |
30 Mar 10 |
nicklas |
31 |
import net.sf.basedb.core.Formula; |
5293 |
30 Mar 10 |
nicklas |
32 |
import net.sf.basedb.core.Item; |
5293 |
30 Mar 10 |
nicklas |
33 |
import net.sf.basedb.core.Metadata; |
5642 |
26 May 11 |
nicklas |
34 |
import net.sf.basedb.core.PhysicalBioAssay; |
5293 |
30 Mar 10 |
nicklas |
35 |
import net.sf.basedb.core.Platform; |
5293 |
30 Mar 10 |
nicklas |
36 |
import net.sf.basedb.core.RawBioAssay; |
5293 |
30 Mar 10 |
nicklas |
37 |
import net.sf.basedb.core.Metadata.PropertyPath; |
5632 |
17 May 11 |
nicklas |
38 |
import net.sf.basedb.core.SystemItems; |
5632 |
17 May 11 |
nicklas |
39 |
import net.sf.basedb.core.Tag; |
5293 |
30 Mar 10 |
nicklas |
40 |
|
5293 |
30 Mar 10 |
nicklas |
41 |
public class TestMetadata |
5293 |
30 Mar 10 |
nicklas |
42 |
{ |
5293 |
30 Mar 10 |
nicklas |
43 |
|
5293 |
30 Mar 10 |
nicklas |
44 |
public static void main(String[] args) |
5293 |
30 Mar 10 |
nicklas |
45 |
{ |
5293 |
30 Mar 10 |
nicklas |
46 |
TestUtil.checkArgs(args); |
5293 |
30 Mar 10 |
nicklas |
47 |
TestUtil.begin(); |
5293 |
30 Mar 10 |
nicklas |
48 |
ok = test_all(); |
5293 |
30 Mar 10 |
nicklas |
49 |
TestUtil.stop(); |
5293 |
30 Mar 10 |
nicklas |
50 |
} |
5293 |
30 Mar 10 |
nicklas |
51 |
|
5293 |
30 Mar 10 |
nicklas |
52 |
private static boolean ok = true; |
5293 |
30 Mar 10 |
nicklas |
53 |
|
5293 |
30 Mar 10 |
nicklas |
54 |
public static Boolean test_all() |
5293 |
30 Mar 10 |
nicklas |
55 |
{ |
5293 |
30 Mar 10 |
nicklas |
56 |
write("++Testing metadata"); |
5293 |
30 Mar 10 |
nicklas |
// Simple property on root class |
5293 |
30 Mar 10 |
nicklas |
58 |
PropertyPath<RawBioAssay, String> name = |
5306 |
12 Apr 10 |
nicklas |
59 |
test_create_property_path(RawBioAssay.class, "name", false); |
5293 |
30 Mar 10 |
nicklas |
// Association + id property |
5688 |
10 Aug 11 |
nicklas |
61 |
PropertyPath<RawBioAssay, Integer> scan = |
5688 |
10 Aug 11 |
nicklas |
62 |
test_create_property_path(RawBioAssay.class, "parentBioAssay.id", false); |
5293 |
30 Mar 10 |
nicklas |
// Null association |
5293 |
30 Mar 10 |
nicklas |
64 |
PropertyPath<RawBioAssay, String> protocolName = |
5688 |
10 Aug 11 |
nicklas |
65 |
test_create_property_path(RawBioAssay.class, "parentBioAssay.protocol.name", false); |
5293 |
30 Mar 10 |
nicklas |
// Date value |
5293 |
30 Mar 10 |
nicklas |
67 |
PropertyPath<RawBioAssay, Date> hybDate = |
6100 |
04 Sep 12 |
nicklas |
68 |
test_create_property_path(RawBioAssay.class, "parentBioAssay.extract.creationEvent.entryDate", false); |
5293 |
30 Mar 10 |
nicklas |
// Long path + case insensitive |
6100 |
04 Sep 12 |
nicklas |
70 |
PropertyPath<RawBioAssay, String> sampleSubtype = |
6100 |
04 Sep 12 |
nicklas |
71 |
test_create_property_path(RawBioAssay.class, "ParentBioAssay.Extract.Parent.ItemSubtype", false); |
5293 |
30 Mar 10 |
nicklas |
// Component |
5293 |
30 Mar 10 |
nicklas |
73 |
PropertyPath<Formula, Float> formulaMinValue = |
5306 |
12 Apr 10 |
nicklas |
74 |
test_create_property_path(Formula.class, "coloring.minValue", false); |
5293 |
30 Mar 10 |
nicklas |
// Collection |
5306 |
12 Apr 10 |
nicklas |
76 |
PropertyPath<Formula, Collection<String>> formulaFormulas = |
5306 |
12 Apr 10 |
nicklas |
77 |
test_create_property_path(Formula.class, "formulas", false); |
5306 |
12 Apr 10 |
nicklas |
// THE TESTS BELOW ARE INVALID PROPERTY PATHS |
5306 |
12 Apr 10 |
nicklas |
// Unknown paths |
5306 |
12 Apr 10 |
nicklas |
80 |
test_create_property_path(RawBioAssay.class, "foo", true); |
5306 |
12 Apr 10 |
nicklas |
81 |
test_create_property_path(RawBioAssay.class, "name.foo", true); |
5688 |
10 Aug 11 |
nicklas |
82 |
test_create_property_path(RawBioAssay.class, "parentBioAssay.foo", true); |
5293 |
30 Mar 10 |
nicklas |
83 |
|
5306 |
12 Apr 10 |
nicklas |
// Collection of entities should fail |
5688 |
10 Aug 11 |
nicklas |
85 |
test_create_property_path(DerivedBioAssay.class, "children", true); |
5306 |
12 Apr 10 |
nicklas |
86 |
|
6100 |
04 Sep 12 |
nicklas |
87 |
int sampleSubtypeId = TestItemSubtype.test_create(Item.SAMPLE, "Test sample subtype"); |
6100 |
04 Sep 12 |
nicklas |
88 |
int sampleId = TestSample.test_create(0, "Test sample", sampleSubtypeId, false); |
5632 |
17 May 11 |
nicklas |
89 |
int labelId = TestTag.test_create("Test label", SystemItems.getId(Tag.LABEL), true); |
6100 |
04 Sep 12 |
nicklas |
90 |
int labeledExtractId = TestExtract.test_create(sampleId, "Test extract", SystemItems.getId(Extract.LABELED), labelId, true); |
6100 |
04 Sep 12 |
nicklas |
91 |
int hybId = TestPhysicalBioAssay.test_create(0, "Test hyb", SystemItems.getId(PhysicalBioAssay.HYBRIDIZATION), 1, labeledExtractId); |
6100 |
04 Sep 12 |
nicklas |
92 |
int scanId = TestDerivedBioAssay.test_create_root("Test scan", hybId, SystemItems.getId(DerivedBioAssay.SCAN), labeledExtractId, 0, 0, 0); |
5293 |
30 Mar 10 |
nicklas |
93 |
int rawId = TestRawBioAssay.test_create(Platform.GENERIC, "genepix", "My raw bioassay", |
6100 |
04 Sep 12 |
nicklas |
94 |
scanId, labeledExtractId, 0, 0, 0, true); |
5293 |
30 Mar 10 |
nicklas |
95 |
int formulaId = TestFormula.test_create("Test metadata", Formula.Type.COLUMN_EXPRESSION, |
5293 |
30 Mar 10 |
nicklas |
96 |
"genepix", new String[] { "ch(1) + ch(2)" }, true); |
5293 |
30 Mar 10 |
nicklas |
97 |
int pluginId = TestPluginDefinition.test_get("net.sf.basedb.plugins.AnnotationFlatFileImporter"); |
5293 |
30 Mar 10 |
nicklas |
98 |
|
5293 |
30 Mar 10 |
nicklas |
99 |
test_use_property_path(name, Item.RAWBIOASSAY, rawId); |
5293 |
30 Mar 10 |
nicklas |
100 |
test_use_property_path(scan, Item.RAWBIOASSAY, rawId); |
5293 |
30 Mar 10 |
nicklas |
101 |
test_use_property_path(protocolName, Item.RAWBIOASSAY, rawId); |
5293 |
30 Mar 10 |
nicklas |
102 |
test_use_property_path(hybDate, Item.RAWBIOASSAY, rawId); |
6100 |
04 Sep 12 |
nicklas |
103 |
test_use_property_path(sampleSubtype, Item.RAWBIOASSAY, rawId); |
5293 |
30 Mar 10 |
nicklas |
104 |
test_use_property_path(formulaMinValue, Item.FORMULA, formulaId); |
5306 |
12 Apr 10 |
nicklas |
105 |
test_use_property_path(formulaFormulas, Item.FORMULA, formulaId); |
5293 |
30 Mar 10 |
nicklas |
106 |
|
5293 |
30 Mar 10 |
nicklas |
107 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
5293 |
30 Mar 10 |
nicklas |
108 |
|
5293 |
30 Mar 10 |
nicklas |
109 |
TestFormula.test_delete(formulaId); |
5293 |
30 Mar 10 |
nicklas |
110 |
TestRawBioAssay.test_delete(rawId); |
5688 |
10 Aug 11 |
nicklas |
111 |
TestDerivedBioAssay.test_delete(scanId); |
5642 |
26 May 11 |
nicklas |
112 |
TestPhysicalBioAssay.test_delete(hybId); |
5641 |
25 May 11 |
nicklas |
113 |
TestExtract.test_delete(labeledExtractId); |
5632 |
17 May 11 |
nicklas |
114 |
TestTag.test_delete(labelId); |
6100 |
04 Sep 12 |
nicklas |
115 |
TestSample.test_delete(sampleId); |
6100 |
04 Sep 12 |
nicklas |
116 |
TestItemSubtype.test_delete(sampleSubtypeId); |
5293 |
30 Mar 10 |
nicklas |
117 |
|
5293 |
30 Mar 10 |
nicklas |
118 |
write("++Testing metadata "+(ok ? "OK" : "Failed")+"\n"); |
5293 |
30 Mar 10 |
nicklas |
119 |
return ok; |
5293 |
30 Mar 10 |
nicklas |
120 |
} |
5293 |
30 Mar 10 |
nicklas |
121 |
|
5306 |
12 Apr 10 |
nicklas |
122 |
static <I, V> PropertyPath<I, V> test_create_property_path(Class<I> itemClass, String path, boolean shouldFail) |
5293 |
30 Mar 10 |
nicklas |
123 |
{ |
5293 |
30 Mar 10 |
nicklas |
124 |
PropertyPath<I, V> propertyPath = null;; |
5293 |
30 Mar 10 |
nicklas |
125 |
try |
5293 |
30 Mar 10 |
nicklas |
126 |
{ |
5306 |
12 Apr 10 |
nicklas |
127 |
Throwable t = null; |
5306 |
12 Apr 10 |
nicklas |
128 |
try |
5306 |
12 Apr 10 |
nicklas |
129 |
{ |
5306 |
12 Apr 10 |
nicklas |
130 |
Metadata<I> metadata = Metadata.getInstance(itemClass); |
5306 |
12 Apr 10 |
nicklas |
131 |
propertyPath = metadata.getPropertyPath(path, true); |
5306 |
12 Apr 10 |
nicklas |
132 |
} |
5306 |
12 Apr 10 |
nicklas |
133 |
catch (Throwable t1) |
5306 |
12 Apr 10 |
nicklas |
134 |
{ |
5306 |
12 Apr 10 |
nicklas |
135 |
t = t1; |
5306 |
12 Apr 10 |
nicklas |
136 |
} |
5306 |
12 Apr 10 |
nicklas |
137 |
if (shouldFail) |
5306 |
12 Apr 10 |
nicklas |
138 |
{ |
5306 |
12 Apr 10 |
nicklas |
139 |
if (t == null) throw new BaseException("Expected path failure"); |
5475 |
03 Nov 10 |
nicklas |
140 |
write("--Create property path: "+propertyPath+" OK [" + t.getMessage() + "]"); |
5306 |
12 Apr 10 |
nicklas |
141 |
} |
5306 |
12 Apr 10 |
nicklas |
142 |
else |
5306 |
12 Apr 10 |
nicklas |
143 |
{ |
5306 |
12 Apr 10 |
nicklas |
144 |
if (t != null) throw t; |
5306 |
12 Apr 10 |
nicklas |
145 |
write("--Create property path '"+propertyPath+"' OK"); |
5306 |
12 Apr 10 |
nicklas |
146 |
} |
5293 |
30 Mar 10 |
nicklas |
147 |
} |
5293 |
30 Mar 10 |
nicklas |
148 |
catch (Throwable ex) |
5293 |
30 Mar 10 |
nicklas |
149 |
{ |
5475 |
03 Nov 10 |
nicklas |
150 |
write("--Create property path '" + itemClass.getSimpleName() + "."+path+"' FAILED"); |
5293 |
30 Mar 10 |
nicklas |
151 |
ex.printStackTrace(); |
5293 |
30 Mar 10 |
nicklas |
152 |
ok = false; |
5293 |
30 Mar 10 |
nicklas |
153 |
} |
5293 |
30 Mar 10 |
nicklas |
154 |
return propertyPath; |
5293 |
30 Mar 10 |
nicklas |
155 |
} |
5293 |
30 Mar 10 |
nicklas |
156 |
|
6875 |
20 Apr 15 |
nicklas |
157 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
5293 |
30 Mar 10 |
nicklas |
158 |
static void test_use_property_path(PropertyPath path, Item itemType, int itemId) |
5293 |
30 Mar 10 |
nicklas |
159 |
{ |
5293 |
30 Mar 10 |
nicklas |
160 |
if (itemId == 0 || path == null) return; |
5293 |
30 Mar 10 |
nicklas |
161 |
DbControl dc = null; |
5293 |
30 Mar 10 |
nicklas |
162 |
try |
5293 |
30 Mar 10 |
nicklas |
163 |
{ |
5293 |
30 Mar 10 |
nicklas |
164 |
dc = TestUtil.getDbControl(); |
5293 |
30 Mar 10 |
nicklas |
165 |
BasicItem item = itemType.getById(dc, itemId); |
5293 |
30 Mar 10 |
nicklas |
166 |
Object value = path.getValue(dc, item); |
5293 |
30 Mar 10 |
nicklas |
167 |
write("--Use property path '"+path+"' OK: " + value); |
5293 |
30 Mar 10 |
nicklas |
168 |
} |
5293 |
30 Mar 10 |
nicklas |
169 |
catch (Throwable ex) |
5293 |
30 Mar 10 |
nicklas |
170 |
{ |
5293 |
30 Mar 10 |
nicklas |
171 |
write("--Use property path '"+path+"' FAILED"); |
5293 |
30 Mar 10 |
nicklas |
172 |
ex.printStackTrace(); |
5293 |
30 Mar 10 |
nicklas |
173 |
ok = false; |
5293 |
30 Mar 10 |
nicklas |
174 |
} |
5293 |
30 Mar 10 |
nicklas |
175 |
finally |
5293 |
30 Mar 10 |
nicklas |
176 |
{ |
5293 |
30 Mar 10 |
nicklas |
177 |
if (dc != null) dc.close(); |
5293 |
30 Mar 10 |
nicklas |
178 |
} |
5293 |
30 Mar 10 |
nicklas |
179 |
} |
5293 |
30 Mar 10 |
nicklas |
180 |
|
5293 |
30 Mar 10 |
nicklas |
181 |
static void write(String message) |
5293 |
30 Mar 10 |
nicklas |
182 |
{ |
5293 |
30 Mar 10 |
nicklas |
183 |
System.out.println(message); |
5293 |
30 Mar 10 |
nicklas |
184 |
} |
5293 |
30 Mar 10 |
nicklas |
185 |
} |