Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / qadevOOo / runner / lib / MultiMethodTest.java
blob75c2a163c8a9b37501aa92ac348ebc2cec85cb58
1 /*
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 .
18 package lib;
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;
30 /**
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
53 * documentation.
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
65 * @see TestResult
67 @SuppressWarnings("unused")
68 public class MultiMethodTest
71 /**
72 * Contains the TestEnvironment being tested, to allow for tests to access
73 * it.
75 protected TestEnvironment tEnv;
76 /**
77 * Contains the TestParameters for the tests, to allow for tests to access
78 * it.
80 protected TestParameters tParam;
81 /**
82 * Contains the Description for the test
83 * it.
85 protected DescEntry entry;
86 /**
87 * Contains a writer to log an information about the interface testing, to
88 * allows for tests to access it.
90 protected PrintWriter log;
91 /**
92 * Contains the TestResult instance for the interface test to collect
93 * information about methods test.
95 protected TestResult tRes;
96 /**
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()
108 tEnv.dispose();
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
126 * @see #before
127 * @see #after
129 public TestResult run(DescEntry entry, TestEnvironment tEnv, TestParameters tParam)
132 log = (PrintWriter) entry.Logger;
134 this.tEnv = tEnv;
135 this.tParam = tParam;
136 // this.log = log;
137 this.entry = entry;
138 this.tRes = new TestResult();
139 Class<?> testedClass;
141 getInterfaceName();
142 System.out.print("checking: [" + entry.longName + "]");
144 // defining a name of the class corresponding to the tested interface
145 // or service
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());
164 return null;
166 System.out.println(" is iface: [" + testedClassName + "] testcode: [" + entry.entryName + "]");
168 Object oObj = UnoRuntime.queryInterface(testedClass, tEnv.getTestObject());
170 if (oObj == null)
172 if (entry.isOptional)
174 Summarizer.summarizeDown(entry, "Not supported but optional.OK");
176 else
178 Summarizer.summarizeDown(entry, "queryInterface returned null.FAILED");
179 entry.ErrorMsg = "queryInterface returned null";
180 entry.hasErrorMsg = true;
183 return null;
186 //setting the field oObj
189 setField("oObj", oObj);
191 catch (Exception e)
193 e.printStackTrace();
194 setSubStates(e.toString());
195 return tRes;
198 // to perform some stuff before all method tests
201 before();
203 catch (Exception e)
205 e.printStackTrace();
206 setSubStates(e.toString());
207 return tRes;
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);
219 catch (Exception e)
221 log.println("Exception while checking: " + aSubEntry.entryName + " : " + e.getMessage());
225 // to perform some stuff after all method tests
228 after();
230 catch (Exception e)
234 return tRes;
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);
285 * sets the substates
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;
312 return bIsOptional;
315 return false;
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);
353 callMethod(method);
354 log.println(method + ": " + tRes.getStatusFor(method));
355 log.println();
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)
377 if (meth == null)
379 setStatus(methName, Status.skipped(false));
381 else
383 Status stat;
387 meth.invoke(this, new Object[0]);
388 return;
390 catch (InvocationTargetException itE)
392 Throwable t = itE.getTargetException();
394 if (t instanceof StatusException)
396 stat = ((StatusException) t).getStatus();
398 else
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)
446 return null;
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());