xref: /AOO41X/main/ucb/qa/complex/tdoc/CheckTransientDocumentsDocumentContent.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.tdoc;
28 
29 import com.sun.star.beans.Property;
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.document.XDocumentSubStorageSupplier;
32 import com.sun.star.embed.ElementModes;
33 import com.sun.star.embed.XStorage;
34 import com.sun.star.frame.XModel;
35 import com.sun.star.frame.XTransientDocumentsDocumentContentFactory;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sdbc.XResultSet;
38 import com.sun.star.sdbc.XRow;
39 import com.sun.star.text.XTextDocument;
40 import com.sun.star.ucb.Command;
41 import com.sun.star.ucb.ContentInfo;
42 import com.sun.star.ucb.OpenCommandArgument2;
43 import com.sun.star.ucb.OpenMode;
44 import com.sun.star.ucb.XCommandProcessor;
45 import com.sun.star.ucb.XContent;
46 import com.sun.star.ucb.XDynamicResultSet;
47 import com.sun.star.uno.UnoRuntime;
48 import util.WriterTools;
49 import util.utils;
50 
51 import org.junit.After;
52 import org.junit.AfterClass;
53 import org.junit.Before;
54 import org.junit.BeforeClass;
55 import org.junit.Test;
56 import org.openoffice.test.OfficeConnection;
57 import static org.junit.Assert.*;
58 /**
59  *
60  */
61 public class CheckTransientDocumentsDocumentContent {
62     // TODO: document doesn't exists
63     private final String testDocuments = "sForm.sxw";
64     private final String folderName = "TestFolder";
65     private XMultiServiceFactory xMSF = null;
66     private XTextDocument xTextDoc = null;
67 
68 //    public String[] getTestMethodNames() {
69 //        return new String[]{"checkTransientDocumentsDocumentContent"};
70 //    }
71 
72     @Before public void before() {
73         xMSF = getMSF();
74         System.out.println("Open a document.");
75         String fileName = TestDocument.getUrl(testDocuments);
76         xTextDoc = WriterTools.loadTextDoc(xMSF, fileName);
77         assertNotNull(xTextDoc);
78     }
79     @After public void after() {
80         System.out.println("Close all documents.");
81         xTextDoc.dispose();
82     }
83 
84     /**
85      * Check the provider of document content: open some documents
86      * and look if they are accessible.
87      */
88     @Test public void checkTransientDocumentsDocumentContent() {
89         try {
90             // create a content provider
91             Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsDocumentContentFactory");
92 
93             XTransientDocumentsDocumentContentFactory xTransientDocumentsDocumentContentFactory =
94                             UnoRuntime.queryInterface(XTransientDocumentsDocumentContentFactory.class, o);
95             // get the model from the opened document
96             XModel xModel = xTextDoc.getCurrentController().getModel();
97 
98             // a little additional check for 114733
99             XDocumentSubStorageSupplier xDocumentSubStorageSupplier = UnoRuntime.queryInterface(XDocumentSubStorageSupplier.class, xModel);
100             String[]names = xDocumentSubStorageSupplier.getDocumentSubStoragesNames();
101             for (int i=0; i<names.length; i++) {
102                 System.out.println("SubStorage names " + i + ": " +names[i]);
103             }
104             XStorage xStorage = xDocumentSubStorageSupplier.getDocumentSubStorage(names[0], ElementModes.READWRITE);
105             assertTrue("Could not get a storage from the XDocumentStorageSupplier.", xStorage != null);
106             // get content
107             XContent xContent = xTransientDocumentsDocumentContentFactory.createDocumentContent(xModel);
108             // actual test: execute some commands
109             XCommandProcessor xCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
110 
111             // create the command and arguments
112             Command command = new Command();
113             OpenCommandArgument2 cmargs2 = new OpenCommandArgument2();
114             Property[]props = new Property[1];
115             props[0] = new Property();
116             props[0].Name = "Title";
117             props[0].Handle = -1;
118             cmargs2.Mode = OpenMode.ALL;
119             cmargs2.Properties = props;
120 
121             command.Name = "open";
122             command.Argument = cmargs2;
123 
124             Object result = xCommandProcessor.execute(command, 0, null);
125             XDynamicResultSet xDynamicResultSet = UnoRuntime.queryInterface(XDynamicResultSet.class, result);
126             XResultSet xResultSet = xDynamicResultSet.getStaticResultSet();
127             XRow xRow = UnoRuntime.queryInterface(XRow.class, xResultSet);
128             // create the new folder 'folderName': first, check if it's already there
129             while(xResultSet.next()) {
130                 String existingFolderName = xRow.getString(1);
131                 System.out.println("Found existing folder: '" + existingFolderName + "'");
132                 if (folderName.equals(existingFolderName)) {
133                     fail("Cannot create a new folder: folder already exists: adapt test or choose a different document.");
134                 }
135             }
136             // create a folder
137             System.out.println("Create new folder "+ folderName);
138             ContentInfo contentInfo = new ContentInfo();
139             contentInfo.Type = "application/vnd.sun.star.tdoc-folder";
140 
141             command.Name = "createNewContent";
142             command.Argument = contentInfo;
143 
144             result = xCommandProcessor.execute(command, 0, null);
145             XContent xNewFolder = UnoRuntime.queryInterface(XContent.class, result);
146 
147             XCommandProcessor xFolderCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xNewFolder);
148             System.out.println("Got the new folder: " + utils.getImplName(xNewFolder));
149 
150             // name the new folder
151             PropertyValue[] titleProp = new PropertyValue[1];
152             titleProp[0] = new PropertyValue();
153             titleProp[0].Name = "Title";
154             titleProp[0].Handle = -1;
155             titleProp[0].Value = folderName;
156             Command titleSetCommand = new Command();
157             titleSetCommand.Name = "setPropertyValues";
158             titleSetCommand.Argument = titleProp;
159             xFolderCommandProcessor.execute(titleSetCommand, 0, null);
160 
161             // 2do: check all this stuff!
162             // commit changes
163 /*            InsertCommandArgument insertArgs = new InsertCommandArgument();
164             insertArgs.Data = null;
165             insertArgs.ReplaceExisting = true;
166             Command commitCommand = new Command();
167             commitCommand.Name = "insert";
168             commitCommand.Argument = insertArgs;
169             xFolderCommandProcessor.execute(commitCommand, 0, null); */
170         }
171         catch (com.sun.star.uno.Exception e) {
172             e.printStackTrace();
173             fail("Could not create test objects.");
174         }
175 
176     }
177 
178 
179     private XMultiServiceFactory getMSF()
180     {
181         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
182         return xMSF1;
183     }
184 
185     // setup and close connections
186     @BeforeClass public static void setUpConnection() throws Exception {
187         System.out.println("setUpConnection()");
188         connection.setUp();
189     }
190 
191     @AfterClass public static void tearDownConnection()
192         throws InterruptedException, com.sun.star.uno.Exception
193     {
194         System.out.println("tearDownConnection()");
195         connection.tearDown();
196     }
197 
198     private static final OfficeConnection connection = new OfficeConnection();
199 
200 
201 }
202