1 package org.hibernate.test.typeparameters; 3 4 import java.sql.Connection ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 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 21 public class TypeParameterTest extends TestCase { 22 23 public TypeParameterTest(String name) { 24 super(name); 25 } 26 27 public void setUp() throws Exception { 28 super.setUp(); 29 } 30 31 public void tearDown() throws Exception { 32 super.tearDown(); 33 } 34 35 38 protected String [] getMappings() { 39 return new String [] { 40 "typeparameters/Typedef.hbm.xml", 41 "typeparameters/Widget.hbm.xml" 42 }; 43 } 44 45 public void testSave() throws Exception { 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 id = (Integer ) s.save(obj); 56 57 t.commit(); 58 s.close(); 59 60 s = openSession(); 61 t = s.beginTransaction(); 62 63 Connection connection = s.connection(); 64 PreparedStatement statement = connection.prepareStatement("SELECT * FROM STRANGE_TYPED_OBJECT WHERE ID=?"); 65 statement.setInt(1, id.intValue()); 66 ResultSet 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 { 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 { 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 { 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 [] args) throws Exception { 137 TestRunner.run(suite()); 138 } 139 } | Popular Tags |