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.uno;
25 
26 import lib.MultiMethodTest;
27 import lib.StatusException;
28 
29 import com.sun.star.uno.XInterface;
30 import com.sun.star.uno.XNamingService;
31 
32 
33 /**
34 * Testing <code>com.sun.star.uno.XNamingService</code>
35 * interface methods. <p>
36 * This test need the following object relations :
37 * <ul>
38 *  <li> <code>'XNamingService.RegisterObject'</code> :
39 *     object to be registered.</li>
40 * <ul> <p>
41 * Test is <b> NOT </b> multithread compilant. <p>
42 * After test completion object environment has to be recreated.
43 */
44 public class _XNamingService extends MultiMethodTest {
45 
46     public static XNamingService oObj = null;
47 
48     private XInterface regObject = null ;
49 
50     /**
51     * Retrieves object relation.
52     * @throws StatusException If the relation not found.
53     */
before()54     public void before() {
55         regObject = (XInterface)
56             tEnv.getObjRelation("XNamingService.RegisterObject") ;
57         if (regObject == null)
58             throw new StatusException("Can't create object to register",
59                 new NullPointerException()) ;
60     }
61 
62     /**
63     * Test calls the method and checks that
64     * no exceptions were thrown. <p>
65     * Has <b> OK </b> status if the method successfully returns
66     * and no exceptions were thrown. <p>
67     * The following method tests are to be completed successfully before :
68     */
_registerObject()69     public void _registerObject() throws StatusException{
70         try {
71             oObj.registerObject("MyFactory", regObject) ;
72         } catch (com.sun.star.uno.Exception e) {
73             log.println("Exception while registering object :" + e) ;
74             tRes.tested("registerObject()", false) ;
75             return ;
76         }
77 
78         tRes.tested("registerObject()", true) ;
79     }
80 
81     /**
82     * Test calls the method and checks return value and that
83     * no exceptions were thrown. <p>
84     * Has <b> OK </b> status if the method returns the same object
85     * that was registered and no exceptions were thrown. <p>
86     * The following method tests are to be completed successfully before :
87     * <ul>
88     *  <li> <code> registerObject </code> : to get in this test the
89     *    object that was registered.</li>
90     * </ul>
91     */
_getRegisteredObject()92     public void _getRegisteredObject() {
93         requiredMethod("registerObject()") ;
94 
95         try {
96             Object getObject = oObj.getRegisteredObject("MyFactory") ;
97 
98             tRes.tested("getRegisteredObject()" ,
99                 regObject.equals(getObject)) ;
100         } catch (com.sun.star.uno.Exception e) {
101             log.println("Exception calling method :" + e) ;
102             tRes.tested("getRegisteredObject()", false) ;
103             return ;
104         }
105     }
106 
107     /**
108     * Test calls the method and trying to get revoked object. <p>
109     * Has <b> OK </b> status if the method successfully returns
110     * and if the method <code>getRegisteredObject</code> returns NULL or
111     * throws expected exception. <p>
112     * The following method tests are to be completed successfully before :
113     * <ul>
114     *  <li> <code> registerObject </code> : to revoke the object registered</li>
115     * </ul>
116     * The following method tests are to be executed before :
117     * <ul>
118     *  <li> <code> getRegisteredObject </code> : before object will be
119     *    revoked </li>
120     * </ul>
121     */
_revokeObject()122     public void _revokeObject() {
123         requiredMethod("registerObject()") ;
124         executeMethod("getRegisteredObject()") ;
125 
126         try {
127             oObj.revokeObject("MyFactory");
128             log.println("Object was revoked");
129         } catch (com.sun.star.uno.Exception e) {
130             log.println("Exception revoking object :" + e) ;
131             tRes.tested("revokeObject()", false) ;
132         }
133 
134         boolean res = true;
135 
136         try {
137             log.println("Trying to getRegistered object ...");
138             Object objregObj = oObj.getRegisteredObject("MyFactory");
139             log.println("No exception");
140             res &= objregObj == null;
141             if (res) {
142                 log.println("But NULL was returned");
143             }
144         } catch(com.sun.star.uno.Exception e) {
145             log.println("Expected exception - OK");
146         }
147 
148         tRes.tested("revokeObject()", res);
149     }
150 }
151 
152