2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import java
.io
.PrintWriter
;
21 import java
.lang
.reflect
.InvocationTargetException
;
22 import java
.lang
.reflect
.Method
;
23 import java
.util
.ArrayList
;
25 import share
.DescEntry
;
26 import stats
.Summarizer
;
28 import com
.sun
.star
.uno
.UnoRuntime
;
31 * The class supports method based interface tests development.
33 * <p>There are some points that should be fulfilled in a subclass to work
34 * correctly in the multi-method framework:
36 * 1. each subclass should define a public field named oObj of type tested
37 * by the subclass, e.g. 'public XText oObj;'. That field will be initialized
38 * by the MultiMethodTest code with the instance of the interface to test.
39 * In a case of service testing the field type should be XPropertySet.
41 * 2. for the test of each method of the tested interface(or a property in the
42 * case of service testing) should be method with the following signature
43 * provided: 'public void _<method name>()', e.g. 'public void _getText()'.
44 * The methods will be called by MultiMethodText code using reflection API
45 * for each method in the interface description.
47 * 3. to set status for a call 'tRes.tested(String method,
48 * boolean result)' should be used. For example 'tRes.tested("getText()",
49 * true)'. Also 'tRes.assert(String assertion, boolean result)' call can
50 * be used. Note, that one can call the methods not necessarily from the
51 * test for the tested method, but from other method tests too (in the
52 * MultiMethodTest subclass). See also TestResult and MultiMethodTest.tRes
55 * 4. the before() and after() methods can be overridden to perform some
56 * actions, accordingly, before and after calling the test methods.
58 * 5. besides tRes, there are some fields initialized in the MultiMethodTest,
59 * that can be used for implementing tests:
61 * - tEnv contains the environment tested
62 * - tParam contains parameters of the test
63 * - log a writer to log information about the test
67 @SuppressWarnings("unused")
68 public class MultiMethodTest
72 * Contains the TestEnvironment being tested, to allow for tests to access
75 protected TestEnvironment tEnv
;
77 * Contains the TestParameters for the tests, to allow for tests to access
80 protected TestParameters tParam
;
82 * Contains the Description for the test
85 protected DescEntry entry
;
87 * Contains a writer to log an information about the interface testing, to
88 * allows for tests to access it.
90 protected PrintWriter log
;
92 * Contains the TestResult instance for the interface test to collect
93 * information about methods test.
95 protected TestResult tRes
;
97 * Contains names of the methods have been already called
99 private final ArrayList
<String
> methCalled
= new ArrayList
<String
>(10);
102 * Disposes the current test environment, which was corrupted by the test.
104 * @see #disposeEnvironment(TestEnvironment)
106 public void disposeEnvironment()
109 TestCase tCase
= tEnv
.getTestCase();
110 tCase
.disposeTestEnvironment(tEnv
, tParam
);
114 * Runs the interface test: its method tests. First, it initializes some
115 * of MultiMethodTest fields, like tRes, log, tEnv, etc. Then, it queries
116 * the tested interface and initializes 'oObj' field (defined in a
117 * subclass). Before calling method tests, before() method calles to allow
118 * initialization of s stuff before testing. Then, the method tests are
119 * called. After them, after() method is called, to allow cleaning up the
120 * stuff initialized in before() and test methods.
122 * @param entry the interface test state
123 * @param tEnv the environment to test
124 * @param tParam the parameters of the test
129 public TestResult
run(DescEntry entry
, TestEnvironment tEnv
, TestParameters tParam
)
132 log
= (PrintWriter
) entry
.Logger
;
135 this.tParam
= tParam
;
138 this.tRes
= new TestResult();
139 Class
<?
> testedClass
;
142 System
.out
.print("checking: [" + entry
.longName
+ "]");
144 // defining a name of the class corresponding to the tested interface
146 String testedClassName
;
148 testedClassName
= getTestedClassName();
150 if (entry
.EntryType
.equals("service"))
152 testedClassName
= "com.sun.star.beans.XPropertySet";
157 testedClass
= Class
.forName(testedClassName
);
159 catch (ClassNotFoundException cnfE
)
161 System
.out
.println();
162 cnfE
.printStackTrace(log
);
163 log
.println("could not find a class : " + getTestedClassName());
166 System
.out
.println(" is iface: [" + testedClassName
+ "] testcode: [" + entry
.entryName
+ "]");
168 Object oObj
= UnoRuntime
.queryInterface(testedClass
, tEnv
.getTestObject());
172 if (entry
.isOptional
)
174 Summarizer
.summarizeDown(entry
, "Not supported but optional.OK");
178 Summarizer
.summarizeDown(entry
, "queryInterface returned null.FAILED");
179 entry
.ErrorMsg
= "queryInterface returned null";
180 entry
.hasErrorMsg
= true;
186 //setting the field oObj
189 setField("oObj", oObj
);
194 setSubStates(e
.toString());
198 // to perform some stuff before all method tests
206 setSubStates(e
.toString());
210 // executing methods tests
211 for (int i
= 0; i
< entry
.SubEntryCount
; i
++)
213 DescEntry aSubEntry
= entry
.SubEntries
[i
];
216 final String sEntryName
= aSubEntry
.entryName
;
217 executeMethod(sEntryName
);
221 log
.println("Exception while checking: " + aSubEntry
.entryName
+ " : " + e
.getMessage());
225 // to perform some stuff after all method tests
238 * Is called before calling method tests, but after initialization.
239 * Subclasses may override to perform actions before method tests.
241 protected void before() throws Exception
246 * Is called after calling method tests. Subclasses may override
247 * to perform actions after method tests.
249 protected void after()
254 * @return the name of the interface or the service tested.
256 protected String
getTestedClassName()
258 String clsName
= this.getClass().getName();
260 int firstDot
= clsName
.indexOf('.');
261 int lastDot
= clsName
.lastIndexOf('.');
263 String append
= "com.sun.star.";
265 if (entry
.longName
.indexOf("::drafts::com::") > -1)
267 append
= "drafts.com.sun.star.";
270 return append
+ clsName
.substring(firstDot
+ 1, lastDot
+ 1) + clsName
.substring(lastDot
+ 2);
274 * Sets a method status.
276 * @param methName the method name to set status
277 * @param methStatus the status to set to the method
279 private void setStatus(String methName
, Status methStatus
)
281 tRes
.tested(methName
, methStatus
);
287 private void setSubStates(String msg
)
289 for (int k
= 0; k
< entry
.SubEntryCount
; k
++)
291 entry
.SubEntries
[k
].hasErrorMsg
= true;
292 entry
.SubEntries
[k
].ErrorMsg
= msg
;
293 if (entry
.SubEntries
[k
].State
.equals("UNKNOWN"))
295 entry
.SubEntries
[k
].State
= msg
;
302 * Checks if the <code>method</code> is optional in the service.
304 protected boolean isOptional(String _method
)
306 for (int k
= 0; k
< entry
.SubEntryCount
; k
++)
308 final String sName
= entry
.SubEntries
[k
].entryName
;
309 if (sName
.equals(_method
))
311 final boolean bIsOptional
= entry
.SubEntries
[k
].isOptional
;
319 * Checks if the <code>method</code> test has been already called.
321 private boolean isCalled(String method
)
323 return methCalled
.contains(method
);
327 * Calling of the method indicates that the <code>method</code> test should
328 * be called. The method checks this and if it is not called, calls it.
329 * If the method is failed or skipped, it throws StatusException.
331 protected void requiredMethod(String method
)
333 log
.println("starting required method: " + method
);
334 executeMethod(method
);
335 Status mtStatus
= tRes
.getStatusFor(method
);
337 if (mtStatus
!= null && (!mtStatus
.isCompleted() || mtStatus
.isFailed()))
339 log
.println("! Required method " + method
+ " failed");
340 throw new StatusException(mtStatus
);
345 * Checks if the <code>method</code> was called, and if not, call it.
346 * On contrary to requiredMethod(), he method doesn't check its status.
348 protected void executeMethod(String method
)
350 if (!isCalled(method
))
352 log
.println("Execute: " + method
);
354 log
.println(method
+ ": " + tRes
.getStatusFor(method
));
360 * Just calls the <code>method</code> test.
362 private void callMethod(String method
)
364 methCalled
.add(method
);
365 invokeTestMethod(getMethodFor(method
), method
);
369 * Invokes a test method of the subclass using reflection API. Handles
370 * the method results and sets its status.
372 * @param meth the subclass' method to invoke
373 * @param methName the name of the method
375 protected void invokeTestMethod(Method meth
, String methName
)
379 setStatus(methName
, Status
.skipped(false));
387 meth
.invoke(this, new Object
[0]);
390 catch (InvocationTargetException itE
)
392 Throwable t
= itE
.getTargetException();
394 if (t
instanceof StatusException
)
396 stat
= ((StatusException
) t
).getStatus();
400 t
.printStackTrace(log
);
401 stat
= Status
.exception(t
);
404 catch (IllegalAccessException iaE
)
406 iaE
.printStackTrace(log
);
407 stat
= Status
.exception(iaE
);
409 catch (IllegalArgumentException iaE
)
411 iaE
.printStackTrace(log
);
412 stat
= Status
.exception(iaE
);
414 catch (ClassCastException ccE
)
416 ccE
.printStackTrace(log
);
417 stat
= Status
.exception(ccE
);
420 setStatus(methName
, stat
);
425 * Finds a testing method for the <code>method</code> of the interface.
427 * @return the testing method, if found, <tt>null</tt> otherwise
429 private Method
getMethodFor(String method
)
431 String mName
= "_" + method
;
433 if (mName
.endsWith("()"))
435 mName
= mName
.substring(0, mName
.length() - 2);
438 final Class
<?
>[] paramTypes
= new Class
[0];
442 return this.getClass().getDeclaredMethod(mName
, paramTypes
);
444 catch (NoSuchMethodException nsmE
)
451 * @return the name of the interface tested
453 public String
getInterfaceName()
455 String clName
= this.getClass().getName();
456 return clName
.substring(clName
.lastIndexOf('.') + 1);
459 private void setField(String fieldName
, Object value
)
460 throws NoSuchFieldException
, IllegalAccessException
462 this.getClass().getField(fieldName
).set(this, value
);
465 protected final void waitForEventIdle() {
466 util
.utils
.waitForEventIdle(tParam
.getMSF());