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