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