KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > map > MapIndexFormulaTest


1 //$Id: MapIndexFormulaTest.java,v 1.4 2005/07/06 03:20:06 oneovthafew Exp $
2
package org.hibernate.test.map;
3
4 import java.util.List JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
9
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.test.TestCase;
13
14 /**
15  * @author Gavin King
16  */

17 public class MapIndexFormulaTest extends TestCase {
18     
19     public MapIndexFormulaTest(String JavaDoc str) {
20         super(str);
21     }
22     
23     public void testIndexFormulaMap() {
24         Session s = openSession();
25         Transaction t = s.beginTransaction();
26         User gavin = new User("gavin", "secret");
27         User turin = new User("turin", "tiger");
28         Group g = new Group("developers");
29         g.getUsers().put("gavin", gavin);
30         g.getUsers().put("turin", turin);
31         s.persist(g);
32         gavin.getSession().put( "foo", new SessionAttribute("foo", "foo bar baz") );
33         gavin.getSession().put( "bar", new SessionAttribute("bar", "foo bar baz 2") );
34         t.commit();
35         s.close();
36         
37         s = openSession();
38         t = s.beginTransaction();
39         g = (Group) s.get(Group.class, "developers");
40         assertEquals( g.getUsers().size(), 2 );
41         g.getUsers().remove("turin");
42         Map JavaDoc smap = ( (User) g.getUsers().get("gavin") ).getSession();
43         assertEquals(smap.size(), 2);
44         smap.remove("bar");
45         t.commit();
46         s.close();
47
48         s = openSession();
49         t = s.beginTransaction();
50         g = (Group) s.get(Group.class, "developers");
51         assertEquals( g.getUsers().size(), 1 );
52         smap = ( (User) g.getUsers().get("gavin") ).getSession();
53         assertEquals(smap.size(), 1);
54         gavin = (User) g.getUsers().put("gavin", turin);
55         s.delete(gavin);
56         assertEquals( s.createQuery("select count(*) from SessionAttribute").uniqueResult(), new Integer JavaDoc(0) );
57         t.commit();
58         s.close();
59
60         s = openSession();
61         t = s.beginTransaction();
62         g = (Group) s.get(Group.class, "developers");
63         assertEquals( g.getUsers().size(), 1 );
64         turin = (User) g.getUsers().get("turin");
65         smap = turin.getSession();
66         assertEquals(smap.size(), 0);
67         assertEquals( s.createQuery("select count(*) from User").uniqueResult(), new Integer JavaDoc(1) );
68         s.delete(g);
69         s.delete(turin);
70         assertEquals( s.createQuery("select count(*) from User").uniqueResult(), new Integer JavaDoc(0) );
71         t.commit();
72         s.close();
73     }
74     
75     public void testSQLQuery() {
76         Session s = openSession();
77         Transaction t = s.beginTransaction();
78         User gavin = new User("gavin", "secret");
79         User turin = new User("turin", "tiger");
80         gavin.getSession().put( "foo", new SessionAttribute("foo", "foo bar baz") );
81         gavin.getSession().put( "bar", new SessionAttribute("bar", "foo bar baz 2") );
82         s.persist(gavin);
83         s.persist(turin);
84         s.flush();
85         s.clear();
86         List JavaDoc results = s.getNamedQuery("userSessionData").setParameter("uname", "%in").list();
87         assertEquals( results.size(), 2 );
88         gavin = (User) ( (Object JavaDoc[]) results.get(0) )[0];
89         assertEquals( gavin.getName(), "gavin" );
90         assertEquals( gavin.getSession().size(), 2 );
91         s.createQuery("delete SessionAttribute").executeUpdate();
92         s.createQuery("delete User").executeUpdate();
93         t.commit();
94         s.close();
95         
96     }
97     
98     protected String JavaDoc[] getMappings() {
99         return new String JavaDoc[] { "map/UserGroup.hbm.xml" };
100     }
101
102     public static Test suite() {
103         return new TestSuite(MapIndexFormulaTest.class);
104     }
105
106 }
107
108
Popular Tags