xref: /AOO42X/main/ucb/qa/complex/tdoc/CheckTransientDocumentsContentProvider.java (revision 831a3cf2bfe69e32910b4418826cb004e450f0da)
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 
22cdf0e10cSrcweir package complex.tdoc;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
25cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
26cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet;
27cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
28cdf0e10cSrcweir import com.sun.star.ucb.Command;
29cdf0e10cSrcweir import com.sun.star.ucb.OpenCommandArgument2;
30cdf0e10cSrcweir import com.sun.star.ucb.OpenMode;
31cdf0e10cSrcweir import com.sun.star.ucb.XCommandProcessor;
32cdf0e10cSrcweir import com.sun.star.ucb.XContent;
33cdf0e10cSrcweir import com.sun.star.ucb.XContentAccess;
34cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifier;
35cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifierFactory;
36cdf0e10cSrcweir import com.sun.star.ucb.XContentProvider;
37cdf0e10cSrcweir import com.sun.star.ucb.XDynamicResultSet;
38cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
39cdf0e10cSrcweir // import complexlib.ComplexTestCase;
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 
getTestMethodNames()60cdf0e10cSrcweir     public String[] getTestMethodNames() {
61cdf0e10cSrcweir         return new String[]{"checkTransientDocumentsContentProvider"};
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
before()64cdf0e10cSrcweir     @Before public void before() {
65cdf0e10cSrcweir         xMSF = getMSF();
66cdf0e10cSrcweir         xTextDoc = new XTextDocument[countDocs];
67cdf0e10cSrcweir         System.out.println("Open some documents.");
68cdf0e10cSrcweir         for (int i=0; i<countDocs; i++) {
69cdf0e10cSrcweir             String fileName = TestDocument.getUrl(testDocuments[i]);
70cdf0e10cSrcweir             xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName);
71cdf0e10cSrcweir             assertNotNull("Can't load document " + fileName, xTextDoc[i]);
72cdf0e10cSrcweir         }
73cdf0e10cSrcweir     }
after()74cdf0e10cSrcweir     @After public void after() {
75cdf0e10cSrcweir         System.out.println("Close all documents.");
76cdf0e10cSrcweir         for (int i=0; i<countDocs; i++) {
77cdf0e10cSrcweir             xTextDoc[i].dispose();
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     /**
82cdf0e10cSrcweir      * Check the provider of document content: open some documents
83cdf0e10cSrcweir      * and look if they are accessible.
84cdf0e10cSrcweir      */
checkTransientDocumentsContentProvider()85cdf0e10cSrcweir     @Test public void checkTransientDocumentsContentProvider() {
86cdf0e10cSrcweir         try {
87cdf0e10cSrcweir             // create a content provider
88cdf0e10cSrcweir             Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
89cdf0e10cSrcweir             XContentProvider xContentProvider =
90cdf0e10cSrcweir                             UnoRuntime.queryInterface(XContentProvider.class, o);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir             // create the ucb
93cdf0e10cSrcweir             XContentIdentifierFactory xContentIdentifierFactory =
94cdf0e10cSrcweir                             UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
95cdf0e10cSrcweir             // create a content identifier from the ucb for tdoc
96cdf0e10cSrcweir             XContentIdentifier xContentIdentifier =
97cdf0e10cSrcweir                             xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/");
98cdf0e10cSrcweir             // get content
99cdf0e10cSrcweir             XContent xContent = xContentProvider.queryContent(xContentIdentifier);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir             // actual test: execute an "open" command with the content
102cdf0e10cSrcweir             XCommandProcessor xCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
103cdf0e10cSrcweir             // build up the command
104cdf0e10cSrcweir             Command command = new Command();
105cdf0e10cSrcweir             OpenCommandArgument2 commandarg2 = new OpenCommandArgument2();
106cdf0e10cSrcweir             commandarg2.Mode = OpenMode.ALL;
107cdf0e10cSrcweir             command.Name = "open";
108cdf0e10cSrcweir             command.Argument = commandarg2;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir             // execute the command
111cdf0e10cSrcweir             Object result = xCommandProcessor.execute(command, 0, null);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir             // check the result
114cdf0e10cSrcweir             System.out.println("Result: "+ result.getClass().toString());
115cdf0e10cSrcweir             XDynamicResultSet xDynamicResultSet = UnoRuntime.queryInterface(XDynamicResultSet.class, result);
116cdf0e10cSrcweir 
117cdf0e10cSrcweir             // check bug of wrong returned service name.
118cdf0e10cSrcweir             XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, xDynamicResultSet);
119cdf0e10cSrcweir             String[] sNames = xServiceInfo.getSupportedServiceNames();
120cdf0e10cSrcweir             String serviceName = sNames[0];
121cdf0e10cSrcweir             if (sNames.length > 1)
122cdf0e10cSrcweir             {
123cdf0e10cSrcweir                 fail("Implementation has been changed. Check this test!");
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir             assertTrue("The service name '" + serviceName + "' is not valid.", !serviceName.equals("com.sun.star.ucb.DynamicContentResultSet"));
126cdf0e10cSrcweir 
127cdf0e10cSrcweir             XResultSet xResultSet = xDynamicResultSet.getStaticResultSet();
128cdf0e10cSrcweir             XContentAccess xContentAccess = UnoRuntime.queryInterface(XContentAccess.class, xResultSet);
129cdf0e10cSrcweir             // iterate over the result: three docs were opened, we should have at least three content identifier strings
130cdf0e10cSrcweir             int countContentIdentifiers = 0;
131cdf0e10cSrcweir             while(xResultSet.next()) {
132cdf0e10cSrcweir                 countContentIdentifiers++;
133cdf0e10cSrcweir                 String identifier = xContentAccess.queryContentIdentifierString();
134cdf0e10cSrcweir                 System.out.println("Identifier of row " + xResultSet.getRow() + ": " + identifier);
135cdf0e10cSrcweir             }
136*888f552bSmseidel             // some feeble test: if the amount >2, we're ok.
137*888f552bSmseidel             // TODO: check better
138cdf0e10cSrcweir             assertTrue("Did only find " + countContentIdentifiers + " open documents." +
139cdf0e10cSrcweir                         " Should have been at least 3.", countContentIdentifiers>2);
140cdf0e10cSrcweir         }
141cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e) {
142cdf0e10cSrcweir             e.printStackTrace();
143cdf0e10cSrcweir             fail("Could not create test objects.");
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
getMSF()148cdf0e10cSrcweir      private XMultiServiceFactory getMSF()
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
151cdf0e10cSrcweir         return xMSF1;
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     // setup and close connections
setUpConnection()155cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
156cdf0e10cSrcweir         System.out.println("setUpConnection()");
157cdf0e10cSrcweir         connection.setUp();
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
tearDownConnection()160cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
161cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         System.out.println("tearDownConnection()");
164cdf0e10cSrcweir         connection.tearDown();
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir }
170