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 
24 package mod._fwl;
25 
26 import com.sun.star.beans.NamedValue;
27 import java.io.PrintWriter;
28 import java.util.Vector;
29 
30 import lib.Status;
31 import lib.StatusException;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
35 import util.utils;
36 
37 import com.sun.star.beans.PropertyValue;
38 import com.sun.star.container.XNameAccess;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 
43 /**
44  * Test for object which is represented by service
45  * <code>com.sun.star.comp.framework.FilterFactory</code>. <p>
46  *
47  * Object implements the following interfaces :
48  * <ul>
49  *  <li> <code>com::sun::star::container::XNameAccess</code></li>
50  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
51  *  <li> <code>com::sun::star::lang::XMultiServiceFactory</code></li>
52  * </ul> <p>
53  *
54  * @see com.sun.star.container.XNameAccess
55  * @see com.sun.star.container.XElementAccess
56  * @see com.sun.star.lang.XMultiServiceFactory
57  * @see ifc.container._XNameAccess
58  * @see ifc.container._XElementAccess
59  * @see ifc.lang._XMultiServiceFactory
60  */
61 public class FilterFactory extends TestCase {
62 
63     /**
64      * Creating a Testenvironment for the interfaces to be tested.
65      * Creates an instance of the service
66      * <code>com.sun.star.comp.framework.FilterFactory</code>.
67      * Prepares relation for
68      * <code>XMultiServiceFactory.createInstanceWithArguments</code> method
69      * test. Only filters wich have their service names can be instantiated
70      * in some reasons (?). For instantiation used filter type as service
71      * name and its name as a parameter. <p>
72      */
createTestEnvironment(TestParameters Param, PrintWriter log)73     protected TestEnvironment createTestEnvironment
74             (TestParameters Param, PrintWriter log) {
75         XInterface oObj = null;
76         Object oInterface = null ;
77 
78         try {
79             oInterface = ((XMultiServiceFactory)Param.getMSF()).createInstance
80                 ("com.sun.star.document.FilterFactory") ;
81         } catch (com.sun.star.uno.Exception e) {
82             log.println("Couldn't get service");
83             e.printStackTrace(log);
84             throw new StatusException("Couldn't get FilterFactory", e );
85         }
86 
87         if (oInterface == null) {
88             log.println("Service wasn't created") ;
89             throw new StatusException(Status.failed("Service wasn't created")) ;
90         }
91 
92         oObj = (XInterface) oInterface ;
93         log.println("ImplName: "+utils.getImplName(oObj));
94 
95         log.println( "creating a new environment for object" );
96         TestEnvironment tEnv = new TestEnvironment( oObj );
97 
98         XNameAccess xNA = (XNameAccess) UnoRuntime.queryInterface
99             (XNameAccess.class, oObj);
100         String[] filterNames = xNA.getElementNames();
101 
102         // XNameContainer; XNameReplace
103         String filterName = filterNames[0];
104         Object[] instance = null;;
105         PropertyValue instanceProp = new PropertyValue();
106         try{
107             instance = (Object[]) xNA.getByName(filterName);
108             PropertyValue[] props = (PropertyValue[]) instance;
109             instanceProp = (PropertyValue) getPropertyValue
110                     (((PropertyValue[]) instance), "FilterService"); //instance[9];
111         } catch (com.sun.star.container.NoSuchElementException e){
112             throw new StatusException(
113             Status.failed("Couldn't get elements from object"));
114         } catch (com.sun.star.lang.WrappedTargetException e){
115             throw new StatusException(
116             Status.failed("Couldn't get elements from object"));
117         }
118 
119         log.println("adding INSTANCE 1 as obj relation to environment");
120 
121         log.println("fill property 'TemplateName' with 'INSTANCE1'");
122         setPropertyValueValue((PropertyValue[])instance, "TemplateName", "INSTANCE1");
123         tEnv.addObjRelation("INSTANCE" +1, instance);
124 
125 
126         // XMSF
127         Vector vFTypes = new Vector();
128         Vector vFArgs = new Vector();
129         for (int i = 0; i < filterNames.length; i++) {
130             PropertyValue[] filterProps = null;
131             try {
132                 System.out.println(filterNames[i]);
133                 filterProps = (PropertyValue[])
134                     xNA.getByName(filterNames[i]);
135             } catch (com.sun.star.lang.WrappedTargetException e) {
136                 throw new StatusException("Couldn't create relation", e);
137             } catch (com.sun.star.container.NoSuchElementException e) {
138                 throw new StatusException("Couldn't create relation", e);
139             }
140             String filterImpl = (String) getPropertyValueValue
141                 (filterProps, "FilterService");
142             if (filterImpl != null && filterImpl.length() > 0) {
143                 String filterType = (String) getPropertyValueValue
144                     (filterProps, "Type");
145                 vFTypes.add(filterType);
146                 PropertyValue prop = new PropertyValue();
147                 prop.Name = "FilterName";
148                 prop.Value = filterNames[i];
149                 vFArgs.add(new Object[] {prop});
150 
151                 log.println("Found filter '" + filterNames[i] + "', type: " +
152                     filterType + ", service name: '" + filterImpl + "'");
153             }
154         }
155 
156         tEnv.addObjRelation("XMSF.serviceNamesWithArgs",
157             vFTypes.toArray(new String[vFTypes.size()]));
158         tEnv.addObjRelation("XMSF.Args",
159             vFArgs.toArray(new Object[vFArgs.size()][]));
160 
161         // com.sun.star.container.XContainerQuery
162         NamedValue[] querySequenze = new NamedValue[1];
163 		NamedValue query = new NamedValue();
164 		query.Name = "Name";
165         query.Value = "Rich Text Format";
166         querySequenze[0] = query;
167 
168         tEnv.addObjRelation("XContainerQuery.createSubSetEnumerationByProperties",
169             querySequenze);
170 
171 
172         return tEnv;
173     } // finish method getTestEnvironment
174 
getPropertyValueValue(PropertyValue[] props, String pName)175     protected Object getPropertyValueValue(PropertyValue[] props, String pName) {
176         int i = 0;
177         while (i < props.length && !props[i].Name.equals(pName)) {
178             i++;
179         }
180         return i < props.length ? props[i].Value : null;
181 
182     }
183 
setPropertyValueValue(PropertyValue[] props, String pName, Object pValue)184     protected void setPropertyValueValue(PropertyValue[] props, String pName, Object pValue) {
185         int i = 0;
186         while (i < props.length && !props[i].Name.equals(pName)) {
187             i++;
188         }
189         props[i].Value = pValue;
190     }
191 
getPropertyValue(PropertyValue[] props, String pName)192     protected PropertyValue getPropertyValue(PropertyValue[] props, String pName) {
193         int i = 0;
194         while (i < props.length && !props[i].Name.equals(pName)) {
195             i++;
196         }
197         return i < props.length ? props[i] : null;
198 
199     }
200 }
201 
202