1 package org.apache.ojb.broker; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 6 import org.apache.commons.lang.builder.ToStringBuilder; 7 import org.apache.commons.lang.builder.ToStringStyle; 8 import org.apache.ojb.broker.query.Criteria; 9 import org.apache.ojb.broker.query.Query; 10 import org.apache.ojb.broker.query.QueryFactory; 11 import org.apache.ojb.junit.PBTestCase; 12 13 14 18 public class NestedFieldsTest extends PBTestCase 19 { 20 23 public static void main(String [] args) 24 { 25 26 String [] arr = {NestedFieldsTest.class.getName()}; 27 junit.textui.TestRunner.main(arr); 28 } 29 30 public void testStoreReadNestedField() 31 { 32 long timestamp = System.currentTimeMillis(); 33 String ddName = "second_level_detail_" + timestamp; 34 String ddDescription = "a real detail description" + timestamp; 35 String entryName = "nested_entry_store_" + timestamp; 36 37 NestedDetailDetail dd = new NestedDetailDetail(ddName, ddDescription); 38 NestedDetail d = new NestedDetail(dd); 39 40 ArrayList entryList = new ArrayList (); 41 entryList.add(new NestedEntry(entryName)); 42 entryList.add(new NestedEntry(entryName)); 43 entryList.add(new NestedEntry(entryName)); 44 45 d.setNestedEntryCollection(entryList); 46 NestedMain nested = new NestedMain("main_object_" + timestamp, d); 47 48 broker.beginTransaction(); 49 broker.store(nested); 50 broker.commitTransaction(); 51 52 Identity oid = new Identity(nested, broker); 53 54 assertNotNull(nested.getNestedDetail()); 55 assertNotNull(nested.getNestedDetail().getNestedDetailDetail()); 56 assertNotNull(nested.getNestedDetail().getNestedDetailDetail().getRealDetailName()); 57 dd = nested.getNestedDetail().getNestedDetailDetail(); 58 assertEquals(ddName, dd.getRealDetailName()); 59 assertEquals(ddDescription, dd.getRealDetailDescription()); 60 assertEquals(3, nested.getNestedDetail().getNestedEntryCollection().size()); 61 62 NestedMain nestedCopy = (NestedMain) broker.getObjectByIdentity(oid); 65 assertNotNull(nestedCopy.getNestedDetail()); 66 assertNotNull(nestedCopy.getNestedDetail().getNestedDetailDetail()); 67 assertNotNull(nestedCopy.getNestedDetail().getNestedDetailDetail().getRealDetailName()); 68 dd = nested.getNestedDetail().getNestedDetailDetail(); 69 assertEquals(ddName, dd.getRealDetailName()); 70 assertEquals(ddDescription, dd.getRealDetailDescription()); 71 assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection()); 72 assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size()); 73 74 broker.clearCache(); 76 nestedCopy = (NestedMain) broker.getObjectByIdentity(oid); 77 assertNotNull(nestedCopy.getNestedDetail()); 78 assertNotNull(nestedCopy.getNestedDetail().getNestedDetailDetail()); 79 assertNotNull(nestedCopy.getNestedDetail().getNestedDetailDetail().getRealDetailName()); 80 dd = nested.getNestedDetail().getNestedDetailDetail(); 81 assertEquals(ddName, dd.getRealDetailName()); 82 assertEquals(ddDescription, dd.getRealDetailDescription()); 83 assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection()); 84 assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size()); 85 } 86 87 90 public void testStoreReadNestedFieldWithNullFields() 91 { 92 long timestamp = System.currentTimeMillis(); 93 String entryName = "nested_entry_" + timestamp; 94 95 NestedDetail d = new NestedDetail(null); 96 97 ArrayList entryList = new ArrayList (); 98 entryList.add(new NestedEntry(entryName)); 99 entryList.add(new NestedEntry(entryName)); 100 entryList.add(new NestedEntry(entryName)); 101 102 d.setNestedEntryCollection(entryList); 103 NestedMain nested = new NestedMain("main_object_" + timestamp, d); 104 105 broker.beginTransaction(); 106 broker.store(nested); 107 broker.commitTransaction(); 108 109 Identity oid = new Identity(nested, broker); 110 111 assertNotNull(nested.getNestedDetail()); 112 assertNull(nested.getNestedDetail().getNestedDetailDetail()); 113 assertEquals(3, nested.getNestedDetail().getNestedEntryCollection().size()); 114 115 NestedMain nestedCopy = (NestedMain) broker.getObjectByIdentity(oid); 118 assertNotNull(nestedCopy.getNestedDetail()); 119 assertNull(nestedCopy.getNestedDetail().getNestedDetailDetail()); 120 assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection()); 121 assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size()); 122 123 broker.clearCache(); 125 nestedCopy = (NestedMain) broker.getObjectByIdentity(oid); 126 assertNotNull(nestedCopy.getNestedDetail()); 127 assertNull(nestedCopy.getNestedDetail().getNestedDetailDetail()); 128 assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection()); 129 assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size()); 130 } 131 132 public void testUpdateNestedField() 133 { 134 long timestamp = System.currentTimeMillis(); 135 String ddName = "second_level_detail_" + timestamp; 136 String ddDescription = "a real detail description" + timestamp; 137 String entryName = "nested_entry_upd_" + timestamp; 138 139 NestedDetailDetail dd = new NestedDetailDetail(ddName, ddDescription); 140 NestedDetail d = new NestedDetail(dd); 141 142 ArrayList entryList = new ArrayList (); 143 entryList.add(new NestedEntry(entryName)); 144 entryList.add(new NestedEntry(entryName)); 145 entryList.add(new NestedEntry(entryName)); 146 147 d.setNestedEntryCollection(entryList); 148 149 NestedMain nested = new NestedMain("main_object_" + timestamp, d); 150 151 broker.beginTransaction(); 152 broker.store(nested); 153 broker.commitTransaction(); 154 155 Identity oid = new Identity(nested, broker); 156 157 broker.clearCache(); 159 nested = (NestedMain) broker.getObjectByIdentity(oid); 160 161 165 nested.getNestedDetail().getNestedDetailDetail().setRealDetailName("update_name_"+timestamp); 166 nested.getNestedDetail().getNestedEntryCollection().add(new NestedEntry(entryName)); 167 broker.beginTransaction(); 168 broker.store(nested); 169 broker.commitTransaction(); 170 171 broker.clearCache(); 173 nested = (NestedMain) broker.getObjectByIdentity(oid); 174 assertEquals("update_name_"+timestamp, nested.getNestedDetail().getNestedDetailDetail().getRealDetailName()); 175 assertNotNull(nested.getNestedDetail().getNestedEntryCollection()); 176 assertEquals(4, nested.getNestedDetail().getNestedEntryCollection().size()); 177 } 178 179 public void testDeleteNestedField() 180 { 181 long timestamp = System.currentTimeMillis(); 182 String ddName = "second_level_detail_" + timestamp; 183 String ddDescription = "a real detail description" + timestamp; 184 String entryName = "nested_entry_del_" + timestamp; 185 186 NestedDetailDetail dd = new NestedDetailDetail(ddName, ddDescription); 187 NestedDetail d = new NestedDetail(dd); 188 189 ArrayList entryList = new ArrayList (); 190 entryList.add(new NestedEntry(entryName)); 191 entryList.add(new NestedEntry(entryName)); 192 entryList.add(new NestedEntry(entryName)); 193 194 d.setNestedEntryCollection(entryList); 195 196 NestedMain nested = new NestedMain("main_object_" + timestamp, d); 197 Identity oid = new Identity(nested, broker); 198 199 broker.beginTransaction(); 200 broker.store(nested); 201 broker.commitTransaction(); 202 203 broker.clearCache(); 205 nested = (NestedMain) broker.getObjectByIdentity(oid); 206 207 broker.beginTransaction(); 208 broker.delete(nested); 209 broker.commitTransaction(); 210 211 nested = (NestedMain) broker.getObjectByIdentity(oid); 212 assertNull("Object was not deleted", nested); 213 214 Criteria crit = new Criteria(); 215 crit.addEqualTo("name", entryName); 216 Query query = QueryFactory.newQuery(NestedEntry.class, crit); 217 Collection result = broker.getCollectionByQuery(query); 218 assertEquals(0, result.size()); 219 } 220 221 222 223 227 public static class NestedMain 228 { 229 private Long objId; 230 private String name; 231 private NestedDetail nestedDetail; 232 233 public NestedMain() 234 { 235 } 236 237 public NestedMain(String name, NestedDetail detail) 238 { 239 this.name = name; 240 this.nestedDetail = detail; 241 } 242 243 public Long getObjId() 244 { 245 return objId; 246 } 247 248 public void setObjId(Long objId) 249 { 250 this.objId = objId; 251 } 252 253 public String getName() 254 { 255 return name; 256 } 257 258 public void setName(String name) 259 { 260 this.name = name; 261 } 262 263 public NestedDetail getNestedDetail() 264 { 265 return nestedDetail; 266 } 267 268 public void setNestedDetail(NestedDetail nestedDetail) 269 { 270 this.nestedDetail = nestedDetail; 271 } 272 273 public String toString() 274 { 275 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE); 276 buf.append("objId", objId). 277 append("name", name). 278 append("detail", nestedDetail); 279 return buf.toString(); 280 } 281 } 282 283 public static class NestedDetail 284 { 285 private NestedDetailDetail nestedDetailDetail; 286 private Collection nestedEntryCollection; 287 288 public NestedDetail() 289 { 290 } 291 292 public Collection getNestedEntryCollection() 293 { 294 return nestedEntryCollection; 295 } 296 297 public void setNestedEntryCollection(Collection nestedEntryCollection) 298 { 299 this.nestedEntryCollection = nestedEntryCollection; 300 } 301 302 public NestedDetail(NestedDetailDetail detail) 303 { 304 this.nestedDetailDetail = detail; 305 } 306 307 public NestedDetailDetail getNestedDetailDetail() 308 { 309 return nestedDetailDetail; 310 } 311 312 public void setNestedDetailDetail(NestedDetailDetail nestedDetailDetail) 313 { 314 this.nestedDetailDetail = nestedDetailDetail; 315 } 316 317 public String toString() 318 { 319 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE); 320 buf.append("detail", nestedDetailDetail); 321 return buf.toString(); 322 } 323 } 324 325 public static class NestedDetailDetail 326 { 327 private String realDetailName; 328 private String realDetailDescription; 329 330 public NestedDetailDetail() 331 { 332 } 333 334 public NestedDetailDetail(String realDetailName, String realDetailDescription) 335 { 336 this.realDetailName = realDetailName; 337 this.realDetailDescription = realDetailDescription; 338 } 339 340 public String getRealDetailName() 341 { 342 return realDetailName; 343 } 344 345 public void setRealDetailName(String realDetailName) 346 { 347 this.realDetailName = realDetailName; 348 } 349 350 public String getRealDetailDescription() 351 { 352 return realDetailDescription; 353 } 354 355 public void setRealDetailDescription(String realDetailDescription) 356 { 357 this.realDetailDescription = realDetailDescription; 358 } 359 360 public String toString() 361 { 362 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE); 363 buf.append("realDetailName", realDetailName). 364 append("realDetailDescription", realDetailDescription); 365 return buf.toString(); 366 } 367 } 368 369 public static class NestedEntry 370 { 371 private Integer id; 372 private Long fkId; 373 private String name; 374 375 public NestedEntry() 376 { 377 } 378 379 public NestedEntry(String name) 380 { 381 this.name = name; 382 } 383 384 public Integer getId() 385 { 386 return id; 387 } 388 389 public void setId(Integer id) 390 { 391 this.id = id; 392 } 393 394 public Long getFkId() 395 { 396 return fkId; 397 } 398 399 public void setFkId(Long fkId) 400 { 401 this.fkId = fkId; 402 } 403 404 public String getName() 405 { 406 return name; 407 } 408 409 public void setName(String name) 410 { 411 this.name = name; 412 } 413 } 414 } 415 | Popular Tags |