1701 |
09 Dec 05 |
nicklas |
1 |
/* |
1701 |
09 Dec 05 |
nicklas |
$Id$ |
1701 |
09 Dec 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
1701 |
09 Dec 05 |
nicklas |
6 |
|
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/ |
1701 |
09 Dec 05 |
nicklas |
9 |
|
1701 |
09 Dec 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
1701 |
09 Dec 05 |
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 |
1701 |
09 Dec 05 |
nicklas |
of the License, or (at your option) any later version. |
1701 |
09 Dec 05 |
nicklas |
14 |
|
1701 |
09 Dec 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
1701 |
09 Dec 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1701 |
09 Dec 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1701 |
09 Dec 05 |
nicklas |
GNU General Public License for more details. |
1701 |
09 Dec 05 |
nicklas |
19 |
|
1701 |
09 Dec 05 |
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/>. |
1701 |
09 Dec 05 |
nicklas |
22 |
*/ |
1701 |
09 Dec 05 |
nicklas |
23 |
import net.sf.basedb.core.*; |
1701 |
09 Dec 05 |
nicklas |
24 |
|
1730 |
20 Dec 05 |
nicklas |
25 |
import net.sf.basedb.util.Enumeration; |
1730 |
20 Dec 05 |
nicklas |
26 |
|
1701 |
09 Dec 05 |
nicklas |
27 |
|
1701 |
09 Dec 05 |
nicklas |
28 |
public class TestContext |
1701 |
09 Dec 05 |
nicklas |
29 |
{ |
1701 |
09 Dec 05 |
nicklas |
30 |
|
1701 |
09 Dec 05 |
nicklas |
31 |
static boolean ok = true; |
1701 |
09 Dec 05 |
nicklas |
32 |
public static void main(String[] args) |
1701 |
09 Dec 05 |
nicklas |
33 |
{ |
1701 |
09 Dec 05 |
nicklas |
34 |
TestUtil.checkArgs(args); |
1701 |
09 Dec 05 |
nicklas |
35 |
TestUtil.begin(); |
1701 |
09 Dec 05 |
nicklas |
36 |
ok = test_all(); |
1701 |
09 Dec 05 |
nicklas |
37 |
TestUtil.stop(); |
1701 |
09 Dec 05 |
nicklas |
38 |
} |
1701 |
09 Dec 05 |
nicklas |
39 |
|
1701 |
09 Dec 05 |
nicklas |
40 |
static boolean test_all() |
1701 |
09 Dec 05 |
nicklas |
41 |
{ |
1701 |
09 Dec 05 |
nicklas |
42 |
write("++Testing context"); |
1701 |
09 Dec 05 |
nicklas |
43 |
write_header(); |
1701 |
09 Dec 05 |
nicklas |
44 |
|
1934 |
07 Feb 06 |
nicklas |
45 |
test_load(Item.FILE, "test", 0); |
1934 |
07 Feb 06 |
nicklas |
46 |
int id = test_create(Item.FILE, "test", "new context"); |
1934 |
07 Feb 06 |
nicklas |
47 |
test_load(Item.FILE, "test", id); |
1934 |
07 Feb 06 |
nicklas |
48 |
test_list(Item.FILE, "test", -1); |
1701 |
09 Dec 05 |
nicklas |
49 |
|
1701 |
09 Dec 05 |
nicklas |
50 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
1934 |
07 Feb 06 |
nicklas |
51 |
test_delete(id); |
1701 |
09 Dec 05 |
nicklas |
52 |
write("++Testing context "+(ok ? "OK" : "Failed")+"\n"); |
1701 |
09 Dec 05 |
nicklas |
53 |
return ok; |
1701 |
09 Dec 05 |
nicklas |
54 |
} |
1701 |
09 Dec 05 |
nicklas |
55 |
|
1934 |
07 Feb 06 |
nicklas |
56 |
static int test_create(Item item, String subContext, String name) |
1701 |
09 Dec 05 |
nicklas |
57 |
{ |
1730 |
20 Dec 05 |
nicklas |
58 |
int id = 0; |
1701 |
09 Dec 05 |
nicklas |
59 |
try |
1701 |
09 Dec 05 |
nicklas |
60 |
{ |
1701 |
09 Dec 05 |
nicklas |
61 |
SessionControl sc = TestUtil.getSessionControl(); |
1934 |
07 Feb 06 |
nicklas |
62 |
ItemContext current = sc.getCurrentContext(item, subContext); |
1701 |
09 Dec 05 |
nicklas |
63 |
current.setRowsPerPage(60); |
1701 |
09 Dec 05 |
nicklas |
64 |
current.setPage(2); |
1701 |
09 Dec 05 |
nicklas |
65 |
current.setSortProperty("name"); |
1708 |
13 Dec 05 |
nicklas |
66 |
current.setSortDirection(ItemContext.SortDirection.ASC); |
1730 |
20 Dec 05 |
nicklas |
67 |
id = sc.saveCurrentContextAs(item, name, true, false); |
1701 |
09 Dec 05 |
nicklas |
68 |
write_item(0, current); |
1701 |
09 Dec 05 |
nicklas |
69 |
write("--Create context OK"); |
1701 |
09 Dec 05 |
nicklas |
70 |
} |
1701 |
09 Dec 05 |
nicklas |
71 |
catch (Throwable ex) |
1701 |
09 Dec 05 |
nicklas |
72 |
{ |
1701 |
09 Dec 05 |
nicklas |
73 |
write("--Create context FAILED"); |
1701 |
09 Dec 05 |
nicklas |
74 |
ex.printStackTrace(); |
1701 |
09 Dec 05 |
nicklas |
75 |
ok = false; |
1701 |
09 Dec 05 |
nicklas |
76 |
} |
1730 |
20 Dec 05 |
nicklas |
77 |
return id; |
1701 |
09 Dec 05 |
nicklas |
78 |
} |
1701 |
09 Dec 05 |
nicklas |
79 |
|
1934 |
07 Feb 06 |
nicklas |
80 |
static void test_load(Item item, String subContext, int id) |
1701 |
09 Dec 05 |
nicklas |
81 |
{ |
1701 |
09 Dec 05 |
nicklas |
82 |
try |
1701 |
09 Dec 05 |
nicklas |
83 |
{ |
1701 |
09 Dec 05 |
nicklas |
84 |
SessionControl sc = TestUtil.getSessionControl(); |
1934 |
07 Feb 06 |
nicklas |
85 |
ItemContext current = id == 0 ? sc.getCurrentContext(item, subContext) : sc.loadContext(id); |
1701 |
09 Dec 05 |
nicklas |
86 |
write_item(0, current); |
1701 |
09 Dec 05 |
nicklas |
87 |
write("--Load context OK"); |
1701 |
09 Dec 05 |
nicklas |
88 |
} |
1701 |
09 Dec 05 |
nicklas |
89 |
catch (Throwable ex) |
1701 |
09 Dec 05 |
nicklas |
90 |
{ |
1701 |
09 Dec 05 |
nicklas |
91 |
write("--Load context FAILED"); |
1701 |
09 Dec 05 |
nicklas |
92 |
ex.printStackTrace(); |
1701 |
09 Dec 05 |
nicklas |
93 |
ok = false; |
1701 |
09 Dec 05 |
nicklas |
94 |
} |
1701 |
09 Dec 05 |
nicklas |
95 |
} |
1701 |
09 Dec 05 |
nicklas |
96 |
|
1701 |
09 Dec 05 |
nicklas |
97 |
|
1934 |
07 Feb 06 |
nicklas |
98 |
static void test_list(Item item, String subContext, int expectedResults) |
1701 |
09 Dec 05 |
nicklas |
99 |
{ |
1701 |
09 Dec 05 |
nicklas |
100 |
try |
1701 |
09 Dec 05 |
nicklas |
101 |
{ |
1701 |
09 Dec 05 |
nicklas |
102 |
SessionControl sc = TestUtil.getSessionControl(); |
1934 |
07 Feb 06 |
nicklas |
103 |
Enumeration<Integer, String> names = sc.getContextNames(item, subContext); |
1701 |
09 Dec 05 |
nicklas |
104 |
for (int i = 0; i<names.size(); i++) |
1701 |
09 Dec 05 |
nicklas |
105 |
{ |
1730 |
20 Dec 05 |
nicklas |
106 |
ItemContext named = sc.loadContext(names.getKey(i)); |
1701 |
09 Dec 05 |
nicklas |
107 |
write_item(i, named); |
1701 |
09 Dec 05 |
nicklas |
108 |
} |
1701 |
09 Dec 05 |
nicklas |
109 |
if (expectedResults >= 0 && expectedResults != names.size()) |
1701 |
09 Dec 05 |
nicklas |
110 |
{ |
1701 |
09 Dec 05 |
nicklas |
111 |
throw new BaseException("Expected "+expectedResults+" results, not "+names.size()); |
1701 |
09 Dec 05 |
nicklas |
112 |
} |
1701 |
09 Dec 05 |
nicklas |
113 |
write("--List contexts OK ("+names.size()+")"); |
1701 |
09 Dec 05 |
nicklas |
114 |
} |
1701 |
09 Dec 05 |
nicklas |
115 |
catch (Throwable ex) |
1701 |
09 Dec 05 |
nicklas |
116 |
{ |
1701 |
09 Dec 05 |
nicklas |
117 |
write("--List contexts FAILED"); |
1701 |
09 Dec 05 |
nicklas |
118 |
ex.printStackTrace(); |
1701 |
09 Dec 05 |
nicklas |
119 |
ok = false; |
1701 |
09 Dec 05 |
nicklas |
120 |
} |
1701 |
09 Dec 05 |
nicklas |
121 |
} |
1701 |
09 Dec 05 |
nicklas |
122 |
|
1934 |
07 Feb 06 |
nicklas |
123 |
static void test_delete(int id) |
1701 |
09 Dec 05 |
nicklas |
124 |
{ |
1701 |
09 Dec 05 |
nicklas |
125 |
try |
1701 |
09 Dec 05 |
nicklas |
126 |
{ |
1701 |
09 Dec 05 |
nicklas |
127 |
SessionControl sc = TestUtil.getSessionControl(); |
1934 |
07 Feb 06 |
nicklas |
128 |
sc.deleteContext(id); |
1701 |
09 Dec 05 |
nicklas |
129 |
write("--Delete context OK"); |
1701 |
09 Dec 05 |
nicklas |
130 |
} |
1701 |
09 Dec 05 |
nicklas |
131 |
catch (Throwable ex) |
1701 |
09 Dec 05 |
nicklas |
132 |
{ |
1701 |
09 Dec 05 |
nicklas |
133 |
write("--Delete context FAILED"); |
1701 |
09 Dec 05 |
nicklas |
134 |
ex.printStackTrace(); |
1701 |
09 Dec 05 |
nicklas |
135 |
ok = false; |
1701 |
09 Dec 05 |
nicklas |
136 |
} |
1701 |
09 Dec 05 |
nicklas |
137 |
} |
1701 |
09 Dec 05 |
nicklas |
138 |
|
1701 |
09 Dec 05 |
nicklas |
139 |
static void write_header() |
1701 |
09 Dec 05 |
nicklas |
140 |
{ |
1701 |
09 Dec 05 |
nicklas |
141 |
if (!TestUtil.getSilent()) |
1701 |
09 Dec 05 |
nicklas |
142 |
{ |
1701 |
09 Dec 05 |
nicklas |
143 |
write(" \tName \tItem\tRows/page\tPage\tSort on\tDirection"); |
1701 |
09 Dec 05 |
nicklas |
144 |
write("-- \t--------- \t----\t---------\t----\t-------\t---------"); |
1701 |
09 Dec 05 |
nicklas |
145 |
} |
1701 |
09 Dec 05 |
nicklas |
146 |
} |
1701 |
09 Dec 05 |
nicklas |
147 |
static void write_item(int i, ItemContext c) |
1701 |
09 Dec 05 |
nicklas |
148 |
throws BaseException |
1701 |
09 Dec 05 |
nicklas |
149 |
{ |
1701 |
09 Dec 05 |
nicklas |
150 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+c.getName()+"\t"+c.getItemType()+"\t"+ |
1701 |
09 Dec 05 |
nicklas |
151 |
c.getRowsPerPage()+"\t"+c.getPage()+"\t"+c.getSortProperty()+"\t"+c.getSortDirection()); |
1701 |
09 Dec 05 |
nicklas |
152 |
} |
1701 |
09 Dec 05 |
nicklas |
153 |
static void write(String message) |
1701 |
09 Dec 05 |
nicklas |
154 |
{ |
1701 |
09 Dec 05 |
nicklas |
155 |
System.out.println(message); |
1701 |
09 Dec 05 |
nicklas |
156 |
} |
1701 |
09 Dec 05 |
nicklas |
157 |
|
1701 |
09 Dec 05 |
nicklas |
158 |
} |
1701 |
09 Dec 05 |
nicklas |
159 |
|