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