1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._fwk;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.Status;
29cdf0e10cSrcweir import lib.StatusException;
30cdf0e10cSrcweir import lib.TestCase;
31cdf0e10cSrcweir import lib.TestEnvironment;
32cdf0e10cSrcweir import lib.TestParameters;
33cdf0e10cSrcweir import util.utils;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.beans.NamedValue;
36cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
37cdf0e10cSrcweir import com.sun.star.container.XHierarchicalNameAccess;
38cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
39cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
40cdf0e10cSrcweir import com.sun.star.container.XNameReplace;
41cdf0e10cSrcweir import com.sun.star.container.XNamed;
42cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
43cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
44cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
45cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
46cdf0e10cSrcweir import com.sun.star.task.XJob;
47cdf0e10cSrcweir import com.sun.star.uno.Type;
48cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
49cdf0e10cSrcweir import com.sun.star.uno.XInterface;
50cdf0e10cSrcweir import com.sun.star.util.XChangesBatch;
51cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir /**
54cdf0e10cSrcweir  * Test for object that implements the following interfaces :
55cdf0e10cSrcweir  * <ul>
56cdf0e10cSrcweir  *  <li><code>com::sun::star::task::XJobExecutor</code></li>
57cdf0e10cSrcweir  * </ul><p>
58cdf0e10cSrcweir  * The following files are needed for testcase creation :
59cdf0e10cSrcweir  * <ul>
60cdf0e10cSrcweir  *  <li> <code>qadevlibs/JobExecutor.jar</code> :
61cdf0e10cSrcweir  *      this is java component <code>test.Job</code> which
62cdf0e10cSrcweir  *      should be registered before or during this testcase
63cdf0e10cSrcweir  *      creation. This component must implement
64cdf0e10cSrcweir  *      <code>com.sun.star.task.XJob</code> interface and count
65cdf0e10cSrcweir  *      <code>execute</code> method calls. It also should
66cdf0e10cSrcweir  *      implement <code>container.XNamed</code> interface and
67cdf0e10cSrcweir  *      <code>getName</code> method must return string with number
68cdf0e10cSrcweir  *      of calls.</li>
69cdf0e10cSrcweir  * <ul> <p>
70cdf0e10cSrcweir  *
71cdf0e10cSrcweir  * Also <b>important prerequicity</b>: if Job and Event is not yet
72cdf0e10cSrcweir  * added to configuration or <code>test.Job</code> component is
73cdf0e10cSrcweir  * not yet registered, the SOffice <b>must be destroyed</b> during
74cdf0e10cSrcweir  * testcase initialization. <p>
75cdf0e10cSrcweir  *
76cdf0e10cSrcweir  * @see com.sun.star.task.XJobExecutor
77cdf0e10cSrcweir  * @see ifc.task._XJobExecutor
78cdf0e10cSrcweir  */
79cdf0e10cSrcweir public class JobExecutor extends TestCase {
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     static Job job = new Job();
82cdf0e10cSrcweir     XNamed xNamed = null;
83cdf0e10cSrcweir     Object oRootCfg = null;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     /**
86cdf0e10cSrcweir      * For testcase initializing :
87cdf0e10cSrcweir      * <ol>
88cdf0e10cSrcweir      *  <li> Implementation <code>test.Job</code> must be registered in
89cdf0e10cSrcweir      *      SOffice registry. </li>
90cdf0e10cSrcweir      *  <li> Configuration in package <code>org.OpenOffice.Office.Jobs</code>
91cdf0e10cSrcweir      *      must be updated. <code>TestJob</code> must be registered for
92cdf0e10cSrcweir      *      service <code>test.Job</code> and event for this job named
93cdf0e10cSrcweir      *      <code>TextEvent</code> must be registered. </li>
94cdf0e10cSrcweir      * </ol>. <p>
95cdf0e10cSrcweir      *
96cdf0e10cSrcweir      * First these two conditions are checked. If job and event are not
97cdf0e10cSrcweir      * registered they are inserted into configuration and commited.
98cdf0e10cSrcweir      * After what SOffice must be destroyed for proper initialization
99cdf0e10cSrcweir      * of <code>JobExecutor</code> after startup. <p>
100cdf0e10cSrcweir      *
101cdf0e10cSrcweir      * Then if the implementation was not registered before it is
102cdf0e10cSrcweir      * registered in soffice <code>applicat.rbd</code> file (registering
103cdf0e10cSrcweir      * the component in currently running Java environment has no effect
104cdf0e10cSrcweir      * for <code>JobExecutor</code> in some reasons). <p>
105cdf0e10cSrcweir      *
106cdf0e10cSrcweir      * Note: SOffice is started again while the next
107cdf0e10cSrcweir      * <code>(XMultiServiceFactory)SOLink.getMSF()</code>  call.
108cdf0e10cSrcweir      */
initialize(TestParameters Param, PrintWriter log)109cdf0e10cSrcweir     protected void initialize(TestParameters Param, PrintWriter log) {
110cdf0e10cSrcweir         boolean serviceRegistered = false;
111cdf0e10cSrcweir         boolean configured = false;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         try {
114cdf0e10cSrcweir             Object obj = ((XMultiServiceFactory)Param.getMSF()).createInstance("test.Job");
115cdf0e10cSrcweir             serviceRegistered = obj != null;
116cdf0e10cSrcweir         } catch(com.sun.star.uno.Exception e) {}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         log.println("Service test.Job is "
119cdf0e10cSrcweir             + (serviceRegistered ? "already" : "not yet")  + " registered.");
120cdf0e10cSrcweir         if (! serviceRegistered){
121cdf0e10cSrcweir             String message = "You have to register 'test.Job' before office is stared.\n";
122cdf0e10cSrcweir             message += "Please run '$OFFICEPATH/program/pkgchk $DOCPTH/qadevlibs/JobExecutor.jar'";
123cdf0e10cSrcweir             throw new StatusException(message, new Exception());
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         XNameAccess jobs = null;
128cdf0e10cSrcweir         XNameAccess events = null;
129cdf0e10cSrcweir         try {
130cdf0e10cSrcweir             Object obj = ((XMultiServiceFactory)Param.getMSF()).createInstance
131cdf0e10cSrcweir                 ("com.sun.star.configuration.ConfigurationProvider");
132cdf0e10cSrcweir             XMultiServiceFactory xConfigMSF = (XMultiServiceFactory)
133cdf0e10cSrcweir                 UnoRuntime.queryInterface(XMultiServiceFactory.class, obj);
134cdf0e10cSrcweir             PropertyValue[] args = new PropertyValue[1];
135cdf0e10cSrcweir             args[0] = new PropertyValue();
136cdf0e10cSrcweir             args[0].Name = "nodepath";
137cdf0e10cSrcweir             args[0].Value = "org.openoffice.Office.Jobs";
138cdf0e10cSrcweir             oRootCfg = xConfigMSF.createInstanceWithArguments(
139cdf0e10cSrcweir                 "com.sun.star.configuration.ConfigurationUpdateAccess", args);
140cdf0e10cSrcweir             XHierarchicalNameAccess xHNA = (XHierarchicalNameAccess)
141cdf0e10cSrcweir                 UnoRuntime.queryInterface(XHierarchicalNameAccess.class, oRootCfg);
142cdf0e10cSrcweir             obj = xHNA.getByHierarchicalName("Jobs");
143cdf0e10cSrcweir             jobs = (XNameAccess) UnoRuntime.queryInterface
144cdf0e10cSrcweir                 (XNameAccess.class, obj);
145cdf0e10cSrcweir             obj = xHNA.getByHierarchicalName("Events");
146cdf0e10cSrcweir             events = (XNameAccess) UnoRuntime.queryInterface
147cdf0e10cSrcweir                 (XNameAccess.class, obj);
148cdf0e10cSrcweir         } catch (Exception e) {
149cdf0e10cSrcweir             throw new StatusException("Couldn't get configuration", e);
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         configured = jobs.hasByName("TestJob") && events.hasByName("TestEvent");
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         log.println("Test job and event is "
155cdf0e10cSrcweir             + (configured ? "already" : "not yet")  + " configured.");
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         if (!configured) {
158cdf0e10cSrcweir             try {
159cdf0e10cSrcweir                 log.println("Adding configuration to Jobs  ...");
160cdf0e10cSrcweir                 XSingleServiceFactory jobsFac = (XSingleServiceFactory)
161cdf0e10cSrcweir                     UnoRuntime.queryInterface(XSingleServiceFactory.class, jobs);
162cdf0e10cSrcweir                 Object oNewJob = jobsFac.createInstance();
163cdf0e10cSrcweir                 XNameReplace xNewJobNR = (XNameReplace)
164cdf0e10cSrcweir                     UnoRuntime.queryInterface(XNameReplace.class, oNewJob);
165cdf0e10cSrcweir                 xNewJobNR.replaceByName("Service", "test.Job");
166cdf0e10cSrcweir                 XNameContainer xJobsNC = (XNameContainer)
167cdf0e10cSrcweir                     UnoRuntime.queryInterface(XNameContainer.class, jobs);
168cdf0e10cSrcweir                 xJobsNC.insertByName("TestJob", oNewJob);
169cdf0e10cSrcweir 
170cdf0e10cSrcweir                 log.println("Adding configuration to Events  ...");
171cdf0e10cSrcweir                 XSingleServiceFactory eventsFac = (XSingleServiceFactory)
172cdf0e10cSrcweir                     UnoRuntime.queryInterface(XSingleServiceFactory.class, events);
173cdf0e10cSrcweir                 Object oNewEvent = eventsFac.createInstance();
174cdf0e10cSrcweir 
175cdf0e10cSrcweir                 XNameAccess xNewEventNA = (XNameAccess)
176cdf0e10cSrcweir                     UnoRuntime.queryInterface(XNameAccess.class, oNewEvent);
177cdf0e10cSrcweir                 Object oJobList = xNewEventNA.getByName("JobList");
178cdf0e10cSrcweir                 XSingleServiceFactory jobListFac = (XSingleServiceFactory)
179cdf0e10cSrcweir                     AnyConverter.toObject(new Type(XSingleServiceFactory.class),
180cdf0e10cSrcweir                     oJobList);
181cdf0e10cSrcweir                 XNameContainer jobListNC = (XNameContainer)
182cdf0e10cSrcweir                     AnyConverter.toObject(new Type(XNameContainer.class),
183cdf0e10cSrcweir                     oJobList);
184cdf0e10cSrcweir                 log.println("\tAdding TimeStamps to Events ...");
185cdf0e10cSrcweir                 Object oNewJobTimeStamps = jobListFac.createInstance();
186cdf0e10cSrcweir 
187cdf0e10cSrcweir                 jobListNC.insertByName("TestJob",  oNewJobTimeStamps);
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 
190cdf0e10cSrcweir                 XNameContainer xEventsNC = (XNameContainer)
191cdf0e10cSrcweir                     UnoRuntime.queryInterface(XNameContainer.class, events);
192cdf0e10cSrcweir                 xEventsNC.insertByName("TestEvent", oNewEvent);
193cdf0e10cSrcweir 
194cdf0e10cSrcweir                 XChangesBatch xCB = (XChangesBatch)
195cdf0e10cSrcweir                     UnoRuntime.queryInterface(XChangesBatch.class, oRootCfg);
196cdf0e10cSrcweir                 xCB.commitChanges();
197cdf0e10cSrcweir 
198cdf0e10cSrcweir                 try {
199cdf0e10cSrcweir                     Thread.sleep(1000);
200cdf0e10cSrcweir                 } catch (InterruptedException ex) {}
201cdf0e10cSrcweir 
202cdf0e10cSrcweir             } catch (com.sun.star.uno.Exception e) {
203cdf0e10cSrcweir                 e.printStackTrace(log);
204cdf0e10cSrcweir                 throw new StatusException("Couldn't change config", e);
205cdf0e10cSrcweir             }
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     /**
211cdf0e10cSrcweir      * Creating a Testenvironment for the interfaces to be tested.
212cdf0e10cSrcweir      *
213cdf0e10cSrcweir      * Service <code>com.sun.star.comp.framework.JobExecutor</code>
214cdf0e10cSrcweir      * is created.
215cdf0e10cSrcweir      */
createTestEnvironment(TestParameters Param, PrintWriter log)216cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
217cdf0e10cSrcweir 
218cdf0e10cSrcweir         XInterface oObj = null;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         Object job = null;
221cdf0e10cSrcweir         try {
222cdf0e10cSrcweir             oObj = (XInterface)((XMultiServiceFactory)Param.getMSF()).createInstance(
223cdf0e10cSrcweir                 "com.sun.star.comp.framework.JobExecutor");
224cdf0e10cSrcweir             job = (XInterface)((XMultiServiceFactory)Param.getMSF()).createInstance("test.Job");
225cdf0e10cSrcweir         } catch(com.sun.star.uno.Exception e) {
226cdf0e10cSrcweir             e.printStackTrace(log);
227cdf0e10cSrcweir             throw new StatusException(
228cdf0e10cSrcweir                 Status.failed("Couldn't create instance"));
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir         xNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, job);
232cdf0e10cSrcweir         log.println("Count = " + xNamed.getName());
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment( oObj );
235cdf0e10cSrcweir 
236cdf0e10cSrcweir         tEnv.addObjRelation("CallCounter", xNamed);
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         return tEnv;
239cdf0e10cSrcweir     } // finish method getTestEnvironment
240cdf0e10cSrcweir 
cleanup( TestParameters Param, PrintWriter log)241cdf0e10cSrcweir     protected void cleanup( TestParameters Param, PrintWriter log) {
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir /**
246cdf0e10cSrcweir  * Currently not used.
247cdf0e10cSrcweir  */
248cdf0e10cSrcweir class Job implements
249cdf0e10cSrcweir         XServiceInfo, XSingleServiceFactory {
250cdf0e10cSrcweir 
251cdf0e10cSrcweir     private static class Impl implements XServiceInfo, XTypeProvider, XJob, XNamed {
252cdf0e10cSrcweir         int callCount = 0;
253cdf0e10cSrcweir 
getImplementationId()254cdf0e10cSrcweir         public byte[] getImplementationId() {
255cdf0e10cSrcweir             return toString().getBytes();
256cdf0e10cSrcweir         }
257cdf0e10cSrcweir 
getTypes()258cdf0e10cSrcweir         public Type[] getTypes() {
259cdf0e10cSrcweir             Class interfaces[] = getClass().getInterfaces();
260cdf0e10cSrcweir             Type types[] = new Type[interfaces.length];
261cdf0e10cSrcweir             for(int i = 0; i < interfaces.length; ++ i)
262cdf0e10cSrcweir                 types[i] = new Type(interfaces[i]);
263cdf0e10cSrcweir             return types;
264cdf0e10cSrcweir         }
265cdf0e10cSrcweir 
execute(NamedValue[] param)266cdf0e10cSrcweir         public Object execute(NamedValue[] param) {
267cdf0e10cSrcweir             callCount++;
268cdf0e10cSrcweir 
269cdf0e10cSrcweir             return null;
270cdf0e10cSrcweir         }
271cdf0e10cSrcweir 
getName()272cdf0e10cSrcweir         public String getName() {
273cdf0e10cSrcweir             return String.valueOf(callCount);
274cdf0e10cSrcweir         }
275cdf0e10cSrcweir 
setName(String n)276cdf0e10cSrcweir         public void setName(String n) {}
277cdf0e10cSrcweir 
supportsService(String name)278cdf0e10cSrcweir         public boolean supportsService(String name) {
279cdf0e10cSrcweir             return __serviceName.equals(name);
280cdf0e10cSrcweir         }
281cdf0e10cSrcweir 
getSupportedServiceNames()282cdf0e10cSrcweir         public String[] getSupportedServiceNames() {
283cdf0e10cSrcweir             return new String[] {__serviceName};
284cdf0e10cSrcweir         }
285cdf0e10cSrcweir 
getImplementationName()286cdf0e10cSrcweir         public String getImplementationName() {
287cdf0e10cSrcweir             return getClass().getName();
288cdf0e10cSrcweir         }
289cdf0e10cSrcweir     }
290cdf0e10cSrcweir 
291cdf0e10cSrcweir     public static final String __serviceName = "test.Job";
292cdf0e10cSrcweir     static Impl impl = new Impl();
293cdf0e10cSrcweir 
createInstanceWithArguments(Object[] args)294cdf0e10cSrcweir     public Object createInstanceWithArguments(Object[] args) {
295cdf0e10cSrcweir 	return impl;
296cdf0e10cSrcweir     }
297cdf0e10cSrcweir 
createInstance()298cdf0e10cSrcweir     public Object createInstance() {
299cdf0e10cSrcweir         return createInstanceWithArguments(null);
300cdf0e10cSrcweir     }
301cdf0e10cSrcweir 
supportsService(String name)302cdf0e10cSrcweir     public boolean supportsService(String name) {
303cdf0e10cSrcweir         return __serviceName.equals(name);
304cdf0e10cSrcweir     }
305cdf0e10cSrcweir 
getSupportedServiceNames()306cdf0e10cSrcweir     public String[] getSupportedServiceNames() {
307cdf0e10cSrcweir         return new String[] {__serviceName};
308cdf0e10cSrcweir     }
309cdf0e10cSrcweir 
getImplementationName()310cdf0e10cSrcweir     public String getImplementationName() {
311cdf0e10cSrcweir         return getClass().getName();
312cdf0e10cSrcweir     }
313cdf0e10cSrcweir }
314