KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > typeparameters > TypeParameterTest


1 //$Id: TypeParameterTest.java,v 1.3 2005/02/12 07:27:31 steveebersole Exp $
2
package org.hibernate.test.typeparameters;
3
4 import java.sql.Connection JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7
8 import org.hibernate.classic.Session;
9 import org.hibernate.Transaction;
10 import org.hibernate.test.TestCase;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15
16 /**
17  * Test for parameterizable types.
18  *
19  * @author Michael Gloegl
20  */

21 public class TypeParameterTest extends TestCase {
22
23     public TypeParameterTest(String JavaDoc name) {
24         super(name);
25     }
26
27     public void setUp() throws Exception JavaDoc {
28         super.setUp();
29     }
30
31     public void tearDown() throws Exception JavaDoc {
32         super.tearDown();
33     }
34
35     /**
36      * @see org.hibernate.test.TestCase#getMappings()
37      */

38     protected String JavaDoc[] getMappings() {
39         return new String JavaDoc[] {
40                 "typeparameters/Typedef.hbm.xml",
41                 "typeparameters/Widget.hbm.xml"
42         };
43     }
44
45     public void testSave() throws Exception JavaDoc {
46         deleteData();
47
48         Session s = openSession();
49
50         Transaction t = s.beginTransaction();
51
52         Widget obj = new Widget();
53         obj.setValueThree(5);
54
55         Integer JavaDoc id = (Integer JavaDoc) s.save(obj);
56
57         t.commit();
58         s.close();
59
60         s = openSession();
61         t = s.beginTransaction();
62
63         Connection JavaDoc connection = s.connection();
64         PreparedStatement JavaDoc statement = connection.prepareStatement("SELECT * FROM STRANGE_TYPED_OBJECT WHERE ID=?");
65         statement.setInt(1, id.intValue());
66         ResultSet JavaDoc resultSet = statement.executeQuery();
67
68         TestCase.assertTrue("A row should have been returned", resultSet.next());
69         TestCase.assertTrue("Default value should have been mapped to null", resultSet.getObject("VALUE_ONE") == null);
70         TestCase.assertTrue("Default value should have been mapped to null", resultSet.getObject("VALUE_TWO") == null);
71         TestCase.assertEquals("Non-Default value should not be changed", resultSet.getInt("VALUE_THREE"), 5);
72         TestCase.assertTrue("Default value should have been mapped to null", resultSet.getObject("VALUE_FOUR") == null);
73
74         t.commit();
75         s.close();
76     }
77
78     public void testLoading() throws Exception JavaDoc {
79         initData();
80
81         Session s = openSession();
82         Transaction t = s.beginTransaction();
83
84         Widget obj = (Widget) s.createQuery("from Widget o where o.string = :string").setString("string", "all-normal").uniqueResult();
85         TestCase.assertEquals("Non-Default value incorrectly loaded", obj.getValueOne(), 7);
86         TestCase.assertEquals("Non-Default value incorrectly loaded", obj.getValueTwo(), 8);
87         TestCase.assertEquals("Non-Default value incorrectly loaded", obj.getValueThree(), 9);
88         TestCase.assertEquals("Non-Default value incorrectly loaded", obj.getValueFour(), 10);
89
90         obj = (Widget) s.createQuery("from Widget o where o.string = :string").setString("string", "all-default").uniqueResult();
91         TestCase.assertEquals("Default value incorrectly loaded", obj.getValueOne(), 1);
92         TestCase.assertEquals("Default value incorrectly loaded", obj.getValueTwo(), 2);
93         TestCase.assertEquals("Default value incorrectly loaded", obj.getValueThree(), -1);
94         TestCase.assertEquals("Default value incorrectly loaded", obj.getValueFour(), -5);
95
96         t.commit();
97         s.close();
98     }
99
100     private void initData() throws Exception JavaDoc {
101         Session s = openSession();
102         Transaction t = s.beginTransaction();
103
104         Widget obj = new Widget();
105         obj.setValueOne(7);
106         obj.setValueTwo(8);
107         obj.setValueThree(9);
108         obj.setValueFour(10);
109         obj.setString("all-normal");
110         s.save(obj);
111
112         obj = new Widget();
113         obj.setValueOne(1);
114         obj.setValueTwo(2);
115         obj.setValueThree(-1);
116         obj.setValueFour(-5);
117         obj.setString("all-default");
118         s.save(obj);
119
120         t.commit();
121         s.close();
122     }
123
124     private void deleteData() throws Exception JavaDoc {
125         Session s = openSession();
126         Transaction t = s.beginTransaction();
127         s.delete("from Widget");
128         t.commit();
129         s.close();
130     }
131
132     public static Test suite() {
133         return new TestSuite(TypeParameterTest.class);
134     }
135
136     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
137         TestRunner.run(suite());
138     }
139 }
Popular Tags