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.lang.XMultiServiceFactory; 26 import com.sun.star.lang.XServiceInfo; 27 import com.sun.star.sdbc.XResultSet; 28 import com.sun.star.text.XTextDocument; 29 import com.sun.star.ucb.Command; 30 import com.sun.star.ucb.OpenCommandArgument2; 31 import com.sun.star.ucb.OpenMode; 32 import com.sun.star.ucb.XCommandProcessor; 33 import com.sun.star.ucb.XContent; 34 import com.sun.star.ucb.XContentAccess; 35 import com.sun.star.ucb.XContentIdentifier; 36 import com.sun.star.ucb.XContentIdentifierFactory; 37 import com.sun.star.ucb.XContentProvider; 38 import com.sun.star.ucb.XDynamicResultSet; 39 import com.sun.star.uno.UnoRuntime; 40 // import complexlib.ComplexTestCase; 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 */ 54 public class CheckTransientDocumentsContentProvider { 55 // TODO: document doesn't exists 56 private final String testDocuments[] = new String[]{/*"sForm.sxw",*/ "chinese.sxw", "Iterator.sxw"}; 57 private final int countDocs = testDocuments.length; 58 private XMultiServiceFactory xMSF = null; 59 private XTextDocument[] xTextDoc = null; 60 <<<<<<< HEAD 61 62 public String[] getTestMethodNames() { 63 return new String[]{"checkTransientDocumentsContentProvider"}; 64 } 65 66 ======= 67 68 >>>>>>> 3309286857 (pre-commit auto remove trailing whitespace from java files (#382)) 69 @Before public void before() { 70 xMSF = getMSF(); 71 xTextDoc = new XTextDocument[countDocs]; 72 System.out.println("Open some documents."); 73 for (int i=0; i<countDocs; i++) { 74 String fileName = TestDocument.getUrl(testDocuments[i]); 75 xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName); 76 assertNotNull("Can't load document " + fileName, xTextDoc[i]); 77 } 78 } 79 @After public void after() { 80 System.out.println("Close all documents."); 81 for (int i=0; i<countDocs; i++) { 82 xTextDoc[i].dispose(); 83 } 84 } 85 86 /** 87 * Check the provider of document content: open some documents 88 * and look if they are accessible. 89 */ 90 @Test public void checkTransientDocumentsContentProvider() { 91 try { 92 // create a content provider 93 Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider"); 94 XContentProvider xContentProvider = 95 UnoRuntime.queryInterface(XContentProvider.class, o); 96 97 // create the ucb 98 XContentIdentifierFactory xContentIdentifierFactory = 99 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker")); 100 // create a content identifier from the ucb for tdoc 101 XContentIdentifier xContentIdentifier = 102 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/"); 103 // get content 104 XContent xContent = xContentProvider.queryContent(xContentIdentifier); 105 106 // actual test: execute an "open" command with the content 107 XCommandProcessor xCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent); 108 // build up the command 109 Command command = new Command(); 110 OpenCommandArgument2 commandarg2 = new OpenCommandArgument2(); 111 commandarg2.Mode = OpenMode.ALL; 112 command.Name = "open"; 113 command.Argument = commandarg2; 114 115 // execute the command 116 Object result = xCommandProcessor.execute(command, 0, null); 117 118 // check the result 119 System.out.println("Result: "+ result.getClass().toString()); 120 XDynamicResultSet xDynamicResultSet = UnoRuntime.queryInterface(XDynamicResultSet.class, result); 121 122 // check bug of wrong returned service name. 123 XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, xDynamicResultSet); 124 String[] sNames = xServiceInfo.getSupportedServiceNames(); 125 String serviceName = sNames[0]; 126 if (sNames.length > 1) 127 { 128 fail("Implementation has been changed. Check this test!"); 129 } 130 assertTrue("The service name '" + serviceName + "' is not valid.", !serviceName.equals("com.sun.star.ucb.DynamicContentResultSet")); 131 132 XResultSet xResultSet = xDynamicResultSet.getStaticResultSet(); 133 XContentAccess xContentAccess = UnoRuntime.queryInterface(XContentAccess.class, xResultSet); 134 // iterate over the result: three docs were opened, we should have at least three content identifier strings 135 int countContentIdentifiers = 0; 136 while(xResultSet.next()) { 137 countContentIdentifiers++; 138 String identifier = xContentAccess.queryContentIdentifierString(); 139 System.out.println("Identifier of row " + xResultSet.getRow() + ": " + identifier); 140 } 141 // some feeble test: if the amount >2, we're ok. 142 // 2do: check better 143 assertTrue("Did only find " + countContentIdentifiers + " open documents." + 144 " Should have been at least 3.", countContentIdentifiers>2); 145 } 146 catch (com.sun.star.uno.Exception e) { 147 e.printStackTrace(); 148 fail("Could not create test objects."); 149 } 150 151 } 152 153 private XMultiServiceFactory getMSF() 154 { 155 final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 156 return xMSF1; 157 } 158 159 // setup and close connections 160 @BeforeClass public static void setUpConnection() throws Exception { 161 System.out.println("setUpConnection()"); 162 connection.setUp(); 163 } 164 165 @AfterClass public static void tearDownConnection() 166 throws InterruptedException, com.sun.star.uno.Exception 167 { 168 System.out.println("tearDownConnection()"); 169 connection.tearDown(); 170 } 171 172 private static final OfficeConnection connection = new OfficeConnection(); 173 174 } 175