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