130 |
10 Mar 05 |
nicklas |
1 |
/* |
130 |
10 Mar 05 |
nicklas |
$Id$ |
130 |
10 Mar 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Johan Enell, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen |
130 |
10 Mar 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/ |
130 |
10 Mar 05 |
nicklas |
9 |
|
130 |
10 Mar 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
130 |
10 Mar 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 |
130 |
10 Mar 05 |
nicklas |
of the License, or (at your option) any later version. |
130 |
10 Mar 05 |
nicklas |
14 |
|
130 |
10 Mar 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
130 |
10 Mar 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
130 |
10 Mar 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
130 |
10 Mar 05 |
nicklas |
GNU General Public License for more details. |
130 |
10 Mar 05 |
nicklas |
19 |
|
130 |
10 Mar 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/>. |
130 |
10 Mar 05 |
nicklas |
22 |
*/ |
130 |
10 Mar 05 |
nicklas |
23 |
import net.sf.basedb.core.*; |
130 |
10 Mar 05 |
nicklas |
24 |
import java.util.EnumSet; |
1297 |
08 Sep 05 |
enell |
25 |
import java.util.List; |
1297 |
08 Sep 05 |
enell |
26 |
import java.util.Set; |
130 |
10 Mar 05 |
nicklas |
27 |
public class TestRoleKey |
130 |
10 Mar 05 |
nicklas |
28 |
{ |
130 |
10 Mar 05 |
nicklas |
29 |
|
130 |
10 Mar 05 |
nicklas |
30 |
static boolean ok = true; |
130 |
10 Mar 05 |
nicklas |
31 |
public static void main(String[] args) |
130 |
10 Mar 05 |
nicklas |
32 |
{ |
130 |
10 Mar 05 |
nicklas |
33 |
TestUtil.checkArgs(args); |
130 |
10 Mar 05 |
nicklas |
34 |
TestUtil.begin(); |
130 |
10 Mar 05 |
nicklas |
35 |
ok = test_all(); |
130 |
10 Mar 05 |
nicklas |
36 |
TestUtil.stop(); |
130 |
10 Mar 05 |
nicklas |
37 |
} |
130 |
10 Mar 05 |
nicklas |
38 |
|
130 |
10 Mar 05 |
nicklas |
39 |
static boolean test_all() |
130 |
10 Mar 05 |
nicklas |
40 |
{ |
130 |
10 Mar 05 |
nicklas |
41 |
write("++Testing role key"); |
130 |
10 Mar 05 |
nicklas |
42 |
write_header(); |
130 |
10 Mar 05 |
nicklas |
// Standard tests: load, list |
130 |
10 Mar 05 |
nicklas |
44 |
int id = test_load(Item.USER); |
130 |
10 Mar 05 |
nicklas |
45 |
|
130 |
10 Mar 05 |
nicklas |
// Extra tests: add, list and remove roles/permissions |
1297 |
08 Sep 05 |
enell |
47 |
test_listroles(id); |
266 |
29 Mar 05 |
nicklas |
48 |
test_addrole(id, SystemItems.getId(Role.GUEST), EnumSet.of(Permission.WRITE)); |
1297 |
08 Sep 05 |
enell |
49 |
test_listroles(id); |
1529 |
27 Oct 05 |
nicklas |
50 |
|
1529 |
27 Oct 05 |
nicklas |
51 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
130 |
10 Mar 05 |
nicklas |
52 |
test_removerole(id, SystemItems.getId(Role.GUEST)); |
1297 |
08 Sep 05 |
enell |
53 |
test_listroles(id); |
130 |
10 Mar 05 |
nicklas |
54 |
|
130 |
10 Mar 05 |
nicklas |
55 |
write("++Testing role key "+(ok ? "OK" : "Failed")+"\n"); |
130 |
10 Mar 05 |
nicklas |
56 |
return ok; |
130 |
10 Mar 05 |
nicklas |
57 |
} |
130 |
10 Mar 05 |
nicklas |
58 |
|
130 |
10 Mar 05 |
nicklas |
59 |
|
130 |
10 Mar 05 |
nicklas |
60 |
static int test_load(Item itemType) |
130 |
10 Mar 05 |
nicklas |
61 |
{ |
130 |
10 Mar 05 |
nicklas |
62 |
if (!TestUtil.hasPermission(Permission.READ, Item.ROLE)) return 0; |
130 |
10 Mar 05 |
nicklas |
63 |
DbControl dc = null; |
130 |
10 Mar 05 |
nicklas |
64 |
int id = 0; |
130 |
10 Mar 05 |
nicklas |
65 |
try |
130 |
10 Mar 05 |
nicklas |
66 |
{ |
130 |
10 Mar 05 |
nicklas |
67 |
dc = TestUtil.getDbControl(); |
130 |
10 Mar 05 |
nicklas |
68 |
RoleKey rk = RoleKey.getByItemType(dc, itemType); |
130 |
10 Mar 05 |
nicklas |
69 |
write_item(0, rk); |
130 |
10 Mar 05 |
nicklas |
70 |
write("--Load role key OK"); |
130 |
10 Mar 05 |
nicklas |
71 |
id = rk.getId(); |
130 |
10 Mar 05 |
nicklas |
72 |
} |
358 |
04 Apr 05 |
nicklas |
73 |
catch (Throwable ex) |
130 |
10 Mar 05 |
nicklas |
74 |
{ |
130 |
10 Mar 05 |
nicklas |
75 |
write("--Load role key FAILED"); |
130 |
10 Mar 05 |
nicklas |
76 |
ex.printStackTrace(); |
130 |
10 Mar 05 |
nicklas |
77 |
ok = false; |
130 |
10 Mar 05 |
nicklas |
78 |
} |
130 |
10 Mar 05 |
nicklas |
79 |
finally |
130 |
10 Mar 05 |
nicklas |
80 |
{ |
130 |
10 Mar 05 |
nicklas |
81 |
if (dc != null) dc.close(); |
130 |
10 Mar 05 |
nicklas |
82 |
} |
130 |
10 Mar 05 |
nicklas |
83 |
return id; |
130 |
10 Mar 05 |
nicklas |
84 |
} |
130 |
10 Mar 05 |
nicklas |
85 |
|
130 |
10 Mar 05 |
nicklas |
86 |
|
130 |
10 Mar 05 |
nicklas |
87 |
static void write_header() |
130 |
10 Mar 05 |
nicklas |
88 |
{ |
130 |
10 Mar 05 |
nicklas |
89 |
if (!TestUtil.getSilent()) |
130 |
10 Mar 05 |
nicklas |
90 |
{ |
130 |
10 Mar 05 |
nicklas |
91 |
write(" \tID \tName \tItem type\tDescription"); |
130 |
10 Mar 05 |
nicklas |
92 |
write("-- \t-- \t--------- \t---------\t-----------"); |
130 |
10 Mar 05 |
nicklas |
93 |
} |
130 |
10 Mar 05 |
nicklas |
94 |
} |
130 |
10 Mar 05 |
nicklas |
95 |
static void write_item(int i, RoleKey rk) |
130 |
10 Mar 05 |
nicklas |
96 |
throws BaseException |
130 |
10 Mar 05 |
nicklas |
97 |
{ |
130 |
10 Mar 05 |
nicklas |
98 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+rk.getId()+"\t"+rk.getName()+"\t"+rk.getItemType()+"\t"+rk.getDescription()); |
130 |
10 Mar 05 |
nicklas |
99 |
} |
1297 |
08 Sep 05 |
enell |
100 |
static void write_item(int i, Role r, Set<Permission> permissions) |
130 |
10 Mar 05 |
nicklas |
101 |
throws BaseException |
130 |
10 Mar 05 |
nicklas |
102 |
{ |
130 |
10 Mar 05 |
nicklas |
103 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+r.getId()+"\t"+r.getName()+"\t"+r.getDescription()+"\t"+permissions); |
130 |
10 Mar 05 |
nicklas |
104 |
} |
130 |
10 Mar 05 |
nicklas |
105 |
static void write(String message) |
130 |
10 Mar 05 |
nicklas |
106 |
{ |
130 |
10 Mar 05 |
nicklas |
107 |
System.out.println(message); |
130 |
10 Mar 05 |
nicklas |
108 |
} |
130 |
10 Mar 05 |
nicklas |
109 |
|
130 |
10 Mar 05 |
nicklas |
110 |
static void test_addrole(int id, int roleId, EnumSet<Permission> permissions) |
130 |
10 Mar 05 |
nicklas |
111 |
{ |
130 |
10 Mar 05 |
nicklas |
112 |
if (id == 0 || roleId == 0 || !TestUtil.hasPermission(Permission.WRITE, Item.ROLE)) return; |
130 |
10 Mar 05 |
nicklas |
113 |
DbControl dc = null; |
130 |
10 Mar 05 |
nicklas |
114 |
try |
130 |
10 Mar 05 |
nicklas |
115 |
{ |
130 |
10 Mar 05 |
nicklas |
116 |
dc = TestUtil.getDbControl(); |
130 |
10 Mar 05 |
nicklas |
117 |
RoleKey rk = RoleKey.getById(dc, id); |
130 |
10 Mar 05 |
nicklas |
118 |
Role r = Role.getById(dc, roleId); |
130 |
10 Mar 05 |
nicklas |
119 |
rk.setPermissions(r, permissions); |
130 |
10 Mar 05 |
nicklas |
120 |
dc.commit(); |
130 |
10 Mar 05 |
nicklas |
121 |
write("--Add role to role key OK"); |
130 |
10 Mar 05 |
nicklas |
122 |
} |
358 |
04 Apr 05 |
nicklas |
123 |
catch (Throwable ex) |
130 |
10 Mar 05 |
nicklas |
124 |
{ |
130 |
10 Mar 05 |
nicklas |
125 |
write("--Add role to role key FAILED"); |
130 |
10 Mar 05 |
nicklas |
126 |
ex.printStackTrace(); |
130 |
10 Mar 05 |
nicklas |
127 |
ok = false; |
130 |
10 Mar 05 |
nicklas |
128 |
} |
130 |
10 Mar 05 |
nicklas |
129 |
finally |
130 |
10 Mar 05 |
nicklas |
130 |
{ |
130 |
10 Mar 05 |
nicklas |
131 |
if (dc != null) dc.close(); |
130 |
10 Mar 05 |
nicklas |
132 |
} |
130 |
10 Mar 05 |
nicklas |
133 |
} |
130 |
10 Mar 05 |
nicklas |
134 |
|
130 |
10 Mar 05 |
nicklas |
135 |
static void test_listroles(int id) |
130 |
10 Mar 05 |
nicklas |
136 |
{ |
1297 |
08 Sep 05 |
enell |
137 |
if (id == 0 || !TestUtil.hasPermission(Permission.READ, Item.ROLE)) return; |
1297 |
08 Sep 05 |
enell |
138 |
DbControl dc = null; |
130 |
10 Mar 05 |
nicklas |
139 |
try |
130 |
10 Mar 05 |
nicklas |
140 |
{ |
1297 |
08 Sep 05 |
enell |
141 |
dc = TestUtil.getDbControl(); |
1297 |
08 Sep 05 |
enell |
142 |
RoleKey rk = RoleKey.getById(dc, id); |
1418 |
07 Oct 05 |
nicklas |
143 |
List<Role> l = Role.getQuery().list(dc); |
130 |
10 Mar 05 |
nicklas |
144 |
for (int i=0; i<l.size(); i++) |
130 |
10 Mar 05 |
nicklas |
145 |
{ |
1297 |
08 Sep 05 |
enell |
146 |
Role r = l.get(i); |
1297 |
08 Sep 05 |
enell |
147 |
write_item(i, r, rk.getPermissions(r)); |
130 |
10 Mar 05 |
nicklas |
148 |
} |
130 |
10 Mar 05 |
nicklas |
149 |
write("--List roles in role key OK ("+l.size()+")"); |
130 |
10 Mar 05 |
nicklas |
150 |
} |
358 |
04 Apr 05 |
nicklas |
151 |
catch (Throwable ex) |
130 |
10 Mar 05 |
nicklas |
152 |
{ |
130 |
10 Mar 05 |
nicklas |
153 |
write("--List roles in role key FAILED"); |
130 |
10 Mar 05 |
nicklas |
154 |
ex.printStackTrace(); |
130 |
10 Mar 05 |
nicklas |
155 |
ok = false; |
130 |
10 Mar 05 |
nicklas |
156 |
} |
130 |
10 Mar 05 |
nicklas |
157 |
finally |
130 |
10 Mar 05 |
nicklas |
158 |
{ |
1297 |
08 Sep 05 |
enell |
159 |
if (dc != null) dc.close(); |
130 |
10 Mar 05 |
nicklas |
160 |
} |
130 |
10 Mar 05 |
nicklas |
161 |
} |
130 |
10 Mar 05 |
nicklas |
162 |
|
130 |
10 Mar 05 |
nicklas |
163 |
static void test_removerole(int id, int roleId) |
130 |
10 Mar 05 |
nicklas |
164 |
{ |
130 |
10 Mar 05 |
nicklas |
165 |
if (id == 0 || roleId == 0 || !TestUtil.hasPermission(Permission.WRITE, Item.ROLE)) return; |
130 |
10 Mar 05 |
nicklas |
166 |
DbControl dc = null; |
130 |
10 Mar 05 |
nicklas |
167 |
try |
130 |
10 Mar 05 |
nicklas |
168 |
{ |
130 |
10 Mar 05 |
nicklas |
169 |
dc = TestUtil.getDbControl(); |
130 |
10 Mar 05 |
nicklas |
170 |
RoleKey rk = RoleKey.getById(dc, id); |
130 |
10 Mar 05 |
nicklas |
171 |
Role r = Role.getById(dc, roleId); |
130 |
10 Mar 05 |
nicklas |
172 |
rk.setPermissions(r, EnumSet.noneOf(Permission.class)); |
130 |
10 Mar 05 |
nicklas |
173 |
dc.commit(); |
130 |
10 Mar 05 |
nicklas |
174 |
write("--Remove role from role key OK"); |
130 |
10 Mar 05 |
nicklas |
175 |
} |
358 |
04 Apr 05 |
nicklas |
176 |
catch (Throwable ex) |
130 |
10 Mar 05 |
nicklas |
177 |
{ |
130 |
10 Mar 05 |
nicklas |
178 |
write("--Remove role from role key FAILED"); |
130 |
10 Mar 05 |
nicklas |
179 |
ex.printStackTrace(); |
130 |
10 Mar 05 |
nicklas |
180 |
ok = false; |
130 |
10 Mar 05 |
nicklas |
181 |
} |
130 |
10 Mar 05 |
nicklas |
182 |
finally |
130 |
10 Mar 05 |
nicklas |
183 |
{ |
130 |
10 Mar 05 |
nicklas |
184 |
if (dc != null) dc.close(); |
130 |
10 Mar 05 |
nicklas |
185 |
} |
130 |
10 Mar 05 |
nicklas |
186 |
} |
130 |
10 Mar 05 |
nicklas |
187 |
|
130 |
10 Mar 05 |
nicklas |
188 |
|
130 |
10 Mar 05 |
nicklas |
189 |
} |
130 |
10 Mar 05 |
nicklas |
190 |
|