Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / document / _XViewDataSupplier.java
blob81a92e7c3ea7398935569d1dc2309075859110e2
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 .
18 package ifc.document;
20 import com.sun.star.beans.PropertyValue;
21 import com.sun.star.container.XIndexAccess;
22 import com.sun.star.container.XIndexContainer;
23 import com.sun.star.document.XViewDataSupplier;
24 import com.sun.star.uno.UnoRuntime;
25 import lib.MultiMethodTest;
26 import lib.Status;
28 /**
29 * Check the XViewDataSupplier interface.
30 * Test idea: take the property values from the index access, change one
31 * property value, put this into the index access and write it back.
32 * Get the property value again and check that the change made it.
34 public class _XViewDataSupplier extends MultiMethodTest {
35 public XViewDataSupplier oObj = null;
36 XIndexAccess xAccess = null;
37 PropertyValue[] newProps = null;
38 PropertyValue[] oldProps = null;
39 String myview = "myview1";
41 public void _getViewData() {
42 xAccess = oObj.getViewData();
43 if (xAccess != null) {
44 setViewID(xAccess, myview);
46 tRes.tested("getViewData()", true);
49 public void _setViewData() {
50 if (xAccess == null) {
51 log.println("No view data to change available");
52 tRes.tested("setViewData()", Status.skipped(true));
54 else {
55 // 2do: provide an own implementation of the XIndexAccess to set.
56 // this will work without "setViewData()", it just checks that a
57 // setViewData can be done.
58 oObj.setViewData(xAccess);
59 XIndexAccess xAccess2 = oObj.getViewData();
60 String newView = getViewID(xAccess2);
61 tRes.tested("setViewData()", newView != null && newView.equals(myview));
65 private void setViewID(XIndexAccess xAccess, String value) {
66 XIndexContainer xIndexContainer = UnoRuntime.queryInterface(XIndexContainer.class, xAccess);
67 int count = xAccess.getCount();
68 try {
69 if (count > 0) {
70 oldProps = (PropertyValue[])xAccess.getByIndex(0);
71 newProps = new PropertyValue[oldProps.length];
72 for (int j=0; j<oldProps.length; j++) {
73 newProps[j] = new PropertyValue();
74 newProps[j].Name = oldProps[j].Name;
75 newProps[j].Handle = oldProps[j].Handle;
76 newProps[j].State = oldProps[j].State;
77 if (oldProps[j].Name.equals("ViewId")) {
78 newProps[j].Value = value;
82 xIndexContainer.insertByIndex(0, newProps);
85 catch(Exception e) {
86 e.printStackTrace(log);
90 private String getViewID(XIndexAccess xAccess) {
91 String retValue = null;
92 int count = xAccess.getCount();
93 try {
94 if (count > 0) {
95 oldProps = (PropertyValue[])xAccess.getByIndex(0);
96 for (int j=0; j<oldProps.length; j++) {
97 if (oldProps[j].Name.equals("ViewId")) {
98 retValue = (String)newProps[j].Value;
104 catch(Exception e) {
105 e.printStackTrace(log);
107 return retValue;