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