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 
28 package ifc.configuration.backend;
29 
30 import com.sun.star.configuration.backend.XSchema;
31 import com.sun.star.io.XActiveDataSink;
32 import com.sun.star.io.XInputStream;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.ucb.XSimpleFileAccess;
35 import com.sun.star.uno.UnoRuntime;
36 
37 import lib.MultiMethodTest;
38 
39 import util.XSchemaHandlerImpl;
40 
41 public class _XSchema extends MultiMethodTest {
42     public XSchema oObj;
43     XSchemaHandlerImpl xSchemaHandlerImpl = new XSchemaHandlerImpl();
44     String filename = null;
45 
46     protected void before() {
47         filename = (String)tEnv.getObjRelation("ParsedFileName");
48     }
49 
50     public void _readComponent() {
51         requiredMethod("readTemplates()");
52         boolean res = false;
53 
54         log.println("Checking for Exception in case of null argument");
55 
56         try {
57             oObj.readComponent(null);
58         } catch (com.sun.star.lang.NullPointerException e) {
59             log.println("Expected Exception -- OK");
60             res = true;
61         } catch (com.sun.star.lang.WrappedTargetException e) {
62             log.println("Unexpected Exception (" + e + ") -- FAILED");
63         } catch (com.sun.star.configuration.backend.MalformedDataException e) {
64             log.println("Unexpected Exception (" + e + ") -- FAILED");
65         }
66 
67         log.println(
68                 "checking readComponent with own XSchemeHandler implementation");
69 
70         try {
71             xSchemaHandlerImpl.cleanCalls();
72             oObj.readComponent(xSchemaHandlerImpl);
73 
74             String implCalled = xSchemaHandlerImpl.getCalls();
75 
76             System.out.println(implCalled);
77 
78             int sc = implCalled.indexOf("startComponent");
79 
80             if (sc < 0) {
81                 log.println("startComponent wasn't called -- FAILED");
82                 res &= false;
83             } else {
84                 log.println("startComponent was called -- OK");
85                 res &= true;
86             }
87 
88             int ec = implCalled.indexOf("endComponent");
89 
90             if (ec < 0) {
91                 log.println("endComponent wasn't called -- FAILED");
92                 res &= false;
93             } else {
94                 log.println("endComponent was called -- OK");
95                 res &= true;
96             }
97         } catch (com.sun.star.lang.NullPointerException e) {
98             log.println("Unexpected Exception (" + e + ") -- FAILED");
99             res &= false;
100         } catch (com.sun.star.lang.WrappedTargetException e) {
101             log.println("Unexpected Exception (" + e + ") -- FAILED");
102             res &= false;
103         } catch (com.sun.star.configuration.backend.MalformedDataException e) {
104             log.println("Unexpected Exception (" + e + ") -- FAILED");
105             res &= false;
106         }
107 
108         tRes.tested("readComponent()", res);
109         reopenFile();
110     }
111 
112     public void _readSchema() {
113         requiredMethod("readComponent()");
114         boolean res = false;
115 
116         log.println("Checking for Exception in case of null argument");
117 
118         try {
119             xSchemaHandlerImpl.cleanCalls();
120             oObj.readSchema(null);
121         } catch (com.sun.star.lang.NullPointerException e) {
122             log.println("Expected Exception -- OK");
123             res = true;
124         } catch (com.sun.star.lang.WrappedTargetException e) {
125             log.println("Unexpected Exception (" + e + ") -- FAILED");
126         } catch (com.sun.star.configuration.backend.MalformedDataException e) {
127             log.println("Unexpected Exception (" + e + ") -- FAILED");
128         }
129 
130         log.println(
131                 "checking read data with own XSchemeHandler implementation");
132 
133         try {
134             xSchemaHandlerImpl.cleanCalls();
135             oObj.readSchema(xSchemaHandlerImpl);
136 
137             String implCalled = xSchemaHandlerImpl.getCalls();
138 
139             int sc = implCalled.indexOf("startSchema");
140 
141             if (sc < 0) {
142                 log.println("startSchema wasn't called -- FAILED");
143                 res &= false;
144             } else {
145                 log.println("startSchema was called -- OK");
146                 res &= true;
147             }
148 
149             int ec = implCalled.indexOf("endSchema");
150 
151             if (ec < 0) {
152                 log.println("endSchema wasn't called -- FAILED");
153                 res &= false;
154             } else {
155                 log.println("endSchema was called -- OK");
156                 res &= true;
157             }
158         } catch (com.sun.star.lang.NullPointerException e) {
159             log.println("Unexpected Exception (" + e + ") -- FAILED");
160             res &= false;
161         } catch (com.sun.star.lang.WrappedTargetException e) {
162             log.println("Unexpected Exception (" + e + ") -- FAILED");
163             res &= false;
164         } catch (com.sun.star.configuration.backend.MalformedDataException e) {
165             log.println("Unexpected Exception (" + e + ") -- FAILED");
166             res &= false;
167         }
168 
169         // check for the wrapped target exception
170         try {
171             xSchemaHandlerImpl.cleanCalls();
172             oObj.readSchema(xSchemaHandlerImpl);
173         } catch (com.sun.star.lang.NullPointerException e) {
174             log.println("Unexpected Exception (" + e + ") -- FAILED");
175         } catch (com.sun.star.lang.WrappedTargetException e) {
176             log.println("Expected Exception -- OK");
177             res = true;
178         } catch (com.sun.star.configuration.backend.MalformedDataException e) {
179             log.println("Unexpected Exception (" + e + ") -- FAILED");
180         }
181 
182         tRes.tested("readSchema()", res);
183         reopenFile();
184     }
185 
186     public void _readTemplates() {
187         boolean res = false;
188 
189         log.println("Checking for Exception in case of null argument");
190 
191         try {
192             oObj.readTemplates(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(
203                 "checking readComponent with own XSchemeHandler implementation");
204 
205         try {
206             xSchemaHandlerImpl.cleanCalls();
207             oObj.readComponent(xSchemaHandlerImpl);
208 
209             String implCalled = xSchemaHandlerImpl.getCalls();
210 
211             int sc = implCalled.indexOf("startGroup");
212 
213             if (sc < 0) {
214                 log.println("startGroup wasn't called -- FAILED");
215                 res &= false;
216             } else {
217                 log.println("startGroup was called -- OK");
218                 res &= true;
219             }
220 
221             int ec = implCalled.indexOf("endNode");
222 
223             if (ec < 0) {
224                 log.println("endNode wasn't called -- FAILED");
225                 res &= false;
226             } else {
227                 log.println("endNode 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         tRes.tested("readTemplates()", res);
242         reopenFile();
243     }
244 
245     /**
246      * reopen the parsed file again, to avoid the wrapped target exception.
247      */
248     private void reopenFile() {
249         XSimpleFileAccess simpleAccess = null;
250         XInputStream xStream = null;
251         try {
252             Object fileacc = ((XMultiServiceFactory)tParam.getMSF()).createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
253             simpleAccess = (XSimpleFileAccess)
254                             UnoRuntime.queryInterface(XSimpleFileAccess.class,fileacc);
255             log.println("Going to parse: "+filename);
256             xStream = simpleAccess.openFileRead(filename);
257         } catch (com.sun.star.uno.Exception e) {
258         }
259 
260         XActiveDataSink xSink = (XActiveDataSink) UnoRuntime.queryInterface(XActiveDataSink.class, oObj);
261         xSink.setInputStream(xStream);
262     }
263 }