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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1448 - (show annotations)
Thu Feb 28 12:31:04 2008 UTC (15 years ago) by phornby
File size: 6727 byte(s)
Derived a consistent method of derivation from EsysException embodied in
EsysExceptionTestCase.cpp.
Eliminated xxxxException::operator=(). It was not used, and just made more work.

run_tests passes on linux.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26