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 ifc.lang;
25 
26 import lib.MultiMethodTest;
27 
28 import com.sun.star.lang.XSingleServiceFactory;
29 import com.sun.star.uno.UnoRuntime;
30 
31 
32 /**
33 /**
34 * Testing <code>com.sun.star.</code>
35 * interface methods :lang.XSingleServiceFactory
36 * <ul>
37 *  <li><code> createInstance()</code></li>
38 *  <li><code> createInstanceWithArguments()</code></li>
39 * </ul> <p>
40 * This test needs the following object relations :
41 * <ul>
42 *  <li> <code>'XSingleServiceFactory.createInstance.negative'</code> :
43 *   <code>String</code> relation; If its value 'true' then
44 *   <code>createInstance</code> method for the object isn't
45 *   supported. </li>
46 *  <li> <code>'XSingleServiceFactory.arguments'</code> <b>(optional)</b>:
47 *   has <code>Object[]</code> type. This relation is used as
48 *   a parameter for <code>createInstanceWithArguments</code>
49 *   method call. If this relation doesn't exist test pass
50 *   zerro length array as argument. </li>
51 *  <li> <code>'XSingleServiceFactory.MustSupport'</code> <b>(optional)</b>:
52 *   of type <code>java.lang.Class[]</code>. This is an array of UNO
53 *   interface classes which must be supported by created instance.
54 *  </li>
55 * <ul> <p>
56 * Test is <b> NOT </b> multithread compilant. <p>
57 * After test completion object environment has to be recreated.
58 * @see com.sun.star.lang.XSingleServiceFactory
59 */
60 public class _XSingleServiceFactory extends MultiMethodTest {
61 
62     public XSingleServiceFactory oObj = null;
63     private Class[] mustSupport = null ;
64 
before()65     public void before() {
66         mustSupport = (Class[]) tEnv.getObjRelation
67             ("XSingleServiceFactory.MustSupport") ;
68     }
69 
70     /**
71     * Just calls the method and check the value returned. <p>
72     *
73     * Has <b>OK</b> status in case if this method is supported
74     * by object and non null value is returned, or if
75     * this method isn't supported then the method call must
76     * rise an exception or return <code>null</code> value.
77     * If the relation exists which specifies required interfaces
78     * supported by created instance then status is <b>OK</b>
79     * if all these interfaces are supported.
80     */
_createInstance()81     public void _createInstance() {
82         // for some objects the method should fail.
83         // If thi is required the property is set to true.
84         String negStr = (String)tEnv.getObjRelation(
85                 "XSingleServiceFactory.createInstance.negative");
86         boolean negative = (negStr != null && negStr.equalsIgnoreCase("true"));
87 
88         if (negative) {
89             log.println("Negative test: createInstance should fail");
90         }
91 
92         try {
93             log.println("Creating Instance: ");
94             Object Inst = oObj.createInstance();
95             boolean bOK = Inst != null ;
96 
97             if (mustSupport != null && bOK) {
98                 for (int i = 0; i < mustSupport.length; i++) {
99                     Object ifc = UnoRuntime.queryInterface(mustSupport[i], Inst) ;
100                     if (ifc == null) {
101                         log.println(" !!! Created instance doesn't support " +
102                             mustSupport[i].toString()) ;
103                     }
104                     bOK &= ifc != null ;
105                 }
106             }
107 
108             tRes.tested("createInstance()",
109                 (negative && Inst == null) || (!negative && bOK));
110         } catch (com.sun.star.uno.Exception ex) {
111             log.println("Exception occured during createInstance()");
112             if (negative) {
113                 ex.printStackTrace(log);
114             }
115             tRes.tested("createInstance()", negative);
116         }
117     }
118 
119     /**
120     * Calls the method and checks the value returned. If relation
121     * with method argument doesn't exist new zerro length array
122     * is created. <p>
123     * Has <b>OK</b> status if non null value is returned.
124     * If the relation exists which specifies required interfaces
125     * supported by created instance then status is <b>OK</b>
126     * if all these interfaces are supported.
127     */
_createInstanceWithArguments()128     public void _createInstanceWithArguments() {
129         Object[] arg = (Object[])tEnv.getObjRelation(
130                 "XSingleServiceFactory.arguments");
131 
132         if (arg == null) {
133             arg = new Object[0];
134         }
135 
136         try {
137             boolean bOK = true ;
138             log.println("Creating Instance with Argument");
139             Object Inst = oObj.createInstanceWithArguments(arg);
140             bOK &= Inst != null ;
141 
142             if (mustSupport != null) {
143                 for (int i = 0; i < mustSupport.length; i++) {
144                     Object ifc = UnoRuntime.queryInterface(mustSupport[i], Inst) ;
145                     if (ifc == null) {
146                         log.println(" !!! Created instance doesn't support " +
147                             mustSupport[i].toString()) ;
148                     }
149                     bOK &= ifc != null ;
150                 }
151             }
152 
153             tRes.tested("createInstanceWithArguments()", bOK);
154         }
155         catch (com.sun.star.uno.Exception ex) {
156             log.println("Exception occured during createInstanceWithArguments()");
157             ex.printStackTrace(log);
158             tRes.tested("createInstanceWithArguments()",false);
159         }
160     }
161 
162 }  // finish class _XSingleServiceFactory
163 
164 
165