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 package ifc.lang;
24 
25 import lib.MultiMethodTest;
26 
27 import com.sun.star.lang.XMultiServiceFactory;
28 
29 
30 /**
31 * Testing <code>com.sun.star.lang.XMultiServiceFactory</code>
32 * interface methods:
33 * <ul>
34 *   <li><code>createInstance()</code></li>
35 *   <li><code>createInstanceWithArguments()</code></li>
36 *   <li><code>getAvailableServiceNames()</code></li>
37 * </ul> <p>
38 *
39 * This test needs the following object relations :
40 * <ul>
41 *  <li> <code>'XMSF.serviceNames'</code> (of type <code>String[]</code>)
42 *    <b>optional</b>:
43 *    the relation used when service names are obtained the way
44 *    other than calling <code>getAvailableServiceNames()</code>
45 *    method.
46 *  </li>
47 *  <li> <code>'XMSF.serviceNamesWithArgs'</code> (of type <code>String[]</code>)
48 *    <b>optional</b>:
49 *    the relation used when service names are obtained the way
50 *    other than calling <code>getAvailableServiceNames()</code>
51 *    method for testing <code>createInstanceWithArguments</code> method.
52 *  </li>
53 *  <li> <code>'XMSF.Args'</code> (of type <code>Object[][]</code>)
54 *    <b>optional</b>:
55 *    if this relation exists than the method
56 *    <code>createInstanceWithArguments</code> is tested. This relation
57 *    supplies arguments for creating instances. If the relation
58 *    <code>'XMSF.serviceNamesWithArgs'</code> is also specified
59 *    then for each service name from that relation appropriate arguments
60 *    are used from arguments array. If not than arguments with index
61 *    0 are used for services creation obtained by
62 *    <code>getAvailableServiceNames</code> method.
63 *  </li>
64 * </ul> <p>
65 *
66 * @see com.sun.star.lang.XMultiServiceFactory
67 */
68 public class _XMultiServiceFactory extends MultiMethodTest {
69     public XMultiServiceFactory oObj = null;
70     public String[] services = null;
71 
72     /**
73     * Test calls the method and checks returned value. <p>
74     * Has <b> OK </b> status if returned value isn't null. <p>
75     */
76     public void _getAvailableServiceNames() {
77         services = oObj.getAvailableServiceNames();
78 
79         for (int i = 0; i < services.length; i++) {
80             log.println("Service" + i + ": " + services[i]);
81         }
82 
83         tRes.tested("getAvailableServiceNames()", services != null);
84     }
85 
86     /**
87      * Test creates instance of the first service from names array
88      * get by <code>getAvailableServiceNames()</code>. If the array
89      * is empty than test looks for names from relation. <p>
90      *
91      * Has <b> OK </b> status if created instance isn't null. <p>
92      *
93      * The following method tests are to be completed successfully before :
94      * <ul>
95      *  <li> <code> getAvailableServiceNames() </code> : to have list of
96      *  supported services </li>
97      * </ul>
98      */
99     public void _createInstance() {
100         requiredMethod("getAvailableServiceNames()");
101 
102         if (services.length == 0) {
103             services = (String[]) tEnv.getObjRelation("XMSF.serviceNames");
104 
105             if (services == null) {
106                 log.println("No service to create.");
107                 tRes.tested("createInstance()", true);
108 
109                 return;
110             }
111         }
112 
113         String needArgs = (String) tEnv.getObjRelation("needArgs");
114 
115         if (needArgs != null) {
116             log.println("The " + needArgs +
117                         " doesn't support createInstance without arguments");
118             tRes.tested("createInstance()", true);
119 
120             return;
121         }
122 
123         boolean res = true;
124 
125         for (int k = 0; k < services.length; k++) {
126             try {
127                 log.println("Creating Instance: " + services[k]);
128 
129                 Object Inst = oObj.createInstance(services[k]);
130                 res = (Inst != null);
131             } catch (com.sun.star.uno.Exception ex) {
132                 log.println("Exception occured during createInstance()");
133                 ex.printStackTrace(log);
134                 res = false;
135             }
136         }
137 
138         tRes.tested("createInstance()", res);
139     }
140 
141     /**
142      * If the relation with arguments is not specified test does nothing.
143      * In other case it tries to create instance by its name from
144      * relation of from array <code>getAvailableServiceNames()</code>
145      * method supplied. <p>
146      *
147      * Has <b> OK </b> status if the created instance is not null. <p>
148      *
149      * The following method tests are to be completed successfully before :
150      * <ul>
151      *  <li> <code> getAvailableServiceNames() </code> : to have list of
152      *  supported services </li>
153      * </ul>
154      */
155     public void _createInstanceWithArguments() {
156         requiredMethod("getAvailableServiceNames()");
157 
158         Object[][] args = (Object[][]) tEnv.getObjRelation("XMSF.Args");
159         String[] sNames = (String[]) tEnv.getObjRelation(
160                                     "XMSF.serviceNamesWithArgs");
161 
162         if (args == null) {
163             log.println("Relation 'XMSF.serviceNamesWithArgs' not found");
164             log.println("The component assumed not support " +
165                         "createInstanceWithArguments()");
166             tRes.tested("createInstanceWithArguments()", true);
167         } else {
168             if (sNames == null) {
169                 sNames = services;
170             }
171 
172             boolean res = true;
173 
174             for (int k = 0; k < sNames.length; k++) {
175                 log.println("Creating service '" + sNames[k] +
176                             "' with arguments");
177 
178                 try {
179                     Object Inst = oObj.createInstanceWithArguments(sNames[k],
180                                                                    args[k]);
181                     res &= (Inst != null);
182                 } catch (com.sun.star.uno.Exception ex) {
183                     log.println(
184                             "Exception occured during createInstanceWithArguments()");
185                     ex.printStackTrace(log);
186                     res = false;
187                 }
188             }
189 
190             tRes.tested("createInstanceWithArguments()", res);
191         }
192     }
193 } // finish class _XMultiServiceFactory
194