1 /*------------------------------------------------------------------------------
  2 Name:      TestC.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Integrate C tests into junit reports
  6 ------------------------------------------------------------------------------*/
  7 package org.xmlBlaster.test.C;
  8 
  9 import java.io.File;
 10 
 11 import java.util.logging.Logger;
 12 import org.xmlBlaster.util.Global;
 13 import org.xmlBlaster.util.Execute;
 14 import org.xmlBlaster.util.I_ExecuteListener;
 15 
 16 //import java.io.*;
 17 
 18 import junit.framework.*;
 19 
 20 
 21 /**
 22  * This client tests the <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.c.socket.html">
 23  * C SOCKET client library, it is only a wrapper around the real C tests in xmlBlaster/testsuite/src/c
 24  * to retrieve the results for our HTML test report (see build.xml 'report' task). 
 25  * <p />
 26  * xmlBlaster needs to be started separately.
 27  * <p />
 28  * Invoke examples:<br />
 29  * <pre>
 30  *    java junit.textui.TestRunner org.xmlBlaster.test.C.TestC
 31  *    java junit.swingui.TestRunner -noloading org.xmlBlaster.test.C.TestC
 32  * </pre>
 33  */
 34 public class TestC extends TestCase implements I_ExecuteListener
 35 {
 36    private final Global glob;
 37    private static Logger log = Logger.getLogger(TestC.class.getName());
 38 
 39    private String pathToCBinary = null;
 40    String sep = File.separator;
 41 
 42    /**
 43     * Constructs the TestC object. 
 44     * <p />
 45     * @param testName   The name used in the test suite
 46     */
 47    public TestC(String testName) {
 48       super(testName);
 49       this.glob = Global.instance();
 50 
 51    }
 52 
 53    /**
 54     * Sets up the fixture.
 55     * <p />
 56     * We start an own xmlBlaster server in a separate thread,
 57     * it is configured to load the tinySQL C driver to test SQL access (with dBase files)
 58     * <p />
 59     * Then we connect as a client
 60     */
 61    protected void setUp()
 62    {
 63       // We register here the demo plugin with xmlBlaster server, supplying an argument to the plugin
 64       /*
 65       Vector argsVec = Util.getOtherServerPortVec(serverPort);
 66       glob.init((String[])argsVec.toArray(new String[argsVec.size()]));
 67       serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob);
 68       log.info("XmlBlaster is ready for testing C client library");
 69       */
 70 
 71       /* Find the location of the C binaries */
 72       String xmlBlasterHome = getXmlBlasterHomePath();
 73       this.pathToCBinary = xmlBlasterHome+sep+"testsuite"+sep+"src"+sep+"c"+sep+"bin";
 74       File f = new File(this.pathToCBinary);
 75       log.fine("Looking under '" + f.toString() + "'");
 76       if (f.exists()) {
 77          log.info("Found C executables under '" + this.pathToCBinary + "'");
 78       }
 79       assertTrue("Path to C binaries not found, no testing of C client library is not possible", this.pathToCBinary!=null);
 80    }
 81 
 82    /**
 83     * @return for example ""
 84     */
 85    private String getXmlBlasterHomePath() {
 86       String xmlBlasterHome = glob.getProperty().get("XMLBLASTER_HOME", "$HOME"+sep+"xmlBlaster");
 87       File f = new File(xmlBlasterHome);
 88       if (f.exists()) {
 89          return xmlBlasterHome;
 90       }
 91       xmlBlasterHome = "..";
 92       f = new File(xmlBlasterHome+"RELEASE_NOTES");
 93       if (f.exists()) {
 94          return xmlBlasterHome;
 95       }
 96       for (int i=0; i<10; i++) {
 97          xmlBlasterHome += sep+"..";
 98          f = new File(xmlBlasterHome+sep+"RELEASE_NOTES");
 99          if (f.exists())
100             return xmlBlasterHome;
101       }
102       return null;
103    }
104 
105    /**
106     * Tears down the fixture.
107     * <p />
108     * cleaning up .... erase() the previous message OID and logout
109     */
110    protected void tearDown()
111    {
112       /*
113       EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread);
114       this.serverThread = null;
115       Util.resetPorts();
116       */
117    }
118 
119    /**
120     * Test all C method invocations against a running xmlBlaster. 
121     */
122    public void test_C_MethodInvocations()
123    {
124       String[] commandArr = { pathToCBinary+sep+"TestMethods" };
125       String[] envArr = { "" };
126 
127       log.info("######## Start test_C_MethodInvocations('" + commandArr[0] + "')");
128 
129       Execute e = new Execute(commandArr, envArr);
130       e.setExecuteListener(this);
131       e.run();
132 
133       if (e.getExitValue() != 0) {
134          fail("C client library test '" + commandArr[0] + "' + failed exit=" + e.getExitValue() + ": " + e.getStderr());
135       }
136 
137       if (e.getErrorText() != null) {
138          fail(e.getErrorText());
139       }
140 
141       if (e.getStdout().indexOf("[TEST FAIL]") != -1) {
142          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStdout());
143       }
144       if (e.getStderr().indexOf("[TEST FAIL]") != -1) {
145          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStderr());
146       }
147 
148       log.info("######## SUCCESS test_C_MethodInvocations('" + commandArr[0] + "') exit=" +
149                e.getExitValue() + " : " + e.getStdout());
150    }
151 
152    /**
153     * Test the C API with illegal arguments. 
154     */
155    public void test_C_IllegalArguments()
156    {
157       String[] commandArr = { pathToCBinary+sep+"TestError" };
158       String[] envArr = { "" };
159 
160       log.info("######## Start test_C_IllegalArguments('" + commandArr[0] + "')");
161 
162       Execute e = new Execute(commandArr, envArr);
163       e.setExecuteListener(this);
164       e.run();
165 
166       if (e.getExitValue() != 0) {
167          fail("C client library test '" + commandArr[0] + "' + failed exit=" + e.getExitValue() + ": " + e.getStderr());
168       }
169 
170       if (e.getErrorText() != null) {
171          fail(e.getErrorText());
172       }
173 
174       if (e.getStdout().indexOf("[TEST FAIL]") != -1) {
175          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStdout());
176       }
177       if (e.getStderr().indexOf("[TEST FAIL]") != -1) {
178          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStderr());
179       }
180 
181       log.info("######## SUCCESS test_C_IllegalArguments('" + commandArr[0] + "') exit=" +
182                e.getExitValue() + " : " + e.getStdout());
183    }
184 
185    /**
186     * Test all C method invocations against a running xmlBlaster. 
187     */
188    public void test_C_Stress()
189    {
190       String[] commandArr = { pathToCBinary+sep+"TestStress" };
191       String[] envArr = { "" };
192 
193       log.info("######## Start test_C_Stress('" + commandArr[0] + "')");
194 
195       Execute e = new Execute(commandArr, envArr);
196       e.setExecuteListener(this);
197       e.run();
198 
199       if (e.getExitValue() != 0) {
200          fail("C client library test '" + commandArr[0] + "' + failed exit=" + e.getExitValue() + ": " + e.getStderr());
201       }
202 
203       if (e.getErrorText() != null) {
204          fail(e.getErrorText());
205       }
206 
207       if (e.getStdout().indexOf("[TEST FAIL]") != -1) {
208          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStdout());
209       }
210       if (e.getStderr().indexOf("[TEST FAIL]") != -1) {
211          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStderr());
212       }
213 
214       log.info("######## SUCCESS test_C_Stress('" + commandArr[0] + "') exit=" +
215                e.getExitValue() + " : " + e.getStdout());
216    }
217 
218    /**
219     * Test all C method invocations against a running xmlBlaster. 
220     */
221    public void test_C_Util()
222    {
223       String[] commandArr = { pathToCBinary+sep+"TestUtil" };
224       String[] envArr = { "" };
225 
226       log.info("######## Start test_C_Util('" + commandArr[0] + "')");
227 
228       Execute e = new Execute(commandArr, envArr);
229       e.setExecuteListener(this);
230       e.run();
231 
232       if (e.getExitValue() != 0) {
233          fail("C client library test '" + commandArr[0] + "' + failed exit=" + e.getExitValue() + ": " + e.getStderr());
234       }
235 
236       if (e.getErrorText() != null) {
237          fail(e.getErrorText());
238       }
239 
240       if (e.getStdout().indexOf("[TEST FAIL]") != -1) {
241          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStdout());
242       }
243       if (e.getStderr().indexOf("[TEST FAIL]") != -1) {
244          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStderr());
245       }
246 
247       log.info("######## SUCCESS test_C_Util('" + commandArr[0] + "') exit=" +
248                e.getExitValue() + " : " + e.getStdout());
249    }
250 
251    /**
252     * Test all C method invocations against a running xmlBlaster. 
253     */
254    public void test_C_XmlUtil()
255    {
256       String[] commandArr = { pathToCBinary+sep+"TestXmlUtil" };
257       String[] envArr = { "" };
258 
259       log.info("######## Start test_C_XmlUtil('" + commandArr[0] + "')");
260 
261       Execute e = new Execute(commandArr, envArr);
262       e.setExecuteListener(this);
263       e.run();
264 
265       if (e.getExitValue() != 0) {
266          fail("C client library test '" + commandArr[0] + "' + failed exit=" + e.getExitValue() + ": " + e.getStderr());
267       }
268 
269       if (e.getErrorText() != null) {
270          fail(e.getErrorText());
271       }
272 
273       if (e.getStdout().indexOf("[TEST FAIL]") != -1) {
274          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStdout());
275       }
276       if (e.getStderr().indexOf("[TEST FAIL]") != -1) {
277          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStderr());
278       }
279 
280       log.info("######## SUCCESS test_C_XmlUtil('" + commandArr[0] + "') exit=" +
281                e.getExitValue() + " : " + e.getStdout());
282    }
283 
284    public void test_C_Timeout()
285    {
286       String[] commandArr = { pathToCBinary+sep+"TestTimeout" };
287       String[] envArr = { "" };
288 
289       log.info("######## Start test_C_Timeout('" + commandArr[0] + "')");
290 
291       Execute e = new Execute(commandArr, envArr);
292       e.setExecuteListener(this);
293       e.run();
294 
295       if (e.getExitValue() != 0) {
296          fail("C client library test '" + commandArr[0] + "' + failed exit=" + e.getExitValue() + ": " + e.getStderr());
297       }
298 
299       if (e.getErrorText() != null) {
300          fail(e.getErrorText());
301       }
302 
303       if (e.getStdout().indexOf("[TEST FAIL]") != -1) {
304          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStdout());
305       }
306       if (e.getStderr().indexOf("[TEST FAIL]") != -1) {
307          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStderr());
308       }
309 
310       log.info("######## SUCCESS test_C_Timeout('" + commandArr[0] + "') exit=" +
311                e.getExitValue() + " : " + e.getStdout());
312    }
313 
314    /**
315     * Test the C API persistent queue implementation. 
316     */
317    public void test_C_Queue()
318    {
319       String[] commandArr = { pathToCBinary+sep+"TestQueue" };
320       String[] envArr = { "" };
321 
322       log.info("######## Start test_C_Queue('" + commandArr[0] + "')");
323 
324       Execute e = new Execute(commandArr, envArr);
325       e.setExecuteListener(this);
326       e.run();
327 
328       if (e.getExitValue() != 0) {
329          fail("C client library test '" + commandArr[0] + "' + failed exit=" + e.getExitValue() + ": " + e.getStderr());
330       }
331 
332       if (e.getErrorText() != null) {
333          fail(e.getErrorText());
334       }
335 
336       if (e.getStdout().indexOf("[TEST FAIL]") != -1) {
337          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStdout());
338       }
339       if (e.getStderr().indexOf("[TEST FAIL]") != -1) {
340          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStderr());
341       }
342 
343       log.info("######## SUCCESS test_C_Queue('" + commandArr[0] + "') exit=" +
344                e.getExitValue() + " : " + e.getStdout());
345    }
346 
347    /**
348     * Test the C API implementation, leaving the server without disconnect. 
349     */
350    public void test_C_LeaveServer()
351    {
352       String[] commandArr = { pathToCBinary+sep+"TestLeaveServer" };
353       String[] envArr = { "" };
354 
355       log.info("######## Start test_C_LeaveServer('" + commandArr[0] + "')");
356 
357       Execute e = new Execute(commandArr, envArr);
358       e.setExecuteListener(this);
359       e.run();
360 
361       if (e.getExitValue() != 0) {
362          fail("C client library test '" + commandArr[0] + "' + failed exit=" + e.getExitValue() + ": " + e.getStderr());
363       }
364 
365       if (e.getErrorText() != null) {
366          fail(e.getErrorText());
367       }
368 
369       if (e.getStdout().indexOf("[TEST FAIL]") != -1) {
370          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStdout());
371       }
372       if (e.getStderr().indexOf("[TEST FAIL]") != -1) {
373          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStderr());
374       }
375 
376       log.info("######## SUCCESS test_C_LeaveServer('" + commandArr[0] + "') exit=" +
377                e.getExitValue() + " : " + e.getStdout());
378    }
379 
380    /**
381     * Test the C API SOCKET implementation. 
382     */
383    public void test_C_Socket()
384    {
385       String[] commandArr = { pathToCBinary+sep+"TestSocket" };
386       String[] envArr = { "" };
387 
388       log.info("######## Start test_C_Socket('" + commandArr[0] + "')");
389 
390       Execute e = new Execute(commandArr, envArr);
391       e.setExecuteListener(this);
392       e.run();
393 
394       if (e.getExitValue() != 0) {
395          fail("C client library test '" + commandArr[0] + "' + failed exit=" + e.getExitValue() + ": " + e.getStderr());
396       }
397 
398       if (e.getErrorText() != null) {
399          fail(e.getErrorText());
400       }
401 
402       if (e.getStdout().indexOf("[TEST FAIL]") != -1) {
403          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStdout());
404       }
405       if (e.getStderr().indexOf("[TEST FAIL]") != -1) {
406          fail("C client library test '" + commandArr[0] + "' + failed: " + e.getStderr());
407       }
408 
409       log.info("######## SUCCESS test_C_Socket('" + commandArr[0] + "') exit=" +
410                e.getExitValue() + " : " + e.getStdout());
411    }
412 
413    public void stdout(String data) {
414       log.info("Native C output: " + data);
415    }
416    public void stderr(String data) {
417       log.severe("Native C output: " + data);
418    }
419 
420    /**
421     * Invoke: java org.xmlBlaster.test.C.TestC
422     * @deprecated Use the TestRunner from the testsuite to run it
423     */
424    public static void main(String args[]) {
425       Global glob = Global.instance();
426       if (glob.init(args) != 0) {
427          System.err.println("Init failed");
428          System.exit(1);
429       }                 
430       TestC test = new TestC("TestC");
431       test.setUp();
432       test.test_C_Util();
433       test.test_C_XmlUtil();
434       test.test_C_Timeout();
435       test.test_C_MethodInvocations();
436       test.test_C_IllegalArguments();
437       test.test_C_Queue();
438       test.test_C_LeaveServer();
439       test.test_C_Socket();
440       test.test_C_Stress();
441       test.tearDown();
442    }
443 }


syntax highlighted by Code2HTML, v. 0.9.1