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 package ifc.configuration.backend;
24 
25 import com.sun.star.configuration.backend.XBackend;
26 import com.sun.star.configuration.backend.XLayer;
27 import com.sun.star.configuration.backend.XUpdateHandler;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.util.XStringSubstitution;
31 
32 import lib.MultiMethodTest;
33 
34 import util.XLayerHandlerImpl;
35 
36 
37 public class _XBackend extends MultiMethodTest {
38     public XBackend oObj;
39 
_getOwnUpdateHandler()40     public void _getOwnUpdateHandler() {
41         boolean res = true;
42 
43         String noUpdate = (String) tEnv.getObjRelation("noUpdate");
44 
45         if (noUpdate != null) {
46             log.println(noUpdate);
47             tRes.tested("getOwnUpdateHandler()", res);
48 
49             return;
50         }
51 
52         try {
53             XUpdateHandler aHandler = oObj.getOwnUpdateHandler(
54                                               "org.openoffice.Office.Linguistic");
55             res &= (aHandler != null);
56 
57             if (aHandler == null) {
58                 log.println("\treturned Layer is NULL -- FAILED");
59             }
60         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
61             log.println("unexpected Exception " + e + " -- FAILED");
62             res = false;
63         } catch (com.sun.star.lang.IllegalArgumentException e) {
64             log.println("unexpected Exception " + e + " -- FAILED");
65             res = false;
66         } catch (com.sun.star.lang.NoSupportException e) {
67             log.println("unexpected Exception " + e + " -- FAILED");
68             res = false;
69         }
70 
71         tRes.tested("getOwnUpdateHandler()", res);
72     }
73 
_getUpdateHandler()74     public void _getUpdateHandler() {
75         boolean res = true;
76 
77         String noUpdate = (String) tEnv.getObjRelation("noUpdate");
78 
79         if (noUpdate != null) {
80             log.println(noUpdate);
81             tRes.tested("getUpdateHandler()", res);
82 
83             return;
84         }
85 
86         try {
87             XUpdateHandler aHandler = oObj.getUpdateHandler(
88                                               "org.openoffice.Office.TypeDetection",
89                                               "illegal");
90             log.println("Exception expected -- FAILED");
91             res = false;
92         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
93             log.println("expected Exception -- OK");
94         } catch (com.sun.star.lang.IllegalArgumentException e) {
95             log.println("unexpected Exception -- FAILED");
96             res = false;
97         } catch (com.sun.star.lang.NoSupportException e) {
98             log.println("unexpected Exception " + e + " -- FAILED");
99             res = false;
100         }
101 
102         try {
103             XStringSubstitution sts = createStringSubstitution(
104                                               (XMultiServiceFactory) tParam.getMSF());
105             String ent = sts.getSubstituteVariableValue("$(inst)") +
106                          "/share/registry";
107             XUpdateHandler aHandler = oObj.getUpdateHandler(
108                                               "org.openoffice.Office.Jobs",
109                                               ent);
110 
111             if (aHandler == null) {
112                 log.println("\treturned Layer is NULL -- FAILED");
113             }
114         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
115             log.println("unexpected Exception -- FAILED");
116             res = false;
117         } catch (com.sun.star.lang.IllegalArgumentException e) {
118             log.println("unexpected Exception -- FAILED");
119             res = false;
120         } catch (com.sun.star.lang.NoSupportException e) {
121             log.println("unexpected Exception " + e + " -- FAILED");
122             res = false;
123         } catch (com.sun.star.container.NoSuchElementException e) {
124             log.println("unexpected Exception " + e + " -- FAILED");
125             res = false;
126         }
127 
128         tRes.tested("getUpdateHandler()", res);
129     }
130 
_listLayers()131     public void _listLayers() {
132         boolean res = true;
133 
134         try {
135             XStringSubstitution sts = createStringSubstitution(
136                                               (XMultiServiceFactory) tParam.getMSF());
137             String ent = sts.getSubstituteVariableValue("$(inst)") +
138                          "/share/registry";
139             XLayer[] Layers = oObj.listLayers(
140                                       "org.openoffice.Office.Linguistic", ent);
141 
142             for (int i = 0; i < Layers.length; i++) {
143                 log.println("Checking Layer " + i);
144                 res &= checkLayer(Layers[i]);
145             }
146         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
147             log.println("unexpected Exception " + e + " -- FAILED");
148             res = false;
149         } catch (com.sun.star.lang.IllegalArgumentException e) {
150             log.println("unexpected Exception " + e + " -- FAILED");
151             res = false;
152         } catch (com.sun.star.container.NoSuchElementException e) {
153             log.println("unexpected Exception " + e + " -- FAILED");
154             res = false;
155         }
156 
157         tRes.tested("listLayers()", res);
158     }
159 
_listOwnLayers()160     public void _listOwnLayers() {
161         boolean res = true;
162 
163         try {
164             XLayer[] Layers = oObj.listOwnLayers(
165                                       "org.openoffice.Office.Common");
166 
167             for (int i = 0; i < Layers.length; i++) {
168                 log.println("Checking Layer " + i);
169                 res &= checkLayer(Layers[i]);
170             }
171             if (Layers.length==0) {
172                 System.out.println("No Layers found -- FAILED");
173                 res &= false;
174             }
175         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
176             log.println("unexpected Exception " + e + " -- FAILED");
177             res = false;
178         } catch (com.sun.star.lang.IllegalArgumentException e) {
179             log.println("unexpected Exception " + e + " -- FAILED");
180             res = false;
181         }
182 
183         tRes.tested("listOwnLayers()", res);
184     }
185 
checkLayer(XLayer aLayer)186     protected boolean checkLayer(XLayer aLayer) {
187         boolean res = false;
188 
189         log.println("Checking for Exception in case of null argument");
190 
191         try {
192             aLayer.readData(null);
193         } catch (com.sun.star.lang.NullPointerException e) {
194             log.println("Expected Exception -- OK");
195             res = true;
196         } catch (com.sun.star.lang.WrappedTargetException e) {
197             log.println("Unexpected Exception (" + e + ") -- FAILED");
198         } catch (com.sun.star.configuration.backend.MalformedDataException e) {
199             log.println("Unexpected Exception (" + e + ") -- FAILED");
200         }
201 
202         log.println("checking read data with own XLayerHandler implementation");
203 
204         try {
205             XLayerHandlerImpl xLayerHandler = new XLayerHandlerImpl();
206             aLayer.readData(xLayerHandler);
207 
208             String implCalled = xLayerHandler.getCalls();
209             log.println(implCalled);
210 
211             int sl = implCalled.indexOf("startLayer");
212 
213             if (sl < 0) {
214                 log.println("startLayer wasn't called -- FAILED");
215                 res &= false;
216             } else {
217                 log.println("startLayer was called -- OK");
218                 res &= true;
219             }
220 
221             int el = implCalled.indexOf("endLayer");
222 
223             if (el < 0) {
224                 log.println("endLayer wasn't called -- FAILED");
225                 res &= false;
226             } else {
227                 log.println("endLayer was called -- OK");
228                 res &= true;
229             }
230         } catch (com.sun.star.lang.NullPointerException e) {
231             log.println("Unexpected Exception (" + e + ") -- FAILED");
232             res &= false;
233         } catch (com.sun.star.lang.WrappedTargetException e) {
234             log.println("Unexpected Exception (" + e + ") -- FAILED");
235             res &= false;
236         } catch (com.sun.star.configuration.backend.MalformedDataException e) {
237             log.println("Unexpected Exception (" + e + ") -- FAILED");
238             res &= false;
239         }
240 
241         return res;
242     }
243 
createStringSubstitution(XMultiServiceFactory xMSF)244     public static XStringSubstitution createStringSubstitution(XMultiServiceFactory xMSF) {
245         Object xPathSubst = null;
246 
247         try {
248             xPathSubst = xMSF.createInstance(
249                                  "com.sun.star.util.PathSubstitution");
250         } catch (com.sun.star.uno.Exception e) {
251             e.printStackTrace();
252         }
253 
254         if (xPathSubst != null) {
255             return (XStringSubstitution) UnoRuntime.queryInterface(
256                            XStringSubstitution.class, xPathSubst);
257         } else {
258             return null;
259         }
260     }
261 }