Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / text / _XText.java
blob02e2f574288a0d0b7cdb1faac331a64868a3bb7c
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 lib.MultiMethodTest;
22 import util.XInstCreator;
24 import com.sun.star.container.XIndexAccess;
25 import com.sun.star.text.XText;
26 import com.sun.star.text.XTextContent;
27 import com.sun.star.text.XTextCursor;
28 import com.sun.star.uno.XInterface;
29 import lib.StatusException;
31 /**
32 * Testing <code>com.sun.star.text.XText</code>
33 * interface methods :
34 * <ul>
35 * <li><code> insertTextContent()</code></li>
36 * <li><code> removeTextContent()</code></li>
37 * </ul> <p>
38 * This test needs the following object relations :
39 * <ul>
40 * <li> <code>'XTEXTINFO'</code> (of type <code>lib.XInstCreator</code>):
41 * creator which can create instances of <code>XTextContent</code>
42 * implementations. </li>
43 * <ul> <p>
44 * Test is <b> NOT </b> multithread compliant. <p>
45 * @see com.sun.star.text.XText
47 public class _XText extends MultiMethodTest {
49 public static XText oObj = null; // oObj filled by MultiMethodTest
50 XTextCursor oCursor = null; // textcursor
51 XInstCreator info = null; // instance creator
52 XInterface oInt = null; // instance to insert and remove
54 /**
55 * First an instance of text content is created using relation
56 * and inserted into text. Then the number of contents is checked
57 * using the relation. Second a <code>null</code> content is tried
58 * to insert. <p>
60 * Has <b> OK </b> status if in the first case after inserting number
61 * of content objects is greater than zero and in the second
62 * case <code>IllegalArgumentException</code> is thrown. <p>
64 public void _insertTextContent() {
65 boolean result = false;
66 info = (XInstCreator)tEnv.getObjRelation( "XTEXTINFO" );
67 oInt = info.createInstance();
69 // write to log what we try next
70 log.println( "test for createTextCursor()" );
71 oCursor = oObj.createTextCursor();
73 // write to log what we try next
74 log.println( "test for insertTextContent()" );
75 try {
76 oObj.insertTextContent(oCursor, (XTextContent)oInt, false);
78 catch( com.sun.star.lang.IllegalArgumentException iaE ){
79 throw new StatusException("Couldn't insert textcontent",iaE);
82 // get indexaccess to the tablecollection
83 XIndexAccess xIA = info.getCollection();
85 // this comparison works just because it has to be at least one
86 // table at this point regardless which thread inserted it
87 // there is although the possibility that the first threads call
88 // failed, the second not and comparison happens after second threads
89 // otherwise if something fails it should have thrown an exception
90 //tRes.tested("insertTextContent()", xIA.getCount() > 0 );
92 if (xIA != null ) {
93 result = (xIA.getCount()>0);
94 } else {
95 result = true;
98 if (!result) log.println("The TextContent wasn't inserted");
101 // try to insert an invalid TextContent
102 log.println( "test for insertTextContent" );
103 try {
104 oObj.insertTextContent(oCursor, null, false);
105 log.println("The expected Exception doesn't occurred");
106 result &= false;
108 catch( com.sun.star.lang.IllegalArgumentException iaE ){
109 // Some exception.FAILED
110 log.println("Expected Exception occurred");
111 String msg = iaE.getMessage();
112 if (msg.equals("")) {
113 log.println("But there is not detailed message");
114 } else {
115 log.println("Detailed message: "+msg);
118 result &= true;
121 tRes.tested("insertTextContent()", result );
126 * Removes the text content added before. <p>
127 * Has <b> OK </b> status if the method successfully returns
128 * and no exceptions were thrown. <p>
129 * The following method tests are to be completed successfully before :
130 * <ul>
131 * <li> <code> insertTextContent() </code> : inserts the content
132 * to be removed in this test. </li>
133 * </ul>
135 public void _removeTextContent() {
137 // leads to a method which should be called previously
138 requiredMethod( "insertTextContent()" );
140 // write to log what we try next
141 log.println( "test for removeTextContent" );
142 try {
143 oObj.removeTextContent( (XTextContent)oInt );
145 catch( com.sun.star.container.NoSuchElementException nseE ){
146 // Some exception.FAILED
147 log.println(nseE.toString());
148 return;
151 // no exception occurred so it works
152 tRes.tested( "removeTextContent()", true );
155 } // finish class _XText