1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package ifc.util; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 27cdf0e10cSrcweir import com.sun.star.container.XNameReplace; 28cdf0e10cSrcweir import com.sun.star.util.XChangesBatch; 29cdf0e10cSrcweir import com.sun.star.util.XChangesListener; 30cdf0e10cSrcweir import com.sun.star.util.XChangesNotifier; 31cdf0e10cSrcweir import java.io.PrintWriter; 32cdf0e10cSrcweir import lib.StatusException; 33cdf0e10cSrcweir import lib.MultiMethodTest; 34cdf0e10cSrcweir 35cdf0e10cSrcweir /** 36cdf0e10cSrcweir * Test the XChangesNotifier interface. To produce some changes, 37cdf0e10cSrcweir * XChangesBatch is used. 38cdf0e10cSrcweir * @see com.sun.star.util.XChangesNotifier 39cdf0e10cSrcweir * @see com.sun.star.util.XChangesBatch 40cdf0e10cSrcweir */ 41cdf0e10cSrcweir public class _XChangesNotifier extends MultiMethodTest { 42cdf0e10cSrcweir 43cdf0e10cSrcweir public XChangesNotifier oObj = null; 44cdf0e10cSrcweir private XChangesBatch xBatch = null; 45cdf0e10cSrcweir private Object changeElement = null; 46cdf0e10cSrcweir private Object originalElement = null; 47cdf0e10cSrcweir private String elementName = null; 48cdf0e10cSrcweir private XPropertySet xProp = null; 49cdf0e10cSrcweir private XNameReplace xNameReplace = null; 50cdf0e10cSrcweir private _XChangesNotifier.MyChangesListener xListener = null; 51cdf0e10cSrcweir 52cdf0e10cSrcweir /** 53cdf0e10cSrcweir * Own implementation of the XChangesListener interface 54cdf0e10cSrcweir * @see com.sun.star.util.XChangesListener 55cdf0e10cSrcweir */ 56cdf0e10cSrcweir private static class MyChangesListener implements XChangesListener { 57cdf0e10cSrcweir /** Just lo a call of the listener **/ 58cdf0e10cSrcweir boolean bChangesOccured = false; 59cdf0e10cSrcweir 60cdf0e10cSrcweir /** A change did occur 61cdf0e10cSrcweir * @param changesEvent The event. 62cdf0e10cSrcweir **/ changesOccurred(com.sun.star.util.ChangesEvent changesEvent)63cdf0e10cSrcweir public void changesOccurred(com.sun.star.util.ChangesEvent changesEvent) { 64cdf0e10cSrcweir bChangesOccured = true; 65cdf0e10cSrcweir } 66cdf0e10cSrcweir 67cdf0e10cSrcweir /** Disposing of the listener 68cdf0e10cSrcweir * @param eventObject The event. 69cdf0e10cSrcweir **/ disposing(com.sun.star.lang.EventObject eventObject)70cdf0e10cSrcweir public void disposing(com.sun.star.lang.EventObject eventObject) { 71cdf0e10cSrcweir bChangesOccured = true; 72cdf0e10cSrcweir } 73cdf0e10cSrcweir 74cdf0e10cSrcweir /** 75cdf0e10cSrcweir * Reset the listener 76cdf0e10cSrcweir */ reset()77cdf0e10cSrcweir public void reset() { 78cdf0e10cSrcweir bChangesOccured = false; 79cdf0e10cSrcweir } 80cdf0e10cSrcweir 81cdf0e10cSrcweir /** 82cdf0e10cSrcweir * Has the listener been called? 83cdf0e10cSrcweir * @return True, if the listener has been called. 84cdf0e10cSrcweir */ didChangesOccur()85cdf0e10cSrcweir public boolean didChangesOccur() { 86cdf0e10cSrcweir return bChangesOccured; 87cdf0e10cSrcweir } 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90cdf0e10cSrcweir /** 91cdf0e10cSrcweir * Before the test: get the 'XChangesNotifier.ChangesBatch' object relation 92cdf0e10cSrcweir * and create the listener. 93cdf0e10cSrcweir */ before()94cdf0e10cSrcweir protected void before() { 95cdf0e10cSrcweir xBatch = (XChangesBatch)tEnv.getObjRelation("XChangesNotifier.ChangesBatch"); 96cdf0e10cSrcweir changeElement = tEnv.getObjRelation("XChangesNotifier.ChangeElement"); 97cdf0e10cSrcweir originalElement = tEnv.getObjRelation("XChangesNotifier.OriginalElement"); 98cdf0e10cSrcweir elementName = (String)tEnv.getObjRelation("XChangesNotifier.PropertyName"); 99cdf0e10cSrcweir 100cdf0e10cSrcweir xProp = (XPropertySet)tEnv.getObjRelation("XChangesNotifier.PropertySet"); 101cdf0e10cSrcweir try { 102cdf0e10cSrcweir if (originalElement == null && xProp != null) 103cdf0e10cSrcweir originalElement = xProp.getPropertyValue(elementName); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 106cdf0e10cSrcweir throw new StatusException("Could not get property '" + elementName + "'.", e); 107cdf0e10cSrcweir } 108cdf0e10cSrcweir 109cdf0e10cSrcweir // or get an XNameReplace 110cdf0e10cSrcweir xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesNotifier.NameReplace"); 111cdf0e10cSrcweir try { 112cdf0e10cSrcweir if (originalElement == null && xNameReplace != null) 113cdf0e10cSrcweir originalElement = xNameReplace.getByName(elementName); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 116cdf0e10cSrcweir throw new StatusException("Could not get element by name '" + elementName + "'.", e); 117cdf0e10cSrcweir } 118cdf0e10cSrcweir 119cdf0e10cSrcweir if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null) || xBatch == null) { 120cdf0e10cSrcweir log.println( 121cdf0e10cSrcweir changeElement == null?"Missing property 'XChangesNotifier.ChangeElement'\n":"" + 122cdf0e10cSrcweir originalElement == null?"Missing property 'XChangesNotifier.OriginalElement'\n":"" + 123cdf0e10cSrcweir elementName == null?"Missing property 'XChangesNotifier.PropertyName'\n":"" + 124cdf0e10cSrcweir xProp == null?"Missing property 'XChangesNotifier.PropertySet'":"" + 125cdf0e10cSrcweir xNameReplace == null?"Missing property 'XChangesNotifier.NameReplace'":"" + 126cdf0e10cSrcweir xBatch == null?"Missing property 'XChangesNotifier.ChangesBatch'":"" 127cdf0e10cSrcweir ); 128cdf0e10cSrcweir throw new StatusException("Some needed object relations are missing.", new Exception()); 129cdf0e10cSrcweir } 130cdf0e10cSrcweir 131cdf0e10cSrcweir xListener = new _XChangesNotifier.MyChangesListener(); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir /** test addChangesListener **/ _addChangesListener()135cdf0e10cSrcweir public void _addChangesListener() { 136cdf0e10cSrcweir oObj.addChangesListener(xListener); 137cdf0e10cSrcweir tRes.tested("addChangesListener()", true); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir /** test removeChangesListener **/ _removeChangesListener()141cdf0e10cSrcweir public void _removeChangesListener() { 142cdf0e10cSrcweir requiredMethod("addChangesListener()"); 143cdf0e10cSrcweir boolean result = true; 144cdf0e10cSrcweir result &= commitChanges(); 145cdf0e10cSrcweir result &= xListener.didChangesOccur(); 146cdf0e10cSrcweir if (!result) 147cdf0e10cSrcweir log.println("Listener has not been called."); 148cdf0e10cSrcweir oObj.removeChangesListener(xListener); 149cdf0e10cSrcweir xListener.reset(); 150cdf0e10cSrcweir result &= redoChanges(); 151cdf0e10cSrcweir boolean result2 = xListener.didChangesOccur(); 152cdf0e10cSrcweir if (result2) 153cdf0e10cSrcweir log.println("Removed listener has been called."); 154cdf0e10cSrcweir 155cdf0e10cSrcweir tRes.tested("removeChangesListener()", result && !result2); 156cdf0e10cSrcweir } 157cdf0e10cSrcweir 158cdf0e10cSrcweir /** 159cdf0e10cSrcweir * Commit a change, using an implementation of the XChangesBatch interface. 160cdf0e10cSrcweir * @return true, if changing worked. 161cdf0e10cSrcweir */ commitChanges()162cdf0e10cSrcweir private boolean commitChanges() { 163cdf0e10cSrcweir if (!executeChange(changeElement)) return false; 164cdf0e10cSrcweir if (!xBatch.hasPendingChanges()) return false; 165cdf0e10cSrcweir try { 166cdf0e10cSrcweir xBatch.commitChanges(); 167cdf0e10cSrcweir } 168cdf0e10cSrcweir catch(com.sun.star.lang.WrappedTargetException e) { 169cdf0e10cSrcweir e.printStackTrace((PrintWriter)log); 170cdf0e10cSrcweir return false; 171cdf0e10cSrcweir } 172cdf0e10cSrcweir return true; 173cdf0e10cSrcweir } 174cdf0e10cSrcweir 175cdf0e10cSrcweir /** 176cdf0e10cSrcweir * Redo the change, using an implementation of the XChangesBatch interface. 177cdf0e10cSrcweir * @return true, if changing worked. 178cdf0e10cSrcweir */ redoChanges()179cdf0e10cSrcweir private boolean redoChanges() { 180cdf0e10cSrcweir if (!executeChange(originalElement)) return false; 181cdf0e10cSrcweir if (!xBatch.hasPendingChanges()) return false; 182cdf0e10cSrcweir try { 183cdf0e10cSrcweir xBatch.commitChanges(); 184cdf0e10cSrcweir } 185cdf0e10cSrcweir catch(com.sun.star.lang.WrappedTargetException e) { 186cdf0e10cSrcweir e.printStackTrace((PrintWriter)log); 187cdf0e10cSrcweir return false; 188cdf0e10cSrcweir } 189cdf0e10cSrcweir return true; 190cdf0e10cSrcweir } 191cdf0e10cSrcweir 192cdf0e10cSrcweir /** 193cdf0e10cSrcweir * Execute the change, use XPropertySet or XNameReplace 194cdf0e10cSrcweir * @return False, if changing did throw an exception. 195cdf0e10cSrcweir */ executeChange(Object element)196cdf0e10cSrcweir private boolean executeChange(Object element) throws StatusException { 197cdf0e10cSrcweir if (xProp != null) { 198cdf0e10cSrcweir try { 199cdf0e10cSrcweir xProp.setPropertyValue(elementName, element); 200cdf0e10cSrcweir } 201cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 202cdf0e10cSrcweir e.printStackTrace((PrintWriter)log); 203cdf0e10cSrcweir return false; 204cdf0e10cSrcweir } 205cdf0e10cSrcweir } 206cdf0e10cSrcweir else if (xNameReplace != null) { 207cdf0e10cSrcweir try { 208cdf0e10cSrcweir xNameReplace.replaceByName(elementName, element); 209cdf0e10cSrcweir } 210cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 211cdf0e10cSrcweir e.printStackTrace((PrintWriter)log); 212cdf0e10cSrcweir return false; 213cdf0e10cSrcweir } 214cdf0e10cSrcweir } 215cdf0e10cSrcweir return true; 216cdf0e10cSrcweir } 217cdf0e10cSrcweir 218cdf0e10cSrcweir } 219