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