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 ifc.document;
25 
26 
27 import lib.MultiMethodTest;
28 import lib.Status;
29 import lib.StatusException;
30 
31 import com.sun.star.beans.PropertyValue;
32 import com.sun.star.document.XExporter;
33 import com.sun.star.document.XFilter;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.uno.UnoRuntime;
36 
37 /**
38 * Testing <code>com.sun.star.document.XFilter</code>
39 * interface methods :
40 * <ul>
41 *  <li><code> filter()</code></li>
42 *  <li><code> cancel()</code></li>
43 * </ul> <p>
44 * This test needs the following object relations :
45 * <ul>
46 *  <li> <code>'MediaDescriptor'</code> (of type <code>PropertyValue[]</code>):
47 *   the property set described in
48 *   <code>com.sun.star.document.MediaDescriptor</code>
49 *  </li>
50 *  <li> <code>'XFilter.Checker'</code> <b>(optional)</b> (of type
51 *   <code>ifc.document._XFilter.FilterChecker</code>) : implementation
52 *   of interface must allow checking that document was exported successfully.
53 *   If the relation doesn't exist then by default successfull filtering
54 *   assumed.
55 *  </li>
56 * <ul> <p>
57 * Test is <b> NOT </b> multithread compilant. <p>
58 * @see com.sun.star.document.XFilter
59 */
60 public class _XFilter extends MultiMethodTest {
61 
62 	public static interface FilterChecker {
checkFilter()63 		boolean checkFilter() ;
64 	}
65 
66 	public XFilter oObj = null;
67 	protected PropertyValue[] mDesc = null ;
68 	protected FilterChecker checker = null ;
69     protected XComponent sourceDoc = null;
70     protected boolean dummy = false;
71 
72 	/**
73 	* Retrieves object relations.
74 	* @throws StatusException If one of relations not found.<br>
75     * In case of Importers only a dummy implementation exists
76     * therefore the methods of this interface will be skipped
77     * in this case
78 	*/
before()79 	public void before() {
80         String name = tEnv.getTestCase().getObjectName();
81         if (name.indexOf("Importer")>0) {
82             log.println(name+" contains only a dummy implementation");
83             log.println("therefore all methods are skipped");
84             dummy = true;
85         }
86 		mDesc = (PropertyValue[]) tEnv.getObjRelation("MediaDescriptor") ;
87 		checker = (FilterChecker) tEnv.getObjRelation("XFilter.Checker") ;
88 		if (mDesc == null && !dummy) throw new StatusException(
89                                     Status.failed("Relation not found.")) ;
90             sourceDoc = (XComponent)tEnv.getObjRelation("SourceDocument");
91         try {
92             if (sourceDoc != null) {
93                 XExporter xEx = (XExporter)UnoRuntime.queryInterface(
94                                                     XExporter.class,oObj);
95                 xEx.setSourceDocument(sourceDoc);
96             }
97         }
98         catch (com.sun.star.lang.IllegalArgumentException e) {}
99 	}
100 
after()101     public void after() {
102         if (dummy) {
103             throw new StatusException(Status.skipped(true));
104         }
105     }
106 
107 	/**
108 	* Just calls the method. <p>
109 	* Has <b> OK </b> status if no runtime exceptions occured
110 	*/
_filter()111 	public void _filter() {
112         if (dummy) {
113             tRes.tested("filter()", true);
114             return;
115         }
116 		boolean result = true ;
117 		result = oObj.filter(mDesc) ;
118 
119 		if (checker == null) {
120 			log.println("!!! Warning : cann't check filter as no relation found");
121 		} else {
122 			result &= checker.checkFilter() ;
123 		}
124 
125 		tRes.tested("filter()", result) ;
126 	}
127 
128 	/**
129 	* Just calls the method. <p>
130 	* Has <b> OK </b> status if no runtime exceptions occured
131 	*/
_cancel()132 	public void _cancel() {
133         if (dummy) {
134             tRes.tested("cancel()",true);
135             return;
136         }
137         requiredMethod("filter()");
138         if (tEnv.getObjRelation("NoFilter.cancel()") != null) {
139             System.out.println("Cancel not tested.");
140             log.println("Method 'cancel()' is not working and therefore "+
141                         "not tested.\nResult is set to SKIPPED.OK");
142             tRes.tested("cancel()", Status.skipped(true));
143             return;
144         }
145 
146         boolean result = false ;
147         FilterThread newFilter = new FilterThread(oObj);
148         newFilter.mdesc = mDesc;
149         newFilter.start();
150         oObj.cancel();
151         while (newFilter.isAlive()) {
152         }
153         result = !newFilter.filterRes;
154         tRes.tested("cancel()", result) ;
155 	}
156 
157     /**
158     * Calls <code>filter()</code> method in a separate thread.
159     * Necessary to check if the cancel method works
160     */
161     protected class FilterThread extends Thread {
162 
163         public boolean filterRes = true ;
164         private XFilter Filter = null ;
165         public PropertyValue[] mdesc = null;
166 
FilterThread(XFilter Filter)167         public FilterThread(XFilter Filter) {
168             this.Filter = Filter ;
169         }
170 
run()171         public void run() {
172             filterRes = Filter.filter(mdesc);
173         }
174     }
175 
176 }
177 
178 
179 
180