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