Source code: org/hibernate/test/ejb3/proxy/Ejb3ProxyTest.java
1 package org.hibernate.test.ejb3.proxy;
2
3 import org.hibernate.test.TestCase;
4 import org.hibernate.test.ejb3.Item;
5 import org.hibernate.cfg.Configuration;
6 import org.hibernate.proxy.EntityNotFoundDelegate;
7 import org.hibernate.Session;
8 import org.hibernate.Transaction;
9 import org.hibernate.Hibernate;
10
11 import java.io.Serializable;
12
13 import junit.framework.AssertionFailedError;
14 import junit.framework.Test;
15 import junit.framework.TestSuite;
16
17 /**
18 * Test relation between proxies and get()/load() processing
19 * and make sure the interactions match the ejb3 expectations
20 *
21 * @author Steve Ebersole
22 */
23 public class Ejb3ProxyTest extends TestCase {
24 public Ejb3ProxyTest(String name) {
25 super( name );
26 }
27
28 public static Test suite() {
29 return new TestSuite( Ejb3ProxyTest.class );
30 }
31
32 public void testEjb3ProxyUsage() {
33 Session s = openSession();
34 Transaction txn = s.beginTransaction();
35
36 Item item = ( Item ) s.load( Item.class, new Long(-1) );
37 assertFalse( Hibernate.isInitialized( item ) );
38 try {
39 Hibernate.initialize( item );
40 fail( "proxy access did not fail on non-existent proxy" );
41 }
42 catch ( EntityNotFoundException e ) {
43 // expected behavior
44 }
45 catch ( Throwable t ) {
46 fail( "unexpected exception type on non-existent proxy access : " + t );
47 }
48
49 s.clear();
50
51 Item item2 = ( Item ) s.load( Item.class, new Long(-1) );
52 assertFalse( Hibernate.isInitialized( item2 ) );
53 assertFalse( item == item2 );
54 try {
55 item2.getName();
56 fail( "proxy access did not fail on non-existent proxy" );
57 }
58 catch ( EntityNotFoundException e ) {
59 // expected behavior
60 }
61 catch ( Throwable t ) {
62 fail( "unexpected exception type on non-existent proxy access : " + t );
63 }
64
65 txn.commit();
66 s.close();
67 }
68
69 /**
70 * The ejb3 find() method maps to the Hibernate get() method
71 */
72 public void testGetSemantics() {
73 Long nonExistentId = new Long( -1 );
74 Session s = openSession();
75 Transaction txn = s.beginTransaction();
76 Item item = ( Item ) s.get( Item.class, nonExistentId );
77 assertNull( "get() of non-existent entity did not return null", item );
78 txn.commit();
79 s.close();
80
81 s = openSession();
82 txn = s.beginTransaction();
83 // first load() it to generate a proxy...
84 item = ( Item ) s.load( Item.class, nonExistentId );
85 assertFalse( Hibernate.isInitialized( item ) );
86 // then try to get() it to make sure we get an exception
87 try {
88 Item item2 = ( Item ) s.get( Item.class, nonExistentId );
89 fail( "force load did not fail on non-existent entity" );
90 }
91 catch ( EntityNotFoundException e ) {
92 // expected behavior
93 }
94 catch( AssertionFailedError e ) {
95 throw e;
96 }
97 catch ( Throwable t ) {
98 fail( "unexpected exception type on non-existent entity force load : " + t );
99 }
100 txn.commit();
101 s.close();
102 }
103
104 protected void configure(Configuration cfg) {
105 super.configure( cfg );
106 cfg.setEntityNotFoundDelegate( new Ejb3EntityNotFoundDelegate() );
107 }
108
109 protected String[] getMappings() {
110 return new String[] { "ejb3/Item.hbm.xml", "ejb3/Part.hbm.xml" };
111 }
112
113 private static class Ejb3EntityNotFoundDelegate implements EntityNotFoundDelegate {
114 public void handleEntityNotFound(String entityName, Serializable id) {
115 throw new EntityNotFoundException( entityName, id );
116 }
117 }
118
119 private static class EntityNotFoundException extends RuntimeException {
120 private final String entityName;
121 private final Serializable id;
122
123 public EntityNotFoundException(String entityName, Serializable id) {
124 this( "unable to locate specified entity", entityName, id );
125 }
126
127 public EntityNotFoundException(String message, String entityName, Serializable id) {
128 super( message );
129 this.entityName = entityName;
130 this.id = id;
131 }
132
133 public String getEntityName() {
134 return entityName;
135 }
136
137 public Serializable getId() {
138 return id;
139 }
140 }
141 }