2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import lib
.MultiMethodTest
;
23 import lib
.StatusException
;
25 import com
.sun
.star
.lang
.XMultiServiceFactory
;
26 import com
.sun
.star
.ucb
.ContentProviderInfo
;
27 import com
.sun
.star
.ucb
.DuplicateProviderException
;
28 import com
.sun
.star
.ucb
.XContentProvider
;
29 import com
.sun
.star
.ucb
.XContentProviderManager
;
30 import com
.sun
.star
.uno
.Exception
;
31 import com
.sun
.star
.uno
.UnoRuntime
;
34 * Tests XContentProviderManager. The test registers two ContentProviders, calls
35 * queryXXX methods to verify results, and deregisters them.
37 * Testing <code>com.sun.star.ucb.XContentProviderManager</code>
40 * <li><code> registerContentProvider()</code></li>
41 * <li><code> deregisterContentProvider()</code></li>
42 * <li><code> queryContentProviders()</code></li>
43 * <li><code> queryContentProvider()</code></li>
45 * The test registers two ContentProviders, calls
46 * queryXXX methods to verify results, and deregisters them. <p>
48 * Test is <b> NOT </b> multithread compilant. <p>
49 * @see com.sun.star.ucb.XContentProviderManager
51 public class _XContentProviderManager
extends MultiMethodTest
{
53 * Contains the tested object.
55 public XContentProviderManager oObj
;
58 * The test scheme name.
60 static final String myScheme
= "test-scheme";
63 * Any preexisting content provider. If it exists it will be hidden by
64 * <code>firstContentProvider</code>, registered with the same
65 * <code>myScheme</code>. Typically there is no preexisting content
66 * provider, unless the catch-all providers GnomeVFSContentProvider or
67 * GIOContentProvider is installed
69 XContentProvider preexistingContentProvider
;
72 * First content provider. It will be hidden by <code>contentProvider
73 * </code>, registered with the same <code>myScheme</code> to test
74 * the "hiding" behaviour.
76 XContentProvider firstContentProvider
;
79 * The main content provider.
81 XContentProvider contentProvider
;
84 * <code>ContentProvider</code>s information which are in the manager
85 * before registering the testing providers.
87 ContentProviderInfo
[] initialProvidersInfo
;
90 * Creates two testing providers.
92 * @see #firstContentProvider
93 * @see #contentProvider
95 public void before() {
96 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)tParam
.getMSF();
98 log
.println("creating testing content providers");
100 firstContentProvider
= UnoRuntime
.queryInterface(
101 XContentProvider
.class, xMSF
.createInstance(
102 "com.sun.star.ucb.FileContentProvider"));
104 contentProvider
= UnoRuntime
.queryInterface(
105 XContentProvider
.class, xMSF
.createInstance(
106 "com.sun.star.ucb.FileContentProvider"));
107 } catch (Exception e
) {
108 log
.println("Can't create content providers " + e
.getMessage());
109 e
.printStackTrace(log
);
110 throw new StatusException("Unexpected exception", e
);
115 * At the beginning call <code>queryContentProviders</code> method
117 * to have info about providers existing before new adding.
118 * It adds two testing contents providers, both for the same scheme.
119 * The second one is added two times: first, in non-replacing mode, to test
120 * <code>DuplicateProviderException</code>, and second, in replacing mode,
121 * to hide the first provider. <p>
123 * The evaluation of results are performed later, in
124 * <code>queryContentProvider()</code>.
126 * Has <b> OK </b> status if in the first provider is registered
127 * without exceptions, the second throws
128 * <code>DuplicateProviderException</code> in non-replacing mode,
129 * and no exceptions in replacing mode. <p>
131 * @see #_queryContentProvider
133 public void _registerContentProvider() {
134 // querying providfers info before inserting them, to verify results
135 initialProvidersInfo
= oObj
.queryContentProviders();
137 // GnomeVFSContentProvider or GIOContentProvider ?, typically null
138 preexistingContentProvider
= oObj
.queryContentProvider(myScheme
);
140 log
.println("registering the first provider");
142 oObj
.registerContentProvider(firstContentProvider
, myScheme
,false);
143 } catch (DuplicateProviderException e
) {
144 log
.println("Unexpected exception thrown " + e
.getMessage());
145 e
.printStackTrace(log
);
146 throw new StatusException("Unexpected exception ", e
);
149 log
.println("registering the second provider in non-replacing mode");
151 oObj
.registerContentProvider(contentProvider
, myScheme
, false);
152 Status
.failed("registerContentProvider(.., .., false)");
153 } catch (DuplicateProviderException e
) {
154 log
.println("DuplicateProviderException thrown - OK");
157 XContentProvider result
;
159 log
.println("registering the second provider in the replace mode");
161 result
= oObj
.registerContentProvider(contentProvider
, myScheme
, true);
162 } catch (DuplicateProviderException e
) {
163 log
.println("Unexpected exception thrown " + e
.getMessage());
164 e
.printStackTrace(log
);
165 throw new StatusException("Unexpected exception ", e
);
168 // check the result is the first provider
169 tRes
.tested("registerContentProvider()",
170 result
.equals(firstContentProvider
));
174 * It calls the method (after registering providers) and compares
175 * its result with the result before registering.
177 * Has <b> OK </b> status if the number of providers increases
178 * by one after registering custom provider.
180 * The following method tests are to be completed successfully before :
182 * <li> <code> registerContentProvider() </code> : to compare number
183 * of providers. </li>
185 * @see #_registerContentProvider
187 public void _queryContentProviders() {
188 executeMethod("registerContentProvider()");
190 ContentProviderInfo
[] providersInfo
= oObj
.queryContentProviders();
191 // verifying that the number of providers increased by 1
192 tRes
.tested("queryContentProviders()",
193 providersInfo
.length
== initialProvidersInfo
.length
+ 1);
197 * It queries for added custom provider using its scheme
198 * and verifies its result with
199 * <code>queryContentProviders()</code> result and with
200 * custom provider created in <code>registerContentProvider()</code>.
201 * Also verifies <code>registerContentProvider()</code>. <p>
203 * Has <b>OK</b> status if the provider returned is found within
204 * all providers and is equal to provider created before.
206 * The following method tests are to be completed successfully before :
208 * <li> <code> registerContentProvider() </code> </li>
211 public void _queryContentProvider() {
212 executeMethod("registerContentProvider()");
214 XContentProvider result
= oObj
.queryContentProvider
215 ("http://www.sun.com");
217 log
.println("finding queryContentProvider() result");
218 boolean found
= false;
220 ContentProviderInfo
[] providersInfo
= oObj
.queryContentProviders();
222 for (int i
= 0; i
< providersInfo
.length
; i
++) {
223 if (result
.equals(providersInfo
[i
].ContentProvider
)
224 /*&& myScheme.equals(providersInfo[i].Scheme)*/) {
230 // boolean gotTheRightOne = util.ValueComparer.equalValue
231 // (result,contentProvider);
233 tRes
.tested("queryContentProvider()", found
);
235 // gotTheRightOne = result.equals(contentProvider);
240 * At first one provider is deregistered, after that provider
241 * is queried, the second provider must be returned for the
242 * specified scheme. Then the second provider is deregistered.
243 * Now <code>null</code> value must be retruned by the method
244 * <code>queryContentProvider</code> on the specified scheme. <p>
246 * Has <b>OK</b> status if in the first case the second provider
247 * remains registered, and after its removing no providers remain
248 * registered for the scheme specified.
250 * The following method tests are to be completed successfully before :
252 * <li> <code> registerContentProvider() </code> : two providers
253 * must be registered. </li>
255 * The following method tests are to be executed before :
257 * <li> <code> queryContentProvider() </code> : to run this test
259 * <li> <code> queryContentProviders() </code> : to run this test
263 public void _deregisterContentProvider() {
264 executeMethod("queryContentProvider()");
265 executeMethod("queryContentProviders()");
266 requiredMethod("registerContentProvider()");
268 log
.println("deregistering the second provider");
269 oObj
.deregisterContentProvider(contentProvider
, myScheme
);
271 XContentProvider res
= oObj
.queryContentProvider(myScheme
);
273 log
.println("deregistering the first provider");
274 oObj
.deregisterContentProvider(firstContentProvider
, myScheme
);
276 res
= oObj
.queryContentProvider(myScheme
);
278 // verifying that the original provider (typically none) is returned.
279 tRes
.tested("deregisterContentProvider()", res
== preexistingContentProvider
);