1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package ifc.awt.tree;
29 
30 import com.sun.star.awt.tree.XMutableTreeNode;
31 import lib.MultiMethodTest;
32 import lib.Status;
33 import lib.StatusException;
34 
35 /**
36  * Testing <code>com.sun.star.awt.tree.XTreeDataModel</code>
37  * interface methods :
38  * <ul>
39  *  <li><code> appendChild()</code></li>
40  *  <li><code> insertChildByIndex()</code></li>
41  *  <li><code> removeChildByIndex()</code></li>
42  *  <li><code> setHasChildrenOnDemand()</code></li>
43  *  <li><code> setDisplayValue()</code></li>
44  *  <li><code> setNodeGraphicURL()</code></li>
45  *  <li><code> setExpandedGraphicURL()</code></li>
46  *  <li><code> setCollapsedGraphicURL()</code></li>
47  *  <li><code> DataValue()</code></li>
48  * </ul> <p>
49  * Test is <b> NOT </b> multithread compilant. <p>
50  *
51  * @see com.sun.star.awt.tree.XTreeDataModel
52  */
53 public class _XMutableTreeNode extends MultiMethodTest {
54 
55     public XMutableTreeNode oObj = null;
56 
57     private int mCount = 0;
58 
59     private XMutableTreeNodeCreator nodeCreator = null;
60 
61     public static interface XMutableTreeNodeCreator{
62         public XMutableTreeNode createNode(String name);
63     }
64 
65     public void before(){
66         nodeCreator = (XMutableTreeNodeCreator) tEnv.getObjRelation("XMutableTreeNodeCreator");
67         if (nodeCreator == null){
68             throw new StatusException(Status.failed(
69                 "Couldn't get relation 'XMutableTreeNodeCreator'"));
70         }
71 
72     }
73 
74      public void _appendChild(){
75         boolean bOK = true;
76 
77         log.println("try to append a valid node...");
78 
79         XMutableTreeNode myNode = nodeCreator.createNode("myNodeToAppend");
80 
81         try {
82             oObj.appendChild(myNode);
83         } catch (com.sun.star.lang.IllegalArgumentException ex) {
84             log.println("ERROR: could not appedn a valid node: " + ex.toString());
85             bOK = false;
86         }
87 
88         log.println("try to append the node a second time...");
89         try {
90 
91             oObj.appendChild(myNode);
92             log.println("ERROR: expected IllegalArgumentException was not thrown => FAILED");
93             bOK = false;
94         } catch (com.sun.star.lang.IllegalArgumentException ex) {
95             log.println("expected IllegalArgumentException was thrown => OK");
96         }
97 
98         log.println("try to append the object itself...");
99         try {
100 
101             oObj.appendChild(oObj);
102             log.println("ERROR: expected IllegalArgumentException was not thrown => FAILED");
103             bOK = false;
104         } catch (com.sun.star.lang.IllegalArgumentException ex) {
105             log.println("expected IllegalArgumentException was thrown => OK");
106         }
107 
108         tRes.tested("appendChild()", bOK);
109     }
110 
111      public void _insertChildByIndex(){
112         boolean bOK = true;
113 
114         XMutableTreeNode myNode = nodeCreator.createNode("myNodeToInsert");
115 
116         try {
117             log.println("try to insert a valid node...");
118             oObj.insertChildByIndex(0, myNode);
119         } catch (com.sun.star.lang.IllegalArgumentException ex) {
120             log.println("ERROR: could not insert a valid node: " + ex.toString());
121             bOK = false;
122         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
123             log.println("ERROR: could not insert a valid node on index '0': " + ex.toString());
124             bOK = false;
125         }
126 
127         try {
128             log.println("try to insert a valid node a second time...");
129             oObj.insertChildByIndex(0, myNode);
130             log.println("ERROR: expected IllegalArgumentException was not thrown => FAILED");
131             bOK = false;
132         } catch (com.sun.star.lang.IllegalArgumentException ex) {
133             log.println("expected IllegalArgumentException wa thrown => OK");
134         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
135             log.println("ERROR: wrong IndexOutOfBoundsException was thrown. Expected is IllegalArgumentException => FAILED");
136             bOK = false;
137         }
138 
139         XMutableTreeNode myNode2 = nodeCreator.createNode("myNodeToInsert2");
140 
141         try {
142             log.println("try to insert a valid node on invalid index '-3'...");
143             oObj.insertChildByIndex(-3, myNode2);
144             log.println("ERROR: expected IndexOutOfBoundsException was not thrown => FAILED");
145             bOK = false;
146         } catch (com.sun.star.lang.IllegalArgumentException ex) {
147             log.println("ERROR: wrong IllegalArgumentException was thrown. Expeced is IndexOutOfBoundsException => FAILED");
148             bOK = false;
149         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
150             log.println("Expected IndexOutOfBoundsException was thrown => OK");
151         }
152 
153         try {
154             log.println("try to insert the object itself...");
155             oObj.insertChildByIndex(0, oObj);
156             log.println("ERROR: expected IllegalArgumentException was not thrown => FAILED");
157             bOK = false;
158         } catch (com.sun.star.lang.IllegalArgumentException ex) {
159             log.println("expected IllegalArgumentException was thrown => OK");
160         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
161             log.println("unexpected IndexOutOfBoundsException was thrown, expected was IllegalArgumentException => FAILED");
162             bOK = false;
163         }
164 
165         tRes.tested("insertChildByIndex()", bOK);
166     }
167 
168      public void _removeChildByIndex(){
169 
170          requiredMethod("insertChildByIndex()");
171          boolean bOK = true;
172 
173         try {
174             log.println("try to remove node at index '0'...");
175             oObj.removeChildByIndex(0);
176         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
177             log.println("ERROR: IndexOutOfBoundsException was thrown => FAILED");
178             bOK = false;
179         }
180 
181         try {
182             log.println("try to remove node at invalid index '-3'");
183             oObj.removeChildByIndex(-3);
184             log.println("ERROR: expeced IndexOutOfBoundsException was not thrown => FAILED");
185             bOK = false;
186         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
187             log.println("expected IndexOutOfBoundsException was thrown => OK");
188         }
189         tRes.tested("removeChildByIndex()", bOK);
190     }
191 
192      public void _setHasChildrenOnDemand(){
193         boolean bOK = true;
194 
195         log.println("setHasChildrenOnDemand(true)");
196         oObj.setHasChildrenOnDemand(true);
197 
198         log.println("setHasChildrenOnDemand(false)");
199         oObj.setHasChildrenOnDemand(false);
200 
201         tRes.tested("setHasChildrenOnDemand()", bOK);
202     }
203 
204      public void _setDisplayValue(){
205         boolean bOK = true;
206 
207         log.println("setDisplayValue(\"MyTestValue\")");
208         oObj.setDisplayValue("MyTestValue");
209 
210         log.println("setDisplayValue(null)");
211         oObj.setDisplayValue(null);
212 
213         log.println("oObj.setDisplayValue(oObj)");
214         oObj.setDisplayValue(oObj);
215 
216         tRes.tested("setDisplayValue()", bOK);
217     }
218 
219      public void _setNodeGraphicURL(){
220         boolean bOK = true;
221 
222         log.println("setNodeGraphicURL(\"MyTestURL\")");
223         oObj.setNodeGraphicURL("MyTestURL");
224 
225         log.println("setNodeGraphicURL(null)");
226         oObj.setNodeGraphicURL(null);
227 
228         tRes.tested("setNodeGraphicURL()", bOK);
229     }
230 
231      public void _setExpandedGraphicURL(){
232         boolean bOK = true;
233 
234         log.println("setExpandedGraphicURL(\"myExpandedURL\")");
235         oObj.setExpandedGraphicURL("myExpandedURL");
236 
237         log.println("setExpandedGraphicURL(null)");
238         oObj.setExpandedGraphicURL(null);
239 
240         tRes.tested("setExpandedGraphicURL()", bOK);
241     }
242 
243      public void _setCollapsedGraphicURL(){
244         boolean bOK = true;
245 
246         log.println("setCollapsedGraphicURL(\"myCollapsedURL\")");
247         oObj.setCollapsedGraphicURL("myCollapsedURL");
248 
249         log.println("setCollapsedGraphicURL(null)");
250         oObj.setCollapsedGraphicURL(null);
251 
252         tRes.tested("setCollapsedGraphicURL()", bOK);
253     }
254 
255      public void _DataValue(){
256         boolean bOK = true;
257 
258         log.println("setDataValue(\"myDataValue\")");
259         oObj.setDataValue("myDataValue");
260 
261         String sDataValue = (String) oObj.getDataValue();
262 
263         if ( ! sDataValue.equals("myDataValue")) {
264             log.println("ERROR: getDataVlaue does not return the value which is inserted before:\n" +
265                 "\texpected: myDataValue\n" +
266                 "\tgot: " + sDataValue);
267             bOK = false;
268         }
269 
270         log.println("setDataValue(null)");
271         oObj.setDataValue(null);
272 
273         Object oDataValue = oObj.getDataValue();
274         if ( oDataValue != null) {
275             log.println("ERROR: getDataVlaue does not return the value which is inserted before:\n" +
276                 "\texpected: null\n" +
277                 "\tgot: " + oDataValue.toString());
278             bOK = false;
279         }
280 
281         log.println("oObj.setDisplayValue(oObj)");
282         oObj.setDisplayValue(oObj);
283 
284         oDataValue = oObj.getDataValue();
285         if ( oDataValue != null) {
286             log.println("ERROR: getDataVlaue does not return the value which is inserted before:\n" +
287                 "\texpected: " + oObj.toString() +"\n" +
288                 "\tgot: " + oDataValue.toString());
289             bOK = false;
290         }
291 
292         tRes.tested("DataValue()", bOK);
293     }
294 
295 }
296