1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
29 import java
.io
.PrintWriter
;
30 import java
.lang
.reflect
.Field
;
31 import java
.lang
.reflect
.InvocationTargetException
;
32 import java
.lang
.reflect
.Method
;
33 import java
.util
.Vector
;
35 import com
.sun
.star
.uno
.UnoRuntime
;
36 import com
.sun
.star
.uno
.XInterface
;
38 import share
.DescEntry
;
39 import lib
.TestParameters
;
40 import stats
.Summarizer
;
43 * The class supports method based interface tests development.
45 * <p>There are some points that should be fulfilled in a subclass to work
46 * correctly in the multi-method framework:
48 * 1. each subclass schould define a public field named oObj of type tested
49 * by the subclass, e.g. 'public XText oObj;'. That field will be initialized
50 * by the MultiMethodTest code with the instance of the interface to test.
51 * In a case of service testing the field type should be XPropertySet.
53 * 2. for the test of each method of the tested interface(or a property in the
54 * case of service testing) should be method with the following signature
55 * provided: 'public void _<method name>()', e.g. 'public void _getText()'.
56 * The methods will be called by MultiMethodText code using reflection API
57 * for each method in the interface description.
59 * 3. to set status for a call 'tRes.tested(String method,
60 * boolean result)' should be used. For example 'tRes.tested("getText()",
61 * true)'. Also 'tRes.assert(String assertion, boolean result)' call can
62 * be used. Note, that one can call the methods not neccesarily from the
63 * test for the tested method, but from other method tests too (in the
64 * MultiMethodTest subclass). See also TestResult and MultiMethodTest.tRes
67 * 4. the before() and after() methods can be overriden to perform some
68 * actions, accordingly, before and after calling the test methods.
70 * 5. besides tRes, there are some fields initialized in the MultiMethodTest,
71 * that can be used for implementing tests:
73 * - tEnv contains the environment tested
74 * - tParam contains parameters of the test
75 * - log a writer to log information about the test
79 public class MultiMethodTest
83 * Contains the TestEnvironment being tested, to allow for tests to access
86 protected TestEnvironment tEnv
;
88 * Contains the TestParameters for the tests, to allow for tests to access
91 protected TestParameters tParam
;
93 * Contains the Description for the test
96 protected DescEntry entry
;
98 * Contains a writer to log an information about the interface testing, to
99 * allows for tests to access it.
101 protected PrintWriter log
;
103 * Contains the TestResult instance for the interface test to collect
104 * information about methods test.
106 protected TestResult tRes
;
108 * Contains names of the methods have been alreadycalled
110 private Vector methCalled
= new Vector(10);
113 * Disposes the test environment, which was corrupted by the test.
115 * @param tEnv the environment to dispose
117 public void disposeEnvironment(TestEnvironment tEnv
)
119 disposeEnvironment();
123 * Disposes the current test environment, which was corrupted by the test.
125 * @see #disposeEnvironment(TestEnvironment)
127 public void disposeEnvironment()
130 TestCase tCase
= tEnv
.getTestCase();
131 tCase
.disposeTestEnvironment(tEnv
, tParam
);
135 * Runs the interface test: its method tests. First, it initializes some
136 * of MultiMethodTest fields, like tRes, log, tEnv, etc. Then, it queries
137 * the tested interface and initializes 'oObj' field (defined in a
138 * subclass). Before calling method tests, before() method calles to allow
139 * initialization of s stuff before testing. Then, the method tests are
140 * called. After them, after() method is called, to allow cleaning up the
141 * stuff initialized in before() and test methods.
143 * @param entry the interface test state
144 * @param tEnv the environment to test
145 * @param tParam the parameters of the test
150 public TestResult
run(DescEntry entry
, TestEnvironment tEnv
, TestParameters tParam
)
153 log
= (PrintWriter
) entry
.Logger
;
156 this.tParam
= tParam
;
159 this.tRes
= new TestResult();
162 // Some fake code for a self test.
163 // For normal test we must not be a "ifc.qadevooo._SelfTest"
164 if (! ("ifc.qadevooo._SelfTest").equals(entry
.entryName
))
166 String ifcName
= getInterfaceName();
167 // System.out.println("checking : " + ifcName);
168 System
.out
.print("checking: [" + entry
.longName
+ "]");
170 // defining a name of the class corresponding to the tested interface
172 String testedClassName
;
174 testedClassName
= getTestedClassName();
176 if (entry
.EntryType
.equals("service"))
178 testedClassName
= "com.sun.star.beans.XPropertySet";
183 testedClass
= Class
.forName(testedClassName
);
185 catch (ClassNotFoundException cnfE
)
187 System
.out
.println();
188 cnfE
.printStackTrace(log
);
189 log
.println("could not find a class : " + getTestedClassName());
192 System
.out
.println(" is iface: [" + testedClassName
+ "] testcode: [" + entry
.entryName
+ "]");
194 // quering the tested interface from the tested object
195 XInterface tCase
= tEnv
.getTestObject();
196 Object oObj
= UnoRuntime
.queryInterface(testedClass
, tEnv
.getTestObject());
200 if (entry
.isOptional
)
202 Summarizer
.summarizeDown(entry
, "Not supported but optional.OK");
206 Summarizer
.summarizeDown(entry
, "queryInterface returned null.FAILED");
207 entry
.ErrorMsg
= "queryInterface returned null";
208 entry
.hasErrorMsg
= true;
214 //setting the field oObj
215 setField("oObj", oObj
);
218 // to perform some stuff before all method tests
226 setSubStates(e
.toString());
230 // executing methods tests
231 for (int i
= 0; i
< entry
.SubEntryCount
; i
++)
233 DescEntry aSubEntry
= entry
.SubEntries
[i
];
236 final String sEntryName
= aSubEntry
.entryName
;
237 executeMethod(sEntryName
);
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()
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 final String sName
= entry
.SubEntries
[k
].entryName
;
329 if (sName
.equals(_method
))
331 final boolean bIsOptional
= entry
.SubEntries
[k
].isOptional
;
339 * Checks if the <code>method</code> test has been already called.
341 protected boolean isCalled(String method
)
343 return methCalled
.contains(method
);
347 * Calling of the method indicates that the <code>method</code> test should
348 * be called. The method checks this and if it is not called, calls it.
349 * If the method is failed or skipped, it throws StatusException.
351 protected void requiredMethod(String method
)
353 log
.println("starting required method: " + method
);
354 executeMethod(method
);
355 Status mtStatus
= tRes
.getStatusFor(method
);
357 if (mtStatus
!= null && (!mtStatus
.isPassed() || mtStatus
.isFailed()))
359 log
.println("! Required method " + method
+ " failed");
360 throw new StatusException(mtStatus
);
365 * Checks if the <code>method</code> was called, and if not, call it.
366 * On contrary to requiredMethod(), he method doesn't check its status.
368 protected void executeMethod(String method
)
370 if (!isCalled(method
))
372 log
.println("Execute: " + method
);
374 log
.println(method
+ ": " + tRes
.getStatusFor(method
));
380 * Just calls the <code>method</code> test.
382 protected void callMethod(String method
)
384 methCalled
.add(method
);
385 invokeTestMethod(getMethodFor(method
), method
);
389 * Invokes a test method of the subclass using reflection API. Handles
390 * the method results and sets its status.
392 * @param meth the subclass' method to invoke
393 * @param methName the name of the method
395 protected void invokeTestMethod(Method meth
, String methName
)
399 setStatus(methName
, Status
.skipped(false));
407 meth
.invoke(this, new Object
[0]);
410 catch (InvocationTargetException itE
)
412 Throwable t
= itE
.getTargetException();
414 if (t
instanceof StatusException
)
416 stat
= ((StatusException
) t
).getStatus();
420 t
.printStackTrace(log
);
421 stat
= Status
.exception(t
);
424 catch (IllegalAccessException iaE
)
426 iaE
.printStackTrace(log
);
427 stat
= Status
.exception(iaE
);
429 catch (IllegalArgumentException iaE
)
431 iaE
.printStackTrace(log
);
432 stat
= Status
.exception(iaE
);
434 catch (ClassCastException ccE
)
436 ccE
.printStackTrace(log
);
437 stat
= Status
.exception(ccE
);
440 setStatus(methName
, stat
);
445 * Finds a testing method for the <code>method</code> of the interface.
447 * @return the testing method, if found, <tt>null</tt> otherwise
449 protected Method
getMethodFor(String method
)
451 String mName
= "_" + method
;
453 if (mName
.endsWith("()"))
455 mName
= mName
.substring(0, mName
.length() - 2);
458 final Class
[] paramTypes
= new Class
[0];
462 return this.getClass().getDeclaredMethod(mName
, paramTypes
);
464 catch (NoSuchMethodException nsmE
)
471 * @return the name of the interface tested
473 public String
getInterfaceName()
475 String clName
= this.getClass().getName();
476 return clName
.substring(clName
.lastIndexOf('.') + 1);
480 * Initializes <code>fieldName</code> of the subclass with
481 * <code>value</code>.
483 * @return Status describing the result of the operation.
485 protected Status
setField(String fieldName
, Object value
)
491 objField
= this.getClass().getField(fieldName
);
493 catch (NoSuchFieldException nsfE
)
495 return Status
.exception(nsfE
);
500 objField
.set(this, value
);
501 return Status
.passed(true);
503 catch (IllegalArgumentException iaE
)
505 return Status
.exception(iaE
);
507 catch (IllegalAccessException iaE
)
509 return Status
.exception(iaE
);