|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #ifndef ITK_ITKIMPL_H_ |
|
18 #define ITK_ITKIMPL_H_ |
|
19 |
|
20 #include <string> |
|
21 |
|
22 #include "cpixfstools.h" |
|
23 |
|
24 namespace Itk |
|
25 { |
|
26 namespace Impl |
|
27 { |
|
28 |
|
29 |
|
30 /** |
|
31 * Tells if it could open the file. |
|
32 */ |
|
33 bool FileExists(const char * path); |
|
34 |
|
35 |
|
36 /** |
|
37 * Redirects the standard input, if necessary |
|
38 */ |
|
39 class InputRedirector |
|
40 { |
|
41 private: |
|
42 int duplicatedStdInFD_; |
|
43 int inFileFD_; |
|
44 |
|
45 public: |
|
46 InputRedirector(const std::string & defFilesBasePath); |
|
47 ~InputRedirector(); |
|
48 }; |
|
49 |
|
50 |
|
51 |
|
52 /** |
|
53 * Redirects the standard output, if necessary |
|
54 */ |
|
55 class OutputRedirector |
|
56 { |
|
57 private: |
|
58 int duplicatedStdOutFD_; |
|
59 int outFileFD_; |
|
60 bool stdErr_; |
|
61 TestMgr * testMgr_; |
|
62 |
|
63 public: |
|
64 /** |
|
65 * Creates an output redirector instance. The same class |
|
66 * is used for redirecting output and error, but there are |
|
67 * slight differences in how they work. See parameter |
|
68 * list. |
|
69 * |
|
70 * @param defFilesBasePath the path of the base of the |
|
71 * test definition files |
|
72 * |
|
73 * @param testMgr must NOT be null for stdout redirection |
|
74 * and must be NULL for redirecting stderr. |
|
75 */ |
|
76 OutputRedirector(const std::string & defFilesBasePath, |
|
77 TestMgr * testMgr); |
|
78 ~OutputRedirector(); |
|
79 }; |
|
80 |
|
81 |
|
82 /** |
|
83 * Evaluates the results of an IO capturing session based on the |
|
84 * defFileBasePath argument (ctor) and what it can find on the |
|
85 * filesystem around those paths. |
|
86 */ |
|
87 class IOCaptureEvaluator |
|
88 { |
|
89 private: |
|
90 // |
|
91 // private members |
|
92 // |
|
93 const std::string defFileBasePath_; |
|
94 TestMgr * testMgr_; |
|
95 const std::string & lenience_; |
|
96 |
|
97 |
|
98 public: |
|
99 // |
|
100 // public operators |
|
101 // |
|
102 void evaluate(); |
|
103 |
|
104 |
|
105 // |
|
106 // lifetime mgmt |
|
107 // |
|
108 IOCaptureEvaluator(const std::string & defFileBasePath, |
|
109 TestMgr * testMgr, |
|
110 const std::string & lenience); |
|
111 |
|
112 private: |
|
113 // |
|
114 // private methods |
|
115 // |
|
116 void copyContent(const char * dstPath, |
|
117 const char * srcPath); |
|
118 void compareContent(const char * expPath, |
|
119 const char * resPath); |
|
120 void evaluate(bool stdErr); |
|
121 }; |
|
122 |
|
123 |
|
124 /** |
|
125 * Only requirement for testFunc is that it should support the |
|
126 * following syntax: testFunc(testMgr); |
|
127 **/ |
|
128 template<typename TESTFUNC> |
|
129 void EvaluateIOCapture(const std::string & defFilesBaseName, |
|
130 TestMgr * testMgr, |
|
131 TESTFUNC testFunc, |
|
132 const std::string & lenience) |
|
133 { |
|
134 using namespace std; |
|
135 using namespace Itk::Impl; |
|
136 |
|
137 string |
|
138 defFilesBasePath; |
|
139 testMgr->ioTestCasesDir(defFilesBasePath); |
|
140 // OBS defFilesBasePath += defFilesBaseName; |
|
141 Cpt::pathappend(defFilesBasePath, |
|
142 defFilesBaseName.c_str()); |
|
143 |
|
144 try |
|
145 { |
|
146 InputRedirector |
|
147 stdInRedirector(defFilesBasePath); |
|
148 OutputRedirector |
|
149 stdOutRedirector(defFilesBasePath, |
|
150 testMgr), // std out |
|
151 stdErrRedirector(defFilesBasePath, |
|
152 NULL); // std err |
|
153 |
|
154 testFunc(testMgr); |
|
155 } |
|
156 catch (IOCaptureExc & exc) |
|
157 { |
|
158 testMgr->ioCaptureError("(?)", |
|
159 exc.what()); |
|
160 } |
|
161 |
|
162 IOCaptureEvaluator |
|
163 evaluator(defFilesBasePath, |
|
164 testMgr, |
|
165 lenience); |
|
166 evaluator.evaluate(); |
|
167 } |
|
168 |
|
169 |
|
170 } |
|
171 } |
|
172 |
|
173 #endif ITK_ITKIMPL_H_ |