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