Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / document / _XDocumentInsertable.java
blobc3417118e2b2aeee4405fcf46758e23a1b11ebc2
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.document;
21 import com.sun.star.io.IOException;
22 import lib.MultiMethodTest;
23 import lib.Status;
24 import lib.StatusException;
25 import util.utils;
27 import com.sun.star.beans.PropertyValue;
28 import com.sun.star.document.XDocumentInsertable;
29 import com.sun.star.text.XTextRange;
30 import com.sun.star.uno.UnoRuntime;
33 /**
34 * Testing <code>com.sun.star.document.XDocumentInsertable</code>
35 * interface methods :
36 * <ul>
37 * <li><code> insertDocumentFromURL()</code></li>
38 * </ul> <p>
39 * This test needs the following object relations :
40 * <ul>
41 * <li> <code>'XDocumentInsertable.Checker'</code>
42 * (of type <code>_XDocumentInsertable.InsertChecker</code>)
43 * <b> optional </b> :
44 * relation for checking if document was inserted properly and
45 * for obtaining document file name. For details see the class
46 * description. If the relation doesn't exist default document
47 * name is used, and <code>XTextRange</code> interface of
48 * component is used for checking.</li>
49 * <ul> <p>
50 * The following predefined files needed to complete the test:
51 * <ul>
52 * <li> <code>XDocumentInsertable.sxw</code> : StarWriter document
53 * which content started with 'XDocumentInsertable test.' string.
54 * The file is needed if no other file name specified by relation.
55 * </li>
56 * <ul> <p>
57 * Test is <b> NOT </b> multithread compliant. <p>
58 * @see com.sun.star.document.XDocumentInsertable
60 public class _XDocumentInsertable extends MultiMethodTest {
62 public XDocumentInsertable oObj = null;
63 protected XTextRange range = null ;
64 protected static final String defaultFileName = "XDocumentInsertable.sxw" ;
65 protected InsertChecker checker = null ;
66 protected String fileName = defaultFileName ;
68 /**
69 * Abstract class for relation passing. It must check if
70 * document was inserted successfully and can specify its
71 * own document name to be inserted.
73 public static abstract class InsertChecker {
74 /**
75 * Must be overridden to check if document was
76 * successfully inserted.
77 * @return <code>true</code> if document was inserted.
79 public abstract boolean isInserted() ;
80 /**
81 * Can be overridden to specify different than default
82 * document name. This document must be situated in
83 * the test document directory, and its name must
84 * be specified relational to this directory. By
85 * default 'XDocumentInsertable.swx' file name returned.
86 * @return File name of the document to be inserted.
88 public String getFileNameToInsert() {
89 return defaultFileName ;
93 /**
94 * Retrieves object relation. If the relation is not found
95 * then the object tested is tried to query <code>XTextRange</code>
96 * interface for testing. If the relation is found then document name
97 * for testing is retrieved, else the default one is used.
99 * @throws StatusException If neither relation found nor
100 * <code>XTextRange</code> interface is queried.
102 @Override
103 public void before() {
104 checker = (InsertChecker)
105 tEnv.getObjRelation("XDocumentInsertable.Checker") ;
107 if (checker == null) {
108 log.println("Relation not found, trying to query for "+
109 "XTextRange ...") ;
110 range = UnoRuntime.queryInterface (XTextRange.class, oObj) ;
111 if (range == null) {
112 log.println("XTextRange isn't supported by the component.");
113 throw new StatusException(Status.failed
114 ("XTextRange isn't supported and relation not found")) ;
116 } else {
117 fileName = checker.getFileNameToInsert();
122 * Tries to insert document from URL specified by relation or
123 * from default URL. If no relation was passed, text range is
124 * checked for existence of loaded document content. In case
125 * if relation was found, then its <code>isInserted</code>
126 * method is used to check insertion.<p>
127 * A Second test uses an invalid URL and checks for correct exceptions.
129 * Has <b> OK </b> status if at first insertion was completed successfully
130 * and no exceptions were thrown and as second an expected exception was thrown. <p>
132 public void _insertDocumentFromURL() {
133 boolean result = true ;
135 try {
136 PropertyValue [] szEmptyArgs = new PropertyValue [0];
137 String docURL = utils.getFullTestURL(fileName) ;
138 log.println("Inserting document from URL '" + docURL + "'");
139 oObj.insertDocumentFromURL(docURL, szEmptyArgs);
141 if (checker == null) {
142 log.println("Checker is not specified, testing through "+
143 "XTextRange ...") ;
144 String text = range.getString() ;
145 log.println("Document text :\n" + text);
146 log.println("---");
147 result &= ( text.indexOf("XDocumentInsertable test.") >= 0 );
148 } else {
149 result &= checker.isInserted();
152 } catch (com.sun.star.lang.IllegalArgumentException ex) {
153 log.println("Exception occurred while testing "+
154 "insertDocumentFromURL()");
155 ex.printStackTrace(log);
156 result = false ;
157 } catch (com.sun.star.io.IOException ex) {
158 log.println("Exception occurred while testing "+
159 "insertDocumentFromURL()");
160 ex.printStackTrace(log);
161 result = false ;
164 if (result) {
165 try {
166 PropertyValue [] szEmptyArgs = new PropertyValue [0];
167 String docURL = "file:///c:/ThisIsAnInvalidURL";
168 log.println("Inserting document from URL '" + docURL + "'");
169 oObj.insertDocumentFromURL(docURL, szEmptyArgs);
171 result=false;
173 } catch (IOException ex) {
174 log.println("expected exception was thrown -> ok");
175 } catch (com.sun.star.lang.IllegalArgumentException ex) {
176 log.println("expected exception was thrown -> ok");
180 tRes.tested("insertDocumentFromURL()", result);
184 * Forces environment recreation.
186 @Override
187 protected void after() {
188 disposeEnvironment();
190 } // finish class _XDocumentInsertable