Source code: org/hibernate/test/naturalid/NaturalIdTest.java
1 //$Id: NaturalIdTest.java 7749 2005-08-04 00:13:38Z oneovthafew $
2 package org.hibernate.test.naturalid;
3
4 import java.lang.reflect.Field;
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 str) {
23 super(str);
24 }
25
26 public void testNaturalIdCheck() throws Exception {
27 Session s = openSession();
28 Transaction t = s.beginTransaction();
29
30 User u = new User("gavin", "hb", "secret");
31 s.persist(u);
32 Field 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 testNonexistentNaturalIdCache() {
47 getSessions().getStatistics().clear();
48
49 Session s = openSession();
50 Transaction t = s.beginTransaction();
51
52 Object nullUser = s.createCriteria(User.class)
53 .add( Restrictions.naturalId()
54 .set("name", "gavin")
55 .set("org", "hb")
56 )
57 .setCacheable(true)
58 .uniqueResult();
59
60 assertNull(nullUser);
61
62 t.commit();
63 s.close();
64
65 assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
66 assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
67 assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 0 );
68
69 s = openSession();
70 t = s.beginTransaction();
71
72 User u = new User("gavin", "hb", "secret");
73 s.persist(u);
74
75 t.commit();
76 s.close();
77
78 getSessions().getStatistics().clear();
79
80 s = openSession();
81 t = s.beginTransaction();
82
83 u = (User) s.createCriteria(User.class)
84 .add( Restrictions.naturalId()
85 .set("name", "gavin")
86 .set("org", "hb")
87 )
88 .setCacheable(true)
89 .uniqueResult();
90
91 assertNotNull(u);
92
93 t.commit();
94 s.close();
95
96 assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
97 assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
98 assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
99
100 getSessions().getStatistics().clear();
101
102 s = openSession();
103 t = s.beginTransaction();
104
105 u = (User) s.createCriteria(User.class)
106 .add( Restrictions.naturalId()
107 .set("name", "gavin")
108 .set("org", "hb")
109 ).setCacheable(true)
110 .uniqueResult();
111
112 s.delete(u);
113
114 t.commit();
115 s.close();
116
117 assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
118 assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
119
120 getSessions().getStatistics().clear();
121
122 s = openSession();
123 t = s.beginTransaction();
124
125 nullUser = s.createCriteria(User.class)
126 .add( Restrictions.naturalId()
127 .set("name", "gavin")
128 .set("org", "hb")
129 )
130 .setCacheable(true)
131 .uniqueResult();
132
133 assertNull(nullUser);
134
135 t.commit();
136 s.close();
137
138 assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
139 assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
140 assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 0 );
141
142 }
143
144 public void testNaturalIdCache() {
145 Session s = openSession();
146 Transaction t = s.beginTransaction();
147
148 User u = new User("gavin", "hb", "secret");
149 s.persist(u);
150
151 t.commit();
152 s.close();
153
154 getSessions().getStatistics().clear();
155
156 s = openSession();
157 t = s.beginTransaction();
158
159 u = (User) s.createCriteria(User.class)
160 .add( Restrictions.naturalId()
161 .set("name", "gavin")
162 .set("org", "hb")
163 )
164 .setCacheable(true)
165 .uniqueResult();
166
167 assertNotNull(u);
168
169 t.commit();
170 s.close();
171
172 assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
173 assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
174 assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
175
176 s = openSession();
177 t = s.beginTransaction();
178
179 User v = new User("xam", "hb", "foobar");
180 s.persist(v);
181
182 t.commit();
183 s.close();
184
185 getSessions().getStatistics().clear();
186
187 s = openSession();
188 t = s.beginTransaction();
189
190 u = (User) s.createCriteria(User.class)
191 .add( Restrictions.naturalId()
192 .set("name", "gavin")
193 .set("org", "hb")
194 ).setCacheable(true)
195 .uniqueResult();
196
197 assertNotNull(u);
198
199 t.commit();
200 s.close();
201
202 assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
203 assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
204
205 s = openSession();
206 t = s.beginTransaction();
207 s.createQuery("delete User").executeUpdate();
208 t.commit();
209 s.close();
210 }
211
212 public void testQuerying() throws Exception {
213 Session s = openSession();
214 Transaction t = s.beginTransaction();
215
216 User u = new User("emmanuel", "hb", "bh");
217 s.persist(u);
218
219 t.commit();
220 s.close();
221
222 s = openSession();
223 t = s.beginTransaction();
224
225 u = (User) s.createQuery( "from User u where u.name = :name" )
226 .setParameter( "name", "emmanuel" ).uniqueResult();
227 assertEquals( "emmanuel", u.getName() );
228 s.delete( u );
229
230 t.commit();
231 s.close();
232 }
233
234
235 protected void configure(Configuration cfg) {
236 cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
237 cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
238 cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
239 }
240
241 protected String[] getMappings() {
242 return new String[] { "naturalid/User.hbm.xml" };
243 }
244
245 public static Test suite() {
246 return new TestSuite(NaturalIdTest.class);
247 }
248
249 }
250