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