Source code: com/xpn/xwiki/test/ObjectTest.java
1
2
3 package com.xpn.xwiki.test;
4
5 import com.xpn.xwiki.XWikiContext;
6 import com.xpn.xwiki.XWikiException;
7 import com.xpn.xwiki.doc.XWikiDocument;
8 import com.xpn.xwiki.objects.*;
9 import com.xpn.xwiki.objects.classes.BaseClass;
10 import com.xpn.xwiki.objects.classes.NumberClass;
11 import junit.framework.TestCase;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 /**
17 * ===================================================================
18 *
19 * Copyright (c) 2003 Ludovic Dubost, All rights reserved.
20 *
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License
23 * as published by the Free Software Foundation; either version 2
24 * of the License, or (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details, published at
30 * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the
31 * root folder of this distribution.
32
33 * Created by
34 * User: Ludovic Dubost
35 * Date: 30 déc. 2003
36 * Time: 22:49:51
37 */
38
39 public class ObjectTest extends TestCase {
40
41 public XWikiContext context = new XWikiContext();
42
43 public void testEqualsNumberProperty() {
44 // Test both cloning and equals
45 IntegerProperty prop = Utils.prepareIntegerProperty();
46 IntegerProperty prop2 = (IntegerProperty) prop.clone();
47
48 // test cloning and equals at the same time
49 assertTrue("Cloning did not created equals integer property", prop.equals(prop2));
50
51 // Test name (this is common to all elements)
52 prop2.setName("toto");
53 assertTrue("Equals did not detect different name", !prop.equals(prop2));
54 prop2 = (IntegerProperty) prop.clone();
55 prop2.setName(null);
56 assertTrue("Equals did not detect different name with null", !prop.equals(prop2));
57 prop.setName(null);
58 assertTrue("Equals did not detect same null name", prop.equals(prop2));
59
60 // Test pretty name (this is common to all elements)
61 prop2 = (IntegerProperty) prop.clone();
62 prop2.setPrettyName("toto");
63 assertTrue("Equals did not detect different pretty name", !prop.equals(prop2));
64 prop2 = (IntegerProperty) prop.clone();
65 prop2.setPrettyName(null);
66 assertTrue("Equals did not detect different pretty name with null", !prop.equals(prop2));
67 prop.setPrettyName(null);
68 assertTrue("Equals did not detect same null pretty name", prop.equals(prop2));
69
70 // Test value (this is integer specific)
71 prop2 = (IntegerProperty) prop.clone();
72 prop2.setValue(new Integer(1000));
73 assertTrue("Equals did not detect different value", !prop.equals(prop2));
74 prop2 = (IntegerProperty) prop.clone();
75 prop2.setValue(null);
76 assertTrue("Equals did not detect different value with null", !prop.equals(prop2));
77 prop.setValue(null);
78 assertTrue("Equals did not detect same null value", prop.equals(prop2));
79 }
80
81 public void testEqualsStringProperty() {
82 // Test both cloning and equals
83 IntegerProperty iprop = new IntegerProperty();
84 StringProperty sprop = new StringProperty();
85 assertNotSame("IntegerProperty cannot be equals to StringProperty", iprop, sprop);
86
87 StringProperty prop = Utils.prepareStringProperty();
88 StringProperty prop2 = (StringProperty) prop.clone();
89
90 // test cloning and equals at the same time
91 assertTrue("Cloning did not created equals string property", prop.equals(prop2));
92
93 // Test value (this is string specific)
94 prop2 = (StringProperty) prop.clone();
95 prop2.setValue("blabla");
96 assertTrue("Equals did not detect different value", !prop.equals(prop2));
97 prop2 = (StringProperty) prop.clone();
98 prop2.setValue(null);
99 assertTrue("Equals did not detect different value with null", !prop.equals(prop2));
100 prop.setValue(null);
101 assertTrue("Equals did not detect same null value", prop.equals(prop2));
102 }
103
104 public void testEqualsListProperty() {
105 // Test both cloning and equals
106 ListProperty iprop = new ListProperty();
107 StringProperty sprop = new StringProperty();
108 assertNotSame("ListProperty cannot be equals to StringProperty", iprop, sprop);
109
110 ListProperty prop = Utils.prepareStringListProperty();
111 ListProperty prop2 = (ListProperty) prop.clone();
112
113 // test cloning and equals at the same time
114 assertEquals("Cloning did not created equals list property", prop, prop2);
115
116 // Test value (this is string specific)
117 prop2 = (ListProperty) prop.clone();
118 List list = new ArrayList();
119 list.add("1");
120 list.add("2");
121 prop2.setValue(list);
122 assertNotSame("Equals did not detect different value", prop, prop2);
123 prop2 = (ListProperty) prop.clone();
124 prop2.setValue(null);
125 assertNotSame("Equals did not detect different value with null", prop, prop2);
126 prop.setValue(null);
127 assertEquals("Equals did not detect same null value", prop, prop2);
128 }
129
130 public void testEqualsObject() throws XWikiException {
131 XWikiDocument doc = new XWikiDocument();
132 Utils.prepareObject(doc);
133 BaseClass bclass = doc.getxWikiClass();
134 BaseObject object = doc.getObject(bclass.getName(), 0);
135 testEqualsObject(object, bclass);
136 }
137
138 public void testEqualsAdvancedObject() throws XWikiException {
139 XWikiDocument doc = new XWikiDocument();
140 Utils.prepareAdvancedObject(doc);
141 BaseClass bclass = doc.getxWikiClass();
142 BaseObject object = doc.getObject(bclass.getName(), 0);
143 testEqualsObject(object, bclass);
144
145 BaseObject object2 = (BaseObject)object.clone();
146 ((StringProperty)object.safeget("category")).setValue(null);
147 assertNotSame("Equals did not detect different List field value", object, object2);
148 }
149
150 public void testEqualsObject(BaseObject object, BaseClass bclass) throws XWikiException {
151 BaseObject object2 = (BaseObject)object.clone();
152
153 // test cloning and equals at the same time
154 assertEquals("Cloning did not created equals objects", object, object2);
155
156 object2.setName("titi");
157 assertNotSame("Equals did not detect different object name", object, object2);
158
159 object2 = (BaseObject)object.clone();
160 ((BaseProperty)object.safeget("age")).setName("titi");
161 assertNotSame("Equals did not detect changed field property", object, object2);
162
163 object2 = (BaseObject)object.clone();
164 ((BaseProperty)object.safeget("age")).setValue(new Integer(45));
165 assertNotSame("Equals did not detect changed field property", object, object2);
166
167 object2 = (BaseObject)object.clone();
168 object.removeField("age");
169 assertNotSame("Equals did not detect missing age field", object, object2);
170
171 object2 = (BaseObject)object.clone();
172 bclass.safeget("age").setName("titi");
173 assertNotSame("Equals did not detect different class property name", object, object2);
174
175 object2 = (BaseObject)object.clone();
176 ((NumberClass)bclass.safeget("age")).setSize(1);
177 assertNotSame("Equals did not detect different class property size", object, object2);
178
179 object2 = (BaseObject)object.clone();
180 bclass.removeField("age");
181 assertNotSame("Equals did not detect different null class", object, object2);
182
183 object2 = (BaseObject)object.clone();
184 object.setClassName(null);
185 assertNotSame("Equals did not detect different null class", object, object2);
186 }
187
188 public void testMergeObject() throws XWikiException {
189 XWikiDocument doc = new XWikiDocument();
190 Utils.prepareObject(doc);
191 BaseObject object = doc.getObject(doc.getxWikiClass().getName(), 0);
192 BaseObject object2 = (BaseObject)object.clone();
193
194 object2.merge(object);
195 // test merging and equals at the same time
196 assertEquals("Merging did not created equals objects", object, object2);
197
198 object2 = (BaseObject)object.clone();
199 object2.removeField("age");
200 object2.merge(object);
201 assertEquals("Merging did not created equals objects", object, object2);
202
203 object2 = (BaseObject)object.clone();
204 ((BaseProperty)object2.safeget("age")).setName("titi");
205 object2.merge(object);
206 assertNotSame("Merging overrided age property", object, object2);
207
208 }
209
210 public void testSetObject() throws XWikiException {
211 XWikiDocument doc = new XWikiDocument("Test", "WebHome");
212 BaseObject bobj = new BaseObject();
213 bobj.setName("Test.WebHome");
214 doc.setObject("Test.WebHome", 0, bobj);
215 assertEquals("Object size should be 1", 1, doc.getObjects("Test.WebHome").size());
216 BaseObject bobj2 = doc.getObject("Test.WebHome", 0);
217 assertEquals("Object name should be Test.WebHome", "Test.WebHome", bobj2.getName());
218
219 BaseObject bobj3 = new BaseObject();
220 bobj3.setName("Test.Toto");
221 doc.setObject("Test.WebHome", 0, bobj3);
222 assertEquals("Object size should be 1", 1, doc.getObjects("Test.WebHome").size());
223 BaseObject bobj3b = doc.getObject("Test.WebHome", 0);
224 assertEquals("Object name should be Test.Toto", "Test.Toto", bobj3b.getName());
225
226 BaseObject bobj4 = new BaseObject();
227 bobj4.setName("Test.WebHome");
228 doc.setObject("Test.WebHome", 2, bobj4);
229 assertEquals("Object size should be 3", 3, doc.getObjects("Test.WebHome").size());
230 assertNull("Object 1 should be null", doc.getObject("Test.WebHome",1));
231 BaseObject obj5 = doc.getObject("Test.WebHome", 2);
232 assertEquals("Object name should be Test.WebHome", "Test.WebHome", obj5.getName());
233 }
234
235 }