KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: ABCTest.java,v 1.2 2004/11/30 04:50:21 oneovthafew Exp $
2
package org.hibernate.test.legacy;
3
4
5 import java.util.List JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
9 import junit.textui.TestRunner;
10 import org.hibernate.classic.Session;
11 import org.hibernate.test.TestCase;
12 import org.hibernate.Transaction;
13
14 public class ABCTest extends TestCase {
15
16     public ABCTest(String JavaDoc arg0) {
17         super(arg0);
18     }
19     
20     public void testFormulaAssociation() throws Throwable JavaDoc {
21         Session s = openSession();
22         Transaction t = s.beginTransaction();
23         D d = new D();
24         Long JavaDoc did = new Long JavaDoc(12);
25         s.save(d, did);
26         A a = new A();
27         a.setName("a");
28         s.save(a, did);
29         t.commit();
30         s.close();
31         
32         s = openSession();
33         t = s.beginTransaction();
34         d = (D) s.get(D.class, did);
35         assertTrue(d.getReverse().getId().equals(did));
36         s.clear();
37         getSessions().evict(D.class);
38         getSessions().evict(A.class);
39         d = (D) s.get(D.class, did);
40         assertTrue(d.inverse.getId().equals(did));
41         assertTrue(d.inverse.getName().equals("a"));
42         s.clear();
43         getSessions().evict(D.class);
44         getSessions().evict(A.class);
45         assertTrue( s.find("from D d join d.reverse r join d.inverse i where i = r").size()==1 );
46         t.commit();
47         s.close();
48     }
49
50     public void testHigherLevelIndexDefinition() throws Throwable JavaDoc {
51         String JavaDoc[] commands = getCfg().generateSchemaCreationScript( getDialect() );
52         int max = commands.length;
53         boolean found = false;
54         for (int indx = 0; indx < max; indx++) {
55             System.out.println("Checking command : " + commands[indx]);
56             found = commands[indx].indexOf("create index indx_a_name") >= 0;
57             if (found)
58                 break;
59         }
60         assertTrue("Unable to locate indx_a_name index creation", found);
61     }
62
63     public void testSubclassing() throws Exception JavaDoc {
64         Session s = openSession();
65         Transaction t = s.beginTransaction();
66         C1 c1 = new C1();
67         D d = new D();
68         d.setAmount(213.34f);
69         c1.setAddress("foo bar");
70         c1.setCount(23432);
71         c1.setName("c1");
72         c1.setBName("a funny name");
73         c1.setD(d);
74         s.save(c1);
75         d.setId( c1.getId() );
76         s.save(d);
77
78         assertTrue( s.find("from C2 c where 1=1 or 1=1").size()==0 );
79
80         t.commit();
81         s.close();
82
83         getSessions().evict(A.class);
84         
85         s = openSession();
86         t = s.beginTransaction();
87         c1 = (C1) s.get( A.class, c1.getId() );
88         assertTrue(
89             c1.getAddress().equals("foo bar") &&
90             (c1.getCount()==23432) &&
91             c1.getName().equals("c1") &&
92             c1.getD().getAmount()>213.3f
93         );
94         assertEquals( "a funny name", c1.getBName() );
95         t.commit();
96         s.close();
97         
98         getSessions().evict(A.class);
99
100         s = openSession();
101         t = s.beginTransaction();
102         c1 = (C1) s.get( B.class, c1.getId() );
103         assertTrue(
104             c1.getAddress().equals("foo bar") &&
105             (c1.getCount()==23432) &&
106             c1.getName().equals("c1") &&
107             c1.getD().getAmount()>213.3f
108         );
109         assertEquals( "a funny name", c1.getBName() );
110         t.commit();
111         s.close();
112
113         s = openSession();
114         t = s.beginTransaction();
115         c1 = (C1) s.load( C1.class, c1.getId() );
116         assertTrue(
117             c1.getAddress().equals("foo bar") &&
118             (c1.getCount()==23432) &&
119             c1.getName().equals("c1") &&
120             c1.getD().getAmount()>213.3f
121         );
122         t.commit();
123         s.close();
124
125         s = openSession();
126         t = s.beginTransaction();
127         List JavaDoc bs = s.createQuery("from B").list();
128         for (int i=0; i<bs.size(); i++) {
129             C1 b = (C1) bs.get(i);
130             s.delete(b);
131             s.delete( b.getD() );
132         }
133         t.commit();
134         s.close();
135     }
136     
137     public void testGetSave() throws Exception JavaDoc {
138         Session s = openSession();
139         Transaction t = s.beginTransaction();
140         assertNull( s.get( D.class, new Long JavaDoc(1) ) );
141         D d = new D();
142         d.setId( new Long JavaDoc(1) );
143         s.save(d);
144         s.flush();
145         assertNotNull( s.get( D.class, new Long JavaDoc(1) ) );
146         s.delete(d);
147         s.flush();
148         t.commit();
149         s.close();
150     }
151
152     public String JavaDoc[] getMappings() {
153         return new String JavaDoc[] { "legacy/ABC.hbm.xml", "legacy/ABCExtends.hbm.xml" };
154     }
155
156     public static Test suite() {
157         return new TestSuite(ABCTest.class);
158     }
159
160     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
161         TestRunner.run( suite() );
162     }
163
164 }
165
166
167
168
169
Popular Tags