1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package ifc.util; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 31*cdf0e10cSrcweir import com.sun.star.container.XNameReplace; 32*cdf0e10cSrcweir import com.sun.star.util.XChangesBatch; 33*cdf0e10cSrcweir import com.sun.star.util.XChangesListener; 34*cdf0e10cSrcweir import com.sun.star.util.XChangesNotifier; 35*cdf0e10cSrcweir import java.io.PrintWriter; 36*cdf0e10cSrcweir import lib.StatusException; 37*cdf0e10cSrcweir import lib.MultiMethodTest; 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir /** 40*cdf0e10cSrcweir * Test the XChangesNotifier interface. To produce some changes, 41*cdf0e10cSrcweir * XChangesBatch is used. 42*cdf0e10cSrcweir * @see com.sun.star.util.XChangesNotifier 43*cdf0e10cSrcweir * @see com.sun.star.util.XChangesBatch 44*cdf0e10cSrcweir */ 45*cdf0e10cSrcweir public class _XChangesNotifier extends MultiMethodTest { 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir public XChangesNotifier oObj = null; 48*cdf0e10cSrcweir private XChangesBatch xBatch = null; 49*cdf0e10cSrcweir private Object changeElement = null; 50*cdf0e10cSrcweir private Object originalElement = null; 51*cdf0e10cSrcweir private String elementName = null; 52*cdf0e10cSrcweir private XPropertySet xProp = null; 53*cdf0e10cSrcweir private XNameReplace xNameReplace = null; 54*cdf0e10cSrcweir private _XChangesNotifier.MyChangesListener xListener = null; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /** 57*cdf0e10cSrcweir * Own implementation of the XChangesListener interface 58*cdf0e10cSrcweir * @see com.sun.star.util.XChangesListener 59*cdf0e10cSrcweir */ 60*cdf0e10cSrcweir private static class MyChangesListener implements XChangesListener { 61*cdf0e10cSrcweir /** Just lo a call of the listener **/ 62*cdf0e10cSrcweir boolean bChangesOccured = false; 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir /** A change did occur 65*cdf0e10cSrcweir * @param changesEvent The event. 66*cdf0e10cSrcweir **/ 67*cdf0e10cSrcweir public void changesOccurred(com.sun.star.util.ChangesEvent changesEvent) { 68*cdf0e10cSrcweir bChangesOccured = true; 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir /** Disposing of the listener 72*cdf0e10cSrcweir * @param eventObject The event. 73*cdf0e10cSrcweir **/ 74*cdf0e10cSrcweir public void disposing(com.sun.star.lang.EventObject eventObject) { 75*cdf0e10cSrcweir bChangesOccured = true; 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir /** 79*cdf0e10cSrcweir * Reset the listener 80*cdf0e10cSrcweir */ 81*cdf0e10cSrcweir public void reset() { 82*cdf0e10cSrcweir bChangesOccured = false; 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir /** 86*cdf0e10cSrcweir * Has the listener been called? 87*cdf0e10cSrcweir * @return True, if the listener has been called. 88*cdf0e10cSrcweir */ 89*cdf0e10cSrcweir public boolean didChangesOccur() { 90*cdf0e10cSrcweir return bChangesOccured; 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir /** 95*cdf0e10cSrcweir * Before the test: get the 'XChangesNotifier.ChangesBatch' object relation 96*cdf0e10cSrcweir * and create the listener. 97*cdf0e10cSrcweir */ 98*cdf0e10cSrcweir protected void before() { 99*cdf0e10cSrcweir xBatch = (XChangesBatch)tEnv.getObjRelation("XChangesNotifier.ChangesBatch"); 100*cdf0e10cSrcweir changeElement = tEnv.getObjRelation("XChangesNotifier.ChangeElement"); 101*cdf0e10cSrcweir originalElement = tEnv.getObjRelation("XChangesNotifier.OriginalElement"); 102*cdf0e10cSrcweir elementName = (String)tEnv.getObjRelation("XChangesNotifier.PropertyName"); 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir xProp = (XPropertySet)tEnv.getObjRelation("XChangesNotifier.PropertySet"); 105*cdf0e10cSrcweir try { 106*cdf0e10cSrcweir if (originalElement == null && xProp != null) 107*cdf0e10cSrcweir originalElement = xProp.getPropertyValue(elementName); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 110*cdf0e10cSrcweir throw new StatusException("Could not get property '" + elementName + "'.", e); 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir // or get an XNameReplace 114*cdf0e10cSrcweir xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesNotifier.NameReplace"); 115*cdf0e10cSrcweir try { 116*cdf0e10cSrcweir if (originalElement == null && xNameReplace != null) 117*cdf0e10cSrcweir originalElement = xNameReplace.getByName(elementName); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 120*cdf0e10cSrcweir throw new StatusException("Could not get element by name '" + elementName + "'.", e); 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null) || xBatch == null) { 124*cdf0e10cSrcweir log.println( 125*cdf0e10cSrcweir changeElement == null?"Missing property 'XChangesNotifier.ChangeElement'\n":"" + 126*cdf0e10cSrcweir originalElement == null?"Missing property 'XChangesNotifier.OriginalElement'\n":"" + 127*cdf0e10cSrcweir elementName == null?"Missing property 'XChangesNotifier.PropertyName'\n":"" + 128*cdf0e10cSrcweir xProp == null?"Missing property 'XChangesNotifier.PropertySet'":"" + 129*cdf0e10cSrcweir xNameReplace == null?"Missing property 'XChangesNotifier.NameReplace'":"" + 130*cdf0e10cSrcweir xBatch == null?"Missing property 'XChangesNotifier.ChangesBatch'":"" 131*cdf0e10cSrcweir ); 132*cdf0e10cSrcweir throw new StatusException("Some needed object relations are missing.", new Exception()); 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir xListener = new _XChangesNotifier.MyChangesListener(); 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir /** test addChangesListener **/ 139*cdf0e10cSrcweir public void _addChangesListener() { 140*cdf0e10cSrcweir oObj.addChangesListener(xListener); 141*cdf0e10cSrcweir tRes.tested("addChangesListener()", true); 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir /** test removeChangesListener **/ 145*cdf0e10cSrcweir public void _removeChangesListener() { 146*cdf0e10cSrcweir requiredMethod("addChangesListener()"); 147*cdf0e10cSrcweir boolean result = true; 148*cdf0e10cSrcweir result &= commitChanges(); 149*cdf0e10cSrcweir result &= xListener.didChangesOccur(); 150*cdf0e10cSrcweir if (!result) 151*cdf0e10cSrcweir log.println("Listener has not been called."); 152*cdf0e10cSrcweir oObj.removeChangesListener(xListener); 153*cdf0e10cSrcweir xListener.reset(); 154*cdf0e10cSrcweir result &= redoChanges(); 155*cdf0e10cSrcweir boolean result2 = xListener.didChangesOccur(); 156*cdf0e10cSrcweir if (result2) 157*cdf0e10cSrcweir log.println("Removed listener has been called."); 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir tRes.tested("removeChangesListener()", result && !result2); 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir /** 163*cdf0e10cSrcweir * Commit a change, using an implementation of the XChangesBatch interface. 164*cdf0e10cSrcweir * @return true, if changing worked. 165*cdf0e10cSrcweir */ 166*cdf0e10cSrcweir private boolean commitChanges() { 167*cdf0e10cSrcweir if (!executeChange(changeElement)) return false; 168*cdf0e10cSrcweir if (!xBatch.hasPendingChanges()) return false; 169*cdf0e10cSrcweir try { 170*cdf0e10cSrcweir xBatch.commitChanges(); 171*cdf0e10cSrcweir } 172*cdf0e10cSrcweir catch(com.sun.star.lang.WrappedTargetException e) { 173*cdf0e10cSrcweir e.printStackTrace((PrintWriter)log); 174*cdf0e10cSrcweir return false; 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir return true; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir /** 180*cdf0e10cSrcweir * Redo the change, using an implementation of the XChangesBatch interface. 181*cdf0e10cSrcweir * @return true, if changing worked. 182*cdf0e10cSrcweir */ 183*cdf0e10cSrcweir private boolean redoChanges() { 184*cdf0e10cSrcweir if (!executeChange(originalElement)) return false; 185*cdf0e10cSrcweir if (!xBatch.hasPendingChanges()) return false; 186*cdf0e10cSrcweir try { 187*cdf0e10cSrcweir xBatch.commitChanges(); 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir catch(com.sun.star.lang.WrappedTargetException e) { 190*cdf0e10cSrcweir e.printStackTrace((PrintWriter)log); 191*cdf0e10cSrcweir return false; 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir return true; 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir /** 197*cdf0e10cSrcweir * Execute the change, use XPropertySet or XNameReplace 198*cdf0e10cSrcweir * @return False, if changing did throw an exception. 199*cdf0e10cSrcweir */ 200*cdf0e10cSrcweir private boolean executeChange(Object element) throws StatusException { 201*cdf0e10cSrcweir if (xProp != null) { 202*cdf0e10cSrcweir try { 203*cdf0e10cSrcweir xProp.setPropertyValue(elementName, element); 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 206*cdf0e10cSrcweir e.printStackTrace((PrintWriter)log); 207*cdf0e10cSrcweir return false; 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir else if (xNameReplace != null) { 211*cdf0e10cSrcweir try { 212*cdf0e10cSrcweir xNameReplace.replaceByName(elementName, element); 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 215*cdf0e10cSrcweir e.printStackTrace((PrintWriter)log); 216*cdf0e10cSrcweir return false; 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir return true; 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir } 223