Results 1 to 9 of 9

Thread: Generated tests still failing on latest Roo on simple two tables schema

  1. #1
    Join Date
    Mar 2008
    Posts
    257

    Default Generated tests still failing on latest Roo on simple two tables schema

    Hi,

    I'm still confused as to why the generated tests fail on such a simple schema.

    I published a full description at https://jira.springsource.org/browse/ROO-3144

    Could anyone give a shot at this ?

    I wonder if I shoulc reopen the jira if it's not inappropriate..

    Kind Regards,

    Stephane
    Stephane

  2. #2
    Join Date
    Mar 2008
    Posts
    257

    Default

    My two tables:

    Code:
    create table if not exists user ( id int unsigned not null auto_increment, version int unsigned not null, name varchar(20), primary key (id), unique (id) ) type = INNODB;
    
    create table if not exists teacher
    (
    id int unsigned not null auto_increment,
    version int unsigned not null,
    user_id int unsigned not null,
    index (user_id), foreign key (user_id) references user(id),
    unique (user_id),
    primary key (id), unique (id)
    ) type = INNODB;
    The console output of the database introspect:
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!--WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.-->
    <database name="deprecated">
        <option key="moduleName" value=""/>
        <option key="activeRecord" value="false"/>
        <option key="includeNonPortableAttributes" value="true"/>
        <option key="testAutomatically" value="false"/>
        <table name="teacher">
            <column name="id" primaryKey="true" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <column name="version" primaryKey="false" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <column name="user_id" primaryKey="false" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <foreign-key foreignTable="user" name="teacher_ibfk_1" onDelete="none" onUpdate="none">
                <option key="exported" value="false"/>
                <reference foreign="id" local="user_id"/>
            </foreign-key>
            <unique name="PRIMARY">
                <unique-column name="id"/>
            </unique>
            <unique name="user_id_2">
                <unique-column name="user_id"/>
            </unique>
            <unique name="id">
                <unique-column name="id"/>
            </unique>
            <index name="user_id">
                <index-column name="user_id"/>
            </index>
        </table>
        <table name="user">
            <column name="id" primaryKey="true" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <column name="version" primaryKey="false" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <column name="name" primaryKey="false" required="false" scale="0" size="20" type="12,VARCHAR"/>
            <foreign-key foreignTable="teacher" name="teacher_ibfk_1" onDelete="none" onUpdate="none">
                <option key="exported" value="true"/>
                <reference foreign="user_id" local="id"/>
            </foreign-key>
            <unique name="PRIMARY">
                <unique-column name="id"/>
            </unique>
            <unique name="id">
                <unique-column name="id"/>
            </unique>
        </table>
    </database>
    Stephane

  3. #3
    Join Date
    Mar 2008
    Posts
    257

    Default

    After the reverse engineering done with:

    database reverse engineer --package ~.domain --schema roo_jira --testAutomatically --includeNonPortableAttributes

    the src/main/resources/dbre.xml file contains:
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!--WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.-->
    <database name="deprecated" package="com.learnintouch.jira.domain">
        <option key="moduleName" value=""/>
        <option key="activeRecord" value="true"/>
        <option key="includeNonPortableAttributes" value="true"/>
        <option key="testAutomatically" value="true"/>
        <table name="teacher">
            <column name="id" primaryKey="true" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <column name="version" primaryKey="false" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <column name="user_id" primaryKey="false" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <foreign-key foreignTable="user" name="teacher_ibfk_1" onDelete="none" onUpdate="none">
                <option key="exported" value="false"/>
                <reference foreign="id" local="user_id"/>
            </foreign-key>
            <unique name="PRIMARY">
                <unique-column name="id"/>
            </unique>
            <unique name="user_id_2">
                <unique-column name="user_id"/>
            </unique>
            <unique name="id">
                <unique-column name="id"/>
            </unique>
            <index name="user_id">
                <index-column name="user_id"/>
            </index>
        </table>
        <table name="user">
            <column name="id" primaryKey="true" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <column name="version" primaryKey="false" required="true" scale="0" size="10" type="4,INT UNSIGNED"/>
            <column name="name" primaryKey="false" required="false" scale="0" size="20" type="12,VARCHAR"/>
            <foreign-key foreignTable="teacher" name="teacher_ibfk_1" onDelete="none" onUpdate="none">
                <option key="exported" value="true"/>
                <reference foreign="user_id" local="id"/>
            </foreign-key>
            <unique name="PRIMARY">
                <unique-column name="id"/>
            </unique>
            <unique name="id">
                <unique-column name="id"/>
            </unique>
        </table>
    </database>
    Stephane

  4. #4
    Join Date
    Mar 2008
    Posts
    257

    Default

    The perform test command shows 8 errors.

    2012-08-07 15:32:11,803 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column 'user_id' cannot be null
    2012-08-07 15:32:11,823 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column 'user_id' cannot be null
    2012-08-07 15:32:11,836 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column 'user_id' cannot be null
    2012-08-07 15:32:11,848 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column 'user_id' cannot be null
    2012-08-07 15:32:11,860 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column 'user_id' cannot be null
    2012-08-07 15:32:11,873 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column 'user_id' cannot be null
    2012-08-07 15:32:11,885 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column 'user_id' cannot be null
    2012-08-07 15:32:11,899 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column 'user_id' cannot be null
    Stephane

  5. #5
    Join Date
    Mar 2008
    Posts
    257

    Default

    The full stack trace:


    -------------------------------------------------------------------------------
    Test set: com.learnintouch.jira.domain.TeacherIntegrationTes t
    -------------------------------------------------------------------------------
    Tests run: 9, Failures: 0, Errors: 8, Skipped: 0, Time elapsed: 4.546 sec <<< FAILURE!
    testCountTeachers(com.learnintouch.jira.domain.Tea cherIntegrationTest) Time elapsed: 0.393 sec <<< ERROR!
    org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [com.learnintouch.jira.domain.Teacher]; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [com.learnintouch.jira.domain.Teacher]
    at org.springframework.orm.jpa.EntityManagerFactoryUt ils.convertJpaAccessExceptionIfPossible(EntityMana gerFactoryUtils.java:326)
    at org.springframework.orm.jpa.aspectj.JpaExceptionTr anslatorAspect.ajc$afterThrowing$org_springframewo rk_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$ 18a1ac9(JpaExceptionTranslatorAspect.aj:15)
    at com.learnintouch.jira.domain.Teacher_Roo_Jpa_Activ eRecord.ajc$interMethod$com_learnintouch_jira_doma in_Teacher_Roo_Jpa_ActiveRecord$com_learnintouch_j ira_domain_Teacher$persist(Teacher_Roo_Jpa_ActiveR ecord.aj:43)
    at com.learnintouch.jira.domain.Teacher.persist(Teach er.java:1)
    at com.learnintouch.jira.domain.Teacher_Roo_Jpa_Activ eRecord.ajc$interMethodDispatch1$com_learnintouch_ jira_domain_Teacher_Roo_Jpa_ActiveRecord$com_learn intouch_jira_domain_Teacher$persist(Teacher_Roo_Jp a_ActiveRecord.aj)
    at com.learnintouch.jira.domain.TeacherDataOnDemand_R oo_DataOnDemand.ajc$interMethod$com_learnintouch_j ira_domain_TeacherDataOnDemand_Roo_DataOnDemand$co m_learnintouch_jira_domain_TeacherDataOnDemand$ini t(TeacherDataOnDemand_Roo_DataOnDemand.aj:74)
    at com.learnintouch.jira.domain.TeacherDataOnDemand.i nit(TeacherDataOnDemand.java:1)
    at com.learnintouch.jira.domain.TeacherDataOnDemand_R oo_DataOnDemand.ajc$interMethodDispatch1$com_learn intouch_jira_domain_TeacherDataOnDemand_Roo_DataOn Demand$com_learnintouch_jira_domain_TeacherDataOnD emand$init(TeacherDataOnDemand_Roo_DataOnDemand.aj )
    at com.learnintouch.jira.domain.TeacherDataOnDemand_R oo_DataOnDemand.ajc$interMethod$com_learnintouch_j ira_domain_TeacherDataOnDemand_Roo_DataOnDemand$co m_learnintouch_jira_domain_TeacherDataOnDemand$get RandomTeacher(TeacherDataOnDemand_Roo_DataOnDemand .aj:49)
    at com.learnintouch.jira.domain.TeacherDataOnDemand.g etRandomTeacher(TeacherDataOnDemand.java:1)
    at com.learnintouch.jira.domain.TeacherDataOnDemand_R oo_DataOnDemand.ajc$interMethodDispatch1$com_learn intouch_jira_domain_TeacherDataOnDemand_Roo_DataOn Demand$com_learnintouch_jira_domain_TeacherDataOnD emand$getRandomTeacher(TeacherDataOnDemand_Roo_Dat aOnDemand.aj)
    at com.learnintouch.jira.domain.TeacherIntegrationTes t_Roo_IntegrationTest.ajc$interMethod$com_learnint ouch_jira_domain_TeacherIntegrationTest_Roo_Integr ationTest$com_learnintouch_jira_domain_TeacherInte grationTest$testCountTeachers(TeacherIntegrationTe st_Roo_IntegrationTest.aj:31)
    at com.learnintouch.jira.domain.TeacherIntegrationTes t.testCountTeachers(TeacherIntegrationTest.java:1)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod .evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements .RunBeforeTestMethodCallbacks.evaluate(RunBeforeTe stMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements .RunAfterTestMethodCallbacks.evaluate(RunAfterTest MethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements .SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.runChild(SpringJUnit4ClassRunner.jav a:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner. java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:222)
    at org.springframework.test.context.junit4.statements .RunBeforeTestClassCallbacks.evaluate(RunBeforeTes tClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements .RunAfterTestClassCallbacks.evaluate(RunAfterTestC lassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.ja va:300)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.run(SpringJUnit4ClassRunner.java:174 )
    at org.apache.maven.surefire.junit4.JUnit4Provider.ex ecute(JUnit4Provider.java:236)
    at org.apache.maven.surefire.junit4.JUnit4Provider.ex ecuteTestSet(JUnit4Provider.java:134)
    at org.apache.maven.surefire.junit4.JUnit4Provider.in voke(JUnit4Provider.java:113)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.inv okeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$P roviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.i nvokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runS uitesInProcess(ForkedBooter.java:103)
    at org.apache.maven.surefire.booter.ForkedBooter.main (ForkedBooter.java:74)
    Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [com.learnintouch.jira.domain.Teacher]
    at org.hibernate.ejb.AbstractEntityManagerImpl.conver t(AbstractEntityManagerImpl.java:1387)
    at org.hibernate.ejb.AbstractEntityManagerImpl.conver t(AbstractEntityManagerImpl.java:1315)
    at org.hibernate.ejb.AbstractEntityManagerImpl.conver t(AbstractEntityManagerImpl.java:1321)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persis t(AbstractEntityManagerImpl.java:843)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.SharedEntityManagerCre ator$SharedEntityManagerInvocationHandler.invoke(S haredEntityManagerCreator.java:240)
    at $Proxy32.persist(Unknown Source)
    ... 45 more
    Caused by: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [com.learnintouch.jira.domain.Teacher]
    at org.hibernate.exception.SQLStateConverter.convert( SQLStateConverter.java:96)
    at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:66)
    at org.hibernate.id.insert.AbstractReturningDelegate. performInsert(AbstractReturningDelegate.java:64)
    at org.hibernate.persister.entity.AbstractEntityPersi ster.insert(AbstractEntityPersister.java:2345)
    at org.hibernate.persister.entity.AbstractEntityPersi ster.insert(AbstractEntityPersister.java:2852)
    at org.hibernate.action.EntityIdentityInsertAction.ex ecute(EntityIdentityInsertAction.java:71)
    at org.hibernate.engine.ActionQueue.execute(ActionQue ue.java:273)
    at org.hibernate.event.def.AbstractSaveEventListener. performSaveOrReplicate(AbstractSaveEventListener.j ava:320)
    at org.hibernate.event.def.AbstractSaveEventListener. performSave(AbstractSaveEventListener.java:203)
    at org.hibernate.event.def.AbstractSaveEventListener. saveWithGeneratedId(AbstractSaveEventListener.java :129)
    at org.hibernate.ejb.event.EJB3PersistEventListener.s aveWithGeneratedId(EJB3PersistEventListener.java:6 9)
    at org.hibernate.event.def.DefaultPersistEventListene r.entityIsTransient(DefaultPersistEventListener.ja va:179)
    at org.hibernate.event.def.DefaultPersistEventListene r.onPersist(DefaultPersistEventListener.java:135)
    at org.hibernate.event.def.DefaultPersistEventListene r.onPersist(DefaultPersistEventListener.java:61)
    at org.hibernate.impl.SessionImpl.firePersist(Session Impl.java:808)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl .java:782)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl .java:786)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persis t(AbstractEntityManagerImpl.java:837)
    ... 51 more
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityCons traintViolationException: Column 'user_id' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    Stephane

  6. #6
    Join Date
    Mar 2008
    Posts
    257

    Default

    The src/test/java/com/learnintouch/jira/domain/TeacherDataOnDemand_Roo_DataOnDemand.aj file generating the test data:

    Code:
    // WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
    // You may push code into the target .java compilation unit if you wish to edit any member(s).
    
    package com.learnintouch.jira.domain;
    
    import com.learnintouch.jira.domain.Teacher;
    import com.learnintouch.jira.domain.TeacherDataOnDemand;
    import com.learnintouch.jira.domain.UserDataOnDemand;
    import java.security.SecureRandom;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Random;
    import javax.validation.ConstraintViolation;
    import javax.validation.ConstraintViolationException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    privileged aspect TeacherDataOnDemand_Roo_DataOnDemand {
        
        declare @type: TeacherDataOnDemand: @Component;
        
        private Random TeacherDataOnDemand.rnd = new SecureRandom();
        
        private List<Teacher> TeacherDataOnDemand.data;
        
        @Autowired
        private UserDataOnDemand TeacherDataOnDemand.userDataOnDemand;
        
        public Teacher TeacherDataOnDemand.getNewTransientTeacher(int index) {
            Teacher obj = new Teacher();
            return obj;
        }
        
        public Teacher TeacherDataOnDemand.getSpecificTeacher(int index) {
            init();
            if (index < 0) {
                index = 0;
            }
            if (index > (data.size() - 1)) {
                index = data.size() - 1;
            }
            Teacher obj = data.get(index);
            Integer id = obj.getId();
            return Teacher.findTeacher(id);
        }
        
        public Teacher TeacherDataOnDemand.getRandomTeacher() {
            init();
            Teacher obj = data.get(rnd.nextInt(data.size()));
            Integer id = obj.getId();
            return Teacher.findTeacher(id);
        }
        
        public boolean TeacherDataOnDemand.modifyTeacher(Teacher obj) {
            return false;
        }
        
        public void TeacherDataOnDemand.init() {
            int from = 0;
            int to = 10;
            data = Teacher.findTeacherEntries(from, to);
            if (data == null) {
                throw new IllegalStateException("Find entries implementation for 'Teacher' illegally returned null");
            }
            if (!data.isEmpty()) {
                return;
            }
            
            data = new ArrayList<Teacher>();
            for (int i = 0; i < 10; i++) {
                Teacher obj = getNewTransientTeacher(i);
                try {
                    obj.persist();
                } catch (ConstraintViolationException e) {
                    StringBuilder msg = new StringBuilder();
                    for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                        ConstraintViolation<?> cv = iter.next();
                        msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                    }
                    throw new RuntimeException(msg.toString(), e);
                }
                obj.flush();
                data.add(obj);
            }
        }
        
    }
    This above Roo generated code looks like constraint violations are not handled by Roo when it comes to generating the test data.

    The get transient teacher is totally ignoring the constraint.
    Last edited by stephaneeybert; Aug 7th, 2012 at 03:22 PM.
    Stephane

  7. #7
    Join Date
    Jan 2010
    Location
    Mislata - Valencia - Spain
    Posts
    162

    Default

    Test not handle relations between entities. Your error is a database constraint. You need push-in your test code and modify it to manage this case.
    Mario Martínez Sánchez
    Project Manager & Software Architect
    --------------------------
    Disid Technologies S.L.
    http://www.disid.com
    --------------------------
    gvNIX
    http://gvnix.googlecode.com
    http://www.gvnix.org

  8. #8
    Join Date
    Mar 2008
    Posts
    257

    Default

    Hi,

    Thanks for the comment. I understand, from what you say, that Roo generated tests do not handle relationships between entities.

    It is too bad for those of us who wish to use Roo on an existing database schema.

    For example, my database schema has some 117 tables with quite many relationships between these.

    It would be a lot of work to hand code the relationships into all these tests Roo generates.

    Is it something that others have done ? Is there some blog article on how to go about it ?

    Thanks !
    Stephane

  9. #9
    Join Date
    Mar 2008
    Posts
    257

    Default

    Here are the tables:

    Code:
    CREATE TABLE `teacher` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `version` int(10) unsigned NOT NULL,
      `user_id` int(10) unsigned NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `user_id_2` (`user_id`),
      UNIQUE KEY `id` (`id`),
      KEY `user_id` (`user_id`),
      CONSTRAINT `teacher_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    Code:
    CREATE TABLE `user` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `version` int(10) unsigned NOT NULL,
      `name` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `id` (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    Stephane

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •