merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _fwk / JobExecutor.java
blob3ad443e55ce09616bff81302aa86a248dbc55382
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: JobExecutor.java,v $
10 * $Revision: 1.6 $
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 ************************************************************************/
31 package mod._fwk;
33 import java.io.PrintWriter;
35 import lib.Status;
36 import lib.StatusException;
37 import lib.TestCase;
38 import lib.TestEnvironment;
39 import lib.TestParameters;
40 import util.utils;
42 import com.sun.star.beans.NamedValue;
43 import com.sun.star.beans.PropertyValue;
44 import com.sun.star.container.XHierarchicalNameAccess;
45 import com.sun.star.container.XNameAccess;
46 import com.sun.star.container.XNameContainer;
47 import com.sun.star.container.XNameReplace;
48 import com.sun.star.container.XNamed;
49 import com.sun.star.lang.XMultiServiceFactory;
50 import com.sun.star.lang.XServiceInfo;
51 import com.sun.star.lang.XSingleServiceFactory;
52 import com.sun.star.lang.XTypeProvider;
53 import com.sun.star.task.XJob;
54 import com.sun.star.uno.Type;
55 import com.sun.star.uno.UnoRuntime;
56 import com.sun.star.uno.XInterface;
57 import com.sun.star.util.XChangesBatch;
58 import com.sun.star.uno.AnyConverter;
60 /**
61 * Test for object that implements the following interfaces :
62 * <ul>
63 * <li><code>com::sun::star::task::XJobExecutor</code></li>
64 * </ul><p>
65 * The following files are needed for testcase creation :
66 * <ul>
67 * <li> <code>qadevlibs/JobExecutor.jar</code> :
68 * this is java component <code>test.Job</code> which
69 * should be registered before or during this testcase
70 * creation. This component must implement
71 * <code>com.sun.star.task.XJob</code> interface and count
72 * <code>execute</code> method calls. It also should
73 * implement <code>container.XNamed</code> interface and
74 * <code>getName</code> method must return string with number
75 * of calls.</li>
76 * <ul> <p>
78 * Also <b>important prerequicity</b>: if Job and Event is not yet
79 * added to configuration or <code>test.Job</code> component is
80 * not yet registered, the SOffice <b>must be destroyed</b> during
81 * testcase initialization. <p>
83 * @see com.sun.star.task.XJobExecutor
84 * @see ifc.task._XJobExecutor
86 public class JobExecutor extends TestCase {
88 static Job job = new Job();
89 XNamed xNamed = null;
90 Object oRootCfg = null;
92 /**
93 * For testcase initializing :
94 * <ol>
95 * <li> Implementation <code>test.Job</code> must be registered in
96 * SOffice registry. </li>
97 * <li> Configuration in package <code>org.OpenOffice.Office.Jobs</code>
98 * must be updated. <code>TestJob</code> must be registered for
99 * service <code>test.Job</code> and event for this job named
100 * <code>TextEvent</code> must be registered. </li>
101 * </ol>. <p>
103 * First these two conditions are checked. If job and event are not
104 * registered they are inserted into configuration and commited.
105 * After what SOffice must be destroyed for proper initialization
106 * of <code>JobExecutor</code> after startup. <p>
108 * Then if the implementation was not registered before it is
109 * registered in soffice <code>applicat.rbd</code> file (registering
110 * the component in currently running Java environment has no effect
111 * for <code>JobExecutor</code> in some reasons). <p>
113 * Note: SOffice is started again while the next
114 * <code>(XMultiServiceFactory)SOLink.getMSF()</code> call.
116 protected void initialize(TestParameters Param, PrintWriter log) {
117 boolean serviceRegistered = false;
118 boolean configured = false;
120 try {
121 Object obj = ((XMultiServiceFactory)Param.getMSF()).createInstance("test.Job");
122 serviceRegistered = obj != null;
123 } catch(com.sun.star.uno.Exception e) {}
125 log.println("Service test.Job is "
126 + (serviceRegistered ? "already" : "not yet") + " registered.");
127 if (! serviceRegistered){
128 String message = "You have to register 'test.Job' before office is stared.\n";
129 message += "Please run '$OFFICEPATH/program/pkgchk $DOCPTH/qadevlibs/JobExecutor.jar'";
130 throw new StatusException(message, new Exception());
134 XNameAccess jobs = null;
135 XNameAccess events = null;
136 try {
137 Object obj = ((XMultiServiceFactory)Param.getMSF()).createInstance
138 ("com.sun.star.configuration.ConfigurationProvider");
139 XMultiServiceFactory xConfigMSF = (XMultiServiceFactory)
140 UnoRuntime.queryInterface(XMultiServiceFactory.class, obj);
141 PropertyValue[] args = new PropertyValue[1];
142 args[0] = new PropertyValue();
143 args[0].Name = "nodepath";
144 args[0].Value = "org.openoffice.Office.Jobs";
145 oRootCfg = xConfigMSF.createInstanceWithArguments(
146 "com.sun.star.configuration.ConfigurationUpdateAccess", args);
147 XHierarchicalNameAccess xHNA = (XHierarchicalNameAccess)
148 UnoRuntime.queryInterface(XHierarchicalNameAccess.class, oRootCfg);
149 obj = xHNA.getByHierarchicalName("Jobs");
150 jobs = (XNameAccess) UnoRuntime.queryInterface
151 (XNameAccess.class, obj);
152 obj = xHNA.getByHierarchicalName("Events");
153 events = (XNameAccess) UnoRuntime.queryInterface
154 (XNameAccess.class, obj);
155 } catch (Exception e) {
156 throw new StatusException("Couldn't get configuration", e);
159 configured = jobs.hasByName("TestJob") && events.hasByName("TestEvent");
161 log.println("Test job and event is "
162 + (configured ? "already" : "not yet") + " configured.");
164 if (!configured) {
165 try {
166 log.println("Adding configuration to Jobs ...");
167 XSingleServiceFactory jobsFac = (XSingleServiceFactory)
168 UnoRuntime.queryInterface(XSingleServiceFactory.class, jobs);
169 Object oNewJob = jobsFac.createInstance();
170 XNameReplace xNewJobNR = (XNameReplace)
171 UnoRuntime.queryInterface(XNameReplace.class, oNewJob);
172 xNewJobNR.replaceByName("Service", "test.Job");
173 XNameContainer xJobsNC = (XNameContainer)
174 UnoRuntime.queryInterface(XNameContainer.class, jobs);
175 xJobsNC.insertByName("TestJob", oNewJob);
177 log.println("Adding configuration to Events ...");
178 XSingleServiceFactory eventsFac = (XSingleServiceFactory)
179 UnoRuntime.queryInterface(XSingleServiceFactory.class, events);
180 Object oNewEvent = eventsFac.createInstance();
182 XNameAccess xNewEventNA = (XNameAccess)
183 UnoRuntime.queryInterface(XNameAccess.class, oNewEvent);
184 Object oJobList = xNewEventNA.getByName("JobList");
185 XSingleServiceFactory jobListFac = (XSingleServiceFactory)
186 AnyConverter.toObject(new Type(XSingleServiceFactory.class),
187 oJobList);
188 XNameContainer jobListNC = (XNameContainer)
189 AnyConverter.toObject(new Type(XNameContainer.class),
190 oJobList);
191 log.println("\tAdding TimeStamps to Events ...");
192 Object oNewJobTimeStamps = jobListFac.createInstance();
194 jobListNC.insertByName("TestJob", oNewJobTimeStamps);
197 XNameContainer xEventsNC = (XNameContainer)
198 UnoRuntime.queryInterface(XNameContainer.class, events);
199 xEventsNC.insertByName("TestEvent", oNewEvent);
201 XChangesBatch xCB = (XChangesBatch)
202 UnoRuntime.queryInterface(XChangesBatch.class, oRootCfg);
203 xCB.commitChanges();
205 try {
206 Thread.sleep(1000);
207 } catch (InterruptedException ex) {}
209 } catch (com.sun.star.uno.Exception e) {
210 e.printStackTrace(log);
211 throw new StatusException("Couldn't change config", e);
218 * Creating a Testenvironment for the interfaces to be tested.
220 * Service <code>com.sun.star.comp.framework.JobExecutor</code>
221 * is created.
223 protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
225 XInterface oObj = null;
227 Object job = null;
228 try {
229 oObj = (XInterface)((XMultiServiceFactory)Param.getMSF()).createInstance(
230 "com.sun.star.comp.framework.JobExecutor");
231 job = (XInterface)((XMultiServiceFactory)Param.getMSF()).createInstance("test.Job");
232 } catch(com.sun.star.uno.Exception e) {
233 e.printStackTrace(log);
234 throw new StatusException(
235 Status.failed("Couldn't create instance"));
238 xNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, job);
239 log.println("Count = " + xNamed.getName());
241 TestEnvironment tEnv = new TestEnvironment( oObj );
243 tEnv.addObjRelation("CallCounter", xNamed);
245 return tEnv;
246 } // finish method getTestEnvironment
248 protected void cleanup( TestParameters Param, PrintWriter log) {
253 * Currently not used.
255 class Job implements
256 XServiceInfo, XSingleServiceFactory {
258 private static class Impl implements XServiceInfo, XTypeProvider, XJob, XNamed {
259 int callCount = 0;
261 public byte[] getImplementationId() {
262 return toString().getBytes();
265 public Type[] getTypes() {
266 Class interfaces[] = getClass().getInterfaces();
267 Type types[] = new Type[interfaces.length];
268 for(int i = 0; i < interfaces.length; ++ i)
269 types[i] = new Type(interfaces[i]);
270 return types;
273 public Object execute(NamedValue[] param) {
274 callCount++;
276 return null;
279 public String getName() {
280 return String.valueOf(callCount);
283 public void setName(String n) {}
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();
298 public static final String __serviceName = "test.Job";
299 static Impl impl = new Impl();
301 public Object createInstanceWithArguments(Object[] args) {
302 return impl;
305 public Object createInstance() {
306 return createInstanceWithArguments(null);
309 public boolean supportsService(String name) {
310 return __serviceName.equals(name);
313 public String[] getSupportedServiceNames() {
314 return new String[] {__serviceName};
317 public String getImplementationName() {
318 return getClass().getName();