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