KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > indexcoll > IndexedCollectionTest


1 //$Id: IndexedCollectionTest.java,v 1.3 2005/07/22 22:36:08 epbernard Exp $
2
package org.hibernate.test.annotations.indexcoll;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.HashMap JavaDoc;
8
9 import org.hibernate.Session;
10 import org.hibernate.Transaction;
11 import org.hibernate.test.annotations.TestCase;
12
13 /**
14  * Test index collections
15  *
16  * @author Emmanuel Bernard
17  */

18 public class IndexedCollectionTest extends TestCase {
19     public void testFkList() throws Exception JavaDoc {
20         Wardrobe w = new Wardrobe();
21         Drawer d1 = new Drawer();
22         Drawer d2 = new Drawer();
23         w.setDrawers( new ArrayList JavaDoc<Drawer>() );
24         w.getDrawers().add(d1);
25         w.getDrawers().add(d2);
26         Session s;
27         Transaction tx;
28         s = openSession();
29         tx = s.beginTransaction();
30         s.persist( w );
31         tx.commit();
32         s.close();
33
34         s = openSession();
35         tx = s.beginTransaction();
36         w = (Wardrobe) s.get( Wardrobe.class, w.getId() );
37         assertNotNull( w );
38         assertNotNull( w.getDrawers() );
39         List JavaDoc<Drawer> result = w.getDrawers();
40         assertEquals( 2, result.size() );
41         assertEquals( d2.getId(), result.get( 1 ).getId() );
42         result.remove( d1 );
43         s.flush();
44         d1 = (Drawer) s.merge( d1 );
45         result.add( d1 );
46         tx.commit();
47         s.close();
48
49         s = openSession();
50         tx = s.beginTransaction();
51         w = (Wardrobe) s.get( Wardrobe.class, w.getId() );
52         assertNotNull( w );
53         assertNotNull( w.getDrawers() );
54         result = w.getDrawers();
55         assertEquals( 2, result.size() );
56         assertEquals( d1.getId(), result.get( 1 ).getId() );
57         s.delete( result.get( 0 ) );
58         s.delete( result.get( 1 ) );
59         s.delete( w );
60         tx.commit();
61         s.close();
62     }
63
64     public void testJoinedTableList() throws Exception JavaDoc {
65         Wardrobe w = new Wardrobe();
66         w.setDrawers( new ArrayList JavaDoc<Drawer>() );
67         Drawer d = new Drawer();
68         w.getDrawers().add(d);
69         Dress d1 = new Dress();
70         Dress d2 = new Dress();
71         d.setDresses( new ArrayList JavaDoc<Dress>() );
72         d.getDresses().add(d1);
73         d.getDresses().add(d2);
74         Session s;
75         Transaction tx;
76         s = openSession();
77         tx = s.beginTransaction();
78         s.persist( d1 );
79         s.persist( d2 );
80         s.persist( w );
81         tx.commit();
82         s.close();
83
84         s = openSession();
85         tx = s.beginTransaction();
86         d = (Drawer) s.get( Drawer.class, d.getId() );
87         assertNotNull( d );
88         assertNotNull( d.getDresses() );
89         List JavaDoc<Dress> result = d.getDresses();
90         assertEquals( 2, result.size() );
91         assertEquals( d2.getId(), result.get( 1 ).getId() );
92         result.remove( d1 );
93         s.flush();
94         d1 = (Dress) s.merge( d1 );
95         result.add( d1 );
96         tx.commit();
97         s.close();
98
99         s = openSession();
100         tx = s.beginTransaction();
101         d = (Drawer) s.get( Drawer.class, d.getId() );
102         assertNotNull( d );
103         assertNotNull( d.getDresses() );
104         result = d.getDresses();
105         assertEquals( 2, result.size() );
106         assertEquals( d1.getId(), result.get( 1 ).getId() );
107         s.delete( result.get( 0 ) );
108         s.delete( result.get( 1 ) );
109         s.delete( d );
110         tx.commit();
111         s.close();
112     }
113
114     public void testMapKey() throws Exception JavaDoc {
115         Session s;
116         Transaction tx;
117         s = openSession();
118         tx = s.beginTransaction();
119         Software hibernate = new Software();
120         hibernate.setName("Hibernate");
121         Version v1 = new Version();
122         v1.setCodeName("HumbaHumba");
123         v1.setNumber("1.0");
124         v1.setSoftware(hibernate);
125         Version v2 = new Version();
126         v2.setCodeName("Copacabana");
127         v2.setNumber("2.0");
128         v2.setSoftware(hibernate);
129         Version v4 = new Version();
130         v4.setCodeName("Dreamland");
131         v4.setNumber("4.0");
132         v4.setSoftware(hibernate);
133         Map JavaDoc<String JavaDoc, Version> link = new HashMap JavaDoc<String JavaDoc, Version>();
134         link.put(v1.getCodeName(), v1);
135         link.put(v2.getCodeName(), v2);
136         link.put(v4.getCodeName(), v4);
137         hibernate.setVersions(link);
138         s.persist(hibernate);
139         s.persist(v1);
140         s.persist(v2);
141         s.persist(v4);
142         tx.commit();
143         s.clear();
144         tx = s.beginTransaction();
145         hibernate = (Software) s.get(Software.class, "Hibernate");
146         assertEquals( 3, hibernate.getVersions().size() );
147         assertEquals( "1.0", hibernate.getVersions().get("HumbaHumba").getNumber() );
148         assertEquals( "2.0", hibernate.getVersions().get("Copacabana").getNumber() );
149         hibernate.getVersions().remove( v4.getCodeName() );
150         tx.commit();
151         s.clear();
152         tx = s.beginTransaction();
153         hibernate = (Software) s.get(Software.class, "Hibernate");
154         assertEquals( "So effect on collection changes", 3, hibernate.getVersions().size() );
155         for ( Version v : hibernate.getVersions().values() ) {
156             s.delete( v );
157         }
158         s.delete( hibernate );
159         tx.commit();
160         s.close();
161     }
162
163     public void testDefaultMapKey() throws Exception JavaDoc {
164         Session s;
165         Transaction tx;
166         s = openSession();
167         tx = s.beginTransaction();
168         AddressBook book = new AddressBook();
169         book.setOwner("Emmanuel");
170         AddressEntryPk helene = new AddressEntryPk("Helene", "Michau");
171         AddressEntry heleneEntry = new AddressEntry();
172         heleneEntry.setBook(book);
173         heleneEntry.setCity("Levallois");
174         heleneEntry.setStreet("Louis Blanc");
175         heleneEntry.setPerson(helene);
176         AddressEntryPk primeMinister = new AddressEntryPk("Dominique", "Villepin");
177         AddressEntry primeMinisterEntry = new AddressEntry();
178         primeMinisterEntry.setBook(book);
179         primeMinisterEntry.setCity("Paris");
180         primeMinisterEntry.setStreet("Hotel Matignon");
181         primeMinisterEntry.setPerson(primeMinister);
182         book.getEntries().put(helene, heleneEntry);
183         book.getEntries().put(primeMinister, primeMinisterEntry);
184         s.persist(book);
185         tx.commit();
186         s.clear();
187         tx = s.beginTransaction();
188         book = (AddressBook) s.get( AddressBook.class, book.getId() );
189         assertEquals( 2, book.getEntries().size() );
190         assertEquals( heleneEntry.getCity(), book.getEntries().get(helene).getCity() );
191         AddressEntryPk fake = new AddressEntryPk("Fake", "Fake");
192         book.getEntries().put(fake, primeMinisterEntry);
193         tx.commit();
194         s.clear();
195         tx = s.beginTransaction();
196         book = (AddressBook) s.get( AddressBook.class, book.getId() );
197         assertEquals( 2, book.getEntries().size() );
198         assertNull( book.getEntries().get(fake) );
199         s.delete(book);
200         tx.commit();
201         s.close();
202     }
203
204     public void testComponentSubPropertyMapKey() throws Exception JavaDoc {
205         Session s;
206         Transaction tx;
207         s = openSession();
208         tx = s.beginTransaction();
209         AddressBook book = new AddressBook();
210         book.setOwner("Emmanuel");
211         AddressEntryPk helene = new AddressEntryPk("Helene", "Michau");
212         AddressEntry heleneEntry = new AddressEntry();
213         heleneEntry.setBook(book);
214         heleneEntry.setCity("Levallois");
215         heleneEntry.setStreet("Louis Blanc");
216         heleneEntry.setPerson(helene);
217         AddressEntryPk primeMinister = new AddressEntryPk("Dominique", "Villepin");
218         AddressEntry primeMinisterEntry = new AddressEntry();
219         primeMinisterEntry.setBook(book);
220         primeMinisterEntry.setCity("Paris");
221         primeMinisterEntry.setStreet("Hotel Matignon");
222         primeMinisterEntry.setPerson(primeMinister);
223         book.getEntries().put(helene, heleneEntry);
224         book.getEntries().put(primeMinister, primeMinisterEntry);
225         s.persist(book);
226         tx.commit();
227         s.clear();
228         tx = s.beginTransaction();
229         book = (AddressBook) s.get( AddressBook.class, book.getId() );
230         assertEquals( 2, book.getLastNameEntries().size() );
231         assertEquals( heleneEntry.getCity(), book.getLastNameEntries().get("Michau").getCity() );
232         AddressEntryPk fake = new AddressEntryPk("Fake", "Fake");
233         book.getEntries().put(fake, primeMinisterEntry);
234         tx.commit();
235         s.clear();
236         tx = s.beginTransaction();
237         book = (AddressBook) s.get( AddressBook.class, book.getId() );
238         assertEquals( 2, book.getEntries().size() );
239         assertNull( book.getEntries().get(fake) );
240         s.delete(book);
241         tx.commit();
242         s.close();
243     }
244
245     public IndexedCollectionTest(String JavaDoc x) {
246         super( x );
247     }
248
249     protected Class JavaDoc[] getMappings() {
250         return new Class JavaDoc[] {
251             Wardrobe.class,
252             Drawer.class,
253             Dress.class,
254             Software.class,
255             Version.class,
256             AddressBook.class,
257             AddressEntry.class,
258             AddressEntryPk.class //should be silently ignored
259
};
260     }
261 }
262
Popular Tags