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 package complex.tdoc;
24 
25 import com.sun.star.beans.XPropertiesChangeNotifier;
26 import com.sun.star.beans.XPropertyContainer;
27 import com.sun.star.beans.XPropertySetInfoChangeNotifier;
28 import com.sun.star.container.XChild;
29 import com.sun.star.lang.XComponent;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.lang.XServiceInfo;
32 import com.sun.star.lang.XTypeProvider;
33 import com.sun.star.text.XTextDocument;
34 import com.sun.star.ucb.XCommandInfoChangeNotifier;
35 import com.sun.star.ucb.XCommandProcessor;
36 import com.sun.star.ucb.XContent;
37 import com.sun.star.ucb.XContentIdentifier;
38 import com.sun.star.ucb.XContentIdentifierFactory;
39 import com.sun.star.ucb.XContentProvider;
40 import com.sun.star.uno.UnoRuntime;
41 import util.WriterTools;
42 
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.openoffice.test.OfficeConnection;
49 import static org.junit.Assert.*;
50 
51 /**
52  * Check the TransientDocumentsContentProvider (TDOC). Three documents are
53  * loaded. Then every possible TDCP content type is instantiated and its
54  * interfaces are tested.<br>
55  * Important: opened documents are numbered in the order they are opened and
56  * numbers are not reused. This test will work only, if you start a new office
57  * with an accept parameter (writer is initially opened). Otherwise loaded
58  * documents are not found.
59  */
60 public class CheckContentProvider {
61     private final String testDocuments[] = new String[]{"filter.sxw", "chinese.sxw", "Iterator.sxw"};
62     private final int countDocs = testDocuments.length;
63     private XMultiServiceFactory xMSF = null;
64     private XTextDocument[] xTextDoc = null;
65     private XContent xContent = null;
66 
67     /**
68      * The test methods: the test methods have to be executed in a specified
69      * order. This order is:
70      * <ol>
71      * <li>"checkTDOCRoot"</li>
72      * <li>"checkTDOCRootInterfaces"</li>
73      * <li>"checkTDOCDocument"</li>
74      * <li>"checkTDOCDocumentInterfaces"</li>
75      * <li>"checkTDOCFolder"</li>
76      * <li>"checkTDOCFolderInterfaces"</li>
77      * <li>"checkTDOCStream"</li>
78      * <li>"checkTDOCStreamInterfaces"</li>
79      * </ol>
80      * Important is, that the test of the element comes first, then the test of
81      * its interfaces.
82      **/
83 //    public String[] getTestMethodNames() {
84 //        return new String[]{"checkTDOCRoot",
85 //                            "checkTDOCRootInterfaces",
86 //                            "checkTDOCDocument",
87 //                            "checkTDOCDocumentInterfaces",
88 //                            "checkTDOCFolder",
89 //                            "checkTDOCFolderInterfaces",
90 //                            "checkTDOCStream",
91 //                            "checkTDOCStreamInterfaces",
92 //                            };
93 //    }
94 
95     /**
96      * Open some documents before the test
97      */
before()98     @Before public void before() {
99         xMSF = getMSF();
100         xTextDoc = new XTextDocument[countDocs];
101         System.out.println("Open some new documents.");
102         for (int i=0; i<countDocs; i++) {
103             String fileName = TestDocument.getUrl(testDocuments[i]);
104             System.out.println("Doc " + i + ": " + fileName);
105             xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName);
106             assertNotNull("Can't load document " + fileName, xTextDoc[i]);
107         }
108     }
109 
110     /**
111      * Close the documents
112      */
after()113     @After public void after() {
114         System.out.println("Close all documents.");
115         for (int i=0; i<countDocs; i++) {
116             xTextDoc[i].dispose();
117         }
118     }
119 
120     /**
121      * Check the tdcp root.
122      */
checkTDOCRoot()123     @Test public void checkTDOCRoot() {
124         try {
125             // create a content provider
126             Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
127             XContentProvider xContentProvider =
128                             UnoRuntime.queryInterface(XContentProvider.class, o);
129 
130             // create the ucb
131             XContentIdentifierFactory xContentIdentifierFactory =
132                             UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
133             // create a content identifier from the ucb for tdoc
134             XContentIdentifier xContentIdentifier =
135                             xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/");
136             // get content
137             xContent = xContentProvider.queryContent(xContentIdentifier);
138 
139             String content = xContent.getContentType();
140             System.out.println("#### Content root: " + content);
141 
142             // try to get some documents: should be "countDocs" at least.
143             XContentIdentifier[] xContentId = new XContentIdentifier[countDocs+5];
144             XContent[] xCont = new XContent[countDocs+5];
145 
146             for (int i=0; i<countDocs+5; i++) {
147                 xContentId[i] = xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/" + i);
148                 // get content
149                 xCont[i] = xContentProvider.queryContent(xContentId[i]);
150                 int returnVal = xContentProvider.compareContentIds(xContentId[i], xContentIdentifier);
151                 String cont = null;
152                 if (xCont[i] != null)
153                 {
154                     cont = xCont[i].getContentType();
155                 }
156                 System.out.println("Document Content " + i + ": " + cont + "  compare with root: " + returnVal);
157 
158                 xContentId[i] = xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/" + i + "/content.xml");
159                 // get content
160                 xCont[i] = xContentProvider.queryContent(xContentId[i]);
161                 cont = null;
162                 if (xCont[i] != null)
163                 {
164                     cont = xCont[i].getContentType();
165                 }
166                 System.out.println("\tContent.xml Content " + i + ": " + cont);
167             }
168 
169             util.dbg.printInterfaces(xContent);
170         }
171         catch(Exception e) {
172             e.printStackTrace();
173             fail("Unexpected Exception: " + e.getMessage());
174         }
175     }
176 
177     /**
178      * Check the interfaces of the root.
179      */
checkTDOCRootInterfaces()180     @Test public void checkTDOCRootInterfaces() {
181         checkInterfaces(false);
182     }
183 
184     /**
185      * Check the tdcp document: document 3 is used.
186      */
checkTDOCDocument()187     @Test public void checkTDOCDocument() {
188         try {
189             xContent = null;
190             Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
191             XContentProvider xContentProvider =
192                             UnoRuntime.queryInterface(XContentProvider.class, o);
193             // create the ucb
194             XContentIdentifierFactory xContentIdentifierFactory =
195                             UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
196             // create a content identifier from the ucb for tdoc
197             XContentIdentifier xContentIdentifier =
198                             xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/3");
199             // get content
200             xContent = xContentProvider.queryContent(xContentIdentifier);
201             // assertNotNull(xContent);
202             String content = xContent.getContentType();
203             System.out.println("#### Document root: " + content);
204         }
205         catch(Exception e) {
206             e.printStackTrace();
207             fail("Unexpected Exception: " + e.getMessage());
208         }
209     }
210 
211     /**
212      * Check the interfaces on the document.
213      */
checkTDOCDocumentInterfaces()214     @Test public void checkTDOCDocumentInterfaces() {
215         checkInterfaces(true);
216     }
217 
218     /**
219      * Check a folder on document 2 (document 2 contains an embedded picture and
220      * therefore contans a subfolder "Pictures"
221      */
checkTDOCFolder()222     @Test public void checkTDOCFolder() {
223         try {
224             xContent = null;
225             Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
226             XContentProvider xContentProvider =
227                             UnoRuntime.queryInterface(XContentProvider.class, o);
228             // create the ucb
229             XContentIdentifierFactory xContentIdentifierFactory =
230                             UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
231             // create a content identifier from the ucb for tdoc
232             XContentIdentifier xContentIdentifier =
233                             xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/2/Pictures");
234             // get content
235             xContent = xContentProvider.queryContent(xContentIdentifier);
236 
237             String content = xContent.getContentType();
238             System.out.println("#### Folder type: " + content);
239         }
240         catch(Exception e) {
241             e.printStackTrace();
242             fail("Unexpected Exception: " + e.getMessage());
243         }
244     }
245 
246     /**
247      * Check the interfaces on the folder.
248      */
checkTDOCFolderInterfaces()249     @Test public void checkTDOCFolderInterfaces() {
250         checkInterfaces(true);
251     }
252 
253     /**
254      * Open a stream to the embedded picture of document 1.
255      */
checkTDOCStream()256     @Test public void checkTDOCStream() {
257         try {
258             xContent = null;
259             Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
260             XContentProvider xContentProvider =
261                             UnoRuntime.queryInterface(XContentProvider.class, o);
262 
263             // create the ucb
264             XContentIdentifierFactory xContentIdentifierFactory =
265                             UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
266             // create a content identifier from the ucb for tdoc
267             XContentIdentifier xContentIdentifier =
268                             xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/1/Pictures/10000000000000640000004B9C743800.gif");
269             // get content
270             xContent = xContentProvider.queryContent(xContentIdentifier);
271 
272             String content = xContent.getContentType();
273             System.out.println("#### Folder type: " + content);
274         }
275         catch(Exception e) {
276             e.printStackTrace();
277             fail("Unexpected Exception: " + e.getMessage());
278         }
279     }
280 
281     /**
282      * Check the interfaces on the stream.
283      */
checkTDOCStreamInterfaces()284     @Test public void checkTDOCStreamInterfaces() {
285         checkInterfaces(true);
286     }
287 
288     /**
289      * Since all tdcp content types implement (nearly) the same interfaces, they
290      * are called here.
291      * Executed interface tests are (in this order):
292      * <ol>
293      * <li>XTypeProvider</li>
294      * <li>XServiceInfo</li>
295      * <li>XCommandProcessor</li>
296      * <li>XChild</li>
297      * <li>XPropertiesChangeNotifier</li>
298      * <li>XPropertySetInfoChangeNotifier</li>
299      * <li>XCommandInfoChangeNotifier</li>
300      * <li>XContent</li>
301      * <li>XPropertyContainer</li>
302      * <li>XComponent</li>
303      * </ol>
304      * @param hasParent True, if the tested content type does have a parent:
305      * only the root has not. Used in the XChild interface test.
306      */
checkInterfaces(boolean hasParent)307     private void checkInterfaces(boolean hasParent) {
308         // check the XTypeProvider interface
309         _XTypeProvider xTypeProvider = new _XTypeProvider();
310         xTypeProvider.oObj = UnoRuntime.queryInterface(XTypeProvider.class, xContent);
311         // xTypeProvider.log = log;
312         assertNotNull("getImplementationId()", xTypeProvider._getImplementationId());
313         assertNotNull("getTypes()", xTypeProvider._getTypes());
314 
315         // check the XSewrviceInfo interface
316         _XServiceInfo xServiceInfo = new _XServiceInfo();
317         xServiceInfo.oObj = UnoRuntime.queryInterface(XServiceInfo.class, xContent);
318         // xServiceInfo.log = log;
319         assertNotNull("getImplementationName()", xServiceInfo._getImplementationName());
320         assertNotNull("getSupportedServiceNames()", xServiceInfo._getSupportedServiceNames());
321         assertNotNull("supportsService()", xServiceInfo._supportsService());
322 
323         // check the XCommandProcessor interface
324         _XCommandProcessor xCommandProcessor = new _XCommandProcessor();
325         xCommandProcessor.oObj = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
326         // xCommandProcessor.log = log;
327         xCommandProcessor.before(getMSF());
328         assertNotNull("createCommandIdentifier()", xCommandProcessor._createCommandIdentifier());
329         assertNotNull("execute()", xCommandProcessor._execute());
330         assertNotNull("abort()", xCommandProcessor._abort());
331 
332         // check the XChild interface
333         _XChild xChild = new _XChild();
334         xChild.oObj = UnoRuntime.queryInterface(XChild.class, xContent);
335         // xChild.log = log;
336         // hasParent dermines, if this content has a parent
337         assertNotNull("getParent()", xChild._getParent(hasParent));
338         // parameter does dermine, if this funczion is supported: generally not supported with tdcp content
339         assertNotNull("setParent()", xChild._setParent(false));
340 
341         // check the XPropertyChangeNotifier interface
342         _XPropertiesChangeNotifier xPropChange = new _XPropertiesChangeNotifier();
343         xPropChange.oObj = UnoRuntime.queryInterface(XPropertiesChangeNotifier.class, xContent);
344         // xPropChange.log = log;
345         assertNotNull("addPropertiesChangeListener()", xPropChange._addPropertiesChangeListener());
346 	assertNotNull("removePropertiesChangeListener()", xPropChange._removePropertiesChangeListener());
347 
348         // check the XPropertySetInfoChangeNotifier interface
349         _XPropertySetInfoChangeNotifier xPropSetInfo = new _XPropertySetInfoChangeNotifier();
350         xPropSetInfo.oObj = UnoRuntime.queryInterface(XPropertySetInfoChangeNotifier.class, xContent);
351         // xPropSetInfo.log = log;
352         assertNotNull("addPropertiesChangeListener()", xPropSetInfo._addPropertiesChangeListener());
353 	assertNotNull("removePropertiesChangeListener()", xPropSetInfo._removePropertiesChangeListener());
354 
355         // check the XCommandInfoChangeNotifier interface
356         _XCommandInfoChangeNotifier xCommandChange = new _XCommandInfoChangeNotifier();
357         xCommandChange.oObj = UnoRuntime.queryInterface(XCommandInfoChangeNotifier.class, xContent);
358         // xCommandChange.log = log;
359         assertNotNull("addCommandInfoChangeListener()", xCommandChange._addCommandInfoChangeListener());
360         assertNotNull("removeCommandInfoChangeListener()", xCommandChange._removeCommandInfoChangeListener());
361 
362         // check the XContent interface
363         _XContent xCont = new _XContent();
364         xCont.oObj = UnoRuntime.queryInterface(XContent.class, xContent);
365         // xCont.log = log;
366         assertNotNull("addContentEventListener()", xCont._addContentEventListener());
367         assertNotNull("getContentType()", xCont._getContentType());
368         assertNotNull("getIdentifier()", xCont._getIdentifier());
369         assertNotNull("removeContentEventListener()", xCont._removeContentEventListener());
370 
371         // check the XPropertyContainer interface
372         _XPropertyContainer xPropCont = new _XPropertyContainer();
373         xPropCont.oObj = UnoRuntime.queryInterface(XPropertyContainer.class, xContent);
374         // xPropCont.log = log;
375         assertNotNull("addProperty()", xPropCont._addProperty());
376         assertNotNull("removeProperty()", xPropCont._removeProperty());
377 
378         // check the XComponent interface
379         _XComponent xComponent = new _XComponent();
380         xComponent.oObj = UnoRuntime.queryInterface(XComponent.class, xContent);
381         // xComponent.log = log;
382         assertNotNull("addEventListener()", xComponent._addEventListener());
383         assertNotNull("removeEventListener()", xComponent._removeEventListener());
384 //        assure("dispose()", xComponent._dispose());
385     }
386 
387 
388 
389 
getMSF()390     private XMultiServiceFactory getMSF()
391     {
392         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
393         return xMSF1;
394     }
395 
396     // setup and close connections
setUpConnection()397     @BeforeClass public static void setUpConnection() throws Exception {
398         System.out.println("setUpConnection()");
399         connection.setUp();
400     }
401 
tearDownConnection()402     @AfterClass public static void tearDownConnection()
403         throws InterruptedException, com.sun.star.uno.Exception
404     {
405         System.out.println("tearDownConnection()");
406         connection.tearDown();
407     }
408 
409     private static final OfficeConnection connection = new OfficeConnection();
410 
411 }
412