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