1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XDocumentInsertable.java,v $
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 ************************************************************************/
33 import com
.sun
.star
.io
.IOException
;
34 import lib
.MultiMethodTest
;
36 import lib
.StatusException
;
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
;
46 * Testing <code>com.sun.star.document.XDocumentInsertable</code>
49 * <li><code> insertDocumentFromURL()</code></li>
51 * This test needs the following object relations :
53 * <li> <code>'XDocumentInsertable.Checker'</code>
54 * (of type <code>_XDocumentInsertable.InsertChecker</code>)
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>
62 * The following predefined files needed to complete the test:
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.
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
;
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
{
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() ;
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 "+
122 UnoRuntime
.queryInterface (XTextRange
.class, oObj
) ;
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")) ;
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 ;
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 "+
156 String text
= range
.getString() ;
157 log
.println("Document text :\n" + text
);
159 result
&= ( text
.indexOf("XDocumentInsertable test.") >= 0 );
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
);
169 } catch (com
.sun
.star
.io
.IOException ex
) {
170 log
.println("Exception occured while testing "+
171 "insertDocumentFromURL()");
172 ex
.printStackTrace(log
);
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
);
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