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