1 | public class TuplizerDynamicEntityTest extends FunctionalTestCase {↵ | | 1 | public class ImprovedTuplizerDynamicEntityTest extends FunctionalTestCase {↵
|
2 | public TuplizerDynamicEntityTest(String x) {↵ | | 2 | public ImprovedTuplizerDynamicEntityTest(String x) {↵
|
3 | super( x );↵ | | 3 | super( x );↵
|
4 | }↵ | | 4 | }↵
|
|
5 | public String[] getMappings() {↵ | | 5 | public String[] getMappings() {↵
|
6 | return new String[] { "dynamicentity/tuplizer/Customer.hbm.xml" };↵ | | 6 | return new String[] { "dynamicentity/tuplizer2/Customer.hbm.xml" };↵
|
7 | }↵ | | 7 | }↵
|
|
8 | public void configure(Configuration cfg) {↵ | | 8 | public void configure(Configuration cfg) {↵
|
9 | super.configure( cfg );↵ | | 9 | super.configure( cfg );↵
|
10 | cfg.setInterceptor( new EntityNameInterceptor() );↵ | | 10 | cfg.getEntityTuplizerFactory().registerDefaultTuplizerClass( EntityMode.POJO, MyEntityTuplizer.class );↵
|
11 | }↵ | | 11 | }↵
|
|
12 | public static TestSuite suite() {↵ | | 12 | public static TestSuite suite() {↵
|
13 | return new FunctionalTestClassTestSuite( TuplizerDynamicEntityTest.class );↵ | | 13 | return new FunctionalTestClassTestSuite( ImprovedTuplizerDynamicEntityTest.class );↵
|
14 | }↵ | | 14 | }↵
|
|
15 | public void testIt() {↵ | | 15 | public void testIt() {↵
|
16 | // Test saving these dyna-proxies↵ | | 16 | // Test saving these dyna-proxies↵
|
17 | Session session = openSession();↵ | | 17 | Session session = openSession();↵
|
18 | session.beginTransaction();↵ | | 18 | session.beginTransaction();↵
|
19 | Company company = ProxyHelper.newCompanyProxy();↵ | | 19 | Company company = ProxyHelper.newCompanyProxy();↵
|
20 | company.setName( "acme" );↵ | | 20 | company.setName( "acme" );↵
|
21 | session.save( company );↵ | | 21 | session.save( company );↵
|
22 | Customer customer = ProxyHelper.newCustomerProxy();↵ | | 22 | Customer customer = ProxyHelper.newCustomerProxy();↵
|
23 | customer.setName( "Steve" );↵ | | 23 | customer.setName( "Steve" );↵
|
24 | customer.setCompany( company );↵ | | 24 | customer.setCompany( company );↵
|
25 | Address address = ProxyHelper.newAddressProxy();↵ | | 25 | Address address = ProxyHelper.newAddressProxy();↵
|
26 | address.setStreet( "somewhere over the rainbow" );↵ | | 26 | address.setStreet( "somewhere over the rainbow" );↵
|
27 | address.setCity( "lawerence, kansas" );↵ | | 27 | address.setCity( "lawerence, kansas" );↵
|
28 | address.setPostalCode( "toto");↵ | | 28 | address.setPostalCode( "toto");↵
|
29 | customer.setAddress( address );↵ | | 29 | customer.setAddress( address );↵
|
30 | customer.setFamily( new HashSet() );↵ | | 30 | customer.setFamily( new HashSet() );↵
|
31 | Person son = ProxyHelper.newPersonProxy();↵ | | 31 | Person son = ProxyHelper.newPersonProxy();↵
|
32 | son.setName( "son" );↵ | | 32 | son.setName( "son" );↵
|
33 | customer.getFamily().add( son );↵ | | 33 | customer.getFamily().add( son );↵
|
34 | Person wife = ProxyHelper.newPersonProxy();↵ | | 34 | Person wife = ProxyHelper.newPersonProxy();↵
|
35 | wife.setName( "wife" );↵ | | 35 | wife.setName( "wife" );↵
|
36 | customer.getFamily().add( wife );↵ | | 36 | customer.getFamily().add( wife );↵
|
37 | session.save( customer );↵ | | 37 | session.save( customer );↵
|
38 | session.getTransaction().commit();↵ | | 38 | session.getTransaction().commit();↵
|
39 | session.close();↵ | | 39 | session.close();↵
|
|
40 | assertNotNull( "company id not assigned", company.getId() );↵ | | 40 | assertNotNull( "company id not assigned", company.getId() );↵
|
41 | assertNotNull( "customer id not assigned", customer.getId() );↵ | | 41 | assertNotNull( "customer id not assigned", customer.getId() );↵
|
42 | assertNotNull( "address id not assigned", address.getId() );↵ | | 42 | assertNotNull( "address id not assigned", address.getId() );↵
|
43 | assertNotNull( "son:Person id not assigned", son.getId() );↵ | | 43 | assertNotNull( "son:Person id not assigned", son.getId() );↵
|
44 | assertNotNull( "wife:Person id not assigned", wife.getId() );↵ | | 44 | assertNotNull( "wife:Person id not assigned", wife.getId() );↵
|
|
45 | // Test loading these dyna-proxies, along with flush processing↵ | | 45 | // Test loading these dyna-proxies, along with flush processing↵
|
46 | session = openSession();↵ | | 46 | session = openSession();↵
|
47 | session.beginTransaction();↵ | | 47 | session.beginTransaction();↵
|
48 | customer = ( Customer ) session.load( Customer.class, customer.getId() );↵ | | 48 | customer = ( Customer ) session.load( Customer.class, customer.getId() );↵
|
49 | assertFalse( "should-be-proxy was initialized", Hibernate.isInitialized( customer ) );↵ | | 49 | assertFalse( "should-be-proxy was initialized", Hibernate.isInitialized( customer ) );↵
|
|
50 | customer.setName( "other" );↵ | | 50 | customer.setName( "other" );↵
|
51 | session.flush();↵ | | 51 | session.flush();↵
|
52 | assertFalse( "should-be-proxy was initialized", Hibernate.isInitialized( customer.getCompany() ) );↵ | | 52 | assertFalse( "should-be-proxy was initialized", Hibernate.isInitialized( customer.getCompany() ) );↵
|
|
53 | session.refresh( customer );↵ | | 53 | session.refresh( customer );↵
|
54 | assertEquals( "name not updated", "other", customer.getName() );↵ | | 54 | assertEquals( "name not updated", "other", customer.getName() );↵
|
55 | assertEquals( "company association not correct", "acme", customer.getCompany().getName() );↵ | | 55 | assertEquals( "company association not correct", "acme", customer.getCompany().getName() );↵
|
|
56 | session.getTransaction().commit();↵ | | 56 | session.getTransaction().commit();↵
|
57 | session.close();↵ | | 57 | session.close();↵
|
|
58 | // Test detached entity re-attachment with these dyna-proxies↵ | | 58 | // Test detached entity re-attachment with these dyna-proxies↵
|
59 | customer.setName( "Steve" );↵ | | 59 | customer.setName( "Steve" );↵
|
60 | session = openSession();↵ | | 60 | session = openSession();↵
|
61 | session.beginTransaction();↵ | | 61 | session.beginTransaction();↵
|
62 | session.update( customer );↵ | | 62 | session.update( customer );↵
|
63 | session.flush();↵ | | 63 | session.flush();↵
|
64 | session.refresh( customer );↵ | | 64 | session.refresh( customer );↵
|
65 | assertEquals( "name not updated", "Steve", customer.getName() );↵ | | 65 | assertEquals( "name not updated", "Steve", customer.getName() );↵
|
66 | session.getTransaction().commit();↵ | | 66 | session.getTransaction().commit();↵
|
67 | session.close();↵ | | 67 | session.close();↵
|
|
68 | // Test querying↵ | | 68 | // Test querying↵
|
69 | session = openSession();↵ | | 69 | session = openSession();↵
|
70 | session.beginTransaction();↵ | | 70 | session.beginTransaction();↵
|
71 | int count = session.createQuery( "from Customer" ).list().size();↵ | | 71 | int count = session.createQuery( "from Customer" ).list().size();↵
|
72 | assertEquals( "querying dynamic entity", 1, count );↵ | | 72 | assertEquals( "querying dynamic entity", 1, count );↵
|
73 | session.clear();↵ | | 73 | session.clear();↵
|
74 | count = session.createQuery( "from Person" ).list().size();↵ | | 74 | count = session.createQuery( "from Person" ).list().size();↵
|
75 | assertEquals( "querying dynamic entity", 3, count );↵ | | 75 | assertEquals( "querying dynamic entity", 3, count );↵
|
76 | session.getTransaction().commit();↵ | | 76 | session.getTransaction().commit();↵
|
77 | session.close();↵ | | 77 | session.close();↵
|
|
78 | // test deleteing↵ | | 78 | // test deleteing↵
|
79 | session = openSession();↵ | | 79 | session = openSession();↵
|
80 | session.beginTransaction();↵ | | 80 | session.beginTransaction();↵
|
81 | session.delete( company );↵ | | 81 | session.delete( company );↵
|
82 | session.delete( customer );↵ | | 82 | session.delete( customer );↵
|
83 | session.getTransaction().commit();↵ | | 83 | session.getTransaction().commit();↵
|
84 | session.close() | | 84 | session.close()
|