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