KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > test > CategoryItemTest


1 package org.hibernate.ce.auction.test;
2
3 import junit.framework.*;
4 import junit.textui.TestRunner;
5 import org.hibernate.ce.auction.model.*;
6 import org.hibernate.ce.auction.persistence.HibernateUtil;
7
8 import java.util.*;
9
10 import org.hibernate.*;
11
12 public class CategoryItemTest extends TestCaseWithData {
13
14     // ********************************************************** //
15

16     public void testCompositeQuery() throws Exception JavaDoc {
17         initData();
18
19         // Query for Category and all categorized Items (three tables joined)
20
HibernateUtil.beginTransaction();
21         Session s = HibernateUtil.getSession();
22
23         Query q = s.createQuery("select c from Category as c left join fetch c.categorizedItems as ci join fetch ci.item as i");
24         Collection result = new HashSet(q.list());
25         assertTrue(result.size() == 2);
26
27         HibernateUtil.commitTransaction();
28         HibernateUtil.closeSession();
29
30         // Check initialization (should be eager fetched)
31
for (Iterator it = result.iterator(); it.hasNext();) {
32             Category cat = (Category) it.next();
33             for (Iterator it2 = cat.getCategorizedItems().iterator(); it2.hasNext();) {
34                 assertTrue(it2.next() != null);
35             }
36         }
37     }
38
39     public void testDeletionFromItem() throws Exception JavaDoc {
40         initData();
41
42         // Delete all links for auctionFour by clearing collection
43
HibernateUtil.beginTransaction();
44         Session s = HibernateUtil.getSession();
45         Item i = (Item)s.get(Item.class, auctionFour.getId());
46         assertTrue(i.getCategorizedItems().size() == 2);
47         i.getCategorizedItems().clear();
48         HibernateUtil.commitTransaction();
49         HibernateUtil.closeSession();
50
51         // Check deletion
52
HibernateUtil.beginTransaction();
53         s = HibernateUtil.getSession();
54         CategorizedItem catItem = (CategorizedItem)s.get(CategorizedItem.class,
55                     new CategorizedItem.Id(carsLuxury.getId(), auctionFour.getId()));
56         assertTrue(catItem == null);
57         i = (Item)s.get(Item.class, auctionFour.getId());
58         assertTrue(i.getCategorizedItems().size() == 0);
59         HibernateUtil.commitTransaction();
60         HibernateUtil.closeSession();
61     }
62
63     public void testDeletionFromCategory() throws Exception JavaDoc {
64         initData();
65
66         // Delete all links for auctionFour by clearing collection
67
HibernateUtil.beginTransaction();
68         Session s = HibernateUtil.getSession();
69         Category c = (Category)s.get(Category.class, carsSUV.getId());
70         assertTrue(c.getCategorizedItems().size() == 2);
71         c.getCategorizedItems().clear();
72         HibernateUtil.commitTransaction();
73         HibernateUtil.closeSession();
74
75         // Check deletion
76
HibernateUtil.beginTransaction();
77         s = HibernateUtil.getSession();
78         CategorizedItem catItem = (CategorizedItem)s.get(CategorizedItem.class,
79                     new CategorizedItem.Id(carsSUV.getId(), auctionThree.getId()));
80         assertTrue(catItem == null);
81         c = (Category)s.get(Category.class, carsSUV.getId());
82         assertTrue(c.getCategorizedItems().size() == 0);
83         HibernateUtil.commitTransaction();
84         HibernateUtil.closeSession();
85
86     }
87
88     // ********************************************************** //
89

90     public CategoryItemTest(String JavaDoc x) {
91         super(x);
92     }
93
94     public static Test suite() {
95         return new TestSuite(CategoryItemTest.class);
96     }
97
98     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
99         TestRunner.run( suite() );
100     }
101
102 }
103
Popular Tags