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