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