1 |
|
2 |
/* $Id$ */ |
3 |
|
4 |
/******************************************************* |
5 |
* |
6 |
* Copyright 2003-2007 by ACceSS MNRF |
7 |
* Copyright 2007 by University of Queensland |
8 |
* |
9 |
* http://esscc.uq.edu.au |
10 |
* Primary Business: Queensland, Australia |
11 |
* Licensed under the Open Software License version 3.0 |
12 |
* http://www.opensource.org/licenses/osl-3.0.php |
13 |
* |
14 |
*******************************************************/ |
15 |
|
16 |
#include <iostream> |
17 |
|
18 |
#include "EsysExceptionTestCase.h" |
19 |
#include "esysUtils/EsysException.h" |
20 |
|
21 |
using namespace std; |
22 |
using namespace CppUnitTest; |
23 |
using namespace esysUtils; |
24 |
|
25 |
class DerivedEx : public EsysException { |
26 |
|
27 |
typedef EsysException Parent; |
28 |
|
29 |
public: |
30 |
|
31 |
/// Default Constructor for Exception |
32 |
DerivedEx() : Parent() { updateMessage(); } |
33 |
|
34 |
/// Constructor for Exception |
35 |
DerivedEx(const char *cstr) : Parent(cstr) { updateMessage(); } |
36 |
|
37 |
/// Constructor for Exception |
38 |
DerivedEx(const string &str) : Parent(str) { updateMessage(); } |
39 |
|
40 |
// Copy Constructor. |
41 |
DerivedEx(const DerivedEx &other): Parent(other) { updateMessage(); } |
42 |
|
43 |
inline virtual DerivedEx & |
44 |
operator=(const DerivedEx &other) THROW_ANY |
45 |
{ |
46 |
Parent::operator=(other); |
47 |
updateMessage(); |
48 |
return *this; |
49 |
} |
50 |
|
51 |
/// Return the exception name |
52 |
virtual const string & exceptionName() const |
53 |
{ |
54 |
return rhubarb; |
55 |
} |
56 |
|
57 |
static const string rhubarb; |
58 |
}; |
59 |
|
60 |
const string DerivedEx::rhubarb("DerivedException"); |
61 |
|
62 |
void EsysExceptionTestCase::testCase0() { |
63 |
|
64 |
assert(true); |
65 |
|
66 |
} |
67 |
|
68 |
void EsysExceptionTestCase::testCase1() { |
69 |
|
70 |
EsysException defEx; |
71 |
|
72 |
// |
73 |
// default exception text should have contents of exceptionName near start |
74 |
// |
75 |
string defString = defEx.toString(); |
76 |
assert(defString.find(defEx.exceptionName()) != string::npos); |
77 |
assert(defString.find(defEx.exceptionName()) < 4); |
78 |
|
79 |
// |
80 |
// default exception text shouldn't be much longer than contents of exception name |
81 |
// |
82 |
assert(defString.size() > defEx.exceptionName().size()); |
83 |
assert((defString.size() - defEx.exceptionName().size()) < 10); |
84 |
|
85 |
string ex1Text("My first funny exception message."); |
86 |
EsysException ex1(ex1Text); |
87 |
|
88 |
// |
89 |
// exception text should have contents of exceptionName near start |
90 |
// |
91 |
string ex1String = ex1.toString(); |
92 |
assert(ex1String.find(ex1.exceptionName()) != string::npos); |
93 |
assert(defString.find(ex1.exceptionName()) < 4); |
94 |
|
95 |
// |
96 |
// exception text should contain entered exception message |
97 |
// |
98 |
assert(ex1String.find(ex1Text) != string::npos); |
99 |
|
100 |
// |
101 |
// copy constructed exception should match original |
102 |
// |
103 |
EsysException copyEx(ex1); |
104 |
string copyString = copyEx.toString(); |
105 |
assert(ex1String == copyString); |
106 |
|
107 |
// |
108 |
// copy assigned exception should match original |
109 |
// |
110 |
EsysException assEx; |
111 |
assEx = ex1; |
112 |
string assString = assEx.toString(); |
113 |
assert(ex1String == assString); |
114 |
|
115 |
// |
116 |
// check throw/catch mechanism |
117 |
// |
118 |
string ex2Text("My second funny exception message."); |
119 |
try { |
120 |
|
121 |
EsysException ex2(ex2Text); |
122 |
throw(ex2); |
123 |
|
124 |
} |
125 |
|
126 |
catch(EsysException& e) { |
127 |
|
128 |
// |
129 |
// exception text should have contents of exceptionName near start |
130 |
// |
131 |
string eString = e.toString(); |
132 |
assert(eString.find(e.exceptionName()) != string::npos); |
133 |
assert(defString.find(e.exceptionName()) < 4); |
134 |
|
135 |
// |
136 |
// exception text should contain entered exception message |
137 |
// |
138 |
assert(eString.find(ex2Text) != string::npos); |
139 |
|
140 |
} |
141 |
|
142 |
} |
143 |
|
144 |
// |
145 |
// test derived EsysException |
146 |
// |
147 |
void EsysExceptionTestCase::testCase2() { |
148 |
|
149 |
DerivedEx defEx; |
150 |
// |
151 |
// default exception text should have contents of exceptionName near start |
152 |
// |
153 |
string defString = defEx.toString(); |
154 |
assert(defString.find(defEx.exceptionName()) != string::npos); |
155 |
assert(defString.find(defEx.exceptionName()) < 4); |
156 |
|
157 |
// |
158 |
// default exception text shouldn't be much longer than contents of exception name |
159 |
// |
160 |
assert(defString.size() > defEx.exceptionName().size()); |
161 |
assert((defString.size() - defEx.exceptionName().size()) < 10); |
162 |
|
163 |
string ex1Text("asdjhieurncidhfjsnfkjefkjndfjkhsdrdfjksdhfweh"); |
164 |
DerivedEx ex1(ex1Text); |
165 |
// |
166 |
// exception text should have contents of exceptionName near start |
167 |
// |
168 |
string ex1String = ex1.toString(); |
169 |
assert(ex1String.find(ex1.exceptionName()) != string::npos); |
170 |
assert(defString.find(ex1.exceptionName()) < 4); |
171 |
|
172 |
// |
173 |
// exception text should contain entered exception message |
174 |
// |
175 |
assert(ex1String.find(ex1Text) != string::npos); |
176 |
|
177 |
// |
178 |
// copy constructed exception should match original |
179 |
// |
180 |
DerivedEx copyEx(ex1); |
181 |
string copyString = copyEx.toString(); |
182 |
assert(ex1String == copyString); |
183 |
|
184 |
// |
185 |
// copy assigned exception should match original |
186 |
// |
187 |
DerivedEx assEx; |
188 |
assEx = ex1; |
189 |
string assString = assEx.toString(); |
190 |
assert(ex1String == assString); |
191 |
|
192 |
// |
193 |
// check throw/catch mechanism |
194 |
// |
195 |
string ex2Text("pjkkjhdfbnkjerbkjsduflfkjahalkgjlklhjhj"); |
196 |
try { |
197 |
|
198 |
DerivedEx ex2(ex2Text); |
199 |
throw(ex2); |
200 |
|
201 |
} |
202 |
|
203 |
catch(DerivedEx& e) { |
204 |
|
205 |
// |
206 |
// exception text should have contents of exceptionName near start |
207 |
// |
208 |
string eString = e.toString(); |
209 |
assert(eString.find(e.exceptionName()) != string::npos); |
210 |
assert(defString.find(e.exceptionName()) < 4); |
211 |
|
212 |
// |
213 |
// exception text should contain entered exception message |
214 |
// |
215 |
assert(eString.find(ex2Text) != string::npos); |
216 |
} |
217 |
|
218 |
// |
219 |
// check throw/catch mechanism |
220 |
// |
221 |
string ex3Text("irfjvniouf;iarhglAKSDIghlAKSDghladg"); |
222 |
try { |
223 |
|
224 |
DerivedEx ex3(ex3Text); |
225 |
throw(ex3); |
226 |
|
227 |
} |
228 |
catch(EsysException& e) { |
229 |
|
230 |
// |
231 |
// exception text should have contents of exceptionName near start |
232 |
// |
233 |
DerivedEx ex4; |
234 |
std::string eString = e.toString(); |
235 |
assert(eString.find(ex4.exceptionName()) != string::npos); |
236 |
assert(defString.find(ex4.exceptionName()) < 4); |
237 |
|
238 |
// |
239 |
// exception text should contain entered exception message |
240 |
// |
241 |
assert(eString.find(ex3Text) != string::npos); |
242 |
|
243 |
} |
244 |
|
245 |
// |
246 |
// test to see if exception name gets lost on rethrow |
247 |
// |
248 |
try { |
249 |
try { |
250 |
DerivedEx ex4("D ex4 text."); |
251 |
throw ex4; |
252 |
} |
253 |
catch (EsysException& e) { |
254 |
cout << endl << e.toString() << endl; |
255 |
throw; |
256 |
} |
257 |
} |
258 |
catch (EsysException& e) { |
259 |
cout << e.toString() << endl; |
260 |
} |
261 |
|
262 |
cout << "Test EsysException may be caught as a std::exception" << endl; |
263 |
try { |
264 |
DerivedEx ex4("Exception caught as std::exception"); |
265 |
throw ex4; |
266 |
} |
267 |
catch (exception& e) { |
268 |
// cout << e.what() << endl; |
269 |
assert(e.what() == string("DerivedException: Exception caught" |
270 |
" as std::exception") |
271 |
); |
272 |
} |
273 |
catch (...) { |
274 |
// |
275 |
// if the exception is caught here there is a problem |
276 |
assert(false); |
277 |
} |
278 |
} |
279 |
|
280 |
TestSuite* EsysExceptionTestCase::suite () |
281 |
{ |
282 |
// |
283 |
// create the suite of tests to perform. |
284 |
TestSuite *testSuite = new TestSuite ("EsysExceptionTestCase"); |
285 |
|
286 |
testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase0",&EsysExceptionTestCase::testCase0)); |
287 |
testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase1",&EsysExceptionTestCase::testCase1)); |
288 |
testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase2",&EsysExceptionTestCase::testCase2)); |
289 |
return testSuite; |
290 |
} |