KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > naturalid > NaturalIdTest


1 //$Id: NaturalIdTest.java,v 1.3 2005/07/18 16:36:24 epbernard Exp $
2
package org.hibernate.test.naturalid;
3
4 import java.lang.reflect.Field JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.cfg.Configuration;
13 import org.hibernate.cfg.Environment;
14 import org.hibernate.criterion.Restrictions;
15 import org.hibernate.test.TestCase;
16
17 /**
18  * @author Gavin King
19  */

20 public class NaturalIdTest extends TestCase {
21     
22     public NaturalIdTest(String JavaDoc str) {
23         super(str);
24     }
25     
26     public void testNaturalIdCheck() throws Exception JavaDoc {
27         Session s = openSession();
28         Transaction t = s.beginTransaction();
29         
30         User u = new User("gavin", "hb", "secret");
31         s.persist(u);
32         Field JavaDoc name = u.getClass().getDeclaredField("name");
33         name.setAccessible(true);
34         name.set(u, "Gavin");
35         try {
36             s.flush();
37             fail();
38         }
39         catch (HibernateException he) {}
40         name.set(u, "gavin");
41         s.delete(u);
42         t.commit();
43         s.close();
44     }
45     
46     public void testNaturalIdCache() {
47         Session s = openSession();
48         Transaction t = s.beginTransaction();
49         
50         User u = new User("gavin", "hb", "secret");
51         s.persist(u);
52         
53         t.commit();
54         s.close();
55         
56         getSessions().getStatistics().clear();
57
58         s = openSession();
59         t = s.beginTransaction();
60         
61         s.createCriteria(User.class)
62             .add( Restrictions.naturalId()
63                 .set("name", "gavin")
64                 .set("org", "hb")
65             )
66             .setCacheable(true)
67             .uniqueResult();
68         
69         t.commit();
70         s.close();
71
72         assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
73         assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
74         assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
75         
76         s = openSession();
77         t = s.beginTransaction();
78         
79         User v = new User("xam", "hb", "foobar");
80         s.persist(v);
81         
82         t.commit();
83         s.close();
84         
85         getSessions().getStatistics().clear();
86
87         s = openSession();
88         t = s.beginTransaction();
89         
90         s.createCriteria(User.class)
91             .add( Restrictions.naturalId()
92                 .set("name", "gavin")
93                 .set("org", "hb")
94             ).setCacheable(true)
95             .uniqueResult();
96         
97         t.commit();
98         s.close();
99         
100         assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
101         assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
102
103     }
104
105     public void testQuerying() throws Exception JavaDoc {
106         Session s = openSession();
107         Transaction t = s.beginTransaction();
108
109         User u = new User("emmanuel", "hb", "bh");
110         s.persist(u);
111
112         t.commit();
113         s.close();
114
115         s = openSession();
116         t = s.beginTransaction();
117
118         u = (User) s.createQuery( "from User u where u.name = :name" )
119             .setParameter( "name", "emmanuel" ).uniqueResult();
120         assertEquals( "emmanuel", u.getName() );
121         s.delete( u );
122
123         t.commit();
124         s.close();
125     }
126
127
128     protected void configure(Configuration cfg) {
129         cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
130         cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
131         cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
132     }
133
134     protected String JavaDoc[] getMappings() {
135         return new String JavaDoc[] { "naturalid/User.hbm.xml" };
136     }
137
138     public static Test suite() {
139         return new TestSuite(NaturalIdTest.class);
140     }
141     
142 }
143
144
Popular Tags