192 |
17 Mar 05 |
jari |
1 |
/* |
192 |
17 Mar 05 |
jari |
$Id$ |
192 |
17 Mar 05 |
jari |
3 |
|
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2005, 2006 Jari Häkkinen, Nicklas Nordborg |
192 |
17 Mar 05 |
jari |
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/ |
192 |
17 Mar 05 |
jari |
8 |
|
192 |
17 Mar 05 |
jari |
BASE is free software; you can redistribute it and/or |
192 |
17 Mar 05 |
jari |
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 |
192 |
17 Mar 05 |
jari |
of the License, or (at your option) any later version. |
192 |
17 Mar 05 |
jari |
13 |
|
192 |
17 Mar 05 |
jari |
BASE is distributed in the hope that it will be useful, |
192 |
17 Mar 05 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
192 |
17 Mar 05 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
192 |
17 Mar 05 |
jari |
GNU General Public License for more details. |
192 |
17 Mar 05 |
jari |
18 |
|
192 |
17 Mar 05 |
jari |
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/>. |
192 |
17 Mar 05 |
jari |
21 |
*/ |
192 |
17 Mar 05 |
jari |
22 |
import net.sf.basedb.core.*; |
192 |
17 Mar 05 |
jari |
23 |
import java.util.EnumSet; |
192 |
17 Mar 05 |
jari |
24 |
|
192 |
17 Mar 05 |
jari |
25 |
public class TestItemKey |
192 |
17 Mar 05 |
jari |
26 |
{ |
192 |
17 Mar 05 |
jari |
27 |
|
192 |
17 Mar 05 |
jari |
28 |
static boolean ok = true; |
192 |
17 Mar 05 |
jari |
29 |
public static void main(String[] args) |
192 |
17 Mar 05 |
jari |
30 |
{ |
192 |
17 Mar 05 |
jari |
31 |
TestUtil.checkArgs(args); |
192 |
17 Mar 05 |
jari |
32 |
TestUtil.begin(); |
192 |
17 Mar 05 |
jari |
33 |
ok = test_all(); |
192 |
17 Mar 05 |
jari |
34 |
TestUtil.stop(); |
192 |
17 Mar 05 |
jari |
35 |
} |
192 |
17 Mar 05 |
jari |
36 |
|
192 |
17 Mar 05 |
jari |
37 |
static boolean test_all() |
192 |
17 Mar 05 |
jari |
38 |
{ |
192 |
17 Mar 05 |
jari |
39 |
write("++Testing item key"); |
192 |
17 Mar 05 |
jari |
40 |
write_header(); |
5024 |
27 Jul 09 |
nicklas |
41 |
|
5024 |
27 Jul 09 |
nicklas |
// Delete "garbage" from previous tests |
5024 |
27 Jul 09 |
nicklas |
43 |
ItemKey.deleteUnusedItemKeys(); |
5024 |
27 Jul 09 |
nicklas |
44 |
|
192 |
17 Mar 05 |
jari |
// Extra test: create user and group for the key |
221 |
23 Mar 05 |
nicklas |
46 |
int userId = TestUser.test_create(true); |
221 |
23 Mar 05 |
nicklas |
47 |
int groupId = TestGroup.test_create(true); |
192 |
17 Mar 05 |
jari |
48 |
|
292 |
31 Mar 05 |
nicklas |
// Standard tests: create, load |
266 |
29 Mar 05 |
nicklas |
50 |
int id = test_create(userId, EnumSet.of(Permission.WRITE), groupId, EnumSet.of(Permission.READ)); |
192 |
17 Mar 05 |
jari |
51 |
test_load(id); |
192 |
17 Mar 05 |
jari |
52 |
|
192 |
17 Mar 05 |
jari |
// Extra tests: create new key with same permission -> id2 == id |
266 |
29 Mar 05 |
nicklas |
54 |
int id2 = test_create(userId, EnumSet.of(Permission.WRITE), groupId, EnumSet.of(Permission.READ)); |
192 |
17 Mar 05 |
jari |
55 |
write("--id==id2 "+(id==id2 ? "OK" : "FAILED id="+id+"; id2="+id2)); |
192 |
17 Mar 05 |
jari |
56 |
ok = ok && (id == id2); |
192 |
17 Mar 05 |
jari |
57 |
|
192 |
17 Mar 05 |
jari |
// Extra tests: create new key with different permission -> id3 != id |
266 |
29 Mar 05 |
nicklas |
59 |
int id3 = test_create(userId, EnumSet.of(Permission.USE), groupId, EnumSet.of(Permission.READ)); |
192 |
17 Mar 05 |
jari |
60 |
write("--id!=id3 "+(id!=id3 ? "OK" : "FAILED id="+id+"; id3="+id3)); |
192 |
17 Mar 05 |
jari |
61 |
ok = ok && (id != id3); |
192 |
17 Mar 05 |
jari |
62 |
|
192 |
17 Mar 05 |
jari |
63 |
|
1529 |
27 Oct 05 |
nicklas |
64 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
192 |
17 Mar 05 |
jari |
65 |
TestUser.test_delete(userId); |
192 |
17 Mar 05 |
jari |
66 |
TestGroup.test_delete(groupId); |
192 |
17 Mar 05 |
jari |
67 |
|
5023 |
27 Jul 09 |
nicklas |
68 |
test_delete_unused(2); |
192 |
17 Mar 05 |
jari |
69 |
|
192 |
17 Mar 05 |
jari |
70 |
write("++Testing item key "+(ok ? "OK" : "Failed")+"\n"); |
192 |
17 Mar 05 |
jari |
71 |
return ok; |
192 |
17 Mar 05 |
jari |
72 |
} |
192 |
17 Mar 05 |
jari |
73 |
|
192 |
17 Mar 05 |
jari |
74 |
static int test_create(int userId, EnumSet<Permission> userPermissions, int groupId, EnumSet<Permission> groupPermissions) |
192 |
17 Mar 05 |
jari |
75 |
{ |
192 |
17 Mar 05 |
jari |
76 |
if (userId == 0 || groupId == 0) return 0; |
192 |
17 Mar 05 |
jari |
77 |
int id = 0; |
192 |
17 Mar 05 |
jari |
78 |
DbControl dc = null; |
192 |
17 Mar 05 |
jari |
79 |
try |
192 |
17 Mar 05 |
jari |
80 |
{ |
192 |
17 Mar 05 |
jari |
81 |
dc = TestUtil.getDbControl(); |
192 |
17 Mar 05 |
jari |
82 |
User u = User.getById(dc, userId); |
192 |
17 Mar 05 |
jari |
83 |
UserPermissions up = new UserPermissions(); |
192 |
17 Mar 05 |
jari |
84 |
up.setPermissions(u, userPermissions); |
192 |
17 Mar 05 |
jari |
85 |
Group g = Group.getById(dc, groupId); |
192 |
17 Mar 05 |
jari |
86 |
GroupPermissions gp = new GroupPermissions(); |
192 |
17 Mar 05 |
jari |
87 |
gp.setPermissions(g, groupPermissions); |
192 |
17 Mar 05 |
jari |
88 |
ItemKey ik = ItemKey.getNewOrExisting(dc, up, gp); |
192 |
17 Mar 05 |
jari |
89 |
id = ik.getId(); |
192 |
17 Mar 05 |
jari |
90 |
write_item(0, ik); |
192 |
17 Mar 05 |
jari |
91 |
write("--Create item key OK"); |
192 |
17 Mar 05 |
jari |
92 |
} |
358 |
04 Apr 05 |
nicklas |
93 |
catch (Throwable ex) |
192 |
17 Mar 05 |
jari |
94 |
{ |
192 |
17 Mar 05 |
jari |
95 |
write("--Create item key FAILED"); |
192 |
17 Mar 05 |
jari |
96 |
ex.printStackTrace(); |
192 |
17 Mar 05 |
jari |
97 |
ok = false; |
192 |
17 Mar 05 |
jari |
98 |
} |
192 |
17 Mar 05 |
jari |
99 |
finally |
192 |
17 Mar 05 |
jari |
100 |
{ |
192 |
17 Mar 05 |
jari |
101 |
if (dc != null) dc.close(); |
192 |
17 Mar 05 |
jari |
102 |
} |
192 |
17 Mar 05 |
jari |
103 |
return id; |
192 |
17 Mar 05 |
jari |
104 |
} |
192 |
17 Mar 05 |
jari |
105 |
|
192 |
17 Mar 05 |
jari |
106 |
static void test_load(int id) |
192 |
17 Mar 05 |
jari |
107 |
{ |
192 |
17 Mar 05 |
jari |
108 |
if (id == 0) return; |
192 |
17 Mar 05 |
jari |
109 |
DbControl dc = null; |
192 |
17 Mar 05 |
jari |
110 |
try |
192 |
17 Mar 05 |
jari |
111 |
{ |
192 |
17 Mar 05 |
jari |
112 |
dc = TestUtil.getDbControl(); |
192 |
17 Mar 05 |
jari |
113 |
ItemKey ik = ItemKey.getById(dc, id); |
192 |
17 Mar 05 |
jari |
114 |
write_item(0, ik); |
192 |
17 Mar 05 |
jari |
115 |
write("--Load item key OK"); |
192 |
17 Mar 05 |
jari |
116 |
} |
358 |
04 Apr 05 |
nicklas |
117 |
catch (Throwable ex) |
192 |
17 Mar 05 |
jari |
118 |
{ |
192 |
17 Mar 05 |
jari |
119 |
write("--Load item key FAILED"); |
192 |
17 Mar 05 |
jari |
120 |
ex.printStackTrace(); |
192 |
17 Mar 05 |
jari |
121 |
ok = false; |
192 |
17 Mar 05 |
jari |
122 |
} |
192 |
17 Mar 05 |
jari |
123 |
finally |
192 |
17 Mar 05 |
jari |
124 |
{ |
192 |
17 Mar 05 |
jari |
125 |
if (dc != null) dc.close(); |
192 |
17 Mar 05 |
jari |
126 |
} |
192 |
17 Mar 05 |
jari |
127 |
} |
192 |
17 Mar 05 |
jari |
128 |
|
5023 |
27 Jul 09 |
nicklas |
129 |
static void test_delete_unused(int expected) |
192 |
17 Mar 05 |
jari |
130 |
{ |
192 |
17 Mar 05 |
jari |
131 |
try |
192 |
17 Mar 05 |
jari |
132 |
{ |
5023 |
27 Jul 09 |
nicklas |
133 |
int numDeleted = ItemKey.deleteUnusedItemKeys(); |
5023 |
27 Jul 09 |
nicklas |
134 |
if (expected >= 0 && numDeleted != expected) |
5023 |
27 Jul 09 |
nicklas |
135 |
{ |
5023 |
27 Jul 09 |
nicklas |
136 |
throw new BaseException("Unexpected number of unused item keys: " + |
5023 |
27 Jul 09 |
nicklas |
137 |
numDeleted + "; expected " + expected); |
5023 |
27 Jul 09 |
nicklas |
138 |
} |
5368 |
22 Jun 10 |
nicklas |
139 |
write("--Delete unused item keys OK (" + numDeleted + ")"); |
192 |
17 Mar 05 |
jari |
140 |
} |
358 |
04 Apr 05 |
nicklas |
141 |
catch (Throwable ex) |
192 |
17 Mar 05 |
jari |
142 |
{ |
192 |
17 Mar 05 |
jari |
143 |
write("--Delete unused item keys FAILED"); |
192 |
17 Mar 05 |
jari |
144 |
ex.printStackTrace(); |
192 |
17 Mar 05 |
jari |
145 |
ok = false; |
192 |
17 Mar 05 |
jari |
146 |
} |
192 |
17 Mar 05 |
jari |
147 |
} |
192 |
17 Mar 05 |
jari |
148 |
|
192 |
17 Mar 05 |
jari |
149 |
|
192 |
17 Mar 05 |
jari |
150 |
static void write_header() |
192 |
17 Mar 05 |
jari |
151 |
{ |
192 |
17 Mar 05 |
jari |
152 |
if (!TestUtil.getSilent()) |
192 |
17 Mar 05 |
jari |
153 |
{ |
192 |
17 Mar 05 |
jari |
154 |
write(" \tID"); |
192 |
17 Mar 05 |
jari |
155 |
write("-- \t--"); |
192 |
17 Mar 05 |
jari |
156 |
} |
192 |
17 Mar 05 |
jari |
157 |
} |
192 |
17 Mar 05 |
jari |
158 |
static void write_item(int i, ItemKey ik) |
192 |
17 Mar 05 |
jari |
159 |
throws BaseException |
192 |
17 Mar 05 |
jari |
160 |
{ |
192 |
17 Mar 05 |
jari |
161 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+ik.getId()); |
192 |
17 Mar 05 |
jari |
162 |
} |
192 |
17 Mar 05 |
jari |
163 |
static void write(String message) |
192 |
17 Mar 05 |
jari |
164 |
{ |
192 |
17 Mar 05 |
jari |
165 |
System.out.println(message); |
192 |
17 Mar 05 |
jari |
166 |
} |
192 |
17 Mar 05 |
jari |
167 |
|
192 |
17 Mar 05 |
jari |
168 |
|
192 |
17 Mar 05 |
jari |
169 |
} |
192 |
17 Mar 05 |
jari |
170 |
|