Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / text / _XTextFieldsSupplier.java
blobe26fefdd62d9eb8f6b42af257af2a2461f9fa9d8
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.text;
21 import com.sun.star.beans.XPropertySet;
22 import com.sun.star.container.XEnumeration;
23 import com.sun.star.container.XEnumerationAccess;
24 import com.sun.star.container.XIndexAccess;
25 import com.sun.star.container.XNameAccess;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.sheet.XSpreadsheet;
28 import com.sun.star.sheet.XSpreadsheetDocument;
29 import com.sun.star.sheet.XSpreadsheets;
30 import com.sun.star.table.XCell;
31 import com.sun.star.text.XDependentTextField;
32 import com.sun.star.text.XText;
33 import com.sun.star.text.XTextContent;
34 import com.sun.star.text.XTextCursor;
35 import com.sun.star.text.XTextFieldsSupplier;
36 import com.sun.star.uno.AnyConverter;
37 import com.sun.star.uno.Type;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 import lib.MultiMethodTest;
41 import lib.StatusException;
43 /**
46 public class _XTextFieldsSupplier extends MultiMethodTest {
48 public XTextFieldsSupplier oObj = null; // oObj filled by MultiMethodTest
49 private boolean mDispose = false;
50 private boolean mbCreateFieldMaster = true;
52 /**
53 * Insert some text fields into a cell on the sheet, so this interface test
54 * makes sense.
56 @Override
57 protected void before() {
58 Object o = tEnv.getObjRelation("XTextFieldsSupplier.MAKEENTRY");
59 if (o != null && ((Boolean)o).booleanValue()) {
60 mDispose = true;
61 mbCreateFieldMaster = false;
62 XCell xCell = (XCell)tEnv.getObjRelation("MAKEENTRYINCELL");
64 XSpreadsheetDocument xSheetDoc = (XSpreadsheetDocument)tEnv.getObjRelation("SPREADSHEET");
66 XText oText = null;
67 XTextContent oContent = null;
68 XInterface aField = null;
70 try {
71 // we want to create an instance of ScCellFieldObj.
72 // to do this we must get an MultiServiceFactory.
74 XMultiServiceFactory _oMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, xSheetDoc);
76 aField = (XInterface)
77 _oMSF.createInstance("com.sun.star.text.TextField.URL");
78 oContent = UnoRuntime.queryInterface(XTextContent.class, aField);
80 XSpreadsheets oSheets = xSheetDoc.getSheets() ;
81 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
82 AnyConverter.toObject(
83 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
85 String[] services = _oMSF.getAvailableServiceNames();
86 for (int i=0; i<services.length; i++) {
87 if (services[i].startsWith("com.sun.star.text.FieldMaster")) {
88 mbCreateFieldMaster = true;
89 log.println("service " + i + ": " + services[i]);
93 if (mbCreateFieldMaster) {
94 Object FieldMaster = _oMSF.createInstance("com.sun.star.text.FieldMaster.User");
95 XPropertySet PFieldMaster = UnoRuntime.queryInterface
96 (XPropertySet.class,FieldMaster);
98 XDependentTextField xTF = UnoRuntime.queryInterface(XDependentTextField.class,aField);
100 PFieldMaster.setPropertyValue("Content","Some content");
102 xTF.attachTextFieldMaster(PFieldMaster);
105 oText = UnoRuntime.queryInterface(XText.class, xCell);
106 XTextCursor the_Cursor = oText.createTextCursor();
108 oText.insertTextContent(
109 the_Cursor, oContent, true);
111 } catch (com.sun.star.lang.WrappedTargetException e) {
112 log.println("Exception occurred while creating test Object.");
113 e.printStackTrace(log);
114 throw new StatusException("Couldn't insert textField.URL", e);
115 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
116 log.println("Exception occurred while creating test Object.");
117 e.printStackTrace(log);
118 throw new StatusException("Couldn't insert textField.URL", e);
119 } catch (com.sun.star.lang.IllegalArgumentException e) {
120 log.println("Exception occurred while creating test Object.");
121 e.printStackTrace(log);
122 throw new StatusException("Couldn't insert textField.URL", e);
123 } catch (com.sun.star.uno.Exception e) {
124 log.println("Exception occurred while creating test Object.");
125 e.printStackTrace(log);
126 throw new StatusException("Couldn't insert textField.URL", e);
132 public void _getTextFields() {
133 XEnumerationAccess xEnumAccess = oObj.getTextFields();
134 XEnumeration xEnum = xEnumAccess.createEnumeration();
135 while(xEnum != null && xEnum.hasMoreElements()) {
136 try {
137 xEnum.nextElement();
139 catch(com.sun.star.container.NoSuchElementException e) {
140 setMethodFalse("getTextFields()", e);
142 catch(com.sun.star.lang.WrappedTargetException e) {
143 setMethodFalse("getTextFields()", e);
146 tRes.tested("getTextFields()", xEnum != null);
149 public void _getTextFieldMasters() {
150 if (mbCreateFieldMaster) {
151 XNameAccess xName = oObj.getTextFieldMasters();
152 util.dbg.printInterfaces(xName);
153 tRes.tested("getTextFieldMasters()", xName != null);
155 else {
156 log.println("Could not test 'getTextFieldMasters' because no field masters can be created on this object.");
157 tRes.tested("getTextFieldMasters()", true);
162 * Just for convenience: log the exception and set the method false.
163 * @param method The name of the method to set to false.
164 * @param e The Exception that occurred.
166 private void setMethodFalse(String method, Exception e) {
167 log.println("Exception while executing '" + method + "'");
168 e.printStackTrace(log);
169 tRes.tested(method, false);
172 @Override
173 protected void after() {
174 if (mDispose)
175 disposeEnvironment();