1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package ifc.configuration.backend;
28 
29 import com.sun.star.configuration.backend.XLayer;
30 import com.sun.star.configuration.backend.XMultiLayerStratum;
31 import com.sun.star.configuration.backend.XUpdatableLayer;
32 import com.sun.star.lang.XMultiServiceFactory;
33 
34 import lib.MultiMethodTest;
35 
36 import util.XLayerHandlerImpl;
37 
38 
39 public class _XMultiLayerStratum extends MultiMethodTest {
40     public XMultiLayerStratum oObj;
41     protected String aLayerID;
42 
43     public void _getLayer() {
44         boolean res = true;
45 
46         try {
47             XLayer aLayer = oObj.getLayer("", "");
48             log.println("Exception expected -- FAILED");
49             res = false;
50         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
51             log.println("unexpected Exception " + e + " -- FAILED");
52             res = false;
53         } catch (com.sun.star.lang.IllegalArgumentException e) {
54             log.println("expected Exception -- OK");
55         }
56 
57         try {
58             XLayer aLayer = oObj.getLayer(aLayerID, "");
59             res &= (aLayer != null);
60 
61             if (aLayer == null) {
62                 log.println("\treturned Layer is NULL -- FAILED");
63             }
64 
65             res &= checkLayer(aLayer);
66         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
67             log.println("unexpected Exception -- FAILED");
68             res = false;
69         } catch (com.sun.star.lang.IllegalArgumentException e) {
70             log.println("unexpected Exception -- FAILED");
71             res = false;
72         }
73 
74         tRes.tested("getLayer()", res);
75     }
76 
77     public void _getLayers() {
78         boolean res = true;
79 
80         try {
81             String[] LayerIds = new String[2];
82             LayerIds[0] = "1 /org/openoffice/Office/Jobs.xcu";
83             LayerIds[1] = "2 /org/openoffice/Office/Linguistic.xcu";
84 
85             XLayer[] Layers = oObj.getLayers(LayerIds, "");
86             res = Layers.length == 2;
87             log.println("Getting two XLayers -- OK");
88             log.println("Checking first on "+LayerIds[0]);
89             res &= checkLayer(Layers[0]);
90             log.println("Checking second on "+LayerIds[1]);
91             res &= checkLayer(Layers[1]);
92         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
93             log.println("unexpected Exception -- FAILED");
94             res = false;
95         } catch (com.sun.star.lang.IllegalArgumentException e) {
96             log.println("unexpected Exception -- FAILED");
97             res = false;
98         }
99 
100         tRes.tested("getLayers()", res);
101     }
102 
103     public void _getMultipleLayers() {
104         boolean res = true;
105 
106         try {
107             String[] LayerIds = new String[2];
108             LayerIds[0] = "1 /org/openoffice/Office/Jobs.xcu";
109             LayerIds[1] = "2 /org/openoffice/Office/Linguistic.xcu";
110             String[] Times = new String[2];
111             Times[0] = "";
112             Times[1] = "";
113 
114             XLayer[] Layers = oObj.getMultipleLayers(LayerIds, Times);
115             res = Layers.length == 2;
116             log.println("Getting two XLayers -- OK");
117             log.println("Checking first on "+LayerIds[0]);
118             res &= checkLayer(Layers[0]);
119             log.println("Checking second on "+LayerIds[1]);
120             res &= checkLayer(Layers[1]);
121         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
122             log.println("unexpected Exception -- FAILED");
123             res = false;
124         } catch (com.sun.star.lang.IllegalArgumentException e) {
125             log.println("unexpected Exception -- FAILED");
126             res = false;
127         }
128 
129         tRes.tested("getMultipleLayers()", res);
130     }
131 
132     public void _getUpdatableLayer() {
133         boolean res = true;
134 
135         try {
136             XUpdatableLayer aLayer = oObj.getUpdatableLayer("");
137             log.println("Exception expected -- FAILED");
138             res = false;
139         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
140             log.println("unexpected Exception " + e + " -- FAILED");
141             res = false;
142         } catch (com.sun.star.lang.IllegalArgumentException e) {
143             log.println("expected Exception -- OK");
144         } catch (com.sun.star.lang.NoSupportException e) {
145             log.println("unexpected Exception -- FAILED");
146             res = false;
147         }
148 
149         try {
150             XUpdatableLayer aLayer = oObj.getUpdatableLayer(aLayerID);
151             res &= (aLayer != null);
152 
153             if (aLayer == null) {
154                 log.println("\treturned Layer is NULL -- FAILED");
155             }
156 
157             res &= checkLayer(aLayer);
158         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
159             log.println("unexpected Exception -- FAILED");
160             res = false;
161         } catch (com.sun.star.lang.IllegalArgumentException e) {
162             log.println("unexpected Exception -- FAILED");
163             res = false;
164         } catch (com.sun.star.lang.NoSupportException e) {
165             log.println("unexpected Exception -- FAILED");
166             res = false;
167         }
168 
169         tRes.tested("getUpdatableLayer()", res);
170     }
171 
172     public void _getUpdateLayerId() {
173         boolean res = true;
174 
175         try {
176             String UpdateLayerID = oObj.getUpdateLayerId(
177                                            "org.openoffice.Office.TypeDetection",
178                                            "illegal");
179             log.println("Exception expected -- FAILED");
180             res = false;
181         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
182             log.println("expected Exception -- OK");
183         } catch (com.sun.star.lang.IllegalArgumentException e) {
184             log.println("unexpected Exception -- FAILED");
185             res = false;
186         } catch (com.sun.star.lang.NoSupportException e) {
187             log.println("unexpected Exception -- FAILED");
188             res = false;
189         }
190 
191         try {
192             String ent = util.utils.getOfficeURL(
193                                  (XMultiServiceFactory) tParam.getMSF()) +
194                          "/../share/registry";
195             String UpdateLayerID = oObj.getUpdateLayerId(
196                                            "org.openoffice.Office.Linguistic", ent);
197             res &= UpdateLayerID.endsWith("Linguistic.xcu");
198 
199             if (!UpdateLayerID.endsWith("Linguistic.xcu")) {
200                 log.println("\tExpected the id to end with Linguistic.xcu");
201                 log.println("\tBut got " + UpdateLayerID);
202                 log.println("\t=> FAILED");
203             }
204         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
205             log.println("unexpected Exception -- FAILED");
206             res = false;
207         } catch (com.sun.star.lang.IllegalArgumentException e) {
208             log.println("unexpected Exception "+e+" -- FAILED");
209             res = false;
210         } catch (com.sun.star.lang.NoSupportException e) {
211             log.println("unexpected Exception -- FAILED");
212             res = false;
213         }
214 
215         tRes.tested("getUpdateLayerId()", res);
216     }
217 
218     public void _listLayerIds() {
219         boolean res = true;
220 
221         try {
222             String[] LayerIDs = oObj.listLayerIds(
223                                         "org.openoffice.Office.TypeDetection",
224                                         "illegal");
225             log.println("Exception expected -- FAILED");
226             res = false;
227         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
228             log.println("expected Exception -- OK");
229         } catch (com.sun.star.lang.IllegalArgumentException e) {
230             log.println("unexpected Exception -- FAILED");
231             res = false;
232         }
233 
234         try {
235             String ent = util.utils.getOfficeURL(
236                                  (XMultiServiceFactory) tParam.getMSF()) +
237                          "/../share/registry";
238             String[] LayerIDs = oObj.listLayerIds("org.openoffice.Office.Jobs",
239                                                   ent);
240             res &= LayerIDs[0].endsWith("Jobs.xcu");
241             aLayerID = LayerIDs[0];
242 
243             if (!LayerIDs[0].endsWith("Jobs.xcu")) {
244                 log.println("\tExpected the id to end with Jobs.xcu");
245                 log.println("\tBut got " + LayerIDs[0]);
246                 log.println("\t=> FAILED");
247             }
248         } catch (com.sun.star.configuration.backend.BackendAccessException e) {
249             log.println("unexpected Exception -- FAILED");
250             res = false;
251         } catch (com.sun.star.lang.IllegalArgumentException e) {
252             log.println("unexpected Exception -- FAILED");
253             res = false;
254         }
255 
256         tRes.tested("listLayerIds()", res);
257     }
258 
259     protected boolean checkLayer(XLayer aLayer) {
260         boolean res = false;
261 
262         log.println("Checking for Exception in case of null argument");
263 
264         try {
265             aLayer.readData(null);
266         } catch (com.sun.star.lang.NullPointerException e) {
267             log.println("Expected Exception -- OK");
268             res = true;
269         } catch (com.sun.star.lang.WrappedTargetException e) {
270             log.println("Unexpected Exception (" + e + ") -- FAILED");
271         } catch (com.sun.star.configuration.backend.MalformedDataException e) {
272             log.println("Unexpected Exception (" + e + ") -- FAILED");
273         }
274 
275         log.println("checking read data with own XLayerHandler implementation");
276 
277         try {
278             XLayerHandlerImpl xLayerHandler = new XLayerHandlerImpl();
279             aLayer.readData(xLayerHandler);
280 
281             String implCalled = xLayerHandler.getCalls();
282             log.println(implCalled);
283 
284             int sl = implCalled.indexOf("startLayer");
285 
286             if (sl < 0) {
287                 log.println("startLayer wasn't called -- FAILED");
288                 res &= false;
289             } else {
290                 log.println("startLayer was called -- OK");
291                 res &= true;
292             }
293 
294             int el = implCalled.indexOf("endLayer");
295 
296             if (el < 0) {
297                 log.println("endLayer wasn't called -- FAILED");
298                 res &= false;
299             } else {
300                 log.println("endLayer was called -- OK");
301                 res &= true;
302             }
303         } catch (com.sun.star.lang.NullPointerException e) {
304             log.println("Unexpected Exception (" + e + ") -- FAILED");
305             res &= false;
306         } catch (com.sun.star.lang.WrappedTargetException e) {
307             log.println("Unexpected Exception (" + e + ") -- FAILED");
308             res &= false;
309         } catch (com.sun.star.configuration.backend.MalformedDataException e) {
310             log.println("Unexpected Exception (" + e + ") -- FAILED");
311             res &= false;
312         }
313 
314         return res;
315     }
316 }