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