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 .
21 import java
.io
.PrintWriter
;
24 import lib
.StatusException
;
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
;
47 * Test for object that implements the following interfaces :
49 * <li><code>com::sun::star::task::XJobExecutor</code></li>
51 * The following files are needed for testcase creation :
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
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();
76 Object oRootCfg
= null;
79 * For testcase initializing :
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>
89 * First these two conditions are checked. If job and event are not
90 * registered they are inserted into configuration and commited.
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 protected void initialize(TestParameters Param
, PrintWriter log
) {
103 boolean serviceRegistered
= false;
104 boolean configured
= false;
107 Object obj
= ((XMultiServiceFactory
)Param
.getMSF()).createInstance("test.Job");
108 serviceRegistered
= obj
!= null;
109 } catch(com
.sun
.star
.uno
.Exception e
) {}
111 log
.println("Service test.Job is "
112 + (serviceRegistered ?
"already" : "not yet") + " registered.");
113 if (! serviceRegistered
){
114 String message
= "You have to register 'test.Job' before office is stared.\n";
115 message
+= "Please run '$OFFICEPATH/program/pkgchk $DOCPTH/qadevlibs/JobExecutor.jar'";
116 throw new StatusException(message
, new Exception());
120 XNameAccess jobs
= null;
121 XNameAccess events
= null;
123 Object obj
= ((XMultiServiceFactory
)Param
.getMSF()).createInstance
124 ("com.sun.star.configuration.ConfigurationProvider");
125 XMultiServiceFactory xConfigMSF
= UnoRuntime
.queryInterface(XMultiServiceFactory
.class, obj
);
126 PropertyValue
[] args
= new PropertyValue
[1];
127 args
[0] = new PropertyValue();
128 args
[0].Name
= "nodepath";
129 args
[0].Value
= "org.openoffice.Office.Jobs";
130 oRootCfg
= xConfigMSF
.createInstanceWithArguments(
131 "com.sun.star.configuration.ConfigurationUpdateAccess", args
);
132 XHierarchicalNameAccess xHNA
= UnoRuntime
.queryInterface(XHierarchicalNameAccess
.class, oRootCfg
);
133 obj
= xHNA
.getByHierarchicalName("Jobs");
134 jobs
= UnoRuntime
.queryInterface
135 (XNameAccess
.class, obj
);
136 obj
= xHNA
.getByHierarchicalName("Events");
137 events
= UnoRuntime
.queryInterface
138 (XNameAccess
.class, obj
);
139 } catch (Exception e
) {
140 throw new StatusException("Couldn't get configuration", e
);
143 configured
= jobs
.hasByName("TestJob") && events
.hasByName("TestEvent");
145 log
.println("Test job and event is "
146 + (configured ?
"already" : "not yet") + " configured.");
150 log
.println("Adding configuration to Jobs ...");
151 XSingleServiceFactory jobsFac
= UnoRuntime
.queryInterface(XSingleServiceFactory
.class, jobs
);
152 Object oNewJob
= jobsFac
.createInstance();
153 XNameReplace xNewJobNR
= UnoRuntime
.queryInterface(XNameReplace
.class, oNewJob
);
154 xNewJobNR
.replaceByName("Service", "test.Job");
155 XNameContainer xJobsNC
= UnoRuntime
.queryInterface(XNameContainer
.class, jobs
);
156 xJobsNC
.insertByName("TestJob", oNewJob
);
158 log
.println("Adding configuration to Events ...");
159 XSingleServiceFactory eventsFac
= UnoRuntime
.queryInterface(XSingleServiceFactory
.class, events
);
160 Object oNewEvent
= eventsFac
.createInstance();
162 XNameAccess xNewEventNA
= UnoRuntime
.queryInterface(XNameAccess
.class, oNewEvent
);
163 Object oJobList
= xNewEventNA
.getByName("JobList");
164 XSingleServiceFactory jobListFac
= (XSingleServiceFactory
)
165 AnyConverter
.toObject(new Type(XSingleServiceFactory
.class),
167 XNameContainer jobListNC
= (XNameContainer
)
168 AnyConverter
.toObject(new Type(XNameContainer
.class),
170 log
.println("\tAdding TimeStamps to Events ...");
171 Object oNewJobTimeStamps
= jobListFac
.createInstance();
173 jobListNC
.insertByName("TestJob", oNewJobTimeStamps
);
176 XNameContainer xEventsNC
= UnoRuntime
.queryInterface(XNameContainer
.class, events
);
177 xEventsNC
.insertByName("TestEvent", oNewEvent
);
179 XChangesBatch xCB
= UnoRuntime
.queryInterface(XChangesBatch
.class, oRootCfg
);
184 } catch (InterruptedException ex
) {}
186 } catch (com
.sun
.star
.uno
.Exception e
) {
187 e
.printStackTrace(log
);
188 throw new StatusException("Couldn't change config", e
);
195 * Creating a Testenvironment for the interfaces to be tested.
197 * Service <code>com.sun.star.comp.framework.JobExecutor</code>
200 protected TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
202 XInterface oObj
= null;
206 oObj
= (XInterface
)((XMultiServiceFactory
)Param
.getMSF()).createInstance(
207 "com.sun.star.comp.framework.JobExecutor");
208 job
= ((XMultiServiceFactory
)Param
.getMSF()).createInstance("test.Job");
209 } catch(com
.sun
.star
.uno
.Exception e
) {
210 e
.printStackTrace(log
);
211 throw new StatusException(
212 Status
.failed("Couldn't create instance"));
215 xNamed
= UnoRuntime
.queryInterface(XNamed
.class, job
);
216 log
.println("Count = " + xNamed
.getName());
218 TestEnvironment tEnv
= new TestEnvironment( oObj
);
220 tEnv
.addObjRelation("CallCounter", xNamed
);
223 } // finish method getTestEnvironment
225 protected void cleanup( TestParameters Param
, PrintWriter log
) {
230 * Currently not used.
233 XServiceInfo
, XSingleServiceFactory
{
235 private static class Impl
implements XServiceInfo
, XTypeProvider
, XJob
, XNamed
{
238 public byte[] getImplementationId() {
239 return toString().getBytes();
242 public Type
[] getTypes() {
243 Class
<?
> interfaces
[] = getClass().getInterfaces();
244 Type types
[] = new Type
[interfaces
.length
];
245 for(int i
= 0; i
< interfaces
.length
; ++ i
)
246 types
[i
] = new Type(interfaces
[i
]);
250 public Object
execute(NamedValue
[] param
) {
256 public String
getName() {
257 return String
.valueOf(callCount
);
260 public void setName(String n
) {}
262 public boolean supportsService(String name
) {
263 return __serviceName
.equals(name
);
266 public String
[] getSupportedServiceNames() {
267 return new String
[] {__serviceName
};
270 public String
getImplementationName() {
271 return getClass().getName();
275 public static final String __serviceName
= "test.Job";
276 static Impl impl
= new Impl();
278 public Object
createInstanceWithArguments(Object
[] args
) {
282 public Object
createInstance() {
283 return createInstanceWithArguments(null);
286 public boolean supportsService(String name
) {
287 return __serviceName
.equals(name
);
290 public String
[] getSupportedServiceNames() {
291 return new String
[] {__serviceName
};
294 public String
getImplementationName() {
295 return getClass().getName();