KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > stateless > StatelessSessionTest


1 //$Id: StatelessSessionTest.java,v 1.3 2005/07/04 02:43:23 oneovthafew Exp $
2
package org.hibernate.test.stateless;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 import org.hibernate.ScrollMode;
8 import org.hibernate.ScrollableResults;
9 import org.hibernate.StatelessSession;
10 import org.hibernate.Transaction;
11 import org.hibernate.test.TestCase;
12
13 /**
14  * @author Gavin King
15  */

16 public class StatelessSessionTest extends TestCase {
17     
18     public StatelessSessionTest(String JavaDoc str) {
19         super(str);
20     }
21
22     public void testCreateUpdateReadDelete() {
23         
24         StatelessSession ss = getSessions().openStatelessSession();
25         Transaction tx = ss.beginTransaction();
26         Document doc = new Document("blah blah blah", "Blahs");
27         ss.insert(doc);
28         tx.commit();
29         
30         ss.beginTransaction();
31         doc.setText("blah blah blah .... blah");
32         ss.update(doc);
33         tx.commit();
34         
35         Document doc2 = (Document) ss.get(Document.class.getName(), "Blahs");
36         assertEquals("Blahs", doc2.getName());
37         assertEquals(doc.getText(), doc2.getText());
38                 
39         doc2 = (Document) ss.createQuery("from Document where text is not null").uniqueResult();
40         assertEquals("Blahs", doc2.getName());
41         assertEquals(doc.getText(), doc2.getText());
42         
43         ScrollableResults sr = ss.createQuery("from Document where text is not null")
44             .scroll(ScrollMode.FORWARD_ONLY);
45         sr.next();
46         doc2 = (Document) sr.get(0);
47         sr.close();
48         assertEquals("Blahs", doc2.getName());
49         assertEquals(doc.getText(), doc2.getText());
50                 
51         doc2 = (Document) ss.createSQLQuery("select * from Document")
52             .addEntity(Document.class)
53             .uniqueResult();
54         assertEquals("Blahs", doc2.getName());
55         assertEquals(doc.getText(), doc2.getText());
56                 
57         doc2 = (Document) ss.createCriteria(Document.class).uniqueResult();
58         assertEquals("Blahs", doc2.getName());
59         assertEquals(doc.getText(), doc2.getText());
60         
61         sr = ss.createCriteria(Document.class).scroll(ScrollMode.FORWARD_ONLY);
62         sr.next();
63         doc2 = (Document) sr.get(0);
64         sr.close();
65         assertEquals("Blahs", doc2.getName());
66         assertEquals(doc.getText(), doc2.getText());
67                 
68         ss.beginTransaction();
69         ss.delete(doc);
70         tx.commit();
71         
72     }
73     
74     
75     protected String JavaDoc[] getMappings() {
76         return new String JavaDoc[] { "stateless/Document.hbm.xml" };
77     }
78
79     public static Test suite() {
80         return new TestSuite(StatelessSessionTest.class);
81     }
82
83 }
84
85
Popular Tags