129 |
10 Mar 05 |
nicklas |
1 |
/* |
129 |
10 Mar 05 |
nicklas |
$Id$ |
129 |
10 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 |
129 |
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/ |
129 |
10 Mar 05 |
nicklas |
9 |
|
129 |
10 Mar 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
129 |
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 |
129 |
10 Mar 05 |
nicklas |
of the License, or (at your option) any later version. |
129 |
10 Mar 05 |
nicklas |
14 |
|
129 |
10 Mar 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
129 |
10 Mar 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
129 |
10 Mar 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
129 |
10 Mar 05 |
nicklas |
GNU General Public License for more details. |
129 |
10 Mar 05 |
nicklas |
19 |
|
129 |
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/>. |
129 |
10 Mar 05 |
nicklas |
22 |
*/ |
129 |
10 Mar 05 |
nicklas |
23 |
import net.sf.basedb.core.*; |
129 |
10 Mar 05 |
nicklas |
24 |
|
129 |
10 Mar 05 |
nicklas |
25 |
public class TestDbControl |
129 |
10 Mar 05 |
nicklas |
26 |
{ |
129 |
10 Mar 05 |
nicklas |
27 |
|
129 |
10 Mar 05 |
nicklas |
28 |
static boolean ok = true; |
129 |
10 Mar 05 |
nicklas |
29 |
public static void main(String[] args) |
129 |
10 Mar 05 |
nicklas |
30 |
{ |
129 |
10 Mar 05 |
nicklas |
31 |
TestUtil.checkArgs(args); |
129 |
10 Mar 05 |
nicklas |
32 |
TestUtil.begin(); |
129 |
10 Mar 05 |
nicklas |
33 |
ok = test_all(); |
129 |
10 Mar 05 |
nicklas |
34 |
TestUtil.stop(); |
129 |
10 Mar 05 |
nicklas |
35 |
} |
129 |
10 Mar 05 |
nicklas |
36 |
|
129 |
10 Mar 05 |
nicklas |
37 |
static boolean test_all() |
129 |
10 Mar 05 |
nicklas |
38 |
{ |
129 |
10 Mar 05 |
nicklas |
39 |
write("++Testing DbControl"); |
129 |
10 Mar 05 |
nicklas |
40 |
test_weak_reference(); |
129 |
10 Mar 05 |
nicklas |
41 |
test_logout(); |
174 |
16 Mar 05 |
nicklas |
42 |
|
221 |
23 Mar 05 |
nicklas |
43 |
int roleId = TestRole.test_create(true); |
174 |
16 Mar 05 |
nicklas |
44 |
test_detach(roleId); |
174 |
16 Mar 05 |
nicklas |
45 |
TestRole.test_delete(roleId); |
174 |
16 Mar 05 |
nicklas |
46 |
|
129 |
10 Mar 05 |
nicklas |
47 |
write("++Testing DbControl "+(ok ? "OK" : "Failed")+"\n"); |
129 |
10 Mar 05 |
nicklas |
48 |
return ok; |
129 |
10 Mar 05 |
nicklas |
49 |
} |
129 |
10 Mar 05 |
nicklas |
50 |
|
129 |
10 Mar 05 |
nicklas |
51 |
static void test_weak_reference() |
129 |
10 Mar 05 |
nicklas |
52 |
{ |
129 |
10 Mar 05 |
nicklas |
53 |
DbControl dc = null; |
129 |
10 Mar 05 |
nicklas |
54 |
try |
129 |
10 Mar 05 |
nicklas |
55 |
{ |
129 |
10 Mar 05 |
nicklas |
56 |
dc = TestUtil.getDbControl(); |
129 |
10 Mar 05 |
nicklas |
57 |
User u = User.getById(dc, SystemItems.getId(User.ROOT)); |
129 |
10 Mar 05 |
nicklas |
58 |
dc = null; // Now we only have a weak reference to the DbControl object from the User object |
2665 |
26 Sep 06 |
nicklas |
59 |
write("--NOTE! The warning message about an unclosed DbControl is expected."); |
129 |
10 Mar 05 |
nicklas |
60 |
System.gc(); // Should clean up the weak reference |
352 |
04 Apr 05 |
nicklas |
61 |
if (!u.isDetached()) |
352 |
04 Apr 05 |
nicklas |
62 |
{ |
352 |
04 Apr 05 |
nicklas |
63 |
throw new BaseException("Expected the item to be detached"); |
352 |
04 Apr 05 |
nicklas |
64 |
} |
129 |
10 Mar 05 |
nicklas |
65 |
} |
256 |
24 Mar 05 |
nicklas |
66 |
catch (PermissionDeniedException ex) |
129 |
10 Mar 05 |
nicklas |
67 |
{ |
129 |
10 Mar 05 |
nicklas |
68 |
write("--Test weak reference OK"); |
129 |
10 Mar 05 |
nicklas |
69 |
} |
358 |
04 Apr 05 |
nicklas |
70 |
catch (Throwable ex) |
129 |
10 Mar 05 |
nicklas |
71 |
{ |
129 |
10 Mar 05 |
nicklas |
72 |
write("--Test weak reference FAILED"); |
129 |
10 Mar 05 |
nicklas |
73 |
ex.printStackTrace(); |
129 |
10 Mar 05 |
nicklas |
74 |
ok = false; |
129 |
10 Mar 05 |
nicklas |
75 |
} |
129 |
10 Mar 05 |
nicklas |
76 |
finally |
129 |
10 Mar 05 |
nicklas |
77 |
{ |
129 |
10 Mar 05 |
nicklas |
78 |
if (dc != null) dc.close(); |
129 |
10 Mar 05 |
nicklas |
79 |
} |
129 |
10 Mar 05 |
nicklas |
80 |
} |
129 |
10 Mar 05 |
nicklas |
81 |
|
129 |
10 Mar 05 |
nicklas |
82 |
static void test_logout() |
129 |
10 Mar 05 |
nicklas |
83 |
{ |
129 |
10 Mar 05 |
nicklas |
84 |
DbControl dc = null; |
129 |
10 Mar 05 |
nicklas |
85 |
try |
129 |
10 Mar 05 |
nicklas |
86 |
{ |
129 |
10 Mar 05 |
nicklas |
87 |
dc = TestUtil.getDbControl(); |
129 |
10 Mar 05 |
nicklas |
88 |
User u = User.getById(dc, SystemItems.getId(User.ROOT)); |
129 |
10 Mar 05 |
nicklas |
89 |
TestUtil.logout(); // Should close all open DbControl objects |
129 |
10 Mar 05 |
nicklas |
90 |
dc.commit(); // Should throw ConnectionClosedException |
129 |
10 Mar 05 |
nicklas |
91 |
throw new BaseException("Expected ConnectionClosedException"); |
129 |
10 Mar 05 |
nicklas |
92 |
} |
129 |
10 Mar 05 |
nicklas |
93 |
catch (ConnectionClosedException ex) |
129 |
10 Mar 05 |
nicklas |
94 |
{ |
129 |
10 Mar 05 |
nicklas |
95 |
write("--Test logout OK"); |
129 |
10 Mar 05 |
nicklas |
96 |
} |
358 |
04 Apr 05 |
nicklas |
97 |
catch (Throwable ex) |
129 |
10 Mar 05 |
nicklas |
98 |
{ |
129 |
10 Mar 05 |
nicklas |
99 |
write("--Test logout FAILED"); |
129 |
10 Mar 05 |
nicklas |
100 |
ex.printStackTrace(); |
129 |
10 Mar 05 |
nicklas |
101 |
ok = false; |
129 |
10 Mar 05 |
nicklas |
102 |
} |
129 |
10 Mar 05 |
nicklas |
103 |
finally |
129 |
10 Mar 05 |
nicklas |
104 |
{ |
129 |
10 Mar 05 |
nicklas |
105 |
if (dc != null) dc.close(); |
174 |
16 Mar 05 |
nicklas |
106 |
try |
174 |
16 Mar 05 |
nicklas |
107 |
{ |
174 |
16 Mar 05 |
nicklas |
108 |
TestUtil.login(); |
174 |
16 Mar 05 |
nicklas |
109 |
} |
358 |
04 Apr 05 |
nicklas |
110 |
catch (Throwable ex) |
174 |
16 Mar 05 |
nicklas |
111 |
{} |
129 |
10 Mar 05 |
nicklas |
112 |
} |
129 |
10 Mar 05 |
nicklas |
113 |
} |
129 |
10 Mar 05 |
nicklas |
114 |
|
174 |
16 Mar 05 |
nicklas |
115 |
static void test_detach(int roleId) |
174 |
16 Mar 05 |
nicklas |
116 |
{ |
174 |
16 Mar 05 |
nicklas |
117 |
if (roleId == 0) return; |
174 |
16 Mar 05 |
nicklas |
118 |
DbControl dc = null; |
174 |
16 Mar 05 |
nicklas |
119 |
try |
174 |
16 Mar 05 |
nicklas |
120 |
{ |
174 |
16 Mar 05 |
nicklas |
121 |
dc = TestUtil.getDbControl(); |
174 |
16 Mar 05 |
nicklas |
122 |
Role r = Role.getById(dc, roleId); |
174 |
16 Mar 05 |
nicklas |
123 |
TestRole.write_item(0, r); |
1455 |
13 Oct 05 |
nicklas |
124 |
|
174 |
16 Mar 05 |
nicklas |
125 |
dc.detachItem(r); |
174 |
16 Mar 05 |
nicklas |
126 |
dc.close(); |
174 |
16 Mar 05 |
nicklas |
127 |
r.setName("Detached role"); |
174 |
16 Mar 05 |
nicklas |
128 |
dc = TestUtil.getDbControl(); |
5058 |
18 Aug 09 |
nicklas |
129 |
dc.reattachItem(r, true); |
174 |
16 Mar 05 |
nicklas |
130 |
dc.commit(); |
174 |
16 Mar 05 |
nicklas |
131 |
dc.close(); |
1455 |
13 Oct 05 |
nicklas |
132 |
|
174 |
16 Mar 05 |
nicklas |
133 |
dc = TestUtil.getDbControl(); |
174 |
16 Mar 05 |
nicklas |
134 |
r = Role.getById(dc, roleId); |
174 |
16 Mar 05 |
nicklas |
135 |
TestRole.write_item(1, r); |
174 |
16 Mar 05 |
nicklas |
136 |
if (!"Detached role".equals(r.getName())) throw new BaseException("Role.getName() != Detached role"); |
174 |
16 Mar 05 |
nicklas |
137 |
write("--Test detach OK"); |
174 |
16 Mar 05 |
nicklas |
138 |
} |
358 |
04 Apr 05 |
nicklas |
139 |
catch (Throwable ex) |
174 |
16 Mar 05 |
nicklas |
140 |
{ |
174 |
16 Mar 05 |
nicklas |
141 |
write("--Test detach FAILED"); |
174 |
16 Mar 05 |
nicklas |
142 |
ex.printStackTrace(); |
174 |
16 Mar 05 |
nicklas |
143 |
ok = false; |
174 |
16 Mar 05 |
nicklas |
144 |
} |
174 |
16 Mar 05 |
nicklas |
145 |
finally |
174 |
16 Mar 05 |
nicklas |
146 |
{ |
174 |
16 Mar 05 |
nicklas |
147 |
if (dc != null) dc.close(); |
174 |
16 Mar 05 |
nicklas |
148 |
} |
174 |
16 Mar 05 |
nicklas |
149 |
} |
129 |
10 Mar 05 |
nicklas |
150 |
|
129 |
10 Mar 05 |
nicklas |
151 |
static void write(String message) |
129 |
10 Mar 05 |
nicklas |
152 |
{ |
129 |
10 Mar 05 |
nicklas |
153 |
System.out.println(message); |
129 |
10 Mar 05 |
nicklas |
154 |
} |
129 |
10 Mar 05 |
nicklas |
155 |
|
129 |
10 Mar 05 |
nicklas |
156 |
} |
129 |
10 Mar 05 |
nicklas |
157 |
|