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 mod._dbaccess;
25 
26 import com.sun.star.beans.PropertyValue;
27 import java.io.PrintWriter;
28 
29 import lib.Status;
30 import lib.StatusException;
31 import lib.TestCase;
32 import lib.TestEnvironment;
33 import lib.TestParameters;
34 
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 import com.sun.star.uno.XNamingService;
40 import com.sun.star.frame.XStorable;
41 import com.sun.star.sdb.XDocumentDataSource;
42 import util.utils;
43 
44 /**
45 * Test for object which is represented by service
46 * <code>com.sun.star.sdb.DatabaseContext</code>. <p>
47 *
48 * Object implements the following interfaces :
49 * <ul>
50 *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
51 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
52 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
53 *  <li> <code>com::sun::star::uno::XNamingService</code></li>
54 * </ul>
55 *
56 * @see com.sun.star.container.XNameAccess
57 * @see com.sun.star.container.XEnumerationAccess
58 * @see com.sun.star.container.XElementAccess
59 * @see com.sun.star.uno.XNamingService
60 * @see com.sun.star.sdb.DatabaseContext
61 * @see ifc.container._XNameAccess
62 * @see ifc.container._XEnumerationAccess
63 * @see ifc.container._XElementAccess
64 * @see ifc.uno._XNamingService
65 */
66 public class ODatabaseContext extends TestCase {
67 
68     /**
69     * Does nothing.
70     */
initialize( TestParameters Param, PrintWriter log)71     protected void initialize ( TestParameters Param, PrintWriter log) {
72 
73     }
74 
75     /**
76     * Creating a Testenvironment for the interfaces to be tested.
77     * Creates service <code>com.sun.star.sdb.DatabaseContext</code>.
78     *     Needed object relations :
79     * <ul>
80     * <li> <code>'XNamingService.RegisterObject'</code> for
81     *      {@link ifc.uno._XNamingService} as an
82     *      instance of <code>com.sun.star.sdb.DataSource</code>
83     *      service. </li>
84     * </ul>
85     */
createTestEnvironment(TestParameters Param, PrintWriter log)86     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
87         XInterface oObj = null;
88         Object oInterface = null;
89         XMultiServiceFactory xMSF = null ;
90 
91         try {
92             xMSF = (XMultiServiceFactory)Param.getMSF();
93             oInterface = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
94         }
95         catch( com.sun.star.uno.Exception e ) {
96             log.println("Service not available" );
97             throw new StatusException("Service not available", e) ;
98         }
99 
100         if (oInterface == null) {
101             log.println("Service wasn't created") ;
102             throw new StatusException(Status.failed("Service wasn't created")) ;
103         }
104 
105         oObj = (XInterface) oInterface;
106 
107         log.println( "    creating a new environment for object" );
108         TestEnvironment tEnv = new TestEnvironment( oObj );
109 
110         // adding obj relation for XNamingService
111         try {
112             xMSF = (XMultiServiceFactory)Param.getMSF();
113             oInterface = xMSF.createInstance( "com.sun.star.sdb.DataSource" );
114 
115             XPropertySet xDSProps = (XPropertySet)
116                 UnoRuntime.queryInterface(XPropertySet.class, oInterface) ;
117 
118             xDSProps.setPropertyValue("URL", "sdbc:dbase:file:///.") ;
119 
120             XDocumentDataSource xDDS = (XDocumentDataSource)
121             UnoRuntime.queryInterface(XDocumentDataSource.class, oInterface);
122             XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class,
123             xDDS.getDatabaseDocument ());
124             String aFile = utils.getOfficeTemp ((XMultiServiceFactory) Param.getMSF ())+"DatabaseContext.odb";
125             log.println("store to '" + aFile + "'");
126             store.storeAsURL(aFile,new PropertyValue[]{});
127 
128             tEnv.addObjRelation("XNamingService.RegisterObject", oInterface) ;
129 
130             tEnv.addObjRelation("INSTANCE", oInterface);
131 
132             tEnv.addObjRelation("XContainer.Container",
133                 (XNamingService) UnoRuntime.queryInterface(
134                                     XNamingService.class, oObj));
135 
136         } catch (com.sun.star.uno.Exception e) {
137             throw new StatusException("Can't create object relation", e) ;
138         } catch (NullPointerException e) {
139             throw new StatusException("Can't create object relation", e) ;
140         }
141 
142         return tEnv;
143     } // finish method getTestEnvironment
144 
145 }
146 
147