GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / groovy / org / codehaus / groovy / grails / orm / hibernate / UserTypeMappingTests.groovy
blobe9b8237fbf3397a8a30aeeaa35e8a031395aa1ad
1 package org.codehaus.groovy.grails.orm.hibernate
3 import javax.sql.DataSource
5 /**
6 * @author Graeme Rocher
7 */
8 class UserTypeMappingTests extends AbstractGrailsHibernateTests{
10 protected void onSetUp() {
11 gcl.parseClass '''
12 import org.hibernate.type.*
13 class UserTypeMappingTest
15 Long id
16 Long version
18 Boolean active
20 static mapping = {
21 table 'type_test'
22 columns {
23 active (column: 'active', type: YesNoType)
29 '''
30 gcl.parseClass '''
31 import org.hibernate.usertype.UserType
33 import org.hibernate.type.Type
34 import java.sql.PreparedStatement
35 import java.sql.ResultSet
36 import java.sql.SQLException
37 import org.hibernate.Hibernate
38 import org.hibernate.HibernateException
39 import java.sql.Types
41 class WeightUserType implements UserType {
43 private static final int[] SQL_TYPES = [ Types.INTEGER ]
44 public int[] sqlTypes() {
45 return SQL_TYPES
48 public Class returnedClass() {
49 return Weight.class
52 public boolean equals(Object x, Object y) throws HibernateException {
53 if (x == y) {
54 return true;
55 } else if (x == null || y == null) {
56 return false;
57 } else {
58 return x.equals(y);
62 public int hashCode(Object x) throws HibernateException {
63 return x.hashCode()
66 public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
67 Weight result = null;
68 int pounds = resultSet.getInt(names[0])
69 if (!resultSet.wasNull()) {
70 result = new Weight(pounds)
72 return result;
75 public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException {
76 if (value == null) {
77 statement.setNull(index);
78 } else {
79 Integer pounds = value.pounds
80 statement.setInt(index, pounds);
84 public Object deepCopy(Object value) throws HibernateException {
85 return value;
88 public boolean isMutable() {
89 return false;
92 public Serializable disassemble(Object value) throws HibernateException {
93 return (Serializable) value;
96 public Object assemble(Serializable state, Object owner) throws HibernateException {
97 return state;
100 public Object replace(Object original, Object target, Object owner) throws HibernateException {
101 return original;
105 class Weight {
106 Integer pounds
107 Weight(Integer pounds)
109 this.pounds= pounds
114 gcl.parseClass '''
115 class UserTypeMappingTestsPerson {
116 Long id
117 Long version
118 String name
119 Weight weight
121 static constraints = {
122 name(unique: true)
123 weight(nullable: true)
126 static mapping = {
127 columns {
128 weight( type:WeightUserType)
138 void testCustomUserType() {
139 def personClass = ga.getDomainClass("UserTypeMappingTestsPerson").clazz
140 def weightClass = ga.classLoader.loadClass("Weight")
142 def person = personClass.newInstance(name:"Fred", weight:weightClass.newInstance(200))
144 person.save(flush:true)
145 session.clear()
147 person = personClass.get(1)
149 assert person
150 assert person.weight
151 assertEquals 200, person.weight.pounds
154 void testUserTypeMapping() {
156 def clz = ga.getDomainClass("UserTypeMappingTest").clazz
159 assert clz.newInstance(active:true).save(flush:true)
161 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
163 def con
164 try {
165 con = ds.getConnection()
166 def statement = con.prepareStatement("select * from type_test")
167 def result = statement.executeQuery()
168 assert result.next()
169 def value = result.getString('active')
171 assertEquals "Y", value
173 } finally {
174 con.close()