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 .
20 import com
.sun
.star
.beans
.Property
;
21 import com
.sun
.star
.beans
.PropertyValue
;
22 import com
.sun
.star
.document
.XDocumentSubStorageSupplier
;
23 import com
.sun
.star
.embed
.ElementModes
;
24 import com
.sun
.star
.embed
.XStorage
;
25 import com
.sun
.star
.frame
.XModel
;
26 import com
.sun
.star
.frame
.XTransientDocumentsDocumentContentFactory
;
27 import com
.sun
.star
.lang
.XMultiServiceFactory
;
28 import com
.sun
.star
.sdbc
.XResultSet
;
29 import com
.sun
.star
.sdbc
.XRow
;
30 import com
.sun
.star
.text
.XTextDocument
;
31 import com
.sun
.star
.ucb
.Command
;
32 import com
.sun
.star
.ucb
.ContentInfo
;
33 import com
.sun
.star
.ucb
.OpenCommandArgument2
;
34 import com
.sun
.star
.ucb
.OpenMode
;
35 import com
.sun
.star
.ucb
.XCommandProcessor
;
36 import com
.sun
.star
.ucb
.XContent
;
37 import com
.sun
.star
.ucb
.XDynamicResultSet
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
39 import util
.WriterTools
;
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
.*;
52 public class CheckTransientDocumentsDocumentContent
{
53 // TODO: document doesn't exists
54 private final String testDocuments
= "sForm.sxw";
55 private final String folderName
= "TestFolder";
56 private XMultiServiceFactory xMSF
= null;
57 private XTextDocument xTextDoc
= null;
59 // public String[] getTestMethodNames() {
60 // return new String[]{"checkTransientDocumentsDocumentContent"};
63 @Before public void before() {
65 System
.out
.println("Open a document.");
66 String fileName
= TestDocument
.getUrl(testDocuments
);
67 xTextDoc
= WriterTools
.loadTextDoc(xMSF
, fileName
);
68 assertNotNull(xTextDoc
);
70 @After public void after() {
71 System
.out
.println("Close all documents.");
76 * Check the provider of document content: open some documents
77 * and look if they are accessible.
79 @Test public void checkTransientDocumentsDocumentContent() {
81 // create a content provider
82 Object o
= xMSF
.createInstance("com.sun.star.comp.ucb.TransientDocumentsDocumentContentFactory");
84 XTransientDocumentsDocumentContentFactory xTransientDocumentsDocumentContentFactory
=
85 UnoRuntime
.queryInterface(XTransientDocumentsDocumentContentFactory
.class, o
);
86 // get the model from the opened document
87 XModel xModel
= xTextDoc
.getCurrentController().getModel();
89 // a little additional check for 114733
90 XDocumentSubStorageSupplier xDocumentSubStorageSupplier
= UnoRuntime
.queryInterface(XDocumentSubStorageSupplier
.class, xModel
);
91 String
[]names
= xDocumentSubStorageSupplier
.getDocumentSubStoragesNames();
92 for (int i
=0; i
<names
.length
; i
++) {
93 System
.out
.println("SubStorage names " + i
+ ": " +names
[i
]);
95 XStorage xStorage
= xDocumentSubStorageSupplier
.getDocumentSubStorage(names
[0], ElementModes
.READWRITE
);
96 assertTrue("Could not get a storage from the XDocumentStorageSupplier.", xStorage
!= null);
98 XContent xContent
= xTransientDocumentsDocumentContentFactory
.createDocumentContent(xModel
);
99 // actual test: execute some commands
100 XCommandProcessor xCommandProcessor
= UnoRuntime
.queryInterface(XCommandProcessor
.class, xContent
);
102 // create the command and arguments
103 Command command
= new Command();
104 OpenCommandArgument2 cmargs2
= new OpenCommandArgument2();
105 Property
[]props
= new Property
[1];
106 props
[0] = new Property();
107 props
[0].Name
= "Title";
108 props
[0].Handle
= -1;
109 cmargs2
.Mode
= OpenMode
.ALL
;
110 cmargs2
.Properties
= props
;
112 command
.Name
= "open";
113 command
.Argument
= cmargs2
;
115 Object result
= xCommandProcessor
.execute(command
, 0, null);
116 XDynamicResultSet xDynamicResultSet
= UnoRuntime
.queryInterface(XDynamicResultSet
.class, result
);
117 XResultSet xResultSet
= xDynamicResultSet
.getStaticResultSet();
118 XRow xRow
= UnoRuntime
.queryInterface(XRow
.class, xResultSet
);
119 // create the new folder 'folderName': first, check if it's already there
120 while(xResultSet
.next()) {
121 String existingFolderName
= xRow
.getString(1);
122 System
.out
.println("Found existing folder: '" + existingFolderName
+ "'");
123 if (folderName
.equals(existingFolderName
)) {
124 fail("Cannot create a new folder: folder already exists: adapt test or choose a different document.");
128 System
.out
.println("Create new folder "+ folderName
);
129 ContentInfo contentInfo
= new ContentInfo();
130 contentInfo
.Type
= "application/vnd.sun.star.tdoc-folder";
132 command
.Name
= "createNewContent";
133 command
.Argument
= contentInfo
;
135 result
= xCommandProcessor
.execute(command
, 0, null);
136 XContent xNewFolder
= UnoRuntime
.queryInterface(XContent
.class, result
);
138 XCommandProcessor xFolderCommandProcessor
= UnoRuntime
.queryInterface(XCommandProcessor
.class, xNewFolder
);
139 System
.out
.println("Got the new folder: " + utils
.getImplName(xNewFolder
));
141 // name the new folder
142 PropertyValue
[] titleProp
= new PropertyValue
[1];
143 titleProp
[0] = new PropertyValue();
144 titleProp
[0].Name
= "Title";
145 titleProp
[0].Handle
= -1;
146 titleProp
[0].Value
= folderName
;
147 Command titleSetCommand
= new Command();
148 titleSetCommand
.Name
= "setPropertyValues";
149 titleSetCommand
.Argument
= titleProp
;
150 xFolderCommandProcessor
.execute(titleSetCommand
, 0, null);
152 // 2do: check all this stuff!
154 /* InsertCommandArgument insertArgs = new InsertCommandArgument();
155 insertArgs.Data = null;
156 insertArgs.ReplaceExisting = true;
157 Command commitCommand = new Command();
158 commitCommand.Name = "insert";
159 commitCommand.Argument = insertArgs;
160 xFolderCommandProcessor.execute(commitCommand, 0, null); */
162 catch (com
.sun
.star
.uno
.Exception e
) {
164 fail("Could not create test objects.");
170 private XMultiServiceFactory
getMSF()
172 final XMultiServiceFactory xMSF1
= UnoRuntime
.queryInterface(XMultiServiceFactory
.class, connection
.getComponentContext().getServiceManager());
176 // setup and close connections
177 @BeforeClass public static void setUpConnection() throws Exception
{
178 System
.out
.println("setUpConnection()");
182 @AfterClass public static void tearDownConnection()
183 throws InterruptedException
, com
.sun
.star
.uno
.Exception
185 System
.out
.println("tearDownConnection()");
186 connection
.tearDown();
189 private static final OfficeConnection connection
= new OfficeConnection();