1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XChangesBatch.java,v $
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 ************************************************************************/
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
;
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;
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");
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");
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)) {
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()");
94 log
.println("Committing changes.");
97 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
98 tRes
.tested("commitChanges()", Status
.exception(e
));
102 executeChange(originalElement
);
104 catch(StatusException e
) {
105 tRes
.tested("hasPendingChanges()", Status
.exception(e
));
110 log
.println("Commit changes back.");
111 oObj
.commitChanges();
113 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
114 tRes
.tested("commitChanges()", Status
.exception(e
));
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);
139 boolean result
= changes
[0].ReplacedElement
.equals(originalElement
);
140 result
&= changes
[0].Element
.equals(changeElement
);
141 tRes
.tested("getPendingChanges()", result
);
145 public void _hasPendingChanges() {
147 executeChange(changeElement
);
149 catch(StatusException e
) {
150 tRes
.tested("hasPendingChanges()", Status
.exception(e
));
153 boolean hasPendingChanges
= oObj
.hasPendingChanges();
154 tRes
.tested("hasPendingChanges()", hasPendingChanges
);
157 private void executeChange(Object element
) throws StatusException
{
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) {
168 xNameReplace
.replaceByName(elementName
, element
);
170 catch(com
.sun
.star
.uno
.Exception e
) {
171 throw new StatusException("Could not replace '" + elementName
+ "' by name.", e
);