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