KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > legacy > MapTest


1 //$Id: MapTest.java,v 1.5 2005/07/15 14:56:36 steveebersole Exp $
2
package org.hibernate.test.legacy;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.hibernate.Session;
10 import org.hibernate.criterion.Example;
11 import org.hibernate.criterion.Expression;
12 import org.hibernate.dialect.HSQLDialect;
13 import org.hibernate.test.TestCase;
14 import org.hibernate.cfg.Configuration;
15 import org.hibernate.cfg.Environment;
16 import org.hibernate.EntityMode;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20 import junit.textui.TestRunner;
21
22
23 public class MapTest extends TestCase {
24
25     public MapTest(String JavaDoc x) {
26         super(x);
27     }
28
29     public void testMap() throws Exception JavaDoc {
30         if ( (getDialect() instanceof HSQLDialect) ) return;
31
32         Session s = openSession().getSession(EntityMode.MAP);
33         Map JavaDoc map = new HashMap JavaDoc();
34         map.put("$type$", "TestMap");
35         map.put("name", "foo");
36         map.put("address", "bar");
37         Map JavaDoc cmp = new HashMap JavaDoc();
38         cmp.put( "a", new Integer JavaDoc(1) );
39         cmp.put( "b", new Float JavaDoc(1.0) );
40         map.put("cmp", cmp);
41         s.save(map);
42         s.flush();
43         s.connection().commit();
44         s.close();
45
46         s = openSession().getSession(EntityMode.MAP);
47         map = (Map JavaDoc) s.get( "TestMap", (Serializable JavaDoc) map.get("id") );
48         assertTrue( map!=null && "foo".equals( map.get("name") ) );
49         assertTrue( map.get("$type$").equals("TestMap") );
50
51         int size = s.createCriteria("TestMap").add( Example.create(map) ).list().size();
52         assertTrue(size==1);
53         s.connection().commit();
54         s.close();
55
56         s = openSession().getSession(EntityMode.MAP);
57         List JavaDoc list = s.createQuery("from TestMap").list();
58         map = (Map JavaDoc) list.get(0);
59         assertTrue( "foo".equals( map.get("name") ) );
60         assertTrue( "bar".equals( map.get("address") ) );
61         cmp = (Map JavaDoc) map.get("cmp");
62         assertTrue( new Integer JavaDoc(1).equals( cmp.get("a") ) && new Float JavaDoc(1.0).equals( cmp.get("b") ) );
63         assertTrue( null==map.get("parent") );
64         map.put("name", "foobar");
65         map.put("parent", map);
66         List JavaDoc bag = (List JavaDoc) map.get("children");
67         bag.add(map);
68         s.flush();
69         s.connection().commit();
70         s.close();
71
72         s = openSession();
73         list = s.createQuery("from TestMap tm where tm.address = 'bar'").list();
74         map = (Map JavaDoc) list.get(0);
75         assertTrue( "foobar".equals( map.get("name") ) );
76         assertTrue( "bar".equals( map.get("address") ) );
77         assertTrue( map==map.get("parent") );
78         bag = (List JavaDoc) map.get("children");
79         assertTrue( bag.size()==1 );
80
81         size = s.createCriteria("TestMap")
82             .add( Expression.eq("address", "bar") )
83                 .createCriteria("parent")
84                 .add( Expression.eq("name", "foobar") )
85             .list()
86             .size();
87         assertTrue(size==1);
88
89         s.delete(map);
90         s.flush();
91         s.connection().commit();
92         s.close();
93
94     }
95
96     public void testMapOneToOne() throws Exception JavaDoc {
97         Map JavaDoc child = new HashMap JavaDoc();
98         Map JavaDoc parent = new HashMap JavaDoc();
99         Session s = openSession();
100         child.put("parent", parent);
101         child.put("$type$", "ChildMap");
102         parent.put("child", child);
103         parent.put("$type$", "ParentMap");
104         s.save(parent);
105         s.flush();
106         s.connection().commit();
107         s.close();
108
109         s = openSession();
110         Map JavaDoc cm = (Map JavaDoc) s.createQuery("from ChildMap cm where cm.parent is not null").uniqueResult();
111         s.delete(cm);
112         s.delete( cm.get("parent") );
113         s.flush();
114         s.connection().commit();
115         s.close();
116
117         child = new HashMap JavaDoc();
118         parent = new HashMap JavaDoc();
119         s = openSession();
120         child.put("parent", parent);
121         child.put("$type$", "ChildMap");
122         parent.put("child", child);
123         parent.put("$type$", "ParentMap");
124         s.save(child);
125         s.flush();
126         s.connection().commit();
127         s.close();
128
129         s = openSession();
130         Map JavaDoc pm = (Map JavaDoc) s.createQuery("from ParentMap cm where cm.child is not null").uniqueResult();
131         s.delete(pm);
132         s.delete( pm.get("child") );
133         s.flush();
134         s.connection().commit();
135         s.close();
136
137     }
138
139     public void testOneToOnePropertyRef() throws Exception JavaDoc {
140         Session s = openSession();
141         s.createQuery("from Commento c where c.marelo.mlmag = 0").list();
142         s.createQuery("from Commento c where c.marelo.commento.mcompr is null").list();
143         s.createQuery("from Commento c where c.marelo.mlink = 0").list();
144         s.createQuery("from Commento c where c.marelo.commento = c").list();
145         s.createQuery("from Commento c where c.marelo.id.mlmag = 0").list();
146         s.createQuery("from Commento c where c.marelo.commento.id = c.id").list();
147         s.createQuery("from Commento c where c.marelo.commento.mclink = c.mclink").list();
148         s.createQuery("from Marelo m where m.commento.id > 0").list();
149         s.createQuery("from Marelo m where m.commento.marelo.commento.marelo.mlmag is not null").list();
150         s.connection().commit();
151         s.close();
152     }
153
154     public String JavaDoc[] getMappings() {
155         return new String JavaDoc[] { "legacy/Map.hbm.xml", "legacy/Commento.hbm.xml", "legacy/Marelo.hbm.xml" };
156     }
157
158     protected void configure(Configuration cfg) {
159         super.configure( cfg );
160         cfg.setProperty(Environment.DEFAULT_ENTITY_MODE, EntityMode.MAP.toString());
161     }
162
163     public static Test suite() {
164         return new TestSuite(MapTest.class);
165     }
166
167     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
168         TestRunner.run( suite() );
169     }
170
171 }
172
173
174
175
176
177
178
179
Popular Tags