1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MultiMethodTest.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import java
.io
.PrintWriter
;
33 import java
.lang
.reflect
.Field
;
34 import java
.lang
.reflect
.InvocationTargetException
;
35 import java
.lang
.reflect
.Method
;
36 import java
.util
.Vector
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
39 import com
.sun
.star
.uno
.XInterface
;
41 import share
.DescEntry
;
42 import lib
.TestParameters
;
43 import stats
.Summarizer
;
46 * The class supports method based interface tests development.
48 * <p>There are some points that should be fulfilled in a subclass to work
49 * correctly in the multi-method framework:
51 * 1. each subclass schould define a public field named oObj of type tested
52 * by the subclass, e.g. 'public XText oObj;'. That field will be initialized
53 * by the MultiMethodTest code with the instance of the interface to test.
54 * In a case of service testing the field type should be XPropertySet.
56 * 2. for the test of each method of the tested interface(or a property in the
57 * case of service testing) should be method with the following signature
58 * provided: 'public void _<method name>()', e.g. 'public void _getText()'.
59 * The methods will be called by MultiMethodText code using reflection API
60 * for each method in the interface description.
62 * 3. to set status for a call 'tRes.tested(String method,
63 * boolean result)' should be used. For example 'tRes.tested("getText()",
64 * true)'. Also 'tRes.assert(String assertion, boolean result)' call can
65 * be used. Note, that one can call the methods not neccesarily from the
66 * test for the tested method, but from other method tests too (in the
67 * MultiMethodTest subclass). See also TestResult and MultiMethodTest.tRes
70 * 4. the before() and after() methods can be overriden to perform some
71 * actions, accordingly, before and after calling the test methods.
73 * 5. besides tRes, there are some fields initialized in the MultiMethodTest,
74 * that can be used for implementing tests:
76 * - tEnv contains the environment tested
77 * - tParam contains parameters of the test
78 * - log a writer to log information about the test
82 public class MultiMethodTest
86 * Contains the TestEnvironment being tested, to allow for tests to access
89 protected TestEnvironment tEnv
;
91 * Contains the TestParameters for the tests, to allow for tests to access
94 protected TestParameters tParam
;
96 * Contains the Description for the test
99 protected DescEntry entry
;
101 * Contains a writer to log an information about the interface testing, to
102 * allows for tests to access it.
104 protected PrintWriter log
;
106 * Contains the TestResult instance for the interface test to collect
107 * information about methods test.
109 protected TestResult tRes
;
111 * Contains names of the methods have been alreadycalled
113 private Vector methCalled
= new Vector(10);
116 * Disposes the test environment, which was corrupted by the test.
118 * @param tEnv the environment to dispose
120 public void disposeEnvironment(TestEnvironment tEnv
)
122 disposeEnvironment();
126 * Disposes the current test environment, which was corrupted by the test.
128 * @see #disposeEnvironment(TestEnvironment)
130 public void disposeEnvironment()
133 TestCase tCase
= tEnv
.getTestCase();
134 tCase
.disposeTestEnvironment(tEnv
, tParam
);
138 * Runs the interface test: its method tests. First, it initializes some
139 * of MultiMethodTest fields, like tRes, log, tEnv, etc. Then, it queries
140 * the tested interface and initializes 'oObj' field (defined in a
141 * subclass). Before calling method tests, before() method calles to allow
142 * initialization of s stuff before testing. Then, the method tests are
143 * called. After them, after() method is called, to allow cleaning up the
144 * stuff initialized in before() and test methods.
146 * @param entry the interface test state
147 * @param tEnv the environment to test
148 * @param tParam the parameters of the test
153 public TestResult
run(DescEntry entry
, TestEnvironment tEnv
, TestParameters tParam
)
156 log
= (PrintWriter
) entry
.Logger
;
159 this.tParam
= tParam
;
162 this.tRes
= new TestResult();
165 // Some fake code for a self test.
166 // For normal test we must not be a "ifc.qadevooo._SelfTest"
167 if (! entry
.entryName
.equals("ifc.qadevooo._SelfTest"))
169 String ifcName
= getInterfaceName();
170 System
.out
.println("checking : " + ifcName
);
172 // defining a name of the class corresponding to the tested interface
174 String testedClassName
;
176 testedClassName
= getTestedClassName();
178 if (entry
.EntryType
.equals("service"))
180 testedClassName
= "com.sun.star.beans.XPropertySet";
185 testedClass
= Class
.forName(testedClassName
);
187 catch (ClassNotFoundException cnfE
)
190 cnfE
.printStackTrace(log
);
191 log
.println("could not find a class : " + getTestedClassName());
196 // quering the tested interface from the tested object
197 XInterface tCase
= tEnv
.getTestObject();
198 Object oObj
= UnoRuntime
.queryInterface(testedClass
, tEnv
.getTestObject());
202 if (entry
.isOptional
)
204 Summarizer
.summarizeDown(entry
, "Not supported but optional.OK");
208 Summarizer
.summarizeDown(entry
, "queryInterface returned null.FAILED");
209 entry
.ErrorMsg
= "queryInterface returned null";
210 entry
.hasErrorMsg
= true;
216 //setting the field oObj
217 setField("oObj", oObj
);
220 // to perform some stuff before all method tests
227 setSubStates(e
.toString());
231 // executing methods tests
232 for (int i
= 0; i
< entry
.SubEntryCount
; i
++)
234 DescEntry aSubEntry
= entry
.SubEntries
[i
];
237 executeMethod(aSubEntry
.entryName
);
241 log
.println("Exception while checking: " + aSubEntry
.entryName
+ " : " + e
.getMessage());
245 // to perform some stuff after all method tests
258 * Is called before calling method tests, but after initialization.
259 * Subclasses may override to perform actions before method tests.
261 protected void before() throws Exception
266 * Is called after calling method tests. Subclasses may override
267 * to perform actions after method tests.
269 protected void after()
274 * @return the name of the interface or the service tested.
276 protected String
getTestedClassName()
278 String clsName
= this.getClass().getName();
280 int firstDot
= clsName
.indexOf(".");
281 int lastDot
= clsName
.lastIndexOf(".");
283 String append
= "com.sun.star.";
285 if (entry
.longName
.indexOf("::drafts::com::") > -1)
287 append
= "drafts.com.sun.star.";
290 return append
+ clsName
.substring(firstDot
+ 1, lastDot
+ 1) + clsName
.substring(lastDot
+ 2);
294 * Sets a method status.
296 * @param methName the method name to set status
297 * @param methStatus the status to set to the method
299 protected void setStatus(String methName
, Status methStatus
)
301 tRes
.tested(methName
, methStatus
);
307 protected void setSubStates(String msg
)
309 for (int k
= 0; k
< entry
.SubEntryCount
; k
++)
311 entry
.SubEntries
[k
].hasErrorMsg
= true;
312 entry
.SubEntries
[k
].ErrorMsg
= msg
;
313 if (entry
.SubEntries
[k
].State
.equals("UNKNOWN"))
315 entry
.SubEntries
[k
].State
= msg
;
322 * Checks if the <code>method</code> is optional in the service.
324 protected boolean isOptional(String method
)
326 for (int k
= 0; k
< entry
.SubEntryCount
; k
++)
328 if (entry
.SubEntries
[k
].entryName
.equals(method
))
330 return entry
.SubEntries
[k
].isOptional
;
337 * Checks if the <code>method</code> test has been already called.
339 protected boolean isCalled(String method
)
341 return methCalled
.contains(method
);
345 * Calling of the method indicates that the <code>method</code> test should
346 * be called. The method checks this and if it is not called, calls it.
347 * If the method is failed or skipped, it throws StatusException.
349 protected void requiredMethod(String method
)
351 log
.println("starting required method: " + method
);
352 executeMethod(method
);
353 Status mtStatus
= tRes
.getStatusFor(method
);
355 if (mtStatus
!= null && (!mtStatus
.isPassed() || mtStatus
.isFailed()))
357 log
.println("! Required method " + method
+ " failed");
358 throw new StatusException(mtStatus
);
363 * Checks if the <code>method</code> was called, and if not, call it.
364 * On contrary to requiredMethod(), he method doesn't check its status.
366 protected void executeMethod(String method
)
368 if (!isCalled(method
))
370 log
.println("Execute: " + method
);
372 log
.println(method
+ ": " + tRes
.getStatusFor(method
));
377 * Just calls the <code>method</code> test.
379 protected void callMethod(String method
)
381 methCalled
.add(method
);
382 invokeTestMethod(getMethodFor(method
), method
);
386 * Invokes a test method of the subclass using reflection API. Handles
387 * the method results and sets its status.
389 * @param meth the subclass' method to invoke
390 * @param methName the name of the method
392 protected void invokeTestMethod(Method meth
, String methName
)
396 setStatus(methName
, Status
.skipped(false));
404 meth
.invoke(this, new Object
[0]);
407 catch (InvocationTargetException itE
)
409 Throwable t
= itE
.getTargetException();
411 if (t
instanceof StatusException
)
413 stat
= ((StatusException
) t
).getStatus();
417 t
.printStackTrace(log
);
418 stat
= Status
.exception(t
);
421 catch (IllegalAccessException iaE
)
423 iaE
.printStackTrace(log
);
424 stat
= Status
.exception(iaE
);
426 catch (IllegalArgumentException iaE
)
428 iaE
.printStackTrace(log
);
429 stat
= Status
.exception(iaE
);
431 catch (ClassCastException ccE
)
433 ccE
.printStackTrace(log
);
434 stat
= Status
.exception(ccE
);
437 setStatus(methName
, stat
);
442 * Finds a testing method for the <code>method</code> of the interface.
444 * @return the testing method, if found, <tt>null</tt> otherwise
446 protected Method
getMethodFor(String method
)
448 String mName
= "_" + method
;
450 if (mName
.endsWith("()"))
452 mName
= mName
.substring(0, mName
.length() - 2);
455 final Class
[] paramTypes
= new Class
[0];
459 return this.getClass().getDeclaredMethod(mName
, paramTypes
);
461 catch (NoSuchMethodException nsmE
)
468 * @return the name of the interface tested
470 public String
getInterfaceName()
472 String clName
= this.getClass().getName();
473 return clName
.substring(clName
.lastIndexOf('.') + 1);
477 * Initializes <code>fieldName</code> of the subclass with
478 * <code>value</code>.
480 * @return Status describing the result of the operation.
482 protected Status
setField(String fieldName
, Object value
)
488 objField
= this.getClass().getField(fieldName
);
490 catch (NoSuchFieldException nsfE
)
492 return Status
.exception(nsfE
);
497 objField
.set(this, value
);
498 return Status
.passed(true);
500 catch (IllegalArgumentException iaE
)
502 return Status
.exception(iaE
);
504 catch (IllegalAccessException iaE
)
506 return Status
.exception(iaE
);