2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import com
.sun
.star
.beans
.XPropertySet
;
22 import com
.sun
.star
.container
.XNameReplace
;
23 import com
.sun
.star
.util
.ElementChange
;
24 import lib
.MultiMethodTest
;
26 import com
.sun
.star
.util
.XChangesBatch
;
28 import lib
.StatusException
;
30 public class _XChangesBatch
extends MultiMethodTest
{
32 public XChangesBatch oObj
;
33 private Object changeElement
= null;
34 private Object originalElement
= null;
35 private String elementName
= null;
36 private XPropertySet xProp
= null;
37 private XNameReplace xNameReplace
= null;
40 * add a change that can be committed
42 protected void before() {
43 changeElement
= tEnv
.getObjRelation("XChangesBatch.ChangeElement");
44 originalElement
= tEnv
.getObjRelation("XChangesBatch.OriginalElement");
45 elementName
= (String
)tEnv
.getObjRelation("XChangesBatch.PropertyName");
47 // to do a change, get an XPropertySet
48 xProp
= (XPropertySet
)tEnv
.getObjRelation("XChangesBatch.PropertySet");
50 if (originalElement
== null && xProp
!= null)
51 originalElement
= xProp
.getPropertyValue(elementName
);
53 catch(com
.sun
.star
.uno
.Exception e
) {
54 throw new StatusException("Could not get property '" + elementName
+ "'.", e
);
57 // or get an XNameReplace
58 xNameReplace
= (XNameReplace
)tEnv
.getObjRelation("XChangesBatch.NameReplace");
60 if (originalElement
== null && xNameReplace
!= null)
61 originalElement
= xNameReplace
.getByName(elementName
);
63 catch(com
.sun
.star
.uno
.Exception e
) {
64 throw new StatusException("Could not get element by name '" + elementName
+ "'.", e
);
67 if (changeElement
== null || originalElement
== null || elementName
== null || (xProp
== null && xNameReplace
== null)) {
69 changeElement
== null?
"Missing property 'XChangesBatch.ChangeElement'\n":"" +
70 originalElement
== null?
"Missing property 'XChangesBatch.OriginalElement'\n":"" +
71 elementName
== null?
"Missing property 'XChangesBatch.PropertyName'\n":"" +
72 xProp
== null?
"Missing property 'XChangesBatch.PropertySet'":"" +
73 xNameReplace
== null?
"Missing property 'XChangesBatch.NameReplace'":""
75 throw new StatusException("Some needed object relations are missing.", new Exception());
79 public void _commitChanges() {
80 requiredMethod("getPendingChanges()");
82 log
.println("Committing changes.");
85 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
86 tRes
.tested("commitChanges()", Status
.exception(e
));
90 executeChange(originalElement
);
92 catch(StatusException e
) {
93 tRes
.tested("hasPendingChanges()", Status
.exception(e
));
98 log
.println("Commit changes back.");
101 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
102 tRes
.tested("commitChanges()", Status
.exception(e
));
105 tRes
.tested("commitChanges()", true);
108 public void _getPendingChanges() {
109 requiredMethod("hasPendingChanges()");
110 ElementChange
[]changes
= oObj
.getPendingChanges();
111 if (changes
== null) {
112 log
.println("Returned changes was 'null'");
113 log
.println("It should have been 1 change.");
114 tRes
.tested("getPendingChanges()", false);
115 } else if (changes
.length
!= 1) {
116 int amount
= changes
.length
;
117 log
.println("Found not the right number of changes: " + amount
);
118 log
.println("It should have been 1 change.");
119 for (int i
=0; i
<amount
; i
++) {
120 System
.out
.println("Detailed Change " + i
+ " -> new Element: '" +
121 changes
[i
].Element
.toString() + "' ReplacedElement: '" +
122 changes
[i
].ReplacedElement
.toString() + "'");
124 tRes
.tested("getPendingChanges()", false);
127 boolean result
= changes
[0].ReplacedElement
.equals(originalElement
);
128 result
&= changes
[0].Element
.equals(changeElement
);
129 tRes
.tested("getPendingChanges()", result
);
133 public void _hasPendingChanges() {
135 executeChange(changeElement
);
137 catch(StatusException e
) {
138 tRes
.tested("hasPendingChanges()", Status
.exception(e
));
141 boolean hasPendingChanges
= oObj
.hasPendingChanges();
142 tRes
.tested("hasPendingChanges()", hasPendingChanges
);
145 private void executeChange(Object element
) throws StatusException
{
148 xProp
.setPropertyValue(elementName
, element
);
150 catch(com
.sun
.star
.uno
.Exception e
) {
151 throw new StatusException("Could not set property '" + elementName
+ "'.", e
);
154 else if (xNameReplace
!= null) {
156 xNameReplace
.replaceByName(elementName
, element
);
158 catch(com
.sun
.star
.uno
.Exception e
) {
159 throw new StatusException("Could not replace '" + elementName
+ "' by name.", e
);