Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / util / _XChangesBatch.java
blobc0e87f3ada97f7a9f1851580be68ddd3baf81e93
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.util;
21 import com.sun.star.beans.XPropertySet;
22 import com.sun.star.container.XNameReplace;
23 import com.sun.star.util.ElementChange;
24 import lib.MultiMethodTest;
26 import com.sun.star.util.XChangesBatch;
27 import lib.Status;
28 import lib.StatusException;
30 public class _XChangesBatch extends MultiMethodTest {
32 public XChangesBatch oObj;
33 private Object changeElement = null;
34 private Object originalElement = null;
35 private String elementName = null;
36 private XPropertySet xProp = null;
37 private XNameReplace xNameReplace = null;
39 /**
40 * add a change that can be committed
42 @Override
43 protected void before() {
44 changeElement = tEnv.getObjRelation("XChangesBatch.ChangeElement");
45 originalElement = tEnv.getObjRelation("XChangesBatch.OriginalElement");
46 elementName = (String)tEnv.getObjRelation("XChangesBatch.PropertyName");
48 // to do a change, get an XPropertySet
49 xProp = (XPropertySet)tEnv.getObjRelation("XChangesBatch.PropertySet");
50 try {
51 if (originalElement == null && xProp != null)
52 originalElement = xProp.getPropertyValue(elementName);
54 catch(com.sun.star.uno.Exception e) {
55 throw new StatusException("Could not get property '" + elementName + "'.", e);
58 // or get an XNameReplace
59 xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesBatch.NameReplace");
60 try {
61 if (originalElement == null && xNameReplace != null)
62 originalElement = xNameReplace.getByName(elementName);
64 catch(com.sun.star.uno.Exception e) {
65 throw new StatusException("Could not get element by name '" + elementName + "'.", e);
68 if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null)) {
69 log.println(
70 (changeElement == null?"Missing property 'XChangesBatch.ChangeElement'\n":"") +
71 (originalElement == null?"Missing property 'XChangesBatch.OriginalElement'\n":"") +
72 (elementName == null?"Missing property 'XChangesBatch.PropertyName'\n":"") +
73 (xProp == null?"Missing property 'XChangesBatch.PropertySet'":"") +
74 (xNameReplace == null?"Missing property 'XChangesBatch.NameReplace'":"")
76 throw new StatusException("Some needed object relations are missing.", new Exception());
80 public void _commitChanges() {
81 requiredMethod("getPendingChanges()");
82 try {
83 log.println("Committing changes.");
84 oObj.commitChanges();
86 catch(com.sun.star.lang.WrappedTargetException e) {
87 tRes.tested("commitChanges()", Status.exception(e));
88 return;
90 try {
91 executeChange(originalElement);
93 catch(StatusException e) {
94 tRes.tested("hasPendingChanges()", Status.exception(e));
95 return;
98 try {
99 log.println("Commit changes back.");
100 oObj.commitChanges();
102 catch(com.sun.star.lang.WrappedTargetException e) {
103 tRes.tested("commitChanges()", Status.exception(e));
104 return;
106 tRes.tested("commitChanges()", true);
109 public void _getPendingChanges() {
110 requiredMethod("hasPendingChanges()");
111 ElementChange[]changes = oObj.getPendingChanges();
112 if (changes == null) {
113 log.println("Returned changes was 'null'");
114 log.println("It should have been 1 change.");
115 tRes.tested("getPendingChanges()", false);
116 } else if (changes.length != 1) {
117 int amount = changes.length;
118 log.println("Found not the right number of changes: " + amount);
119 log.println("It should have been 1 change.");
120 for (int i=0; i<amount; i++) {
121 System.out.println("Detailed Change " + i + " -> new Element: '" +
122 changes[i].Element.toString() + "' ReplacedElement: '" +
123 changes[i].ReplacedElement.toString() + "'");
125 tRes.tested("getPendingChanges()", false);
127 else {
128 boolean result = changes[0].ReplacedElement.equals(originalElement);
129 result &= changes[0].Element.equals(changeElement);
130 tRes.tested("getPendingChanges()", result);
134 public void _hasPendingChanges() {
135 try {
136 executeChange(changeElement);
138 catch(StatusException e) {
139 tRes.tested("hasPendingChanges()", Status.exception(e));
140 return;
142 boolean hasPendingChanges = oObj.hasPendingChanges();
143 tRes.tested("hasPendingChanges()", hasPendingChanges);
146 private void executeChange(Object element) throws StatusException {
147 if (xProp != null) {
148 try {
149 xProp.setPropertyValue(elementName, element);
151 catch(com.sun.star.uno.Exception e) {
152 throw new StatusException("Could not set property '" + elementName + "'.", e);
155 else if (xNameReplace != null) {
156 try {
157 xNameReplace.replaceByName(elementName, element);
159 catch(com.sun.star.uno.Exception e) {
160 throw new StatusException("Could not replace '" + elementName + "' by name.", e);