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.ElementChange; 29cdf0e10cSrcweir import lib.MultiMethodTest; 30cdf0e10cSrcweir 31cdf0e10cSrcweir import com.sun.star.util.XChangesBatch; 32cdf0e10cSrcweir import lib.Status; 33cdf0e10cSrcweir import lib.StatusException; 34cdf0e10cSrcweir 35cdf0e10cSrcweir public class _XChangesBatch extends MultiMethodTest { 36cdf0e10cSrcweir 37cdf0e10cSrcweir public XChangesBatch oObj; 38cdf0e10cSrcweir private Object changeElement = null; 39cdf0e10cSrcweir private Object originalElement = null; 40cdf0e10cSrcweir private String elementName = null; 41cdf0e10cSrcweir private XPropertySet xProp = null; 42cdf0e10cSrcweir private XNameReplace xNameReplace = null; 43cdf0e10cSrcweir 44cdf0e10cSrcweir /** 45cdf0e10cSrcweir * add a change that can be committed 46cdf0e10cSrcweir */ before()47cdf0e10cSrcweir protected void before() { 48cdf0e10cSrcweir changeElement = tEnv.getObjRelation("XChangesBatch.ChangeElement"); 49cdf0e10cSrcweir originalElement = tEnv.getObjRelation("XChangesBatch.OriginalElement"); 50cdf0e10cSrcweir elementName = (String)tEnv.getObjRelation("XChangesBatch.PropertyName"); 51cdf0e10cSrcweir 52cdf0e10cSrcweir // to do a change, get an XPropertySet 53cdf0e10cSrcweir xProp = (XPropertySet)tEnv.getObjRelation("XChangesBatch.PropertySet"); 54cdf0e10cSrcweir try { 55cdf0e10cSrcweir if (originalElement == null && xProp != null) 56cdf0e10cSrcweir originalElement = xProp.getPropertyValue(elementName); 57cdf0e10cSrcweir } 58cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 59cdf0e10cSrcweir throw new StatusException("Could not get property '" + elementName + "'.", e); 60cdf0e10cSrcweir } 61cdf0e10cSrcweir 62cdf0e10cSrcweir // or get an XNameReplace 63cdf0e10cSrcweir xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesBatch.NameReplace"); 64cdf0e10cSrcweir try { 65cdf0e10cSrcweir if (originalElement == null && xNameReplace != null) 66cdf0e10cSrcweir originalElement = xNameReplace.getByName(elementName); 67cdf0e10cSrcweir } 68cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 69cdf0e10cSrcweir throw new StatusException("Could not get element by name '" + elementName + "'.", e); 70cdf0e10cSrcweir } 71cdf0e10cSrcweir 72cdf0e10cSrcweir if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null)) { 73cdf0e10cSrcweir log.println( 74cdf0e10cSrcweir changeElement == null?"Missing property 'XChangesBatch.ChangeElement'\n":"" + 75cdf0e10cSrcweir originalElement == null?"Missing property 'XChangesBatch.OriginalElement'\n":"" + 76cdf0e10cSrcweir elementName == null?"Missing property 'XChangesBatch.PropertyName'\n":"" + 77cdf0e10cSrcweir xProp == null?"Missing property 'XChangesBatch.PropertySet'":"" + 78cdf0e10cSrcweir xNameReplace == null?"Missing property 'XChangesBatch.NameReplace'":"" 79cdf0e10cSrcweir ); 80cdf0e10cSrcweir throw new StatusException("Some needed object relations are missing.", new Exception()); 81cdf0e10cSrcweir } 82cdf0e10cSrcweir } 83cdf0e10cSrcweir _commitChanges()84cdf0e10cSrcweir public void _commitChanges() { 85cdf0e10cSrcweir requiredMethod("getPendingChanges()"); 86cdf0e10cSrcweir try { 87cdf0e10cSrcweir log.println("Committing changes."); 88cdf0e10cSrcweir oObj.commitChanges(); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir catch(com.sun.star.lang.WrappedTargetException e) { 91cdf0e10cSrcweir tRes.tested("commitChanges()", Status.exception(e)); 92cdf0e10cSrcweir return; 93cdf0e10cSrcweir } 94cdf0e10cSrcweir try { 95cdf0e10cSrcweir executeChange(originalElement); 96cdf0e10cSrcweir } 97cdf0e10cSrcweir catch(StatusException e) { 98cdf0e10cSrcweir tRes.tested("hasPendingChanges()", Status.exception(e)); 99cdf0e10cSrcweir return; 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir try { 103cdf0e10cSrcweir log.println("Commit changes back."); 104cdf0e10cSrcweir oObj.commitChanges(); 105cdf0e10cSrcweir } 106cdf0e10cSrcweir catch(com.sun.star.lang.WrappedTargetException e) { 107cdf0e10cSrcweir tRes.tested("commitChanges()", Status.exception(e)); 108cdf0e10cSrcweir return; 109cdf0e10cSrcweir } 110cdf0e10cSrcweir tRes.tested("commitChanges()", true); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir _getPendingChanges()113cdf0e10cSrcweir public void _getPendingChanges() { 114cdf0e10cSrcweir requiredMethod("hasPendingChanges()"); 115cdf0e10cSrcweir ElementChange[]changes = oObj.getPendingChanges(); 116cdf0e10cSrcweir if (changes == null) { 117cdf0e10cSrcweir log.println("Returned changes was 'null'"); 118cdf0e10cSrcweir log.println("It should have been 1 change."); 119cdf0e10cSrcweir tRes.tested("getPendingChanges()", false); 120cdf0e10cSrcweir } else if (changes.length != 1) { 121cdf0e10cSrcweir int amount = changes.length; 122cdf0e10cSrcweir log.println("Found not the right number of changes: " + amount); 123cdf0e10cSrcweir log.println("It should have been 1 change."); 124cdf0e10cSrcweir for (int i=0; i<amount; i++) { 125cdf0e10cSrcweir System.out.println("Detailed Change " + i + " -> new Element: '" + 126cdf0e10cSrcweir changes[i].Element.toString() + "' ReplacedElement: '" + 127cdf0e10cSrcweir changes[i].ReplacedElement.toString() + "'"); 128cdf0e10cSrcweir } 129cdf0e10cSrcweir tRes.tested("getPendingChanges()", false); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir else { 132cdf0e10cSrcweir boolean result = changes[0].ReplacedElement.equals(originalElement); 133cdf0e10cSrcweir result &= changes[0].Element.equals(changeElement); 134cdf0e10cSrcweir tRes.tested("getPendingChanges()", result); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir } 137cdf0e10cSrcweir _hasPendingChanges()138cdf0e10cSrcweir public void _hasPendingChanges() { 139cdf0e10cSrcweir try { 140cdf0e10cSrcweir executeChange(changeElement); 141cdf0e10cSrcweir } 142cdf0e10cSrcweir catch(StatusException e) { 143cdf0e10cSrcweir tRes.tested("hasPendingChanges()", Status.exception(e)); 144cdf0e10cSrcweir return; 145cdf0e10cSrcweir } 146cdf0e10cSrcweir boolean hasPendingChanges = oObj.hasPendingChanges(); 147cdf0e10cSrcweir tRes.tested("hasPendingChanges()", hasPendingChanges); 148cdf0e10cSrcweir } 149cdf0e10cSrcweir executeChange(Object element)150cdf0e10cSrcweir private void executeChange(Object element) throws StatusException { 151cdf0e10cSrcweir if (xProp != null) { 152cdf0e10cSrcweir try { 153cdf0e10cSrcweir xProp.setPropertyValue(elementName, element); 154cdf0e10cSrcweir } 155cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 156cdf0e10cSrcweir throw new StatusException("Could not set property '" + elementName + "'.", e); 157cdf0e10cSrcweir } 158cdf0e10cSrcweir } 159cdf0e10cSrcweir else if (xNameReplace != null) { 160cdf0e10cSrcweir try { 161cdf0e10cSrcweir xNameReplace.replaceByName(elementName, element); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 164cdf0e10cSrcweir throw new StatusException("Could not replace '" + elementName + "' by name.", e); 165cdf0e10cSrcweir } 166cdf0e10cSrcweir } 167cdf0e10cSrcweir } 168cdf0e10cSrcweir } 169