Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / util / _XChangesNotifier.java
blob73592df4e2481cca08164341bfdd654e48619513
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.XChangesBatch;
24 import com.sun.star.util.XChangesListener;
25 import com.sun.star.util.XChangesNotifier;
26 import lib.StatusException;
27 import lib.MultiMethodTest;
29 /**
30 * Test the XChangesNotifier interface. To produce some changes,
31 * XChangesBatch is used.
32 * @see com.sun.star.util.XChangesNotifier
33 * @see com.sun.star.util.XChangesBatch
35 public class _XChangesNotifier extends MultiMethodTest {
37 public XChangesNotifier oObj = null;
38 private XChangesBatch xBatch = null;
39 private Object changeElement = null;
40 private Object originalElement = null;
41 private String elementName = null;
42 private XPropertySet xProp = null;
43 private XNameReplace xNameReplace = null;
44 private _XChangesNotifier.MyChangesListener xListener = null;
46 /**
47 * Own implementation of the XChangesListener interface
48 * @see com.sun.star.util.XChangesListener
50 private static class MyChangesListener implements XChangesListener {
51 /** Just lo a call of the listener **/
52 boolean bChangesOccurred = false;
54 /** A change did occur
55 * @param changesEvent The event.
56 **/
57 public void changesOccurred(com.sun.star.util.ChangesEvent changesEvent) {
58 bChangesOccurred = true;
61 /** Disposing of the listener
62 * @param eventObject The event.
63 **/
64 public void disposing(com.sun.star.lang.EventObject eventObject) {
65 bChangesOccurred = true;
68 /**
69 * Reset the listener
71 public void reset() {
72 bChangesOccurred = false;
75 /**
76 * Has the listener been called?
77 * @return True, if the listener has been called.
79 public boolean didChangesOccur() {
80 return bChangesOccurred;
84 /**
85 * Before the test: get the 'XChangesNotifier.ChangesBatch' object relation
86 * and create the listener.
88 @Override
89 protected void before() {
90 xBatch = (XChangesBatch)tEnv.getObjRelation("XChangesNotifier.ChangesBatch");
91 changeElement = tEnv.getObjRelation("XChangesNotifier.ChangeElement");
92 originalElement = tEnv.getObjRelation("XChangesNotifier.OriginalElement");
93 elementName = (String)tEnv.getObjRelation("XChangesNotifier.PropertyName");
95 xProp = (XPropertySet)tEnv.getObjRelation("XChangesNotifier.PropertySet");
96 try {
97 if (originalElement == null && xProp != null)
98 originalElement = xProp.getPropertyValue(elementName);
100 catch(com.sun.star.uno.Exception e) {
101 throw new StatusException("Could not get property '" + elementName + "'.", e);
104 // or get an XNameReplace
105 xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesNotifier.NameReplace");
106 try {
107 if (originalElement == null && xNameReplace != null)
108 originalElement = xNameReplace.getByName(elementName);
110 catch(com.sun.star.uno.Exception e) {
111 throw new StatusException("Could not get element by name '" + elementName + "'.", e);
114 if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null) || xBatch == null) {
115 log.println(
116 (changeElement == null?"Missing property 'XChangesNotifier.ChangeElement'\n":"") +
117 (originalElement == null?"Missing property 'XChangesNotifier.OriginalElement'\n":"") +
118 (elementName == null?"Missing property 'XChangesNotifier.PropertyName'\n":"") +
119 (xProp == null?"Missing property 'XChangesNotifier.PropertySet'":"") +
120 (xNameReplace == null?"Missing property 'XChangesNotifier.NameReplace'":"") +
121 (xBatch == null?"Missing property 'XChangesNotifier.ChangesBatch'":"")
123 throw new StatusException("Some needed object relations are missing.", new Exception());
126 xListener = new _XChangesNotifier.MyChangesListener();
129 /** test addChangesListener **/
130 public void _addChangesListener() {
131 oObj.addChangesListener(xListener);
132 tRes.tested("addChangesListener()", true);
135 /** test removeChangesListener **/
136 public void _removeChangesListener() {
137 requiredMethod("addChangesListener()");
138 boolean result = true;
139 result &= commitChanges();
140 result &= xListener.didChangesOccur();
141 if (!result)
142 log.println("Listener has not been called.");
143 oObj.removeChangesListener(xListener);
144 xListener.reset();
145 result &= redoChanges();
146 boolean result2 = xListener.didChangesOccur();
147 if (result2)
148 log.println("Removed listener has been called.");
150 tRes.tested("removeChangesListener()", result && !result2);
154 * Commit a change, using an implementation of the XChangesBatch interface.
155 * @return true, if changing worked.
157 private boolean commitChanges() {
158 if (!executeChange(changeElement)) return false;
159 if (!xBatch.hasPendingChanges()) return false;
160 try {
161 xBatch.commitChanges();
163 catch(com.sun.star.lang.WrappedTargetException e) {
164 e.printStackTrace(log);
165 return false;
167 return true;
171 * Redo the change, using an implementation of the XChangesBatch interface.
172 * @return true, if changing worked.
174 private boolean redoChanges() {
175 if (!executeChange(originalElement)) return false;
176 if (!xBatch.hasPendingChanges()) return false;
177 try {
178 xBatch.commitChanges();
180 catch(com.sun.star.lang.WrappedTargetException e) {
181 e.printStackTrace(log);
182 return false;
184 return true;
188 * Execute the change, use XPropertySet or XNameReplace
189 * @return False, if changing did throw an exception.
191 private boolean executeChange(Object element) throws StatusException {
192 if (xProp != null) {
193 try {
194 xProp.setPropertyValue(elementName, element);
196 catch(com.sun.star.uno.Exception e) {
197 e.printStackTrace(log);
198 return false;
201 else if (xNameReplace != null) {
202 try {
203 xNameReplace.replaceByName(elementName, element);
205 catch(com.sun.star.uno.Exception e) {
206 e.printStackTrace(log);
207 return false;
210 return true;