1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.lang;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import lib.MultiMethodTest;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /**
33cdf0e10cSrcweir /**
34cdf0e10cSrcweir * Testing <code>com.sun.star.</code>
35cdf0e10cSrcweir * interface methods :lang.XSingleServiceFactory
36cdf0e10cSrcweir * <ul>
37cdf0e10cSrcweir *  <li><code> createInstance()</code></li>
38cdf0e10cSrcweir *  <li><code> createInstanceWithArguments()</code></li>
39cdf0e10cSrcweir * </ul> <p>
40cdf0e10cSrcweir * This test needs the following object relations :
41cdf0e10cSrcweir * <ul>
42cdf0e10cSrcweir *  <li> <code>'XSingleServiceFactory.createInstance.negative'</code> :
43cdf0e10cSrcweir *   <code>String</code> relation; If its value 'true' then
44cdf0e10cSrcweir *   <code>createInstance</code> method for the object isn't
45cdf0e10cSrcweir *   supported. </li>
46cdf0e10cSrcweir *  <li> <code>'XSingleServiceFactory.arguments'</code> <b>(optional)</b>:
47cdf0e10cSrcweir *   has <code>Object[]</code> type. This relation is used as
48cdf0e10cSrcweir *   a parameter for <code>createInstanceWithArguments</code>
49cdf0e10cSrcweir *   method call. If this relation doesn't exist test pass
50cdf0e10cSrcweir *   zerro length array as argument. </li>
51cdf0e10cSrcweir *  <li> <code>'XSingleServiceFactory.MustSupport'</code> <b>(optional)</b>:
52cdf0e10cSrcweir *   of type <code>java.lang.Class[]</code>. This is an array of UNO
53cdf0e10cSrcweir *   interface classes which must be supported by created instance.
54cdf0e10cSrcweir *  </li>
55cdf0e10cSrcweir * <ul> <p>
56cdf0e10cSrcweir * Test is <b> NOT </b> multithread compilant. <p>
57cdf0e10cSrcweir * After test completion object environment has to be recreated.
58cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory
59cdf0e10cSrcweir */
60cdf0e10cSrcweir public class _XSingleServiceFactory extends MultiMethodTest {
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     public XSingleServiceFactory oObj = null;
63cdf0e10cSrcweir     private Class[] mustSupport = null ;
64cdf0e10cSrcweir 
before()65cdf0e10cSrcweir     public void before() {
66cdf0e10cSrcweir         mustSupport = (Class[]) tEnv.getObjRelation
67cdf0e10cSrcweir             ("XSingleServiceFactory.MustSupport") ;
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     /**
71cdf0e10cSrcweir     * Just calls the method and check the value returned. <p>
72cdf0e10cSrcweir     *
73cdf0e10cSrcweir     * Has <b>OK</b> status in case if this method is supported
74cdf0e10cSrcweir     * by object and non null value is returned, or if
75cdf0e10cSrcweir     * this method isn't supported then the method call must
76cdf0e10cSrcweir     * rise an exception or return <code>null</code> value.
77cdf0e10cSrcweir     * If the relation exists which specifies required interfaces
78cdf0e10cSrcweir     * supported by created instance then status is <b>OK</b>
79cdf0e10cSrcweir     * if all these interfaces are supported.
80cdf0e10cSrcweir     */
_createInstance()81cdf0e10cSrcweir     public void _createInstance() {
82cdf0e10cSrcweir         // for some objects the method should fail.
83cdf0e10cSrcweir         // If thi is required the property is set to true.
84cdf0e10cSrcweir         String negStr = (String)tEnv.getObjRelation(
85cdf0e10cSrcweir                 "XSingleServiceFactory.createInstance.negative");
86cdf0e10cSrcweir         boolean negative = (negStr != null && negStr.equalsIgnoreCase("true"));
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         if (negative) {
89cdf0e10cSrcweir             log.println("Negative test: createInstance should fail");
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         try {
93cdf0e10cSrcweir             log.println("Creating Instance: ");
94cdf0e10cSrcweir             Object Inst = oObj.createInstance();
95cdf0e10cSrcweir             boolean bOK = Inst != null ;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             if (mustSupport != null && bOK) {
98cdf0e10cSrcweir                 for (int i = 0; i < mustSupport.length; i++) {
99cdf0e10cSrcweir                     Object ifc = UnoRuntime.queryInterface(mustSupport[i], Inst) ;
100cdf0e10cSrcweir                     if (ifc == null) {
101cdf0e10cSrcweir                         log.println(" !!! Created instance doesn't support " +
102cdf0e10cSrcweir                             mustSupport[i].toString()) ;
103cdf0e10cSrcweir                     }
104cdf0e10cSrcweir                     bOK &= ifc != null ;
105cdf0e10cSrcweir                 }
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir             tRes.tested("createInstance()",
109cdf0e10cSrcweir                 (negative && Inst == null) || (!negative && bOK));
110cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception ex) {
111cdf0e10cSrcweir             log.println("Exception occured during createInstance()");
112cdf0e10cSrcweir             if (negative) {
113cdf0e10cSrcweir                 ex.printStackTrace(log);
114cdf0e10cSrcweir             }
115cdf0e10cSrcweir             tRes.tested("createInstance()", negative);
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     /**
120cdf0e10cSrcweir     * Calls the method and checks the value returned. If relation
121cdf0e10cSrcweir     * with method argument doesn't exist new zerro length array
122cdf0e10cSrcweir     * is created. <p>
123cdf0e10cSrcweir     * Has <b>OK</b> status if non null value is returned.
124cdf0e10cSrcweir     * If the relation exists which specifies required interfaces
125cdf0e10cSrcweir     * supported by created instance then status is <b>OK</b>
126cdf0e10cSrcweir     * if all these interfaces are supported.
127cdf0e10cSrcweir     */
_createInstanceWithArguments()128cdf0e10cSrcweir     public void _createInstanceWithArguments() {
129cdf0e10cSrcweir         Object[] arg = (Object[])tEnv.getObjRelation(
130cdf0e10cSrcweir                 "XSingleServiceFactory.arguments");
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         if (arg == null) {
133cdf0e10cSrcweir             arg = new Object[0];
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         try {
137cdf0e10cSrcweir             boolean bOK = true ;
138cdf0e10cSrcweir             log.println("Creating Instance with Argument");
139cdf0e10cSrcweir             Object Inst = oObj.createInstanceWithArguments(arg);
140cdf0e10cSrcweir             bOK &= Inst != null ;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir             if (mustSupport != null) {
143cdf0e10cSrcweir                 for (int i = 0; i < mustSupport.length; i++) {
144cdf0e10cSrcweir                     Object ifc = UnoRuntime.queryInterface(mustSupport[i], Inst) ;
145cdf0e10cSrcweir                     if (ifc == null) {
146cdf0e10cSrcweir                         log.println(" !!! Created instance doesn't support " +
147cdf0e10cSrcweir                             mustSupport[i].toString()) ;
148cdf0e10cSrcweir                     }
149cdf0e10cSrcweir                     bOK &= ifc != null ;
150cdf0e10cSrcweir                 }
151cdf0e10cSrcweir             }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             tRes.tested("createInstanceWithArguments()", bOK);
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir         catch (com.sun.star.uno.Exception ex) {
156cdf0e10cSrcweir             log.println("Exception occured during createInstanceWithArguments()");
157cdf0e10cSrcweir             ex.printStackTrace(log);
158cdf0e10cSrcweir             tRes.tested("createInstanceWithArguments()",false);
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir }  // finish class _XSingleServiceFactory
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 
165