1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski 
24*b1cdbd2cSJim Jagielski package ifc.ucb;
25*b1cdbd2cSJim Jagielski 
26*b1cdbd2cSJim Jagielski import lib.MultiMethodTest;
27*b1cdbd2cSJim Jagielski import lib.Status;
28*b1cdbd2cSJim Jagielski import lib.StatusException;
29*b1cdbd2cSJim Jagielski 
30*b1cdbd2cSJim Jagielski import com.sun.star.lang.XMultiServiceFactory;
31*b1cdbd2cSJim Jagielski import com.sun.star.ucb.ContentProviderInfo;
32*b1cdbd2cSJim Jagielski import com.sun.star.ucb.DuplicateProviderException;
33*b1cdbd2cSJim Jagielski import com.sun.star.ucb.XContentProvider;
34*b1cdbd2cSJim Jagielski import com.sun.star.ucb.XContentProviderManager;
35*b1cdbd2cSJim Jagielski import com.sun.star.uno.Exception;
36*b1cdbd2cSJim Jagielski import com.sun.star.uno.UnoRuntime;
37*b1cdbd2cSJim Jagielski 
38*b1cdbd2cSJim Jagielski /**
39*b1cdbd2cSJim Jagielski * Tests XContentProviderManager. The test registers two ContentProviders, calls
40*b1cdbd2cSJim Jagielski * queryXXX methods to verify results, and deregisters them.
41*b1cdbd2cSJim Jagielski *
42*b1cdbd2cSJim Jagielski * Testing <code>com.sun.star.ucb.XContentProviderManager</code>
43*b1cdbd2cSJim Jagielski * interface methods :
44*b1cdbd2cSJim Jagielski * <ul>
45*b1cdbd2cSJim Jagielski *  <li><code> registerContentProvider()</code></li>
46*b1cdbd2cSJim Jagielski *  <li><code> deregisterContentProvider()</code></li>
47*b1cdbd2cSJim Jagielski *  <li><code> queryContentProviders()</code></li>
48*b1cdbd2cSJim Jagielski *  <li><code> queryContentProvider()</code></li>
49*b1cdbd2cSJim Jagielski * </ul> <p>
50*b1cdbd2cSJim Jagielski * The test registers two ContentProviders, calls
51*b1cdbd2cSJim Jagielski * queryXXX methods to verify results, and deregisters them. <p>
52*b1cdbd2cSJim Jagielski *
53*b1cdbd2cSJim Jagielski * Test is <b> NOT </b> multithread compilant. <p>
54*b1cdbd2cSJim Jagielski * @see com.sun.star.ucb.XContentProviderManager
55*b1cdbd2cSJim Jagielski */
56*b1cdbd2cSJim Jagielski public class _XContentProviderManager extends MultiMethodTest {
57*b1cdbd2cSJim Jagielski     /**
58*b1cdbd2cSJim Jagielski      * Contains the tested object.
59*b1cdbd2cSJim Jagielski      */
60*b1cdbd2cSJim Jagielski     public XContentProviderManager oObj;
61*b1cdbd2cSJim Jagielski 
62*b1cdbd2cSJim Jagielski     /**
63*b1cdbd2cSJim Jagielski      * The test scheme name.
64*b1cdbd2cSJim Jagielski      */
65*b1cdbd2cSJim Jagielski     static final String myScheme = "test-scheme";
66*b1cdbd2cSJim Jagielski 
67*b1cdbd2cSJim Jagielski     /**
68*b1cdbd2cSJim Jagielski      * First content provider. It will be hidden by <code>contentProvider
69*b1cdbd2cSJim Jagielski      * </code>, registred with the same <code>myScheme</code> to test
70*b1cdbd2cSJim Jagielski      * the "hiding" behaviour.
71*b1cdbd2cSJim Jagielski      */
72*b1cdbd2cSJim Jagielski     XContentProvider firstContentProvider;
73*b1cdbd2cSJim Jagielski 
74*b1cdbd2cSJim Jagielski     /**
75*b1cdbd2cSJim Jagielski      * The main content provider.
76*b1cdbd2cSJim Jagielski      */
77*b1cdbd2cSJim Jagielski     XContentProvider contentProvider;
78*b1cdbd2cSJim Jagielski 
79*b1cdbd2cSJim Jagielski     /**
80*b1cdbd2cSJim Jagielski      * <code>ContentProvider</code>s information which are in the manager
81*b1cdbd2cSJim Jagielski      * before registering the testing providers.
82*b1cdbd2cSJim Jagielski      */
83*b1cdbd2cSJim Jagielski     ContentProviderInfo[] initialProvidersInfo;
84*b1cdbd2cSJim Jagielski 
85*b1cdbd2cSJim Jagielski     /**
86*b1cdbd2cSJim Jagielski      * Creates two testing providers.
87*b1cdbd2cSJim Jagielski      *
88*b1cdbd2cSJim Jagielski      * @see #firstContentProvider
89*b1cdbd2cSJim Jagielski      * @see #contentProvider
90*b1cdbd2cSJim Jagielski      */
before()91*b1cdbd2cSJim Jagielski     public void before() {
92*b1cdbd2cSJim Jagielski         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
93*b1cdbd2cSJim Jagielski 
94*b1cdbd2cSJim Jagielski         log.println("creating testing content providers");
95*b1cdbd2cSJim Jagielski         try {
96*b1cdbd2cSJim Jagielski             firstContentProvider = (XContentProvider)UnoRuntime.queryInterface(
97*b1cdbd2cSJim Jagielski                     XContentProvider.class, xMSF.createInstance(
98*b1cdbd2cSJim Jagielski                             "com.sun.star.ucb.FileContentProvider"));
99*b1cdbd2cSJim Jagielski 
100*b1cdbd2cSJim Jagielski             contentProvider = (XContentProvider)UnoRuntime.queryInterface(
101*b1cdbd2cSJim Jagielski                     XContentProvider.class, xMSF.createInstance(
102*b1cdbd2cSJim Jagielski                             "com.sun.star.ucb.FileContentProvider"));
103*b1cdbd2cSJim Jagielski         } catch (Exception e) {
104*b1cdbd2cSJim Jagielski             log.println("Can't create content providers " + e.getMessage());
105*b1cdbd2cSJim Jagielski             e.printStackTrace(log);
106*b1cdbd2cSJim Jagielski             throw new StatusException("Unexpected exception", e);
107*b1cdbd2cSJim Jagielski         }
108*b1cdbd2cSJim Jagielski     }
109*b1cdbd2cSJim Jagielski 
110*b1cdbd2cSJim Jagielski     /**
111*b1cdbd2cSJim Jagielski      * At the beginning call <code>queryContentProviders</code> method
112*b1cdbd2cSJim Jagielski      *
113*b1cdbd2cSJim Jagielski      * to have info about providers existing before new adding.
114*b1cdbd2cSJim Jagielski      * It adds two testing contents providers, both for the same scheme.
115*b1cdbd2cSJim Jagielski      * The second one is added two times: first, in non-replacing mode, to test
116*b1cdbd2cSJim Jagielski      * <code>DuplicateProviderException</code>, and second, in replacing mode,
117*b1cdbd2cSJim Jagielski      * to hide the first provider. <p>
118*b1cdbd2cSJim Jagielski      *
119*b1cdbd2cSJim Jagielski      * The evaluation of results are performed later, in
120*b1cdbd2cSJim Jagielski      * <code>queryContentProvider()</code>.
121*b1cdbd2cSJim Jagielski      *
122*b1cdbd2cSJim Jagielski      * Has <b> OK </b> status if in the first provider is registered
123*b1cdbd2cSJim Jagielski      * without exceptions, the second throws
124*b1cdbd2cSJim Jagielski      * <code>DuplicateProviderException</code> in non-replacing mode,
125*b1cdbd2cSJim Jagielski      * and no exceptions in replacing mode. <p>
126*b1cdbd2cSJim Jagielski      *
127*b1cdbd2cSJim Jagielski      * @see #_queryContentProvider
128*b1cdbd2cSJim Jagielski      */
_registerContentProvider()129*b1cdbd2cSJim Jagielski     public void _registerContentProvider() {
130*b1cdbd2cSJim Jagielski         // querying providfers info before inserting them, to verify results
131*b1cdbd2cSJim Jagielski         initialProvidersInfo = oObj.queryContentProviders();
132*b1cdbd2cSJim Jagielski 
133*b1cdbd2cSJim Jagielski         log.println("registering the first provider");
134*b1cdbd2cSJim Jagielski         try {
135*b1cdbd2cSJim Jagielski             oObj.registerContentProvider(firstContentProvider, myScheme,false);
136*b1cdbd2cSJim Jagielski         } catch (DuplicateProviderException e) {
137*b1cdbd2cSJim Jagielski             log.println("Unexpected exception thrown " + e.getMessage());
138*b1cdbd2cSJim Jagielski             e.printStackTrace(log);
139*b1cdbd2cSJim Jagielski             throw new StatusException("Unexpected exception ", e);
140*b1cdbd2cSJim Jagielski         }
141*b1cdbd2cSJim Jagielski 
142*b1cdbd2cSJim Jagielski         log.println("registering the second provider in non-replacing mode");
143*b1cdbd2cSJim Jagielski         try {
144*b1cdbd2cSJim Jagielski             oObj.registerContentProvider(contentProvider, myScheme, false);
145*b1cdbd2cSJim Jagielski             Status.failed("registerContentProvider(.., .., false)");
146*b1cdbd2cSJim Jagielski         } catch (DuplicateProviderException e) {
147*b1cdbd2cSJim Jagielski             log.println("DuplicateProviderException thrown - OK");
148*b1cdbd2cSJim Jagielski         }
149*b1cdbd2cSJim Jagielski 
150*b1cdbd2cSJim Jagielski         XContentProvider result;
151*b1cdbd2cSJim Jagielski 
152*b1cdbd2cSJim Jagielski         log.println("registering the second provider in the replace mode");
153*b1cdbd2cSJim Jagielski         try {
154*b1cdbd2cSJim Jagielski             result = oObj.registerContentProvider(contentProvider, myScheme, true);
155*b1cdbd2cSJim Jagielski         } catch (DuplicateProviderException e) {
156*b1cdbd2cSJim Jagielski             log.println("Unexpected exception thrown " + e.getMessage());
157*b1cdbd2cSJim Jagielski             e.printStackTrace(log);
158*b1cdbd2cSJim Jagielski             throw new StatusException("Unexpected exception ", e);
159*b1cdbd2cSJim Jagielski         }
160*b1cdbd2cSJim Jagielski 
161*b1cdbd2cSJim Jagielski         // check the result is the first provider
162*b1cdbd2cSJim Jagielski         tRes.tested("registerContentProvider()",
163*b1cdbd2cSJim Jagielski             result.equals(firstContentProvider));
164*b1cdbd2cSJim Jagielski     }
165*b1cdbd2cSJim Jagielski 
166*b1cdbd2cSJim Jagielski     /**
167*b1cdbd2cSJim Jagielski      * It calls the method (after registering providers) and compares
168*b1cdbd2cSJim Jagielski      * its result with the result  before registering.
169*b1cdbd2cSJim Jagielski      *
170*b1cdbd2cSJim Jagielski      * Has <b> OK </b> status if the number of providers increases
171*b1cdbd2cSJim Jagielski      * by one after registering custom provider.
172*b1cdbd2cSJim Jagielski      *
173*b1cdbd2cSJim Jagielski      * The following method tests are to be completed successfully before :
174*b1cdbd2cSJim Jagielski      * <ul>
175*b1cdbd2cSJim Jagielski      *  <li> <code> registerContentProvider() </code> : to compare number
176*b1cdbd2cSJim Jagielski      *  of providers. </li>
177*b1cdbd2cSJim Jagielski      * </ul>
178*b1cdbd2cSJim Jagielski      * @see #_registerContentProvider
179*b1cdbd2cSJim Jagielski      */
_queryContentProviders()180*b1cdbd2cSJim Jagielski     public void _queryContentProviders() {
181*b1cdbd2cSJim Jagielski         executeMethod("registerContentProvider()");
182*b1cdbd2cSJim Jagielski 
183*b1cdbd2cSJim Jagielski         ContentProviderInfo[] providersInfo = oObj.queryContentProviders();
184*b1cdbd2cSJim Jagielski         // verifying that the number of providers increased by 1
185*b1cdbd2cSJim Jagielski         tRes.tested("queryContentProviders()",
186*b1cdbd2cSJim Jagielski                 providersInfo.length == initialProvidersInfo.length + 1);
187*b1cdbd2cSJim Jagielski     }
188*b1cdbd2cSJim Jagielski 
189*b1cdbd2cSJim Jagielski     /**
190*b1cdbd2cSJim Jagielski      * It queries for added custom provider using its scheme
191*b1cdbd2cSJim Jagielski      * and verifies its result with
192*b1cdbd2cSJim Jagielski      * <code>queryContentProviders()</code> result and with
193*b1cdbd2cSJim Jagielski      * custom provider created in <code>registerContentProvider()</code>.
194*b1cdbd2cSJim Jagielski      * Also verifies <code>registerContentProvider()</code>. <p>
195*b1cdbd2cSJim Jagielski      *
196*b1cdbd2cSJim Jagielski      * Has <b>OK</b> status if the provider returned is found within
197*b1cdbd2cSJim Jagielski      * all providers and is equal to provider created before.
198*b1cdbd2cSJim Jagielski      *
199*b1cdbd2cSJim Jagielski      * The following method tests are to be completed successfully before :
200*b1cdbd2cSJim Jagielski      * <ul>
201*b1cdbd2cSJim Jagielski      *  <li> <code> registerContentProvider() </code> </li>
202*b1cdbd2cSJim Jagielski      * </ul>
203*b1cdbd2cSJim Jagielski      */
_queryContentProvider()204*b1cdbd2cSJim Jagielski     public void _queryContentProvider() {
205*b1cdbd2cSJim Jagielski         executeMethod("registerContentProvider()");
206*b1cdbd2cSJim Jagielski 
207*b1cdbd2cSJim Jagielski         XContentProvider result = oObj.queryContentProvider
208*b1cdbd2cSJim Jagielski             ("http://www.sun.com");
209*b1cdbd2cSJim Jagielski 
210*b1cdbd2cSJim Jagielski         log.println("finding queryContentProvider() result");
211*b1cdbd2cSJim Jagielski         boolean found = false;
212*b1cdbd2cSJim Jagielski 
213*b1cdbd2cSJim Jagielski         ContentProviderInfo[] providersInfo = oObj.queryContentProviders();
214*b1cdbd2cSJim Jagielski 
215*b1cdbd2cSJim Jagielski         for (int i = 0; i < providersInfo.length; i++) {
216*b1cdbd2cSJim Jagielski             if (result.equals(providersInfo[i].ContentProvider)
217*b1cdbd2cSJim Jagielski                     /*&& myScheme.equals(providersInfo[i].Scheme)*/) {
218*b1cdbd2cSJim Jagielski                 found = true;
219*b1cdbd2cSJim Jagielski                 break;
220*b1cdbd2cSJim Jagielski             }
221*b1cdbd2cSJim Jagielski         }
222*b1cdbd2cSJim Jagielski 
223*b1cdbd2cSJim Jagielski //        boolean gotTheRightOne = util.ValueComparer.equalValue
224*b1cdbd2cSJim Jagielski //            (result,contentProvider);
225*b1cdbd2cSJim Jagielski 
226*b1cdbd2cSJim Jagielski         tRes.tested("queryContentProvider()", found);
227*b1cdbd2cSJim Jagielski 
228*b1cdbd2cSJim Jagielski //        gotTheRightOne = result.equals(contentProvider);
229*b1cdbd2cSJim Jagielski 
230*b1cdbd2cSJim Jagielski     }
231*b1cdbd2cSJim Jagielski 
232*b1cdbd2cSJim Jagielski     /**
233*b1cdbd2cSJim Jagielski      * At first one provider is deregistered, after that provider
234*b1cdbd2cSJim Jagielski      * is queried, the second provider must be returned for the
235*b1cdbd2cSJim Jagielski      * specified scheme. Then the second provider is deregistered.
236*b1cdbd2cSJim Jagielski      * Now <code>null</code> value must be retruned by the method
237*b1cdbd2cSJim Jagielski      * <code>queryContentProvider</code> on the specified scheme. <p>
238*b1cdbd2cSJim Jagielski      *
239*b1cdbd2cSJim Jagielski      * Has <b>OK</b> status if in the first case the second provider
240*b1cdbd2cSJim Jagielski      * remains registered, and after its removing no providers remain
241*b1cdbd2cSJim Jagielski      * registered for the scheme specified.
242*b1cdbd2cSJim Jagielski      *
243*b1cdbd2cSJim Jagielski      * The following method tests are to be completed successfully before :
244*b1cdbd2cSJim Jagielski      * <ul>
245*b1cdbd2cSJim Jagielski      *  <li> <code> registerContentProvider() </code> : two providers
246*b1cdbd2cSJim Jagielski      *  must be registered. </li>
247*b1cdbd2cSJim Jagielski      * </ul>
248*b1cdbd2cSJim Jagielski      * The following method tests are to be executed before :
249*b1cdbd2cSJim Jagielski      * <ul>
250*b1cdbd2cSJim Jagielski      *  <li> <code> queryContentProvider() </code> : to run this test
251*b1cdbd2cSJim Jagielski      *   finally. </li>
252*b1cdbd2cSJim Jagielski      *  <li> <code> queryContentProviders() </code> : to run this test
253*b1cdbd2cSJim Jagielski      *   finally.  </li>
254*b1cdbd2cSJim Jagielski      * </ul>
255*b1cdbd2cSJim Jagielski      */
_deregisterContentProvider()256*b1cdbd2cSJim Jagielski     public void _deregisterContentProvider() {
257*b1cdbd2cSJim Jagielski         executeMethod("queryContentProvider()");
258*b1cdbd2cSJim Jagielski         executeMethod("queryContentProviders()");
259*b1cdbd2cSJim Jagielski         requiredMethod("registerContentProvider()");
260*b1cdbd2cSJim Jagielski 
261*b1cdbd2cSJim Jagielski         log.println("deregistering the second provider");
262*b1cdbd2cSJim Jagielski         oObj.deregisterContentProvider(contentProvider, myScheme);
263*b1cdbd2cSJim Jagielski 
264*b1cdbd2cSJim Jagielski         XContentProvider res = oObj.queryContentProvider(myScheme);
265*b1cdbd2cSJim Jagielski 
266*b1cdbd2cSJim Jagielski         log.println("deregistering the first provider");
267*b1cdbd2cSJim Jagielski         oObj.deregisterContentProvider(firstContentProvider, myScheme);
268*b1cdbd2cSJim Jagielski 
269*b1cdbd2cSJim Jagielski         res = oObj.queryContentProvider(myScheme);
270*b1cdbd2cSJim Jagielski 
271*b1cdbd2cSJim Jagielski         // verifying that no provider is returned
272*b1cdbd2cSJim Jagielski         tRes.tested("deregisterContentProvider()", res == null);
273*b1cdbd2cSJim Jagielski     }
274*b1cdbd2cSJim Jagielski }
275