/************************************************************** * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * *************************************************************/ package mod._fwk; import java.io.PrintWriter; import lib.Status; import lib.StatusException; import lib.TestCase; import lib.TestEnvironment; import lib.TestParameters; import util.utils; import com.sun.star.beans.NamedValue; import com.sun.star.beans.PropertyValue; import com.sun.star.container.XHierarchicalNameAccess; import com.sun.star.container.XNameAccess; import com.sun.star.container.XNameContainer; import com.sun.star.container.XNameReplace; import com.sun.star.container.XNamed; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.lang.XServiceInfo; import com.sun.star.lang.XSingleServiceFactory; import com.sun.star.lang.XTypeProvider; import com.sun.star.task.XJob; import com.sun.star.uno.Type; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XInterface; import com.sun.star.util.XChangesBatch; import com.sun.star.uno.AnyConverter; /** * Test for object that implements the following interfaces : *
com::sun::star::task::XJobExecutor
* The following files are needed for testcase creation : *
qadevlibs/JobExecutor.jar
:
* this is java component test.Job
which
* should be registered before or during this testcase
* creation. This component must implement
* com.sun.star.task.XJob
interface and count
* execute
method calls. It also should
* implement container.XNamed
interface and
* getName
method must return string with number
* of calls.
*
* Also important prerequicity: if Job and Event is not yet
* added to configuration or test.Job
component is
* not yet registered, the SOffice must be destroyed during
* testcase initialization.
* * @see com.sun.star.task.XJobExecutor * @see ifc.task._XJobExecutor */ public class JobExecutor extends TestCase { static Job job = new Job(); XNamed xNamed = null; Object oRootCfg = null; /** * For testcase initializing : *
test.Job
must be registered in
* SOffice registry. org.OpenOffice.Office.Jobs
* must be updated. TestJob
must be registered for
* service test.Job
and event for this job named
* TextEvent
must be registered.
*
* First these two conditions are checked. If job and event are not
* registered they are inserted into configuration and commited.
* After what SOffice must be destroyed for proper initialization
* of JobExecutor
after startup.
*
* Then if the implementation was not registered before it is
* registered in soffice applicat.rbd
file (registering
* the component in currently running Java environment has no effect
* for JobExecutor
in some reasons).
*
* Note: SOffice is started again while the next
* (XMultiServiceFactory)SOLink.getMSF()
call.
*/
protected void initialize(TestParameters Param, PrintWriter log) {
boolean serviceRegistered = false;
boolean configured = false;
try {
Object obj = ((XMultiServiceFactory)Param.getMSF()).createInstance("test.Job");
serviceRegistered = obj != null;
} catch(com.sun.star.uno.Exception e) {}
log.println("Service test.Job is "
+ (serviceRegistered ? "already" : "not yet") + " registered.");
if (! serviceRegistered){
String message = "You have to register 'test.Job' before office is stared.\n";
message += "Please run '$OFFICEPATH/program/pkgchk $DOCPTH/qadevlibs/JobExecutor.jar'";
throw new StatusException(message, new Exception());
}
XNameAccess jobs = null;
XNameAccess events = null;
try {
Object obj = ((XMultiServiceFactory)Param.getMSF()).createInstance
("com.sun.star.configuration.ConfigurationProvider");
XMultiServiceFactory xConfigMSF = (XMultiServiceFactory)
UnoRuntime.queryInterface(XMultiServiceFactory.class, obj);
PropertyValue[] args = new PropertyValue[1];
args[0] = new PropertyValue();
args[0].Name = "nodepath";
args[0].Value = "org.openoffice.Office.Jobs";
oRootCfg = xConfigMSF.createInstanceWithArguments(
"com.sun.star.configuration.ConfigurationUpdateAccess", args);
XHierarchicalNameAccess xHNA = (XHierarchicalNameAccess)
UnoRuntime.queryInterface(XHierarchicalNameAccess.class, oRootCfg);
obj = xHNA.getByHierarchicalName("Jobs");
jobs = (XNameAccess) UnoRuntime.queryInterface
(XNameAccess.class, obj);
obj = xHNA.getByHierarchicalName("Events");
events = (XNameAccess) UnoRuntime.queryInterface
(XNameAccess.class, obj);
} catch (Exception e) {
throw new StatusException("Couldn't get configuration", e);
}
configured = jobs.hasByName("TestJob") && events.hasByName("TestEvent");
log.println("Test job and event is "
+ (configured ? "already" : "not yet") + " configured.");
if (!configured) {
try {
log.println("Adding configuration to Jobs ...");
XSingleServiceFactory jobsFac = (XSingleServiceFactory)
UnoRuntime.queryInterface(XSingleServiceFactory.class, jobs);
Object oNewJob = jobsFac.createInstance();
XNameReplace xNewJobNR = (XNameReplace)
UnoRuntime.queryInterface(XNameReplace.class, oNewJob);
xNewJobNR.replaceByName("Service", "test.Job");
XNameContainer xJobsNC = (XNameContainer)
UnoRuntime.queryInterface(XNameContainer.class, jobs);
xJobsNC.insertByName("TestJob", oNewJob);
log.println("Adding configuration to Events ...");
XSingleServiceFactory eventsFac = (XSingleServiceFactory)
UnoRuntime.queryInterface(XSingleServiceFactory.class, events);
Object oNewEvent = eventsFac.createInstance();
XNameAccess xNewEventNA = (XNameAccess)
UnoRuntime.queryInterface(XNameAccess.class, oNewEvent);
Object oJobList = xNewEventNA.getByName("JobList");
XSingleServiceFactory jobListFac = (XSingleServiceFactory)
AnyConverter.toObject(new Type(XSingleServiceFactory.class),
oJobList);
XNameContainer jobListNC = (XNameContainer)
AnyConverter.toObject(new Type(XNameContainer.class),
oJobList);
log.println("\tAdding TimeStamps to Events ...");
Object oNewJobTimeStamps = jobListFac.createInstance();
jobListNC.insertByName("TestJob", oNewJobTimeStamps);
XNameContainer xEventsNC = (XNameContainer)
UnoRuntime.queryInterface(XNameContainer.class, events);
xEventsNC.insertByName("TestEvent", oNewEvent);
XChangesBatch xCB = (XChangesBatch)
UnoRuntime.queryInterface(XChangesBatch.class, oRootCfg);
xCB.commitChanges();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
} catch (com.sun.star.uno.Exception e) {
e.printStackTrace(log);
throw new StatusException("Couldn't change config", e);
}
}
}
/**
* Creating a Testenvironment for the interfaces to be tested.
*
* Service com.sun.star.comp.framework.JobExecutor
* is created.
*/
protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
XInterface oObj = null;
Object job = null;
try {
oObj = (XInterface)((XMultiServiceFactory)Param.getMSF()).createInstance(
"com.sun.star.comp.framework.JobExecutor");
job = (XInterface)((XMultiServiceFactory)Param.getMSF()).createInstance("test.Job");
} catch(com.sun.star.uno.Exception e) {
e.printStackTrace(log);
throw new StatusException(
Status.failed("Couldn't create instance"));
}
xNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, job);
log.println("Count = " + xNamed.getName());
TestEnvironment tEnv = new TestEnvironment( oObj );
tEnv.addObjRelation("CallCounter", xNamed);
return tEnv;
} // finish method getTestEnvironment
protected void cleanup( TestParameters Param, PrintWriter log) {
}
}
/**
* Currently not used.
*/
class Job implements
XServiceInfo, XSingleServiceFactory {
private static class Impl implements XServiceInfo, XTypeProvider, XJob, XNamed {
int callCount = 0;
public byte[] getImplementationId() {
return toString().getBytes();
}
public Type[] getTypes() {
Class interfaces[] = getClass().getInterfaces();
Type types[] = new Type[interfaces.length];
for(int i = 0; i < interfaces.length; ++ i)
types[i] = new Type(interfaces[i]);
return types;
}
public Object execute(NamedValue[] param) {
callCount++;
return null;
}
public String getName() {
return String.valueOf(callCount);
}
public void setName(String n) {}
public boolean supportsService(String name) {
return __serviceName.equals(name);
}
public String[] getSupportedServiceNames() {
return new String[] {__serviceName};
}
public String getImplementationName() {
return getClass().getName();
}
}
public static final String __serviceName = "test.Job";
static Impl impl = new Impl();
public Object createInstanceWithArguments(Object[] args) {
return impl;
}
public Object createInstance() {
return createInstanceWithArguments(null);
}
public boolean supportsService(String name) {
return __serviceName.equals(name);
}
public String[] getSupportedServiceNames() {
return new String[] {__serviceName};
}
public String getImplementationName() {
return getClass().getName();
}
}