1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import com
.sun
.star
.beans
.XPropertySet
;
31 import com
.sun
.star
.container
.XNameReplace
;
32 import com
.sun
.star
.util
.ElementChange
;
33 import lib
.MultiMethodTest
;
35 import com
.sun
.star
.util
.XChangesBatch
;
37 import lib
.StatusException
;
39 public class _XChangesBatch
extends MultiMethodTest
{
41 public XChangesBatch oObj
;
42 private Object changeElement
= null;
43 private Object originalElement
= null;
44 private String elementName
= null;
45 private XPropertySet xProp
= null;
46 private XNameReplace xNameReplace
= null;
49 * add a change that can be committed
51 protected void before() {
52 changeElement
= tEnv
.getObjRelation("XChangesBatch.ChangeElement");
53 originalElement
= tEnv
.getObjRelation("XChangesBatch.OriginalElement");
54 elementName
= (String
)tEnv
.getObjRelation("XChangesBatch.PropertyName");
56 // to do a change, get an XPropertySet
57 xProp
= (XPropertySet
)tEnv
.getObjRelation("XChangesBatch.PropertySet");
59 if (originalElement
== null && xProp
!= null)
60 originalElement
= xProp
.getPropertyValue(elementName
);
62 catch(com
.sun
.star
.uno
.Exception e
) {
63 throw new StatusException("Could not get property '" + elementName
+ "'.", e
);
66 // or get an XNameReplace
67 xNameReplace
= (XNameReplace
)tEnv
.getObjRelation("XChangesBatch.NameReplace");
69 if (originalElement
== null && xNameReplace
!= null)
70 originalElement
= xNameReplace
.getByName(elementName
);
72 catch(com
.sun
.star
.uno
.Exception e
) {
73 throw new StatusException("Could not get element by name '" + elementName
+ "'.", e
);
76 if (changeElement
== null || originalElement
== null || elementName
== null || (xProp
== null && xNameReplace
== null)) {
78 changeElement
== null?
"Missing property 'XChangesBatch.ChangeElement'\n":"" +
79 originalElement
== null?
"Missing property 'XChangesBatch.OriginalElement'\n":"" +
80 elementName
== null?
"Missing property 'XChangesBatch.PropertyName'\n":"" +
81 xProp
== null?
"Missing property 'XChangesBatch.PropertySet'":"" +
82 xNameReplace
== null?
"Missing property 'XChangesBatch.NameReplace'":""
84 throw new StatusException("Some needed object relations are missing.", new Exception());
88 public void _commitChanges() {
89 requiredMethod("getPendingChanges()");
91 log
.println("Committing changes.");
94 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
95 tRes
.tested("commitChanges()", Status
.exception(e
));
99 executeChange(originalElement
);
101 catch(StatusException e
) {
102 tRes
.tested("hasPendingChanges()", Status
.exception(e
));
107 log
.println("Commit changes back.");
108 oObj
.commitChanges();
110 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
111 tRes
.tested("commitChanges()", Status
.exception(e
));
114 tRes
.tested("commitChanges()", true);
117 public void _getPendingChanges() {
118 requiredMethod("hasPendingChanges()");
119 ElementChange
[]changes
= oObj
.getPendingChanges();
120 if (changes
== null) {
121 log
.println("Returned changes was 'null'");
122 log
.println("It should have been 1 change.");
123 tRes
.tested("getPendingChanges()", false);
124 } else if (changes
.length
!= 1) {
125 int amount
= changes
.length
;
126 log
.println("Found not the right number of changes: " + amount
);
127 log
.println("It should have been 1 change.");
128 for (int i
=0; i
<amount
; i
++) {
129 System
.out
.println("Detailed Change " + i
+ " -> new Element: '" +
130 changes
[i
].Element
.toString() + "' ReplacedElement: '" +
131 changes
[i
].ReplacedElement
.toString() + "'");
133 tRes
.tested("getPendingChanges()", false);
136 boolean result
= changes
[0].ReplacedElement
.equals(originalElement
);
137 result
&= changes
[0].Element
.equals(changeElement
);
138 tRes
.tested("getPendingChanges()", result
);
142 public void _hasPendingChanges() {
144 executeChange(changeElement
);
146 catch(StatusException e
) {
147 tRes
.tested("hasPendingChanges()", Status
.exception(e
));
150 boolean hasPendingChanges
= oObj
.hasPendingChanges();
151 tRes
.tested("hasPendingChanges()", hasPendingChanges
);
154 private void executeChange(Object element
) throws StatusException
{
157 xProp
.setPropertyValue(elementName
, element
);
159 catch(com
.sun
.star
.uno
.Exception e
) {
160 throw new StatusException("Could not set property '" + elementName
+ "'.", e
);
163 else if (xNameReplace
!= null) {
165 xNameReplace
.replaceByName(elementName
, element
);
167 catch(com
.sun
.star
.uno
.Exception e
) {
168 throw new StatusException("Could not replace '" + elementName
+ "' by name.", e
);