Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _fwk / JobExecutor.java
blob7d518d924cbc7409f04717b8ec989bd8aecf01e4
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 .
19 package mod._fwk;
21 import java.io.PrintWriter;
23 import lib.Status;
24 import lib.StatusException;
25 import lib.TestCase;
26 import lib.TestEnvironment;
27 import lib.TestParameters;
28 import com.sun.star.beans.NamedValue;
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.container.XHierarchicalNameAccess;
31 import com.sun.star.container.XNameAccess;
32 import com.sun.star.container.XNameContainer;
33 import com.sun.star.container.XNameReplace;
34 import com.sun.star.container.XNamed;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.lang.XServiceInfo;
37 import com.sun.star.lang.XSingleServiceFactory;
38 import com.sun.star.lang.XTypeProvider;
39 import com.sun.star.task.XJob;
40 import com.sun.star.uno.Type;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 import com.sun.star.util.XChangesBatch;
44 import com.sun.star.uno.AnyConverter;
46 /**
47 * Test for object that implements the following interfaces :
48 * <ul>
49 * <li><code>com::sun::star::task::XJobExecutor</code></li>
50 * </ul><p>
51 * The following files are needed for testcase creation :
52 * <ul>
53 * <li> <code>qadevlibs/JobExecutor.jar</code> :
54 * this is java component <code>test.Job</code> which
55 * should be registered before or during this testcase
56 * creation. This component must implement
57 * <code>com.sun.star.task.XJob</code> interface and count
58 * <code>execute</code> method calls. It also should
59 * implement <code>container.XNamed</code> interface and
60 * <code>getName</code> method must return string with number
61 * of calls.</li>
62 * <ul> <p>
64 * Also <b>important prerequicity</b>: if Job and Event is not yet
65 * added to configuration or <code>test.Job</code> component is
66 * not yet registered, the SOffice <b>must be destroyed</b> during
67 * testcase initialization. <p>
69 * @see com.sun.star.task.XJobExecutor
70 * @see ifc.task._XJobExecutor
72 public class JobExecutor extends TestCase {
74 static Job job = new Job();
75 XNamed xNamed = null;
76 Object oRootCfg = null;
78 /**
79 * For testcase initializing :
80 * <ol>
81 * <li> Implementation <code>test.Job</code> must be registered in
82 * SOffice registry. </li>
83 * <li> Configuration in package <code>org.OpenOffice.Office.Jobs</code>
84 * must be updated. <code>TestJob</code> must be registered for
85 * service <code>test.Job</code> and event for this job named
86 * <code>TextEvent</code> must be registered. </li>
87 * </ol>. <p>
89 * First these two conditions are checked. If job and event are not
90 * registered they are inserted into configuration and committed.
91 * After what SOffice must be destroyed for proper initialization
92 * of <code>JobExecutor</code> after startup. <p>
94 * Then if the implementation was not registered before it is
95 * registered in soffice <code>applicat.rbd</code> file (registering
96 * the component in currently running Java environment has no effect
97 * for <code>JobExecutor</code> in some reasons). <p>
99 * Note: SOffice is started again while the next
100 * <code>(XMultiServiceFactory)SOLink.getMSF()</code> call.
102 @Override
103 protected void initialize(TestParameters Param, PrintWriter log) {
104 boolean serviceRegistered = false;
105 boolean configured = false;
107 try {
108 Object obj = Param.getMSF().createInstance("test.Job");
109 serviceRegistered = obj != null;
110 } catch(com.sun.star.uno.Exception e) {}
112 log.println("Service test.Job is "
113 + (serviceRegistered ? "already" : "not yet") + " registered.");
114 if (! serviceRegistered){
115 String message = "You have to register 'test.Job' before office is stared.\n";
116 message += "Please run '$OFFICEPATH/program/pkgchk $DOCPTH/qadevlibs/JobExecutor.jar'";
117 throw new StatusException(message, new Exception());
121 XNameAccess jobs = null;
122 XNameAccess events = null;
123 try {
124 Object obj = Param.getMSF().createInstance
125 ("com.sun.star.configuration.ConfigurationProvider");
126 XMultiServiceFactory xConfigMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, obj);
127 PropertyValue[] args = new PropertyValue[1];
128 args[0] = new PropertyValue();
129 args[0].Name = "nodepath";
130 args[0].Value = "org.openoffice.Office.Jobs";
131 oRootCfg = xConfigMSF.createInstanceWithArguments(
132 "com.sun.star.configuration.ConfigurationUpdateAccess", args);
133 XHierarchicalNameAccess xHNA = UnoRuntime.queryInterface(XHierarchicalNameAccess.class, oRootCfg);
134 obj = xHNA.getByHierarchicalName("Jobs");
135 jobs = UnoRuntime.queryInterface
136 (XNameAccess.class, obj);
137 obj = xHNA.getByHierarchicalName("Events");
138 events = UnoRuntime.queryInterface
139 (XNameAccess.class, obj);
140 } catch (Exception e) {
141 throw new StatusException("Couldn't get configuration", e);
144 configured = jobs.hasByName("TestJob") && events.hasByName("TestEvent");
146 log.println("Test job and event is "
147 + (configured ? "already" : "not yet") + " configured.");
149 if (!configured) {
150 try {
151 log.println("Adding configuration to Jobs ...");
152 XSingleServiceFactory jobsFac = UnoRuntime.queryInterface(XSingleServiceFactory.class, jobs);
153 Object oNewJob = jobsFac.createInstance();
154 XNameReplace xNewJobNR = UnoRuntime.queryInterface(XNameReplace.class, oNewJob);
155 xNewJobNR.replaceByName("Service", "test.Job");
156 XNameContainer xJobsNC = UnoRuntime.queryInterface(XNameContainer.class, jobs);
157 xJobsNC.insertByName("TestJob", oNewJob);
159 log.println("Adding configuration to Events ...");
160 XSingleServiceFactory eventsFac = UnoRuntime.queryInterface(XSingleServiceFactory.class, events);
161 Object oNewEvent = eventsFac.createInstance();
163 XNameAccess xNewEventNA = UnoRuntime.queryInterface(XNameAccess.class, oNewEvent);
164 Object oJobList = xNewEventNA.getByName("JobList");
165 XSingleServiceFactory jobListFac = (XSingleServiceFactory)
166 AnyConverter.toObject(new Type(XSingleServiceFactory.class),
167 oJobList);
168 XNameContainer jobListNC = (XNameContainer)
169 AnyConverter.toObject(new Type(XNameContainer.class),
170 oJobList);
171 log.println("\tAdding TimeStamps to Events ...");
172 Object oNewJobTimeStamps = jobListFac.createInstance();
174 jobListNC.insertByName("TestJob", oNewJobTimeStamps);
177 XNameContainer xEventsNC = UnoRuntime.queryInterface(XNameContainer.class, events);
178 xEventsNC.insertByName("TestEvent", oNewEvent);
180 XChangesBatch xCB = UnoRuntime.queryInterface(XChangesBatch.class, oRootCfg);
181 xCB.commitChanges();
183 util.utils.pause(1000);
185 } catch (com.sun.star.uno.Exception e) {
186 e.printStackTrace(log);
187 throw new StatusException("Couldn't change config", e);
194 * Creating a Testenvironment for the interfaces to be tested.
196 * Service <code>com.sun.star.comp.framework.JobExecutor</code>
197 * is created.
199 @Override
200 protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
202 XInterface oObj = null;
204 Object job = null;
205 try {
206 oObj = (XInterface)Param.getMSF().createInstance(
207 "com.sun.star.comp.framework.JobExecutor");
208 job = Param.getMSF().createInstance("test.Job");
209 } catch(com.sun.star.uno.Exception e) {
210 throw new StatusException(e, Status.failed("Couldn't create instance"));
213 xNamed = UnoRuntime.queryInterface(XNamed.class, job);
214 log.println("Count = " + xNamed.getName());
216 TestEnvironment tEnv = new TestEnvironment( oObj );
218 tEnv.addObjRelation("CallCounter", xNamed);
220 return tEnv;
221 } // finish method getTestEnvironment
223 @Override
224 protected void cleanup( TestParameters Param, PrintWriter log) {
229 * Currently not used.
231 class Job implements
232 XServiceInfo, XSingleServiceFactory {
234 private static class Impl implements XServiceInfo, XTypeProvider, XJob, XNamed {
235 int callCount = 0;
237 public byte[] getImplementationId() {
238 return new byte[0];
241 public Type[] getTypes() {
242 Class<?> interfaces[] = getClass().getInterfaces();
243 Type types[] = new Type[interfaces.length];
244 for(int i = 0; i < interfaces.length; ++ i)
245 types[i] = new Type(interfaces[i]);
246 return types;
249 public Object execute(NamedValue[] param) {
250 callCount++;
252 return null;
255 public String getName() {
256 return String.valueOf(callCount);
259 public void setName(String n) {}
261 public boolean supportsService(String name) {
262 return __serviceName.equals(name);
265 public String[] getSupportedServiceNames() {
266 return new String[] {__serviceName};
269 public String getImplementationName() {
270 return getClass().getName();
274 private static final String __serviceName = "test.Job";
275 private static Impl impl = new Impl();
277 public Object createInstanceWithArguments(Object[] args) {
278 return impl;
281 public Object createInstance() {
282 return createInstanceWithArguments(null);
285 public boolean supportsService(String name) {
286 return __serviceName.equals(name);
289 public String[] getSupportedServiceNames() {
290 return new String[] {__serviceName};
293 public String getImplementationName() {
294 return getClass().getName();