Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / ucb / qa / complex / tdoc / CheckTransientDocumentsContentProvider.java
blob6697709180c2bc7e9f4570b14a1a51d04b574945
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package complex.tdoc;
20 import com.sun.star.lang.XMultiServiceFactory;
21 import com.sun.star.lang.XServiceInfo;
22 import com.sun.star.sdbc.XResultSet;
23 import com.sun.star.text.XTextDocument;
24 import com.sun.star.ucb.Command;
25 import com.sun.star.ucb.OpenCommandArgument2;
26 import com.sun.star.ucb.OpenMode;
27 import com.sun.star.ucb.XCommandProcessor;
28 import com.sun.star.ucb.XContent;
29 import com.sun.star.ucb.XContentAccess;
30 import com.sun.star.ucb.XContentIdentifier;
31 import com.sun.star.ucb.XContentIdentifierFactory;
32 import com.sun.star.ucb.XContentProvider;
33 import com.sun.star.ucb.XDynamicResultSet;
34 import com.sun.star.uno.UnoRuntime;
35 // import complexlib.ComplexTestCase;
36 import util.WriterTools;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.openoffice.test.OfficeConnection;
44 import static org.junit.Assert.*;
46 /**
49 public class CheckTransientDocumentsContentProvider {
50 // TODO: document doesn't exists
51 private final String testDocuments[] = new String[]{/*"sForm.sxw",*/ "chinese.sxw", "Iterator.sxw"};
52 private final int countDocs = testDocuments.length;
53 private XMultiServiceFactory xMSF = null;
54 private XTextDocument[] xTextDoc = null;
56 public String[] getTestMethodNames() {
57 return new String[]{"checkTransientDocumentsContentProvider"};
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]);
70 @After public void after() {
71 System.out.println("Close all documents.");
72 for (int i=0; i<countDocs; i++) {
73 xTextDoc[i].dispose();
77 /**
78 * Check the provider of document content: open some documents
79 * and look if they are accessible.
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);
88 // create unconfigured 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);
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;
106 // execute the command
107 Object result = xCommandProcessor.execute(command, 0, null);
109 // check the result
110 System.out.println("Result: "+ result.getClass().toString());
111 XDynamicResultSet xDynamicResultSet = UnoRuntime.queryInterface(XDynamicResultSet.class, result);
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)
119 fail("Implementation has been changed. Check this test!");
121 assertTrue("The service name '" + serviceName + "' is not valid.", !serviceName.equals("com.sun.star.ucb.DynamicContentResultSet"));
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);
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);
137 catch (com.sun.star.uno.Exception e) {
138 e.printStackTrace();
139 fail("Could not create test objects.");
144 private XMultiServiceFactory getMSF()
146 final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
147 return xMSF1;
150 // setup and close connections
151 @BeforeClass public static void setUpConnection() throws Exception {
152 System.out.println("setUpConnection()");
153 connection.setUp();
156 @AfterClass public static void tearDownConnection()
157 throws InterruptedException, com.sun.star.uno.Exception
159 System.out.println("tearDownConnection()");
160 connection.tearDown();
163 private static final OfficeConnection connection = new OfficeConnection();