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.XPropertySetInfo; 31 import com.sun.star.frame.XModel; 32 import com.sun.star.lang.XMultiServiceFactory; 33 import com.sun.star.text.XTextDocument; 34 import com.sun.star.ucb.Command; 35 import com.sun.star.ucb.XCommandProcessor; 36 import com.sun.star.ucb.XContent; 37 import com.sun.star.ucb.XContentIdentifier; 38 import com.sun.star.ucb.XContentIdentifierFactory; 39 import com.sun.star.ucb.XContentProvider; 40 import com.sun.star.uno.UnoRuntime; 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 public class CheckTransientDocumentsContent { 54 // TODO: document doesn't exists 55 private final String testDocuments[] = new String[]{"sForm.sxw"};//, "chinese.sxw", "Iterator.sxw"}; 56 private final int countDocs = testDocuments.length; 57 private XMultiServiceFactory xMSF = null; 58 private XTextDocument[] xTextDoc = null; 59 60 public String[] getTestMethodNames() { 61 return new String[] {"checkTransientDocumentsContent"}; 62 } 63 64 @Before public void before() { 65 xMSF = getMSF(); 66 xTextDoc = new XTextDocument[countDocs]; 67 System.out.println("Open some documents."); 68 for (int i=0; i<countDocs; i++) { 69 String fileName = TestDocument.getUrl(testDocuments[i]); 70 xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName); 71 assertNotNull("Can't load document " + fileName, xTextDoc[i]); 72 } 73 } 74 @After public void after() { 75 System.out.println("Close all documents."); 76 for (int i=0; i<countDocs; i++) { 77 xTextDoc[i].dispose(); 78 } 79 } 80 81 /** 82 * Check the content of one document 83 */ 84 @Test public void checkTransientDocumentsContent() { 85 try { 86 // create the ucb 87 XContentIdentifierFactory xContentIdentifierFactory = 88 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker")); 89 XContentProvider xContentProvider = 90 UnoRuntime.queryInterface(XContentProvider.class, xContentIdentifierFactory); 91 // create a content identifier from the ucb for tdoc 92 XContentIdentifier xContentIdentifier = 93 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/1"); 94 // get content 95 XContent xContent = xContentProvider.queryContent(xContentIdentifier); 96 97 // actual test: commands to get some properties 98 XCommandProcessor xCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent); 99 // build up the command 100 Command command = new Command(); 101 command.Name = "getPropertySetInfo"; 102 command.Handle = -1; 103 104 // execute the command 105 Object result = xCommandProcessor.execute(command, 0, null); 106 107 // check the result 108 System.out.println("Result: "+ result.getClass().toString()); 109 XPropertySetInfo xPropertySetInfo = UnoRuntime.queryInterface(XPropertySetInfo.class, result); 110 Property[] props = xPropertySetInfo.getProperties(); 111 boolean res = false; 112 for(int i=0; i<props.length; i++) { 113 String propName = props[i].Name; 114 res |= propName.equals("DocumentModel"); 115 System.out.println("Found property: " + propName + " type: " + props[i].Type.getTypeName()); 116 } 117 assertNotNull("Did not find property 'DocumentModel' in the Property array.", res); 118 119 // get on property 120 command.Name = "getPropertyValues"; 121 command.Handle = -1; 122 Property[] prop = new Property[1]; 123 prop[0] = new Property(); 124 prop[0].Name = "DocumentModel"; 125 prop[0].Handle = -1; 126 command.Argument = prop; 127 128 // execute the command 129 result = xCommandProcessor.execute(command, 0, null); 130 131 // check the result 132 System.out.println("Result: "+ result.getClass().toString()); 133 134 XModel xModel = UnoRuntime.queryInterface(XModel.class, result); 135 assertTrue("Did not get property 'DocumentModel'.", xModel == null); 136 } 137 catch (com.sun.star.uno.Exception e) { 138 e.printStackTrace(); 139 fail("Could not create test objects."); 140 } 141 142 } 143 144 private XMultiServiceFactory getMSF() 145 { 146 final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 147 return xMSF1; 148 } 149 150 // setup and close connections 151 @BeforeClass public static void setUpConnection() throws Exception { 152 System.out.println("setUpConnection()"); 153 connection.setUp(); 154 } 155 156 @AfterClass public static void tearDownConnection() 157 throws InterruptedException, com.sun.star.uno.Exception 158 { 159 System.out.println("tearDownConnection()"); 160 connection.tearDown(); 161 } 162 163 private static final OfficeConnection connection = new OfficeConnection(); 164 165 166 } 167