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