KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > dynamic > DynamicClassTest


1 //$Id: DynamicClassTest.java,v 1.4 2005/03/06 16:31:24 oneovthafew Exp $
2
package org.hibernate.test.dynamic;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import junit.framework.Test;
11 import junit.framework.TestSuite;
12
13 import org.hibernate.EntityMode;
14 import org.hibernate.Hibernate;
15 import org.hibernate.Session;
16 import org.hibernate.Transaction;
17 import org.hibernate.cfg.Configuration;
18 import org.hibernate.cfg.Environment;
19 import org.hibernate.test.TestCase;
20
21 /**
22  * @author Gavin King
23  */

24 public class DynamicClassTest extends TestCase {
25     
26     public DynamicClassTest(String JavaDoc str) {
27         super(str);
28     }
29
30     protected void configure(Configuration cfg) {
31         cfg.setProperty(Environment.DEFAULT_ENTITY_MODE, EntityMode.MAP.toString());
32     }
33
34     public void testLazyDynamicClass() {
35         Session s = openSession();
36         assertTrue( "Incorrectly handled default_entity_mode", s.getEntityMode() == EntityMode.MAP );
37         Session other = s.getSession( EntityMode.MAP );
38         assertEquals( "openSession() using same entity-mode returned new session", s, other );
39
40         other = s.getSession( EntityMode.POJO );
41         other.close();
42         assertTrue( !other.isOpen() );
43         assertTrue( other.isConnected() ); // because it is linked to the "root" session's connection
44

45         s.close();
46
47         s = openSession();
48         Transaction t = s.beginTransaction();
49
50         Map JavaDoc cars = new HashMap JavaDoc();
51         cars.put("description", "Cars");
52         Map JavaDoc monaro = new HashMap JavaDoc();
53         monaro.put("productLine", cars);
54         monaro.put("name", "monaro");
55         monaro.put("description", "Holden Monaro");
56         Map JavaDoc hsv = new HashMap JavaDoc();
57         hsv.put("productLine", cars);
58         hsv.put("name", "hsv");
59         hsv.put("description", "Holden Commodore HSV");
60         List JavaDoc models = new ArrayList JavaDoc();
61         cars.put("models", models);
62         models.add(hsv);
63         models.add(monaro);
64         s.save("ProductLine", cars);
65         t.commit();
66         s.close();
67
68         s = openSession();
69         t = s.beginTransaction();
70         
71         cars = (Map JavaDoc) s.createQuery("from ProductLine pl order by pl.description").uniqueResult();
72         models = (List JavaDoc) cars.get("models");
73         assertFalse( Hibernate.isInitialized(models) );
74         assertEquals( models.size(), 2);
75         assertTrue( Hibernate.isInitialized(models) );
76         
77         s.clear();
78         
79         List JavaDoc list = s.createQuery("from Model m").list();
80         for ( Iterator JavaDoc i=list.iterator(); i.hasNext(); ) {
81             assertFalse( Hibernate.isInitialized( ( (Map JavaDoc) i.next() ).get("productLine") ) );
82         }
83         Map JavaDoc model = (Map JavaDoc) list.get(0);
84         assertTrue( ( (List JavaDoc) ( (Map JavaDoc) model.get("productLine") ).get("models") ).contains(model) );
85         s.clear();
86         
87         t.commit();
88         s.close();
89
90         s = openSession();
91         t = s.beginTransaction();
92         cars = (Map JavaDoc) s.createQuery("from ProductLine pl order by pl.description").uniqueResult();
93         s.delete(cars);
94         t.commit();
95         s.close();
96     }
97
98     
99     protected String JavaDoc[] getMappings() {
100         return new String JavaDoc[] { "dynamic/ProductLine.hbm.xml" };
101     }
102
103     public static Test suite() {
104         return new TestSuite(DynamicClassTest.class);
105     }
106
107 }
108
109
Popular Tags