xref: /trunk/main/ucb/qa/complex/tdoc/CheckTransientDocumentsContentProvider.java (revision 5cd1c82638b65a86a575c9871d07bec79d4064b8)
1c07c6c98SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3c07c6c98SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4c07c6c98SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5c07c6c98SAndrew Rist  * distributed with this work for additional information
6c07c6c98SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7c07c6c98SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8c07c6c98SAndrew Rist  * "License"); you may not use this file except in compliance
9c07c6c98SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11c07c6c98SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13c07c6c98SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14c07c6c98SAndrew Rist  * software distributed under the License is distributed on an
15c07c6c98SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16c07c6c98SAndrew Rist  * KIND, either express or implied.  See the License for the
17c07c6c98SAndrew Rist  * specific language governing permissions and limitations
18c07c6c98SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20c07c6c98SAndrew Rist  *************************************************************/
21c07c6c98SAndrew Rist 
22c07c6c98SAndrew Rist 
23cdf0e10cSrcweir package complex.tdoc;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
27cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet;
28cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
29cdf0e10cSrcweir import com.sun.star.ucb.Command;
30cdf0e10cSrcweir import com.sun.star.ucb.OpenCommandArgument2;
31cdf0e10cSrcweir import com.sun.star.ucb.OpenMode;
32cdf0e10cSrcweir import com.sun.star.ucb.XCommandProcessor;
33cdf0e10cSrcweir import com.sun.star.ucb.XContent;
34cdf0e10cSrcweir import com.sun.star.ucb.XContentAccess;
35cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifier;
36cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifierFactory;
37cdf0e10cSrcweir import com.sun.star.ucb.XContentProvider;
38cdf0e10cSrcweir import com.sun.star.ucb.XDynamicResultSet;
39cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40cdf0e10cSrcweir import util.WriterTools;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir import org.junit.After;
43cdf0e10cSrcweir import org.junit.AfterClass;
44cdf0e10cSrcweir import org.junit.Before;
45cdf0e10cSrcweir import org.junit.BeforeClass;
46cdf0e10cSrcweir import org.junit.Test;
47cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
48cdf0e10cSrcweir import static org.junit.Assert.*;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir /**
51cdf0e10cSrcweir  *
52cdf0e10cSrcweir  */
53cdf0e10cSrcweir public class CheckTransientDocumentsContentProvider {
54cdf0e10cSrcweir     // TODO: document doesn't exists
55cdf0e10cSrcweir     private final String testDocuments[] = new String[]{/*"sForm.sxw",*/ "chinese.sxw", "Iterator.sxw"};
56cdf0e10cSrcweir     private final int countDocs = testDocuments.length;
57cdf0e10cSrcweir     private XMultiServiceFactory xMSF = null;
58cdf0e10cSrcweir     private XTextDocument[] xTextDoc = null;
59cdf0e10cSrcweir 
before()60cdf0e10cSrcweir     @Before public void before() {
61cdf0e10cSrcweir         xMSF = getMSF();
62cdf0e10cSrcweir         xTextDoc = new XTextDocument[countDocs];
63cdf0e10cSrcweir         System.out.println("Open some documents.");
64cdf0e10cSrcweir         for (int i=0; i<countDocs; i++) {
65cdf0e10cSrcweir             String fileName = TestDocument.getUrl(testDocuments[i]);
66cdf0e10cSrcweir             xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName);
67cdf0e10cSrcweir             assertNotNull("Can't load document " + fileName, xTextDoc[i]);
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir     }
after()70cdf0e10cSrcweir     @After public void after() {
71cdf0e10cSrcweir         System.out.println("Close all documents.");
72cdf0e10cSrcweir         for (int i=0; i<countDocs; i++) {
73cdf0e10cSrcweir             xTextDoc[i].dispose();
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     /**
78cdf0e10cSrcweir      * Check the provider of document content: open some documents
79cdf0e10cSrcweir      * and look if they are accessible.
80cdf0e10cSrcweir      */
checkTransientDocumentsContentProvider()81cdf0e10cSrcweir     @Test public void checkTransientDocumentsContentProvider() {
82cdf0e10cSrcweir         try {
83cdf0e10cSrcweir             // create a content provider
84cdf0e10cSrcweir             Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
85cdf0e10cSrcweir             XContentProvider xContentProvider =
86cdf0e10cSrcweir                             UnoRuntime.queryInterface(XContentProvider.class, o);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir             // create the ucb
89cdf0e10cSrcweir             XContentIdentifierFactory xContentIdentifierFactory =
90cdf0e10cSrcweir                             UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
91cdf0e10cSrcweir             // create a content identifier from the ucb for tdoc
92cdf0e10cSrcweir             XContentIdentifier xContentIdentifier =
93cdf0e10cSrcweir                             xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/");
94cdf0e10cSrcweir             // get content
95cdf0e10cSrcweir             XContent xContent = xContentProvider.queryContent(xContentIdentifier);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             // actual test: execute an "open" command with the content
98cdf0e10cSrcweir             XCommandProcessor xCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
99cdf0e10cSrcweir             // build up the command
100cdf0e10cSrcweir             Command command = new Command();
101cdf0e10cSrcweir             OpenCommandArgument2 commandarg2 = new OpenCommandArgument2();
102cdf0e10cSrcweir             commandarg2.Mode = OpenMode.ALL;
103cdf0e10cSrcweir             command.Name = "open";
104cdf0e10cSrcweir             command.Argument = commandarg2;
105cdf0e10cSrcweir 
106cdf0e10cSrcweir             // execute the command
107cdf0e10cSrcweir             Object result = xCommandProcessor.execute(command, 0, null);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir             // check the result
110cdf0e10cSrcweir             System.out.println("Result: "+ result.getClass().toString());
111cdf0e10cSrcweir             XDynamicResultSet xDynamicResultSet = UnoRuntime.queryInterface(XDynamicResultSet.class, result);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir             // check bug of wrong returned service name.
114cdf0e10cSrcweir             XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, xDynamicResultSet);
115cdf0e10cSrcweir             String[] sNames = xServiceInfo.getSupportedServiceNames();
116cdf0e10cSrcweir             String serviceName = sNames[0];
117cdf0e10cSrcweir             if (sNames.length > 1)
118cdf0e10cSrcweir             {
119cdf0e10cSrcweir                 fail("Implementation has been changed. Check this test!");
120cdf0e10cSrcweir             }
121cdf0e10cSrcweir             assertTrue("The service name '" + serviceName + "' is not valid.", !serviceName.equals("com.sun.star.ucb.DynamicContentResultSet"));
122cdf0e10cSrcweir 
123cdf0e10cSrcweir             XResultSet xResultSet = xDynamicResultSet.getStaticResultSet();
124cdf0e10cSrcweir             XContentAccess xContentAccess = UnoRuntime.queryInterface(XContentAccess.class, xResultSet);
125cdf0e10cSrcweir             // iterate over the result: three docs were opened, we should have at least three content identifier strings
126cdf0e10cSrcweir             int countContentIdentifiers = 0;
127cdf0e10cSrcweir             while(xResultSet.next()) {
128cdf0e10cSrcweir                 countContentIdentifiers++;
129cdf0e10cSrcweir                 String identifier = xContentAccess.queryContentIdentifierString();
130cdf0e10cSrcweir                 System.out.println("Identifier of row " + xResultSet.getRow() + ": " + identifier);
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir             // some feeble test: if the amount >2, we're ok.
133*5cd1c826Smseidel             // TODO: check better
134cdf0e10cSrcweir             assertTrue("Did only find " + countContentIdentifiers + " open documents." +
135cdf0e10cSrcweir                         " Should have been at least 3.", countContentIdentifiers>2);
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e) {
138cdf0e10cSrcweir             e.printStackTrace();
139cdf0e10cSrcweir             fail("Could not create test objects.");
140cdf0e10cSrcweir         }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
getMSF()144cdf0e10cSrcweir      private XMultiServiceFactory getMSF()
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
147cdf0e10cSrcweir         return xMSF1;
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     // setup and close connections
setUpConnection()151cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
152cdf0e10cSrcweir         System.out.println("setUpConnection()");
153cdf0e10cSrcweir         connection.setUp();
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir 
tearDownConnection()156cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
157cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
158cdf0e10cSrcweir     {
159cdf0e10cSrcweir         System.out.println("tearDownConnection()");
160cdf0e10cSrcweir         connection.tearDown();
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
164cdf0e10cSrcweir 
165cdf0e10cSrcweir }
166