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 import com.sun.star.io.IOException;
27 import lib.MultiMethodTest;
28 import lib.Status;
29 import lib.StatusException;
30 import util.utils;
31 
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.document.XDocumentInsertable;
34 import com.sun.star.text.XTextRange;
35 import com.sun.star.uno.UnoRuntime;
36 
37 
38 /**
39 * Testing <code>com.sun.star.document.XDocumentInsertable</code>
40 * interface methods :
41 * <ul>
42 *  <li><code> insertDocumentFromURL()</code></li>
43 * </ul> <p>
44 * This test needs the following object relations :
45 * <ul>
46 *  <li> <code>'XDocumentInsertable.Checker'</code>
47 *   (of type <code>_XDocumentInsertable.InsertChecker</code>)
48 *   <b> optional </b> :
49 *   relation for checking if document was inserted properly and
50 *   for obtaining document file name. For details see the class
51 *   description. If the relation doesn't exist default document
52 *   name is used, and <code>XTextRange</code> interface of
53 *   component is used for checking.</li>
54 * <ul> <p>
55 * The following predefined files needed to complete the test:
56 * <ul>
57 *  <li> <code>XDocumentInsertable.sxw</code> : StarWriter document
58 *    which content started with 'XDocumentInsertable test.' string.
59 *    The file is needed if no other file name specified by relation.
60 *    </li>
61 * <ul> <p>
62 * Test is <b> NOT </b> multithread compilant. <p>
63 * @see com.sun.star.document.XDocumentInsertable
64 */
65 public class _XDocumentInsertable extends MultiMethodTest {
66 
67     public XDocumentInsertable oObj = null;
68     protected XTextRange range = null ;
69     protected static final String defaultFileName = "XDocumentInsertable.sxw" ;
70     protected InsertChecker checker = null ;
71     protected String fileName = defaultFileName ;
72 
73     /**
74      * Abstract class for relation passing. It must check if
75      * document was inserted successfully and can specify its
76      * own document name to be inserted.
77      */
78     public static abstract class InsertChecker {
79         /**
80          * Must be overriden to check if document was
81          * successfully inserted.
82          * @return <code>true</code> if document was inserted.
83          */
84         public abstract boolean isInserted() ;
85         /**
86          * Can be overriden to specify different than default
87          * document name. This document must be situated in
88          * the test document disrectory, and its name must
89          * be specified relational to this directory. By
90          * default 'XDocumentInsertable.swx' file name returned.
91          * @return File name of the document to be inserted.
92          */
93         public String getFileNameToInsert() {
94             return defaultFileName ;
95         }
96     }
97 
98     /**
99      * Retrieves object relation. If the relation is not found
100      * then the object tested is tried to query <code>XTextRange</code>
101      * interface for testing. If the relation is found then document name
102      * for testing is retrieved, else the default one is used.
103      *
104      * @throws StatusException If neither relation found nor
105      * <code>XTextRange</code> interface is queried.
106      */
107     public void before() {
108         checker = (InsertChecker)
109             tEnv.getObjRelation("XDocumentInsertable.Checker") ;
110 
111         if (checker == null) {
112             log.println("Relaion not found, trying to query for "+
113                 "XTextRange ...") ;
114             range = (XTextRange)
115                 UnoRuntime.queryInterface (XTextRange.class, oObj) ;
116             if (range == null) {
117                 log.println("XTextRange isn't supported by the component.");
118                 throw new StatusException(Status.failed
119                     ("XTextRange isn't supported and relation not found")) ;
120             }
121         } else {
122             fileName = checker.getFileNameToInsert();
123         }
124     }
125 
126     /**
127     * Tries to insert document from URL specified by relation or
128     * from default URL. If no relation was passed, text range is
129     * checked for existance of loaded document content. In case
130     * if relation was found, then its <code>isInserted</code>
131     * method is used to check insertion.<p>
132     * A Second test uses an invalid URL and checks for correct exceptions.
133     *
134     * Has <b> OK </b> status if at first insertion was completed successfully
135     * and no exceptions were thrown and as second a expected excption was thrown. <p>
136     */
137     public void _insertDocumentFromURL() {
138         boolean result = true ;
139 
140         try {
141             PropertyValue [] szEmptyArgs = new PropertyValue [0];
142             String docURL = utils.getFullTestURL(fileName) ;
143             log.println("Inserting document from URL '" + docURL + "'");
144             oObj.insertDocumentFromURL(docURL, szEmptyArgs);
145 
146             if (checker == null) {
147                 log.println("Checker is not specified, testing through "+
148                     "XTextRange ...") ;
149                 String text = range.getString() ;
150                 log.println("Document text :\n" + text);
151                 log.println("---");
152                 result &= ( text.indexOf("XDocumentInsertable test.") >= 0 );
153             } else {
154                 result &= checker.isInserted();
155             }
156 
157         } catch (com.sun.star.lang.IllegalArgumentException ex) {
158             log.println("Exception occured while testing "+
159                 "insertDocumentFromURL()");
160             ex.printStackTrace(log);
161             result = false ;
162         } catch (com.sun.star.io.IOException ex) {
163             log.println("Exception occured while testing "+
164                 "insertDocumentFromURL()");
165             ex.printStackTrace(log);
166             result = false ;
167         }
168 
169         try {
170             PropertyValue [] szEmptyArgs = new PropertyValue [0];
171             String docURL = "file:///c:/ThisIsAnInvaldURL";
172             log.println("Inserting document from URL '" + docURL + "'");
173             oObj.insertDocumentFromURL(docURL, szEmptyArgs);
174 
175             result=false;
176 
177         } catch (IOException ex) {
178             log.println("expected exception was thrown -> ok");
179         } catch (com.sun.star.lang.IllegalArgumentException ex) {
180             log.println("expected exception was thrown -> ok");
181         }
182 
183 
184         tRes.tested("insertDocumentFromURL()", result);
185     }
186 
187     /**
188     * Forces environment recreation.
189     */
190     protected void after() {
191         disposeEnvironment();
192     }
193 }  // finish class _XDocumentInsertable
194 
195