Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / mod / _svx / SvxUnoTextRangeEnumeration.java
blob8fe19154298cd0d45735a8d3a80d5b5993006a9f
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 mod._svx;
21 import java.io.PrintWriter;
23 import lib.TestCase;
24 import lib.TestEnvironment;
25 import lib.TestParameters;
26 import util.DrawTools;
27 import util.SOfficeFactory;
29 import com.sun.star.container.XEnumerationAccess;
30 import com.sun.star.drawing.XShape;
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.text.ControlCharacter;
33 import com.sun.star.text.XSimpleText;
34 import com.sun.star.text.XTextCursor;
35 import com.sun.star.uno.AnyConverter;
36 import com.sun.star.uno.Type;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
40 public class SvxUnoTextRangeEnumeration extends TestCase {
42 static XComponent xDrawDoc;
44 /**
45 * in general this method creates a testdocument
47 * @param tParam class which contains additional test parameters
48 * @param log class to log the test state and result
51 * @see TestParameters
52 * @see PrintWriter
55 @Override
56 protected void initialize( TestParameters tParam, PrintWriter log ) throws Exception {
57 log.println( "creating a drawdoc" );
58 xDrawDoc = DrawTools.createDrawDoc(tParam.getMSF());
61 /**
62 * in general this method disposes the testenvironment and document
64 * @param tParam class which contains additional test parameters
65 * @param log class to log the test state and result
68 * @see TestParameters
69 * @see PrintWriter
72 @Override
73 protected void cleanup( TestParameters tParam, PrintWriter log ) {
74 log.println( " disposing xDrawDoc " );
75 util.DesktopTools.closeDoc(xDrawDoc);
79 /**
80 * creating a TestEnvironment for the interfaces to be tested
82 * @param tParam class which contains additional test parameters
83 * @param log class to log the test state and result
85 * @return Status class
87 * @see TestParameters
88 * @see PrintWriter
90 @Override
91 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
93 XInterface oObj = null;
94 // create testobject here
96 XEnumerationAccess xEA = null ;
97 try {
98 SOfficeFactory SOF = SOfficeFactory.getFactory(tParam.getMSF()) ;
99 XShape oShape = SOF.createShape
100 (xDrawDoc,5000,3500,7500,5000,"Text");
101 DrawTools.getShapes(DrawTools.getDrawPage(xDrawDoc,0)).add(oShape) ;
103 XSimpleText text = UnoRuntime.queryInterface
104 (XSimpleText.class, oShape) ;
106 XTextCursor cursor = text.createTextCursor() ;
108 text.insertString(cursor, "Paragraph 1", false) ;
109 text.insertControlCharacter(cursor,
110 ControlCharacter.PARAGRAPH_BREAK, false) ;
111 text.insertString(cursor, "Paragraph 2", false) ;
112 text.insertControlCharacter(cursor,
113 ControlCharacter.PARAGRAPH_BREAK, false) ;
114 text.insertString(cursor, "Paragraph 3", false) ;
115 text.insertControlCharacter(cursor,
116 ControlCharacter.PARAGRAPH_BREAK, false) ;
118 xEA = UnoRuntime.queryInterface
119 (XEnumerationAccess.class, text) ;
121 XInterface oTextContent = (XInterface) AnyConverter.toObject(
122 new Type(XInterface.class),xEA.createEnumeration().nextElement());
124 xEA = UnoRuntime.queryInterface
125 (XEnumerationAccess.class, oTextContent) ;
127 oObj = xEA.createEnumeration();
129 } catch (Exception e) {
130 log.println("Can't create test object") ;
131 e.printStackTrace(log) ;
134 // create test environment here
135 TestEnvironment tEnv = new TestEnvironment( oObj );
137 tEnv.addObjRelation("ENUM", xEA);
139 return tEnv;
140 } // finish method getTestEnvironment