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
;
23 import lib
.StatusException
;
25 import lib
.TestEnvironment
;
26 import lib
.TestParameters
;
27 import com
.sun
.star
.beans
.NamedValue
;
28 import com
.sun
.star
.beans
.PropertyValue
;
29 import com
.sun
.star
.container
.XHierarchicalNameAccess
;
30 import com
.sun
.star
.container
.XNameAccess
;
31 import com
.sun
.star
.container
.XNameContainer
;
32 import com
.sun
.star
.container
.XNameReplace
;
33 import com
.sun
.star
.container
.XNamed
;
34 import com
.sun
.star
.lang
.XMultiServiceFactory
;
35 import com
.sun
.star
.lang
.XServiceInfo
;
36 import com
.sun
.star
.lang
.XSingleServiceFactory
;
37 import com
.sun
.star
.lang
.XTypeProvider
;
38 import com
.sun
.star
.task
.XJob
;
39 import com
.sun
.star
.uno
.Type
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.uno
.XInterface
;
42 import com
.sun
.star
.util
.XChangesBatch
;
43 import com
.sun
.star
.uno
.AnyConverter
;
46 * Test for object that implements the following interfaces :
48 * <li><code>com::sun::star::task::XJobExecutor</code></li>
50 * The following files are needed for testcase creation :
52 * <li> <code>qadevlibs/JobExecutor.jar</code> :
53 * this is java component <code>test.Job</code> which
54 * should be registered before or during this testcase
55 * creation. This component must implement
56 * <code>com.sun.star.task.XJob</code> interface and count
57 * <code>execute</code> method calls. It also should
58 * implement <code>container.XNamed</code> interface and
59 * <code>getName</code> method must return string with number
63 * Also <b>important prerequicity</b>: if Job and Event is not yet
64 * added to configuration or <code>test.Job</code> component is
65 * not yet registered, the SOffice <b>must be destroyed</b> during
66 * testcase initialization. <p>
68 * @see com.sun.star.task.XJobExecutor
69 * @see ifc.task._XJobExecutor
71 public class JobExecutor
extends TestCase
{
73 static Job job
= new Job();
75 Object oRootCfg
= null;
78 * For testcase initializing :
80 * <li> Implementation <code>test.Job</code> must be registered in
81 * SOffice registry. </li>
82 * <li> Configuration in package <code>org.OpenOffice.Office.Jobs</code>
83 * must be updated. <code>TestJob</code> must be registered for
84 * service <code>test.Job</code> and event for this job named
85 * <code>TextEvent</code> must be registered. </li>
88 * First these two conditions are checked. If job and event are not
89 * registered they are inserted into configuration and committed.
90 * After what SOffice must be destroyed for proper initialization
91 * of <code>JobExecutor</code> after startup. <p>
93 * Then if the implementation was not registered before it is
94 * registered in soffice <code>applicat.rbd</code> file (registering
95 * the component in currently running Java environment has no effect
96 * for <code>JobExecutor</code> in some reasons). <p>
98 * Note: SOffice is started again while the next
99 * <code>(XMultiServiceFactory)SOLink.getMSF()</code> call.
102 protected void initialize(TestParameters Param
, PrintWriter log
) throws Exception
{
103 boolean configured
= false;
106 Param
.getMSF().createInstance("test.Job");
107 } catch(com
.sun
.star
.uno
.Exception e
) {
108 log
.println("Service test.Job is not yet registered.");
109 String message
= "You have to register 'test.Job' before office is started.\n";
110 message
+= "Please run '$OFFICEPATH/program/pkgchk $DOCPTH/qadevlibs/JobExecutor.jar'";
111 throw new StatusException(message
, e
);
116 XNameAccess jobs
= null;
117 XNameAccess events
= null;
118 Object obj
= Param
.getMSF().createInstance
119 ("com.sun.star.configuration.ConfigurationProvider");
120 XMultiServiceFactory xConfigMSF
= UnoRuntime
.queryInterface(XMultiServiceFactory
.class, obj
);
121 PropertyValue
[] args
= new PropertyValue
[1];
122 args
[0] = new PropertyValue();
123 args
[0].Name
= "nodepath";
124 args
[0].Value
= "org.openoffice.Office.Jobs";
125 oRootCfg
= xConfigMSF
.createInstanceWithArguments(
126 "com.sun.star.configuration.ConfigurationUpdateAccess", args
);
127 XHierarchicalNameAccess xHNA
= UnoRuntime
.queryInterface(XHierarchicalNameAccess
.class, oRootCfg
);
128 obj
= xHNA
.getByHierarchicalName("Jobs");
129 jobs
= UnoRuntime
.queryInterface
130 (XNameAccess
.class, obj
);
131 obj
= xHNA
.getByHierarchicalName("Events");
132 events
= UnoRuntime
.queryInterface
133 (XNameAccess
.class, obj
);
135 configured
= jobs
.hasByName("TestJob") && events
.hasByName("TestEvent");
137 log
.println("Test job and event is "
138 + (configured ?
"already" : "not yet") + " configured.");
141 log
.println("Adding configuration to Jobs ...");
142 XSingleServiceFactory jobsFac
= UnoRuntime
.queryInterface(XSingleServiceFactory
.class, jobs
);
143 Object oNewJob
= jobsFac
.createInstance();
144 XNameReplace xNewJobNR
= UnoRuntime
.queryInterface(XNameReplace
.class, oNewJob
);
145 xNewJobNR
.replaceByName("Service", "test.Job");
146 XNameContainer xJobsNC
= UnoRuntime
.queryInterface(XNameContainer
.class, jobs
);
147 xJobsNC
.insertByName("TestJob", oNewJob
);
149 log
.println("Adding configuration to Events ...");
150 XSingleServiceFactory eventsFac
= UnoRuntime
.queryInterface(XSingleServiceFactory
.class, events
);
151 Object oNewEvent
= eventsFac
.createInstance();
153 XNameAccess xNewEventNA
= UnoRuntime
.queryInterface(XNameAccess
.class, oNewEvent
);
154 Object oJobList
= xNewEventNA
.getByName("JobList");
155 XSingleServiceFactory jobListFac
= (XSingleServiceFactory
)
156 AnyConverter
.toObject(new Type(XSingleServiceFactory
.class),
158 XNameContainer jobListNC
= (XNameContainer
)
159 AnyConverter
.toObject(new Type(XNameContainer
.class),
161 log
.println("\tAdding TimeStamps to Events ...");
162 Object oNewJobTimeStamps
= jobListFac
.createInstance();
164 jobListNC
.insertByName("TestJob", oNewJobTimeStamps
);
166 XNameContainer xEventsNC
= UnoRuntime
.queryInterface(XNameContainer
.class, events
);
167 xEventsNC
.insertByName("TestEvent", oNewEvent
);
169 XChangesBatch xCB
= UnoRuntime
.queryInterface(XChangesBatch
.class, oRootCfg
);
172 util
.utils
.waitForEventIdle(Param
.getMSF());
178 * Creating a TestEnvironment for the interfaces to be tested.
180 * Service <code>com.sun.star.comp.framework.JobExecutor</code>
184 protected TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) throws Exception
{
186 XInterface oObj
= (XInterface
)Param
.getMSF().createInstance(
187 "com.sun.star.comp.framework.JobExecutor");
188 Object job
= Param
.getMSF().createInstance("test.Job");
190 xNamed
= UnoRuntime
.queryInterface(XNamed
.class, job
);
191 log
.println("Count = " + xNamed
.getName());
193 TestEnvironment tEnv
= new TestEnvironment( oObj
);
195 tEnv
.addObjRelation("CallCounter", xNamed
);
198 } // finish method getTestEnvironment
201 protected void cleanup( TestParameters Param
, PrintWriter log
) {
206 * Currently not used.
209 XServiceInfo
, XSingleServiceFactory
{
211 private static class Impl
implements XServiceInfo
, XTypeProvider
, XJob
, XNamed
{
214 public byte[] getImplementationId() {
218 public Type
[] getTypes() {
219 Class
<?
> interfaces
[] = getClass().getInterfaces();
220 Type types
[] = new Type
[interfaces
.length
];
221 for(int i
= 0; i
< interfaces
.length
; ++ i
)
222 types
[i
] = new Type(interfaces
[i
]);
226 public Object
execute(NamedValue
[] param
) {
232 public String
getName() {
233 return String
.valueOf(callCount
);
236 public void setName(String n
) {}
238 public boolean supportsService(String name
) {
239 return __serviceName
.equals(name
);
242 public String
[] getSupportedServiceNames() {
243 return new String
[] {__serviceName
};
246 public String
getImplementationName() {
247 return getClass().getName();
251 private static final String __serviceName
= "test.Job";
252 private static Impl impl
= new Impl();
254 public Object
createInstanceWithArguments(Object
[] args
) {
258 public Object
createInstance() {
259 return createInstanceWithArguments(null);
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();