/[escript]/release/4.0/esysUtils/test/EsysExceptionTestCase.cpp
ViewVC logotype

Contents of /release/4.0/esysUtils/test/EsysExceptionTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5379 - (show annotations)
Mon Dec 15 23:58:17 2014 UTC (6 years, 6 months ago) by jfenwick
File size: 7527 byte(s)
branching for release prep

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26