All pastes #2053745 Raw Edit

Derived Property Tests

public java v1 · immutable
#2053745 ·published 2011-05-04 09:08 UTC
rendered paste body
import org.codehaus.groovy.grails.commons.ApplicationHolder as AH/* Purpose: get info on Hibernate's interaction between derived properties and cache *//* Finds:  The derived property is always searchable, as this involved a database search.  The derived property of an object will always be null if the version that was saved is still in the cache  How one retrieves the object, get, findBy*, findAllBy*, ... does not matter. It will still use the cached version.  To get it to update the cached version one will either need to clear the cache of the current session or  just refresh the object. The latter is probably less expensive :) */class ClassWithDerivedPropertyTests extends GroovyTestCase {  void testIsDerivedPropertySearchable() {    /* a*b == 1 */    new ClassWithDerivedProperty(a: 1, b: 1).save()    new ClassWithDerivedProperty(a: 1, b: 1).save()    new ClassWithDerivedProperty(a: 1, b: 1).save()    assert ClassWithDerivedProperty.findAllByATimesB(1).size() == 3    /* a*b = 10 */    new ClassWithDerivedProperty(a: 2, b: 5).save()    new ClassWithDerivedProperty(a: 2, b: 5).save()    assert ClassWithDerivedProperty.findAllByATimesB(10).size() == 2  }  void testDerivedPropertyIsNullAfterSaveWhenNotFlushing() {    def obj = new ClassWithDerivedProperty(a: 2, b: 5)    obj.save()    /* retrieved from cache, not db - expected to be null */    obj = ClassWithDerivedProperty.get(obj.id)    assertNotNull obj    assertNull obj.aTimesB  }  void testDerivedPropertyIsNotNullAfterSaveWhenFlushingAndClearingSession() {    def obj = new ClassWithDerivedProperty(a: 2, b: 5)    obj.save()    flushAndClear()    /* NOT retrieved from cache, but from the db - expected to be not null */    obj = ClassWithDerivedProperty.get(obj.id)    assertNotNull obj    assertNotNull obj.aTimesB    assert obj.aTimesB == 2 * 5  }  void testDerivedPropertyIsNullWhenUsingFindByAndNotClearingSession() {    assert ClassWithDerivedProperty.findAll().size() == 0    def obj = new ClassWithDerivedProperty(a: 2, b: 5)    obj.save()    /* NOT retrieved from cache, but from the db - expected to be not null */    obj = ClassWithDerivedProperty.findByA(2)    assertNotNull obj//    System.err.println "testDerivedPropertyIsNullWhenUsingFindByAndNotClearingSession obj ${obj}"    assertNull obj.aTimesB  }  void testDerivedPropertyIsNotNullWhenUsingFindByAndClearingSession() {    assert ClassWithDerivedProperty.findAll().size() == 0    def obj = new ClassWithDerivedProperty(a: 2, b: 5)    obj.save()    flushAndClear()    obj = ClassWithDerivedProperty.findByA(2)    assertNotNull obj//    System.err.println "testDerivedPropertyIsNotNullWhenUsingFindByAndClearingSession obj ${obj}"    assertNotNull obj.aTimesB    assert obj.aTimesB == 2 * 5  }  void testDerivedPropertyIsNullEvenIfFlushingSave() {    def obj = new ClassWithDerivedProperty(a: 2, b: 5)    obj.save()    flush()    obj = ClassWithDerivedProperty.findByA(2)    assertNotNull obj    assertNull obj.aTimesB  }  void testDerivedPropertyIsNotNullIfClearingTheSession() {    def obj = new ClassWithDerivedProperty(a: 2, b: 5)    obj.save()    clear()    obj = ClassWithDerivedProperty.findByA(2)    assertNotNull obj    assertNotNull obj.aTimesB  }  void testDerivedPropertyIsNotNullIfDoingRefresh() {    def obj = new ClassWithDerivedProperty(a: 2, b: 5)    obj.save()    obj.refresh()    obj = ClassWithDerivedProperty.findByA(2)    assertNotNull obj    assertNotNull obj.aTimesB    assert obj.aTimesB == 2 * 5  }  void testDerivedPropertyOfAReturnedLisIsNotNullIfClearingSession() {    new ClassWithDerivedProperty(a: 1, b: 5).save()    new ClassWithDerivedProperty(a: 2, b: 5).save()    new ClassWithDerivedProperty(a: 3, b: 5).save()    new ClassWithDerivedProperty(a: 4, b: 5).save()    new ClassWithDerivedProperty(a: 5, b: 5).save()    def list = ClassWithDerivedProperty.findAllByB(5)    list.each { assert it.aTimesB == null }    clear()    def list2 = ClassWithDerivedProperty.findAllByB(5)    list2.each {      assert it.aTimesB != null      assert it.aTimesB == (it.a * it.b)    }  }  def flushAndClear() {    flush()    clear()  }  def clear() {    AH.application.mainContext.sessionFactory.currentSession.clear()  }  def flush() {    AH.application.mainContext.sessionFactory.currentSession.flush()  }}/* domain class used - goes in its own file */class ClassWithDerivedProperty {  Integer a  Integer b  Integer aTimesB  static mapping = {    aTimesB formula: 'A*B'  }  String toString() {    return "id: ${id} a:${a} b:${b} aTimesB:${aTimesB} "  }}