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