KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > EntityTest


1 //$Id: EntityTest.java,v 1.5 2005/06/20 18:53:26 epbernard Exp $
2
package org.hibernate.test.annotations;
3
4 import java.util.List JavaDoc;
5 import java.util.Date JavaDoc;
6
7 import org.hibernate.HibernateException;
8 import org.hibernate.Session;
9 import org.hibernate.StaleStateException;
10 import org.hibernate.Transaction;
11 import org.hibernate.Hibernate;
12 import org.hibernate.Query;
13
14 /**
15  * @author Emmanuel Bernard
16  */

17 public class EntityTest extends TestCase {
18
19     public EntityTest(String JavaDoc x) {
20         super(x);
21     }
22     
23     public void testLoad() throws Exception JavaDoc {
24         //put an object in DB
25
Session s = openSession();
26         Transaction tx = s.beginTransaction();
27         Flight firstOne = new Flight();
28         firstOne.setId( new Long JavaDoc(1) );
29         firstOne.setName("AF3202");
30         firstOne.setDuration( new Long JavaDoc(1000000) );
31         firstOne.setDurationInSec(2000);
32         s.save(firstOne);
33         s.flush();
34         tx.commit();
35         s.close();
36         
37         //read it
38
s = openSession();
39         tx = s.beginTransaction();
40         firstOne = (Flight) s.get( Flight.class, new Long JavaDoc(1) );
41         assertNotNull(firstOne);
42         assertEquals( new Long JavaDoc(1), firstOne.getId() );
43         assertEquals( "AF3202", firstOne.getName() );
44         assertEquals( new Long JavaDoc(1000000), firstOne.getDuration() );
45         assertFalse( "Transient is not working", 2000l == firstOne.getDurationInSec() );
46         tx.commit();
47         s.close();
48     }
49     
50     public void testColumn() throws Exception JavaDoc {
51         //put an object in DB
52
Session s = openSession();
53         Transaction tx = s.beginTransaction();
54         Flight firstOne = new Flight();
55         firstOne.setId( new Long JavaDoc(1) );
56         firstOne.setName(null);
57         
58         try {
59             s.save(firstOne);
60             tx.commit();
61             fail("Name column should be not null");
62         } catch (HibernateException e) {
63             //fine
64
}
65         finally {
66             s.close();
67         }
68         
69         //insert an object and check that name is not updatable
70
s = openSession();
71         tx = s.beginTransaction();
72         firstOne = new Flight();
73         firstOne.setId( new Long JavaDoc(1) );
74         firstOne.setName("AF3202");
75         firstOne.setTriggeredData("should not be insertable");
76         tx.commit();
77         s.close();
78         
79         s = openSession();
80         tx = s.beginTransaction();
81         firstOne = (Flight) s.get( Flight.class, new Long JavaDoc(1) );
82         assertNotNull(firstOne);
83         assertEquals( new Long JavaDoc(1), firstOne.getId() );
84         assertEquals( "AF3202", firstOne.getName() );
85         assertFalse( "should not be insertable".equals( firstOne.getTriggeredData() ) );
86         firstOne.setName("BA1234");
87         firstOne.setTriggeredData("should not be updatable");
88         tx.commit();
89         s.close();
90         
91         s = openSession();
92         tx = s.beginTransaction();
93         firstOne = (Flight) s.get( Flight.class, new Long JavaDoc(1) );
94         assertNotNull(firstOne);
95         assertEquals( new Long JavaDoc(1), firstOne.getId() );
96         assertEquals( "AF3202", firstOne.getName() );
97         assertFalse( "should not be updatable".equals( firstOne.getTriggeredData() ) );
98         tx.commit();
99         s.close();
100     }
101
102     public void testColumnUnique() throws Exception JavaDoc {
103         Session s;
104         Transaction tx;
105         s = openSession();
106         tx = s.beginTransaction();
107         Sky sky = new Sky();
108         sky.id = new Long JavaDoc(2);
109         sky.color = "blue";
110         sky.day = "monday";
111         sky.month = "January";
112
113         Sky sameSky = new Sky();
114         sameSky.id = new Long JavaDoc(3);
115         sameSky.color = "blue";
116         sky.day = "tuesday";
117         sky.month="January";
118
119         try {
120             s.save(sky);
121             s.flush();
122             s.save(sameSky);
123             tx.commit();
124             fail("unique constraints not respected");
125         }
126         catch(HibernateException e) {
127             //success
128
} finally {
129             if (tx != null) tx.rollback();
130             s.close();
131         }
132     }
133
134     public void testUniqueConstraint() throws Exception JavaDoc {
135         int id=5;
136         Session s;
137         Transaction tx;
138         s = openSession();
139         tx = s.beginTransaction();
140         Sky sky = new Sky();
141         sky.id = new Long JavaDoc(id++);
142         sky.color = "green";
143         sky.day = "monday";
144         sky.month="March";
145
146         Sky otherSky = new Sky();
147         otherSky.id = new Long JavaDoc(id++);
148         otherSky.color = "red";
149         otherSky.day = "friday";
150         otherSky.month="March";
151
152         Sky sameSky = new Sky();
153         sameSky.id = new Long JavaDoc(id++);
154         sameSky.color = "green";
155         sameSky.day = "monday";
156         sameSky.month="March";
157
158         s.save(sky);
159         s.flush();
160
161         s.save(otherSky);
162         tx.commit();
163         s.close();
164
165         s = openSession();
166         tx = s.beginTransaction();
167         try {
168             s.save(sameSky);
169             tx.commit();
170             fail("unique constraints not respected");
171         }
172         catch(HibernateException e) {
173             //success
174
} finally {
175             if (tx != null) tx.rollback();
176             s.close();
177         }
178     }
179
180     public void testVersion() throws Exception JavaDoc {
181 // put an object in DB
182
Session s = openSession();
183         Transaction tx = s.beginTransaction();
184         Flight firstOne = new Flight();
185         firstOne.setId( new Long JavaDoc(2) );
186         firstOne.setName("AF3202");
187         firstOne.setDuration( new Long JavaDoc(500) );
188         s.save(firstOne);
189         s.flush();
190         tx.commit();
191         s.close();
192
193         //read it
194
s = openSession();
195         tx = s.beginTransaction();
196         firstOne = (Flight) s.get( Flight.class, new Long JavaDoc(2) );
197         tx.commit();
198         s.close();
199         
200         //read it again
201
s = openSession();
202         tx = s.beginTransaction();
203         Flight concurrentOne = (Flight) s.get( Flight.class, new Long JavaDoc(2) );
204         concurrentOne.setDuration( new Long JavaDoc(1000) );
205         s.update(concurrentOne);
206         tx.commit();
207         s.close();
208         assertFalse(firstOne == concurrentOne);
209         assertFalse( firstOne.getVersion().equals( concurrentOne.getVersion() ) );
210         
211         //reattach the first one
212
s = openSession();
213         tx = s.beginTransaction();
214         firstOne.setName("Second access");
215         s.update(firstOne);
216         try {
217             tx.commit();
218             fail("Optimistic locking should work");
219         }
220         catch (StaleStateException e) {
221             //fine
222
}
223         finally {
224             if (tx != null) tx.rollback();
225             s.close();
226         }
227         
228     }
229     
230     public void testFieldAccess() throws Exception JavaDoc {
231         Session s;
232         Transaction tx;
233         s = openSession();
234         tx = s.beginTransaction();
235         Sky sky = new Sky();
236         sky.id = new Long JavaDoc(1);
237         sky.color = "black";
238         Sky.area = "Paris";
239         s.save(sky);
240         tx.commit();
241         s.close();
242         Sky.area = "London";
243
244         s = openSession();
245         tx = s.beginTransaction();
246         sky = (Sky) s.get(Sky.class, sky.id);
247         assertNotNull(sky);
248         assertEquals("black", sky.color);
249         assertFalse("Paris".equals( Sky.area ) );
250         tx.commit();
251         s.close();
252     }
253
254     public void testEntityName() throws Exception JavaDoc {
255         Session s = openSession();
256         Transaction tx = s.beginTransaction();
257         Company comp = new Company();
258         s.persist(comp);
259         comp.setName("JBoss Inc");
260         tx.commit();
261         s.close();
262
263         s = openSession();
264         tx = s.beginTransaction();
265         List JavaDoc result = s.createQuery("from Corporation").list();
266         assertNotNull(result);
267         assertEquals( 1, result.size() );
268         tx.commit();
269         s.close();
270
271     }
272
273     public void testNonGetter() throws Exception JavaDoc {
274         Session s = openSession();
275         Transaction tx = s.beginTransaction();
276         Flight airFrance = new Flight();
277         airFrance.setId( new Long JavaDoc(747) );
278         airFrance.setName("Paris-Amsterdam");
279         airFrance.setDuration( new Long JavaDoc(10) );
280         airFrance.setFactor(25);
281         s.persist(airFrance);
282         tx.commit();
283         s.close();
284
285         s = openSession();
286         tx = s.beginTransaction();
287         airFrance = (Flight) s.get( Flight.class, airFrance.getId() );
288         assertNotNull(airFrance);
289         assertEquals( new Long JavaDoc(10), airFrance.getDuration() );
290         assertFalse( 25 == airFrance.getFactor(false) );
291         s.delete(airFrance);
292         tx.commit();
293         s.close();
294     }
295
296     public void testFormula() throws Exception JavaDoc {
297         Session s = openSession();
298         Transaction tx = s.beginTransaction();
299         Flight airFrance = new Flight();
300         airFrance.setId( new Long JavaDoc(747) );
301         airFrance.setName("Paris-Amsterdam");
302         airFrance.setDuration( new Long JavaDoc(10) );
303         airFrance.setMaxAltitude(10000);
304         s.persist(airFrance);
305         tx.commit();
306         s.close();
307
308         s = openSession();
309         tx = s.beginTransaction();
310         airFrance = (Flight) s.get( Flight.class, airFrance.getId() );
311         assertNotNull(airFrance);
312         assertEquals( new Long JavaDoc(10), airFrance.getDuration() );
313         assertEquals( 10000000, airFrance.getMaxAltitudeInMilimeter() );
314         s.delete(airFrance);
315         tx.commit();
316         s.close();
317     }
318
319     public void testTemporalType() throws Exception JavaDoc {
320         Session s = openSession();
321         Transaction tx = s.beginTransaction();
322         Flight airFrance = new Flight();
323         airFrance.setId( new Long JavaDoc(747) );
324         airFrance.setName("Paris-Amsterdam");
325         airFrance.setDuration( new Long JavaDoc(10) );
326         airFrance.setDepartureDate( new Date JavaDoc(05, 06, 21, 10, 0, 0) );
327         airFrance.setFactor(25);
328         s.persist(airFrance);
329         tx.commit();
330         s.close();
331
332         s = openSession();
333         tx = s.beginTransaction();
334         Query q = s.createQuery("from Flight f where f.departureDate = :departureDate");
335         q.setParameter("departureDate", airFrance.getDepartureDate(), Hibernate.DATE);
336         Flight copyAirFrance = (Flight) q.uniqueResult();
337         assertNotNull(airFrance);
338         assertEquals(
339                 new Date JavaDoc(05, 06, 21),
340                 copyAirFrance.getDepartureDate()
341         );
342         s.delete(copyAirFrance);
343         tx.commit();
344         s.close();
345     }
346
347     public void testBasic() throws Exception JavaDoc {
348         Session s = openSession();
349         Transaction tx = s.beginTransaction();
350         Flight airFrance = new Flight();
351         airFrance.setId( new Long JavaDoc(747) );
352         airFrance.setName("Paris-Amsterdam");
353         airFrance.setDuration( null );
354         airFrance.setMaxAltitude(10000);
355         try {
356             s.persist(airFrance);
357             tx.commit();
358             fail("Basic(optional=false) fails");
359         }
360         catch (Exception JavaDoc e) {
361             //success
362
if (tx != null) tx.rollback();
363         }
364         finally {
365             s.close();
366         }
367     }
368
369     /**
370      * @see org.hibernate.test.annotations.TestCase#getMappings()
371      */

372     protected Class JavaDoc[] getMappings() {
373         return new Class JavaDoc[] {
374             Flight.class,
375             Company.class,
376             Sky.class
377         };
378     }
379
380 }
381
382
Popular Tags