merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / util / _XChangesBatch.java
blobe0da6419a8d50326927f42981cfd5276692df981
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XChangesBatch.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.util;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.container.XNameReplace;
35 import com.sun.star.util.ElementChange;
36 import lib.MultiMethodTest;
38 import com.sun.star.util.XChangesBatch;
39 import lib.Status;
40 import lib.StatusException;
42 public class _XChangesBatch extends MultiMethodTest {
44 public XChangesBatch oObj;
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;
51 /**
52 * add a change that can be committed
54 protected void before() {
55 changeElement = tEnv.getObjRelation("XChangesBatch.ChangeElement");
56 originalElement = tEnv.getObjRelation("XChangesBatch.OriginalElement");
57 elementName = (String)tEnv.getObjRelation("XChangesBatch.PropertyName");
59 // to do a change, get an XPropertySet
60 xProp = (XPropertySet)tEnv.getObjRelation("XChangesBatch.PropertySet");
61 try {
62 if (originalElement == null && xProp != null)
63 originalElement = xProp.getPropertyValue(elementName);
65 catch(com.sun.star.uno.Exception e) {
66 throw new StatusException("Could not get property '" + elementName + "'.", e);
69 // or get an XNameReplace
70 xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesBatch.NameReplace");
71 try {
72 if (originalElement == null && xNameReplace != null)
73 originalElement = xNameReplace.getByName(elementName);
75 catch(com.sun.star.uno.Exception e) {
76 throw new StatusException("Could not get element by name '" + elementName + "'.", e);
79 if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null)) {
80 log.println(
81 changeElement == null?"Missing property 'XChangesBatch.ChangeElement'\n":"" +
82 originalElement == null?"Missing property 'XChangesBatch.OriginalElement'\n":"" +
83 elementName == null?"Missing property 'XChangesBatch.PropertyName'\n":"" +
84 xProp == null?"Missing property 'XChangesBatch.PropertySet'":"" +
85 xNameReplace == null?"Missing property 'XChangesBatch.NameReplace'":""
87 throw new StatusException("Some needed object relations are missing.", new Exception());
91 public void _commitChanges() {
92 requiredMethod("getPendingChanges()");
93 try {
94 log.println("Committing changes.");
95 oObj.commitChanges();
97 catch(com.sun.star.lang.WrappedTargetException e) {
98 tRes.tested("commitChanges()", Status.exception(e));
99 return;
101 try {
102 executeChange(originalElement);
104 catch(StatusException e) {
105 tRes.tested("hasPendingChanges()", Status.exception(e));
106 return;
109 try {
110 log.println("Commit changes back.");
111 oObj.commitChanges();
113 catch(com.sun.star.lang.WrappedTargetException e) {
114 tRes.tested("commitChanges()", Status.exception(e));
115 return;
117 tRes.tested("commitChanges()", true);
120 public void _getPendingChanges() {
121 requiredMethod("hasPendingChanges()");
122 ElementChange[]changes = oObj.getPendingChanges();
123 if (changes == null) {
124 log.println("Returned changes was 'null'");
125 log.println("It should have been 1 change.");
126 tRes.tested("getPendingChanges()", false);
127 } else if (changes.length != 1) {
128 int amount = changes.length;
129 log.println("Found not the right number of changes: " + amount);
130 log.println("It should have been 1 change.");
131 for (int i=0; i<amount; i++) {
132 System.out.println("Detailed Change " + i + " -> new Element: '" +
133 changes[i].Element.toString() + "' ReplacedElement: '" +
134 changes[i].ReplacedElement.toString() + "'");
136 tRes.tested("getPendingChanges()", false);
138 else {
139 boolean result = changes[0].ReplacedElement.equals(originalElement);
140 result &= changes[0].Element.equals(changeElement);
141 tRes.tested("getPendingChanges()", result);
145 public void _hasPendingChanges() {
146 try {
147 executeChange(changeElement);
149 catch(StatusException e) {
150 tRes.tested("hasPendingChanges()", Status.exception(e));
151 return;
153 boolean hasPendingChanges = oObj.hasPendingChanges();
154 tRes.tested("hasPendingChanges()", hasPendingChanges);
157 private void executeChange(Object element) throws StatusException {
158 if (xProp != null) {
159 try {
160 xProp.setPropertyValue(elementName, element);
162 catch(com.sun.star.uno.Exception e) {
163 throw new StatusException("Could not set property '" + elementName + "'.", e);
166 else if (xNameReplace != null) {
167 try {
168 xNameReplace.replaceByName(elementName, element);
170 catch(com.sun.star.uno.Exception e) {
171 throw new StatusException("Could not replace '" + elementName + "' by name.", e);