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