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._servicemgr.uno;
25 
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.container.XEnumeration;
28 import com.sun.star.container.XSet;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.uno.XComponentContext;
32 import com.sun.star.uno.XInterface;
33 import java.io.PrintWriter;
34 import lib.StatusException;
35 import lib.TestCase;
36 import lib.TestEnvironment;
37 import lib.TestParameters;
38 import com.sun.star.uno.AnyConverter;
39 import com.sun.star.uno.Type;
40 
41 /**
42  * Test for object which is represented by service
43  * <code>com.sun.star.comp.stoc.OServiceManager</code>. <p>
44  *
45  * Object implements the following interfaces :
46  * <ul>
47  *  <li> <code>com::sun::star::lang::XMultiServiceFactory</code></li>
48  *  <li> <code>com::sun::star::container::XContentEnumerationAccess</code></li>
49  *  <li> <code>com::sun::star::container::XSet</code></li>
50  *  <li> <code>com::sun::star::lang::XMultiComponentFactory</code></li>
51  *  <li> <code>com::sun::star::lang::XServiceInfo</code></li>
52  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
53  *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
54  *  <li> <code>com::sun::star::lang::XComponent</code></li>
55  * </ul> <p>
56  *
57  * This object test <b> is NOT </b> designed to be run in several
58  * threads concurently.
59  *
60  * @see com.sun.star.lang.XMultiServiceFactory
61  * @see com.sun.star.container.XContentEnumerationAccess
62  * @see com.sun.star.container.XSet
63  * @see com.sun.star.lang.XMultiComponentFactory
64  * @see com.sun.star.lang.XServiceInfo
65  * @see com.sun.star.container.XElementAccess
66  * @see com.sun.star.container.XEnumerationAccess
67  * @see com.sun.star.lang.XComponent
68  * @see ifc.lang._XMultiServiceFactory
69  * @see ifc.container._XContentEnumerationAccess
70  * @see ifc.container._XSet
71  * @see ifc.lang._XMultiComponentFactory
72  * @see ifc.lang._XServiceInfo
73  * @see ifc.container._XElementAccess
74  * @see ifc.container._XEnumerationAccess
75  * @see ifc.lang._XComponent
76  */
77 public class OServiceManager extends TestCase {
78 
79     /**
80      * Creating a Testenvironment for the interfaces to be tested.
81      * Creates an instance of the service
82      * <code>com.sun.star.comp.stoc.OServiceManager</code>.
83      *     Object relations created :
84      * <ul>
85      *  <li> <code>'NewElement'</code> for
86      *      {@link ifc.container._XSet} :
87      *      element to be inserted into manager. Here
88      *      a new <code>ServiceManager</code> implementation is
89      *      create using
90      *      <code>tools.SOConnect.createSimpleServiceManager()</code>
91      *      and one of services contained in it is taken.</li>
92      * </ul>
93      */
createTestEnvironment(TestParameters Param, PrintWriter log)94     protected TestEnvironment createTestEnvironment
95             (TestParameters Param, PrintWriter log) {
96 
97         XInterface oObj = null;
98         Object oInterface = null;
99 
100         try {
101             XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
102             oInterface = xMSF.createInstance
103                 ( "com.sun.star.comp.stoc.OServiceManager" );
104         } catch( com.sun.star.uno.Exception e ) {
105             log.println("ServiceManager service not available" );
106         }
107 
108         // adding a service to the manager
109         Object newElement = null ;
110         XMultiServiceFactory srvMan = null;
111         XComponentContext xContext = null;
112 
113         try {
114 
115             srvMan = com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();
116 
117         } catch ( java.lang.Exception ex) {
118             log.println("Error creating SimpleServiceManager :");
119             ex.printStackTrace(log);
120         }
121 
122         try {
123             XSet set = (XSet) UnoRuntime.queryInterface
124                 (XSet.class, oInterface) ;
125             XSet set1 = (XSet) UnoRuntime.queryInterface
126                 (XSet.class, srvMan) ;
127             XEnumeration oEnum = set1.createEnumeration();
128             Object srv = oEnum.nextElement();
129 
130             set.insert(srv) ;
131 
132             newElement = oEnum.nextElement();
133 
134             XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(
135                                             XPropertySet.class, oInterface);
136             if (xProp != null) {
137                 xContext = (XComponentContext) AnyConverter.toObject(
138                     new Type(XComponentContext.class),
139                             xProp.getPropertyValue("DefaultContext"));
140             }
141         } catch (com.sun.star.uno.Exception e) {
142             log.println("Can't insert a service to the ServiceManager") ;
143             throw new StatusException("Can't create object environment", e) ;
144         }
145 
146         oObj = (XInterface) oInterface;
147 
148         log.println( "    creating a new environment for object" );
149         TestEnvironment tEnv = new TestEnvironment( oObj );
150 
151         // adding relation for XSet interface
152         tEnv.addObjRelation("NewElement", newElement) ;
153 
154         // adding relation for XPropertySet
155         tEnv.addObjRelation("PTT",new String[]{"DefaultContext","none","none"});
156 
157         //adding relation for XMultiComponentFactory
158         if (xContext != null) {
159             tEnv.addObjRelation("DC", xContext);
160         }
161         return tEnv;
162     } // finish method getTestEnvironment
163 
164 }    // finish class OServiceManager
165 
166