GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / groovy / org / codehaus / groovy / grails / orm / hibernate / BidirectionalListMappingTests.groovy
blobf1f5fb141b371b2d0d87f1d0de4e7609f653c172
1 package org.codehaus.groovy.grails.orm.hibernate
3 import org.codehaus.groovy.grails.orm.hibernate.cfg.DefaultGrailsDomainConfiguration
4 import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
5 import org.hibernate.mapping.PersistentClass
6 import org.hibernate.mapping.Property
7 import org.hibernate.mapping.ManyToOne
8 import org.hibernate.mapping.Column
9 import org.hibernate.mapping.Table
10 import org.hibernate.mapping.SimpleValue
11 import org.hibernate.mapping.KeyValue
12 import org.hibernate.mapping.OneToMany
13 import org.hibernate.engine.CascadeStyle
14 import org.hibernate.mapping.PropertyGeneration
15 import org.hibernate.FetchMode
16 import org.hibernate.type.ManyToOneType
17 import org.hibernate.mapping.List
18 import org.hibernate.type.IntegerType
19 import org.hibernate.type.LongType
20 import org.hibernate.mapping.IndexBackref
21 import org.hibernate.mapping.Backref
22 import org.hibernate.mapping.DependantValue
24 /**
25 * @author Graeme Rocher
26 * @since 1.0
28 * Created: Mar 12, 2008
30 class BidirectionalListMappingTests extends GroovyTestCase {
31 DefaultGrailsDomainConfiguration config
33 protected void setUp() {
35 def gcl = new GroovyClassLoader()
36 gcl.parseClass '''
37 class TestFaqSection
39 Long id
40 Long version
41 String title
42 List elements
43 static hasMany = [elements:TestFaqElement]
45 class TestFaqElement
47 Long id
48 Long version
49 String question
50 String answer
51 TestFaqSection section
53 '''
55 config = new DefaultGrailsDomainConfiguration()
57 DefaultGrailsApplication application = new DefaultGrailsApplication(gcl.loadedClasses, gcl)
58 application.initialise()
59 config.grailsApplication = application
62 config.buildMappings()
68 void testIndexBackrefMapping() {
69 PersistentClass faqSection = config.getClassMapping("TestFaqSection")
70 PersistentClass faqElement = config.getClassMapping("TestFaqElement")
71 IndexBackref elementsIndexBackref = faqElement.getProperty("_elementsIndexBackref")
73 assertTrue elementsIndexBackref.isBackRef()
74 assertFalse elementsIndexBackref.isBasicPropertyAccessor()
75 assertFalse elementsIndexBackref.isComposite()
76 assertTrue elementsIndexBackref.isInsertable()
77 assertFalse elementsIndexBackref.isLazy()
78 assertFalse elementsIndexBackref.isNaturalIdentifier()
79 assertTrue elementsIndexBackref.isOptimisticLocked()
80 assertTrue elementsIndexBackref.isOptional()
81 assertFalse elementsIndexBackref.isSelectable()
82 assertFalse elementsIndexBackref.isSelectable()
84 assertNull elementsIndexBackref.getCascade()
85 assertEquals CascadeStyle.NONE, elementsIndexBackref.getCascadeStyle()
86 assertEquals "TestFaqSection.elements", elementsIndexBackref.getCollectionRole()
87 assertEquals 1, elementsIndexBackref.getColumnSpan()
88 assertEquals "TestFaqSection", elementsIndexBackref.getEntityName()
89 assertEquals PropertyGeneration.NEVER, elementsIndexBackref.getGeneration()
90 assertEquals "_elementsIndexBackref", elementsIndexBackref.getName()
91 assertNull elementsIndexBackref.getNodeName()
92 assertNull elementsIndexBackref.getPropertyAccessorName()
93 assertEquals IntegerType, elementsIndexBackref.getType().getClass()
94 assertEquals SimpleValue, elementsIndexBackref.getValue().getClass()
96 SimpleValue value = elementsIndexBackref.getValue()
98 assertFalse value.isAlternateUniqueKey()
99 assertFalse value.isCascadeDeleteEnabled()
100 assertTrue value.isNullable()
101 assertTrue value.isSimpleValue()
102 assertTrue value.isTypeSpecified()
103 assertTrue value.isUpdateable()
106 void testCollectionBackrefMapping() {
107 PersistentClass faqSection = config.getClassMapping("TestFaqSection")
108 PersistentClass faqElement = config.getClassMapping("TestFaqElement")
109 Backref elementsBackref =faqElement.getProperty("_TestFaqSection_elementsBackref")
111 assertTrue elementsBackref.isBackRef()
112 assertFalse elementsBackref.isBasicPropertyAccessor()
113 assertFalse elementsBackref.isComposite()
114 assertTrue elementsBackref.isInsertable()
115 assertFalse elementsBackref.isLazy()
116 assertFalse elementsBackref.isNaturalIdentifier()
117 assertTrue elementsBackref.isOptimisticLocked()
118 assertFalse elementsBackref.isOptional()
119 assertFalse elementsBackref.isSelectable()
120 assertFalse elementsBackref.isUpdateable()
122 assertNull elementsBackref.getCascade()
123 assertEquals CascadeStyle.NONE, elementsBackref.getCascadeStyle()
124 assertEquals "TestFaqSection.elements", elementsBackref.getCollectionRole()
125 assertEquals 1, elementsBackref.getColumnSpan()
126 assertEquals "TestFaqSection", elementsBackref.getEntityName()
127 assertEquals PropertyGeneration.NEVER, elementsBackref.getGeneration()
128 assertEquals "_TestFaqSection_elementsBackref", elementsBackref.getName()
129 assertNull elementsBackref.getNodeName()
130 assertEquals "TestFaqElement", elementsBackref.getPersistentClass().getClassName()
131 assertNull elementsBackref.getPropertyAccessorName()
132 assertEquals LongType, elementsBackref.getType().getClass()
133 assertEquals DependantValue, elementsBackref.getValue().getClass()
136 DependantValue value = elementsBackref.getValue()
138 assertFalse value.isAlternateUniqueKey()
139 assertFalse value.isCascadeDeleteEnabled()
140 assertFalse value.isNullable()
141 assertTrue value.isSimpleValue()
142 assertFalse value.isTypeSpecified()
143 assertTrue value.isUpdateable()
144 assertEquals 1,value.getColumnInsertability().size()
145 assertTrue value.getColumnInsertability()[0]
146 assertEquals 1,value.getColumnUpdateability().size()
147 assertTrue value.getColumnUpdateability()[0]
149 assertEquals 1, value.getColumnSpan()
150 assertEquals FetchMode.SELECT, value.getFetchMode()
151 assertNull value.getForeignKeyName()
152 assertEquals "assigned", value.getIdentifierGeneratorStrategy()
153 assertNull value.getNullValue()
154 assertEquals LongType, value.getType().getClass()
161 void testManySidePropertyMapping() {
162 PersistentClass faqSection = config.getClassMapping("TestFaqSection")
163 PersistentClass faqElement = config.getClassMapping("TestFaqElement")
164 Property section = faqElement.getProperty("section")
166 assertFalse section.isBackRef()
167 assertFalse section.isComposite()
168 assertFalse section.isInsertable()
169 assertFalse section.isLazy()
170 assertFalse section.isNaturalIdentifier()
171 assertTrue section.isOptimisticLocked()
172 assertFalse section.isOptional()
173 assertTrue section.isSelectable()
174 assertFalse section.isUpdateable()
176 assertEquals "none", section.getCascade()
177 assertEquals CascadeStyle.NONE, section.getCascadeStyle()
178 assertEquals 1, section.getColumnSpan()
179 assertEquals PropertyGeneration.NEVER, section.getGeneration()
180 assertEquals "section", section.getName()
183 void testManySideColumnMapping() {
184 PersistentClass faqSection = config.getClassMapping("TestFaqSection")
185 PersistentClass faqElement = config.getClassMapping("TestFaqElement")
186 Property section = faqElement.getProperty("section")
187 Column sectionColumn = section.getColumnIterator().next()
189 assertEquals "section_id", sectionColumn.getCanonicalName()
190 assertNull sectionColumn.getCheckConstraint()
191 assertNull sectionColumn.getComment()
192 assertNull sectionColumn.getDefaultValue()
193 assertEquals 255, sectionColumn.getLength()
194 assertEquals "section_id", sectionColumn.getName()
195 assertEquals 19, sectionColumn.getPrecision()
196 assertEquals "section_id", sectionColumn.getQuotedName()
197 assertEquals 2, sectionColumn.getScale()
198 assertEquals "section_id", sectionColumn.getText()
199 assertEquals 0, sectionColumn.getTypeIndex()
203 void testManyToOneMapping() {
204 PersistentClass faqSection = config.getClassMapping("TestFaqSection")
205 PersistentClass faqElement = config.getClassMapping("TestFaqElement")
206 Property section = faqElement.getProperty("section")
208 ManyToOne manyToOne = section.getValue()
209 assertEquals 1,manyToOne.getColumnInsertability().size()
210 assertTrue manyToOne.getColumnInsertability()[0]
211 assertEquals 1,manyToOne.getColumnUpdateability().size()
212 assertTrue manyToOne.getColumnUpdateability()[0]
215 assertFalse manyToOne.isAlternateUniqueKey()
216 assertFalse manyToOne.isCascadeDeleteEnabled()
217 assertFalse manyToOne.isEmbedded()
218 assertFalse manyToOne.isIgnoreNotFound()
219 assertFalse manyToOne.isLazy()
220 assertFalse manyToOne.isNullable()
221 assertTrue manyToOne.isSimpleValue()
222 assertTrue manyToOne.isTypeSpecified()
223 assertFalse manyToOne.isUnwrapProxy()
224 assertTrue manyToOne.isUpdateable()
227 assertEquals 1, manyToOne.getConstraintColumns().size()
228 assertEquals FetchMode.DEFAULT, manyToOne.getFetchMode()
229 assertNull manyToOne.getForeignKeyName()
230 assertEquals "assigned", manyToOne.getIdentifierGeneratorStrategy()
231 assertNull manyToOne.getNullValue()
232 assertEquals "TestFaqSection", manyToOne.getReferencedEntityName()
233 assertNull manyToOne.getReferencedPropertyName()
234 assertEquals ManyToOneType, manyToOne.getType().getClass()
235 assertEquals "TestFaqSection", manyToOne.getTypeName()
241 void testListMapping() {
244 List list = config.getCollectionMapping("TestFaqSection.elements")
246 assertFalse list.isAlternateUniqueKey()
247 assertFalse list.isArray()
248 assertFalse list.isCustomDeleteAllCallable()
249 assertFalse list.isCustomDeleteCallable()
250 assertFalse list.isCustomInsertCallable()
251 assertFalse list.isCustomUpdateCallable()
252 assertTrue list.isEmbedded()
253 assertFalse list.isExtraLazy()
254 assertFalse list.isIdentified()
255 assertTrue list.isIndexed()
256 assertFalse list.isInverse()
257 assertTrue list.isLazy()
258 assertTrue list.isList()
259 assertFalse list.isMap()
260 assertTrue list.isMutable()
261 assertTrue list.isNullable()
262 assertTrue list.isOneToMany()
263 assertTrue list.isOptimisticLocked()
264 assertFalse list.isPrimitiveArray()
265 assertFalse list.isSet()
266 assertFalse list.isSorted()
267 assertFalse list.isSubselectLoadable()
270 assertEquals 0,list.getBaseIndex()
271 // assertEquals FaqElement,list.getCollectionPersisterClass()
272 Table t = list.getCollectionTable()
273 assert t
274 assertEquals 0, list.getColumnInsertability().size()
275 assertNull list.getCacheConcurrencyStrategy()
276 assertEquals "TestFaqSection.elements", list.getCacheRegionName()
277 assertEquals 0,list.getColumnSpan()
278 assertEquals 0, list.getColumnUpdateability().size()
279 assertNull list.getElementNodeName()
280 SimpleValue index = list.getIndex()
283 assertEquals 1,index.getColumnInsertability().size()
284 assertTrue index.getColumnInsertability()[0]
285 assertEquals 1,index.getColumnUpdateability().size()
286 assertTrue index.getColumnUpdateability()[0]
288 assertEquals 1, index.getColumnSpan()
290 Column indexColumn = index.getColumnIterator().next()
291 assertEquals "elements_idx", indexColumn.getCanonicalName()
292 assertNull indexColumn.getCheckConstraint()
293 assertNull indexColumn.getComment()
294 assertNull indexColumn.getDefaultValue()
295 assertEquals 255, indexColumn.getLength()
296 assertEquals "elements_idx", indexColumn.getName()
297 assertEquals 19, indexColumn.getPrecision()
298 assertEquals "elements_idx", indexColumn.getQuotedName()
299 assertEquals 2, indexColumn.getScale()
300 assertEquals "elements_idx", indexColumn.getText()
301 SimpleValue indexColumnValue = indexColumn.getValue()
303 assertEquals FetchMode.SELECT, index.getFetchMode()
304 assertNull index.getForeignKeyName()
305 assertEquals "assigned", index.getIdentifierGeneratorStrategy()
306 assertNull index.getNullValue()
307 assertEquals IntegerType, index.getType()?.getClass()
308 assertEquals "integer", index.getTypeName()
309 assertNull index.getTypeParameters()
313 KeyValue key = list.getKey()
315 assertEquals 1,key.getColumnInsertability().size()
316 assertTrue key.getColumnInsertability()[0]
317 assertEquals 1,key.getColumnUpdateability().size()
318 assertTrue key.getColumnUpdateability()[0]
320 assertEquals 1, key.getColumnSpan()
321 assertEquals FetchMode.SELECT, key.getFetchMode()
322 assertNull key.getNullValue()
323 assertEquals LongType, key.getType().getClass()
326 OneToMany element = list.getElement()
328 assertEquals 1, element.getColumnSpan()
329 assertEquals FetchMode.JOIN, element.getFetchMode()
330 PersistentClass associatedClass = element.getAssociatedClass()
331 assertEquals "TestFaqElement", associatedClass.getClassName()
332 assertEquals ManyToOneType, element.getType().getClass()