Thursday, August 5, 2010

DataOnDemand and Number Fields - Unit tests fail on Short and @Min(1L)

I just opened the following bug (ROO-1176). Though one could look at this and say "what junk", I look at this and marvel at the ease of the work-around. I think this is another positive check for ROO.

This bug covers two separate issues, I have placed them together because they seem to stem from the same code.

FIRST:
Creating an entity with number type fields and applying an @Min constraint greater than zero will fail. It seems that either the getNewTransient_() method or the init() method should take the @Min constraint into account. The init() method sends indexes that are from 0 to 9, but if you have an @Min of 1, the getNewTransient_() will try to populate your field with a zero, which will fail when the object persist() method is called.

SECOND:
Creating an entity with number type fields that are smaller than an int fail when the testPersist() test is called, because it calls getNewTransient__(Integer.MAX_VALUE) which cannot be squeezed into a Short (for example).

WORK AROUND:
Override the getNewTransient__() method and add this to the beginning
if(index == 0){
index = 1;
} else if(index <>
index = index * -1;
}
if(index > Short.MAX_VALUE){
Random rnd = new Random(new Integer(index).longValue());
index = rnd.nextInt(Short.MAX_VALUE);
}

LOG.ROO:
The following is a log.roo for a test.
// Spring Roo 1.1.0.M1 [rev 3a0b8a3] log opened at 2010-08-05 11:46:45
project --topLevelPackage com.things
persistence setup --database HYPERSONIC_IN_MEMORY --provider HIBERNATE
entity --class ~.domain.ShortThings --testAutomatically
field string --fieldName name field number --fieldName thingValue --type java.lang.Short --min 1 --notNull
entity --class ~.domain.LongThings --testAutomatically
field number --fieldName thingValue --type java.lang.Long --min 1 --notNull
perform tests
// Spring Roo 1.1.0.M1 [rev 3a0b8a3] log opened at 2010-08-05 11:54:26


No comments:

Post a Comment