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