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.filter.misc; 24 25 import com.sun.star.beans.PropertyValue; 26 import com.sun.star.container.NoSuchElementException; 27 import com.sun.star.container.XNameAccess; 28 import com.sun.star.container.XNameContainer; 29 import com.sun.star.container.XNameReplace; 30 import com.sun.star.lang.IllegalArgumentException; 31 import com.sun.star.lang.WrappedTargetException; 32 import com.sun.star.lang.WrappedTargetRuntimeException; 33 import com.sun.star.lang.XMultiServiceFactory; 34 import com.sun.star.uno.UnoRuntime; 35 import com.sun.star.uno.XInterface; 36 import com.sun.star.util.XFlushable; 37 // import complexlib.ComplexTestCase; 38 // import util.utils; 39 40 // ---------- junit imports ----------------- 41 import org.junit.After; 42 import org.junit.AfterClass; 43 import org.junit.Before; 44 import org.junit.BeforeClass; 45 import org.junit.Test; 46 import org.openoffice.test.OfficeConnection; 47 import static org.junit.Assert.*; 48 // ------------------------------------------ 49 50 /** 51 * This complex test checks the functionality of the properties "Finalized" and "Mandatory" of 52 * the services <CODE>com.sun.star.document.FilterFactory</CODE> and 53 * <CODE>com.sun.star.document.TypeDetection</CODE>. 54 * 55 * Each of these services represent a container of <CODE>PropertyValue[]</CODE>. 56 * The <CODE>PropertyValue[]</CODE> contains among others the properties called 57 * <CODE>Finalized</CODE> and <CODE>Mandatory</CODE>. If the property 58 * <CODE>Finalized</CODE> is set to <CODE>true</CODE>, a filter can be removed 59 * but will not be able to be changed. 60 * If the property <CODE>Mandatory</CODE> is set to <CODE>true</CODE>, the filter 61 * can not be removed. 62 * 63 * Every filter, which is registered to the office, will be tested. For every filter-test 64 * a new instance of the mentioned services will be created. 65 66 * During the test the property <CODE>UIName</CODE> 67 * will be changed and the service will be flushed. The test checks for expected exceptions: 68 * If the property <CODE>Finalized</CODE> equals 69 * <CODE>true</CODE> the tests check if an <CODE>Exception</CODE> must be thrown. 70 * The next step of the test is the removal of the filter was removed, then the service 71 * will be flushed. The test checks for expected exceptions: If the property 72 * <CODE>Mandatory</CODE> equals <CODE>true</CODE>, an <CODE>Exception</CODE> must 73 * be thrown. 74 * This test results <CODE>false</CODE> state if there is no filter available with: 75 * <CODE>Finalized=true</CODE> 76 * <CODE>Finalized=false</CODE> 77 * <CODE>Mandatory=true</CODE> 78 * <CODE>Mandatory=false</CODE> 79 */ 80 public class FinalizedMandatoryTest 81 { 82 83 static XMultiServiceFactory xMSF; 84 85 /** 86 * A function to tell the framework, which test functions are available. 87 * @return All test methods. 88 */ 89 // public String[] getTestMethodNames() { 90 // return new String[]{"checkReadonlySupportFilterFactory", 91 // "checkReadonlySupportTypeDetection"}; 92 // } 93 /** Create the environment for following tests. 94 * Use either a component loader from desktop or 95 * from frame 96 * @throws Exception Exception 97 */ before()98 @Before public void before() throws Exception 99 { 100 101 // create TypeDetection 102 xMSF = getMSF(); 103 assertNotNull("Could not get XMultiServiceFactory", xMSF); 104 105 } 106 107 /** 108 * Creates an instance for the given <CODE>serviceName</CODE> 109 * @param serviceName the name of the service which should be created 110 * @throws Exception was thrown if creation fails 111 * @return <CODE>XInterface</CODE> of service 112 */ getTestObject(String serviceName)113 private XInterface getTestObject(String serviceName) throws Exception 114 { 115 116 Object oInterface = xMSF.createInstance(serviceName); 117 118 assertNotNull("Service wasn't created", oInterface); 119 // if (oInterface == null) { 120 // failed("Service wasn't created") ; 121 // throw new Exception("could not create service '"+serviceName+"'"); 122 // } 123 return (XInterface) oInterface; 124 } 125 126 /** 127 * call the function <CODE>checkReadonlySupport</CODE> to test <CODE>com.sun.star.document.FilterFactory</CODE> 128 * @see com.sun.star.document.FilterFactory 129 */ checkReadonlySupportFilterFactory()130 @Test public void checkReadonlySupportFilterFactory() 131 { 132 checkReadonlySupport("com.sun.star.document.FilterFactory"); 133 } 134 135 /** 136 * call the function <CODE>checkReadonlySupport</CODE> to test <CODE>com.sun.star.document.TypeDetection</CODE> 137 * @see com.sun.star.document.TypeDetection 138 */ checkReadonlySupportTypeDetection()139 @Test public void checkReadonlySupportTypeDetection() 140 { 141 checkReadonlySupport("com.sun.star.document.TypeDetection"); 142 } 143 144 /** 145 * test the given service <CODE>serviceName</CODE>. 146 * For every filter a new instance was created and the tests started. 147 * @param serviceName the name of the service to test 148 */ checkReadonlySupport(String serviceName)149 private void checkReadonlySupport(String serviceName) 150 { 151 System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); 152 System.out.println("testing service '" + serviceName + "'"); 153 154 XInterface oObj = null; 155 try 156 { 157 oObj = getTestObject(serviceName); 158 System.out.println("ImplName: " + util.utils.getImplName(oObj)); 159 } 160 catch (java.lang.Exception e) 161 { 162 fail("could not get test object"); 163 } 164 165 boolean mandantoryTrue = false; 166 boolean mandantoryFalse = false; 167 boolean finalizedTrue = false; 168 boolean finalizedFalse = false; 169 170 171 XNameAccess xNA = UnoRuntime.queryInterface(XNameAccess.class, oObj); 172 String[] filterNames = xNA.getElementNames(); 173 174 // XNameContainer; XNameReplace 175 String filterName = filterNames[0]; 176 Object[] instance = null; 177 178 for (int i = 0; i < filterNames.length; i++) 179 { 180 System.out.println("------------------------------------------------"); 181 try 182 { 183 PropertyValue instanceProp = new PropertyValue(); 184 filterName = filterNames[i]; 185 System.out.println(filterName); 186 187 // testobject must new created for every test. 188 // We change in a loop the container and try to flush this changes. 189 // If we get an expected exception this container is corrupt. It's 190 // similar to a document which could not be saved because of invalid 191 // contend. While you don't remove the invalid conted you will never 192 // be able to save the document. Same here. 193 try 194 { 195 oObj = getTestObject(serviceName); 196 } 197 catch (java.lang.Exception e) 198 { 199 fail("could not get test object"); 200 } 201 202 xNA = UnoRuntime.queryInterface(XNameAccess.class, oObj); 203 XNameContainer xNC = UnoRuntime.queryInterface(XNameContainer.class, oObj); 204 XNameReplace xNR = UnoRuntime.queryInterface(XNameReplace.class, oObj); 205 XFlushable xFlush = UnoRuntime.queryInterface(XFlushable.class, oObj); 206 207 instance = (Object[]) xNA.getByName(filterName); 208 PropertyValue[] props = (PropertyValue[]) instance; 209 210 printPropertyValues(props); 211 212 boolean isMandatory = ((Boolean) getPropertyValueValue(props, "Mandatory")).booleanValue(); 213 boolean isFinalized = ((Boolean) getPropertyValueValue(props, "Finalized")).booleanValue(); 214 215 // memory if every state is available 216 mandantoryTrue |= isMandatory; 217 mandantoryFalse |= !isMandatory; 218 219 finalizedTrue |= isFinalized; 220 finalizedFalse |= !isFinalized; 221 222 //change the filter 223 setPropertyValueValue((PropertyValue[]) instance, "UIName", "dummy"); 224 225 // 1a.) try to change the filter in the container 226 try 227 { 228 xNR.replaceByName(filterName, instance); 229 } 230 catch (IllegalArgumentException e) 231 { 232 fail("could not replace filter properties ('" + filterName + "')"); 233 } 234 235 // 1b.) try to write the changed filter to the configuration. 236 // This must result in an exception if the filter is finalized. 237 boolean flushError = false; 238 try 239 { 240 xFlush.flush(); 241 } 242 catch (WrappedTargetRuntimeException e) 243 { 244 flushError = true; 245 assertTrue("Unexpected exception while flushing changed filter '" + filterName + "'", isFinalized); 246 } 247 assertTrue("Expected exception was not thrown while flushing changed filter '" + filterName + "' Finalized:" + isFinalized, 248 !(flushError ^ isFinalized)); 249 250 251 252 // 2a.) try to remove the filter from the container 253 try 254 { 255 xNC.removeByName(filterName); 256 } 257 catch (NoSuchElementException e) 258 { 259 fail("could not remove filter from container ('" + filterName + "')"); 260 } 261 // 1b.) try to write the changed filter to the configuration. 262 // This must result in an exception if the filter is mandatory 263 flushError = false; 264 try 265 { 266 xFlush.flush(); 267 } 268 catch (WrappedTargetRuntimeException e) 269 { 270 flushError = true; 271 assertTrue("Unexpected exception while flushing removed filter '" + filterName + "'", isMandatory); 272 } 273 assertTrue("Expected exception was not thrown while flushing removed filter '" + filterName + "' Mandatory:" + isMandatory, 274 !(flushError ^ isMandatory)); 275 276 } 277 catch (NoSuchElementException e) 278 { 279 fail("Couldn't get elements from object"); 280 } 281 catch (WrappedTargetException e) 282 { 283 fail("Couldn't get elements from object"); 284 } 285 } 286 String preMsg = "Could not find filter with state "; 287 String postMsg = " Please check if such filter is installed!"; 288 assertTrue(preMsg + "'Mandatory=true'" + postMsg, mandantoryTrue); 289 assertTrue(preMsg + "'Mandatory=false'" + postMsg, mandantoryFalse); 290 assertTrue(preMsg + "'Finalized=true'" + postMsg, finalizedTrue); 291 assertTrue(preMsg + "'Finalized=false'" + postMsg, finalizedFalse); 292 } 293 294 /** 295 * print all properties with its values to <CODE>logger</CODE>. For debug purposes. 296 * @see stats.SimpleLogWriter 297 * @see com.sun.star.beans.PropertyValue 298 * @param props Sequence of PropertyValue 299 */ printPropertyValues(PropertyValue[] props)300 protected void printPropertyValues(PropertyValue[] props) 301 { 302 int i = 0; 303 while (i < props.length) 304 { 305 System.out.println(props[i].Name + ":" + props[i].Value.toString()); 306 i++; 307 } 308 if (i < props.length) 309 { 310 System.out.println(props[i].Name + ":" + props[i].Value.toString()); 311 } 312 } 313 314 /** 315 * returns the value of the specified (<CODE>pName</CODE>) property from a sequence of <CODE>PropertyValue</CODE> 316 * @param props a sequence of <CODE>PropertyValue</CODE> 317 * @param pName the name of the property the value should be returned 318 * @return the value of the property 319 */ getPropertyValueValue(PropertyValue[] props, String pName)320 protected Object getPropertyValueValue(PropertyValue[] props, String pName) 321 { 322 int i = 0; 323 while (i < props.length && !props[i].Name.equals(pName)) 324 { 325 i++; 326 } 327 return i < props.length ? props[i].Value : null; 328 } 329 330 /** 331 * set a value of the specified (<CODE>pName</CODE>) property inside a sequence of <CODE>PropertyValue</CODE> 332 * @param props sequence of <CODE>PropertyValue</CODE> 333 * @param pName name of the property which should be changed 334 * @param pValue the value the property should be assigned 335 */ setPropertyValueValue(PropertyValue[] props, String pName, Object pValue)336 protected void setPropertyValueValue(PropertyValue[] props, String pName, Object pValue) 337 { 338 int i = 0; 339 while (i < props.length && !props[i].Name.equals(pName)) 340 { 341 i++; 342 } 343 props[i].Value = pValue; 344 } 345 getMSF()346 private XMultiServiceFactory getMSF() 347 { 348 final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 349 return xMSF1; 350 } 351 352 // setup and close connections 353 @BeforeClass setUpConnection()354 public static void setUpConnection() throws Exception 355 { 356 System.out.println("setUpConnection()"); 357 connection.setUp(); 358 } 359 360 @AfterClass tearDownConnection()361 public static void tearDownConnection() 362 throws InterruptedException, com.sun.star.uno.Exception 363 { 364 System.out.println("tearDownConnection()"); 365 connection.tearDown(); 366 } 367 private static final OfficeConnection connection = new OfficeConnection(); 368 } 369