/[escript]/branches/windows_from_1456_trunk_1522_merged_in/esysUtils/test/EsysExceptionTestCase.cpp
ViewVC logotype

Contents of /branches/windows_from_1456_trunk_1522_merged_in/esysUtils/test/EsysExceptionTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1529 - (show annotations)
Tue Apr 29 02:03:19 2008 UTC (12 years, 7 months ago) by phornby
File size: 7095 byte(s)
Add an updateMessage to DerivedEX::operator=(), just in case.
This test case is the pattern to be used in deriving EsysExceptions.
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 virtual DerivedEx &
44 operator=(const DerivedEx &other) THROW_ANY
45 {
46 Parent::operator=(other);
47 // call this just incase....
48 updateMessage();
49 return *this;
50 }
51
52 /// Return the exception name
53 virtual const string & exceptionName() const
54 {
55 return rhubarb;
56 }
57
58 static const string rhubarb;
59 };
60
61 const string DerivedEx::rhubarb("DerivedException");
62
63 void EsysExceptionTestCase::testCase0() {
64
65 assert(true);
66
67 }
68
69 void EsysExceptionTestCase::testCase1() {
70
71 EsysException defEx;
72
73 //
74 // default exception text should have contents of exceptionName near start
75 //
76 string defString = defEx.toString();
77 assert(defString.find(defEx.exceptionName()) != string::npos);
78 assert(defString.find(defEx.exceptionName()) < 4);
79
80 //
81 // default exception text shouldn't be much longer than contents of exception name
82 //
83 assert(defString.size() > defEx.exceptionName().size());
84 assert((defString.size() - defEx.exceptionName().size()) < 10);
85
86 string ex1Text("My first funny exception message.");
87 EsysException ex1(ex1Text);
88
89 //
90 // exception text should have contents of exceptionName near start
91 //
92 string ex1String = ex1.toString();
93 assert(ex1String.find(ex1.exceptionName()) != string::npos);
94 assert(defString.find(ex1.exceptionName()) < 4);
95
96 //
97 // exception text should contain entered exception message
98 //
99 assert(ex1String.find(ex1Text) != string::npos);
100
101 //
102 // copy constructed exception should match original
103 //
104 EsysException copyEx(ex1);
105 string copyString = copyEx.toString();
106 assert(ex1String == copyString);
107
108 //
109 // copy assigned exception should match original
110 //
111 EsysException assEx;
112 assEx = ex1;
113 string assString = assEx.toString();
114 assert(ex1String == assString);
115
116 //
117 // check throw/catch mechanism
118 //
119 string ex2Text("My second funny exception message.");
120 try {
121
122 EsysException ex2(ex2Text);
123 throw(ex2);
124
125 }
126
127 catch(EsysException& e) {
128
129 //
130 // exception text should have contents of exceptionName near start
131 //
132 string eString = e.toString();
133 assert(eString.find(e.exceptionName()) != string::npos);
134 assert(defString.find(e.exceptionName()) < 4);
135
136 //
137 // exception text should contain entered exception message
138 //
139 assert(eString.find(ex2Text) != string::npos);
140
141 }
142
143 }
144
145 //
146 // test derived EsysException
147 //
148 void EsysExceptionTestCase::testCase2() {
149
150 DerivedEx defEx;
151 //
152 // default exception text should have contents of exceptionName near start
153 //
154 string defString = defEx.toString();
155 assert(defString.find(defEx.exceptionName()) != string::npos);
156 assert(defString.find(defEx.exceptionName()) < 4);
157
158 //
159 // default exception text shouldn't be much longer than contents of exception name
160 //
161 assert(defString.size() > defEx.exceptionName().size());
162 assert((defString.size() - defEx.exceptionName().size()) < 10);
163
164 string ex1Text("asdjhieurncidhfjsnfkjefkjndfjkhsdrdfjksdhfweh");
165 DerivedEx ex1(ex1Text);
166 //
167 // exception text should have contents of exceptionName near start
168 //
169 string ex1String = ex1.toString();
170 assert(ex1String.find(ex1.exceptionName()) != string::npos);
171 assert(defString.find(ex1.exceptionName()) < 4);
172
173 //
174 // exception text should contain entered exception message
175 //
176 assert(ex1String.find(ex1Text) != string::npos);
177
178 //
179 // copy constructed exception should match original
180 //
181 DerivedEx copyEx(ex1);
182 string copyString = copyEx.toString();
183 assert(ex1String == copyString);
184
185 //
186 // copy assigned exception should match original
187 //
188 DerivedEx assEx;
189 assEx = ex1;
190 string assString = assEx.toString();
191 assert(ex1String == assString);
192
193 //
194 // check throw/catch mechanism
195 //
196 string ex2Text("pjkkjhdfbnkjerbkjsduflfkjahalkgjlklhjhj");
197 try {
198
199 DerivedEx ex2(ex2Text);
200 throw(ex2);
201
202 }
203
204 catch(DerivedEx& e) {
205
206 //
207 // exception text should have contents of exceptionName near start
208 //
209 string eString = e.toString();
210 assert(eString.find(e.exceptionName()) != string::npos);
211 assert(defString.find(e.exceptionName()) < 4);
212
213 //
214 // exception text should contain entered exception message
215 //
216 assert(eString.find(ex2Text) != string::npos);
217 }
218
219 //
220 // check throw/catch mechanism
221 //
222 string ex3Text("irfjvniouf;iarhglAKSDIghlAKSDghladg");
223 try {
224
225 DerivedEx ex3(ex3Text);
226 throw(ex3);
227
228 }
229 catch(EsysException& e) {
230
231 //
232 // exception text should have contents of exceptionName near start
233 //
234 DerivedEx ex4;
235 std::string eString = e.toString();
236 assert(eString.find(ex4.exceptionName()) != string::npos);
237 assert(defString.find(ex4.exceptionName()) < 4);
238
239 //
240 // exception text should contain entered exception message
241 //
242 assert(eString.find(ex3Text) != string::npos);
243
244 }
245
246 //
247 // test to see if exception name gets lost on rethrow
248 //
249 try {
250 try {
251 DerivedEx ex4("D ex4 text.");
252 throw ex4;
253 }
254 catch (EsysException& e) {
255 cout << endl << e.toString() << endl;
256 throw;
257 }
258 }
259 catch (EsysException& e) {
260 cout << e.toString() << endl;
261 }
262
263 cout << "Test EsysException may be caught as a std::exception" << endl;
264 try {
265 DerivedEx ex4("Exception caught as std::exception");
266 throw ex4;
267 }
268 catch (exception& e) {
269 // cout << e.what() << endl;
270 assert(e.what() == string("DerivedException: Exception caught"
271 " as std::exception")
272 );
273 }
274 catch (...) {
275 //
276 // if the exception is caught here there is a problem
277 assert(false);
278 }
279 }
280
281 TestSuite* EsysExceptionTestCase::suite ()
282 {
283 //
284 // create the suite of tests to perform.
285 TestSuite *testSuite = new TestSuite ("EsysExceptionTestCase");
286
287 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase0",&EsysExceptionTestCase::testCase0));
288 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase1",&EsysExceptionTestCase::testCase1));
289 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase2",&EsysExceptionTestCase::testCase2));
290 return testSuite;
291 }

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26