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