Add copyright comment headers to appropriate files
[gssweb.git] / json_gssapi / test / test_with_options.cpp
1 /*
2  * Copyright (c) 2014, 2015 JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #ifndef _1_cppunit_cpp_
36 #define _1_cppunit_cpp_
37
38 /*
39  * Copyright (c) 2008, Robert Emerson
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions are met:
44  *     * Redistributions of source code must retain the above copyright
45  *       notice, this list of conditions and the following disclaimer.
46  *     * Redistributions in binary form must reproduce the above copyright
47  *       notice, this list of conditions and the following disclaimer in the
48  *       documentation and/or other materials provided with the distribution.
49  *     * Neither the name of Robert Emerson nor the
50  *       names of its contributors may be used to endorse or promote products
51  *       derived from this software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY ROBERT EMERSON ''AS IS'' AND ANY
54  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
55  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56  * DISCLAIMED. IN NO EVENT SHALL ROBERT EMERSON BE LIABLE FOR ANY
57  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
59  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
60  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
62  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  */
64
65 #include <unistd.h>
66
67 #include <iostream>
68 #include <exception>
69 #include <memory>
70 #include <string>
71 #include <map>
72
73 #include <cppunit/CompilerOutputter.h>
74 #include <cppunit/TextOutputter.h>
75 #include <cppunit/XmlOutputter.h>
76 #include <cppunit/extensions/TestFactoryRegistry.h>
77 #include <cppunit/TestResult.h>
78 #include <cppunit/TestResultCollector.h>
79 #include <cppunit/TestRunner.h>
80 #include <cppunit/TextTestProgressListener.h>
81 #include <cppunit/BriefTestProgressListener.h>
82 #include <cppunit/TestSuccessListener.h>
83 #include <cppunit/Portability.h>
84
85 using namespace CPPUNIT_NS;
86
87 using std::cerr;
88 using std::cout;
89 using std::endl;
90 using std::exception;
91 using std::auto_ptr;
92 using std::string;
93 using std::map;
94 using std::make_pair;
95
96 static const int RET_OK = 0;
97 static const int RET_USAGE = -1;
98 static const int RET_BAD_OUTPUTTER = -2;
99 static const int RET_BAD_LISTENER = -3;
100 static const int RET_BAD_TEST = -4;
101
102 typedef map<string, Outputter *> OutputterMap;
103 typedef map<string, TestListener *> ListenerMap;
104
105 void usage(const char *image, 
106            const char *defaultTest,
107            const OutputterMap &outputters,
108            const ListenerMap &listeners)
109 {
110   cout << endl;
111   cout << "Usage: " << basename(image) 
112        << " (-o outputter) (-p progress) (-t test) (-r count)" << endl;
113   cout << "       " << basename(image) 
114        << " [ -l | -h ]" << endl;
115   cout << endl;
116   cout << "A CppUnit command line test execution tool. " << endl;
117   cout << endl;
118   cout << "Built against cppunit version: " << CPPUNIT_VERSION << endl;
119   cout << endl;
120   cout << "Options:" << endl;
121   cout << endl;
122   cout << "    -o Output style. Choices: ";
123   
124   {
125     OutputterMap::const_iterator it = outputters.begin();
126     
127     while (it != outputters.end()) { 
128       cout << it->first << " "; 
129       ++it;
130     }
131   }
132
133   cout << "Default: compiler" << endl;
134   cout << "    -p Progress style. Choices: ";
135   {
136     ListenerMap::const_iterator it = listeners.begin();
137
138     while (it != listeners.end()) { 
139       cout << it->first << " "; 
140       ++it;
141     }
142   }
143
144   cout << "Default: dots";
145   cout << endl;
146   cout << "    -t Runs given the test only. Default: \"" << defaultTest << "\"" << endl;
147   cout << "    -r Repeat count. Default: 1" << endl;
148   cout << endl;
149   cout << "    -l List all available tests." << endl;
150   cout << "    -h Print this usage message." << endl;
151   cout << endl;
152   cout << "Returns 0 on success, negative for usage errors, positive errors plus failures." << endl;
153   cout << endl;
154   cout << "If the image name is a valid test name, then this is the default test executed." << endl;
155   cout << endl;
156
157   return;
158 }
159
160 // Recursive dumps the given Test heirarchy to cout
161 void dump(Test *test)
162 {
163   if (0 == test) return;
164
165   cout << test->getName() << endl;
166
167   if (0 == test->getChildTestCount()) return;
168
169   for (int i = 0; i < test->getChildTestCount(); i++) {
170     dump(test->getChildTestAt(i));
171   }
172
173   return;
174 }
175
176 // Recursively seeks the Test matching the given name, returns 0 otherwise.
177 Test *find(Test *test, const string &name)
178 {
179   if (0 == test) return 0;
180   if (name == test->getName()) return test;
181   if (0 == test->getChildTestCount()) return 0;
182
183   for (int i = 0; i < test->getChildTestCount(); i++) {
184     Test *found = find(test->getChildTestAt(i), name);
185     if (found) return found;
186   }
187
188   return 0;
189 }
190
191 int main(int argc, char **argv)
192 {
193   TestResult result;
194   TestResultCollector collector;
195   result.addListener(&collector);
196   
197   OutputterMap allOutputters;
198   {
199     allOutputters.insert(make_pair("compiler", 
200                                    new CompilerOutputter(&collector, cout)));
201     allOutputters.insert(make_pair("text", 
202                                    new TextOutputter(&collector, cout)));
203     allOutputters.insert(make_pair("xml", 
204                                    new XmlOutputter(&collector, cout)));
205     allOutputters.insert(make_pair("none", static_cast<Outputter *>(0)));
206   }
207   Outputter *outputter = allOutputters.find("compiler")->second;
208  
209   ListenerMap allListeners;
210   {
211     allListeners.insert(make_pair("dots", new TextTestProgressListener()));
212     allListeners.insert(make_pair("brief", new BriefTestProgressListener()));
213     allListeners.insert(make_pair("none", static_cast<TestListener *>(0)));
214   }
215   TestListener *listener = allListeners.find("dots")->second;
216
217   string runTest = basename(argv[0]);
218
219   if (!find(TestFactoryRegistry::getRegistry().makeTest(), runTest)) {
220       runTest = "All Tests";
221   }
222
223   int repeat = 1;
224   char flag = 0;
225   while ((flag = getopt(argc, argv, "r:t:o:p:lh")) != -1) {
226
227     switch(flag) {
228
229     case 'r':
230       repeat = atoi(optarg);
231       break;
232
233     case 'o':
234       {
235         OutputterMap::const_iterator it = allOutputters.find(optarg);
236         if (it == allOutputters.end()) {
237           cerr << "Unknown outputter: " << optarg << endl;
238           return RET_BAD_OUTPUTTER;
239         }
240         outputter = it->second;
241       }
242       break;
243
244     case 'p':
245       {
246         string progress(optarg);
247         ListenerMap::const_iterator it = allListeners.find(optarg);
248         if (it == allListeners.end()) {
249           cerr << "Unknown listener: " << optarg << endl;
250           return RET_BAD_LISTENER;
251         }
252         listener = it->second;
253       }
254       break;
255
256     case 'l':
257       {
258         Test *all = TestFactoryRegistry::getRegistry().makeTest();
259         dump(all);
260         return RET_OK;
261       }
262       break; // not reached
263
264     case 't':
265       {
266         runTest = optarg;
267       }
268       break;
269
270     case 'h':
271     default:
272       usage(argv[0], runTest.c_str(), allOutputters, allListeners);
273       return RET_USAGE;
274       break; // not reached
275     }
276   }
277   if (listener != 0) result.addListener(listener);
278
279   Test *run = find(TestFactoryRegistry::getRegistry().makeTest(), runTest);
280   if (run == 0) {
281     cerr << "Unknown test case: " << runTest << endl;
282     return RET_BAD_TEST;
283   }
284   
285   TestRunner runner;
286   runner.addTest(run);
287   
288   for (int i = 0; i < repeat; i++) {
289     runner.run(result);
290   }
291
292   if (outputter) outputter->write();
293
294   return collector.testErrors() + collector.testFailures();
295
296 }
297
298 #endif // _1_cppunit_cpp_