Search This Blog

Monday 16 July 2012

Testing the Interceptor

In the previous post we created a simple Interceptor that logged different operations to the console when they occurred. For testing I created an Entity class that has a one-to-one relation and a collection and decided to test out various operations on it.
public class Entity {
    private String name;
    private Integer id;
    private Master master;//one-to-one
        //one-to-many
    private Set<Child> children = new HashSet<Child>();
        //getter-setters
}
The Master and Child classes are similar simple entities.
public class Master {
    private Long id;
    private String data;
        //getter-setters
}
public class Child {
    private Integer id;
    private Entity parent;
    private Integer key;
        //getter-setters
}
I decided to create an Entity object and observe the logs:
static Integer NEW_ID = 0;
static Integer NEW_CHILD_ID = 0;
final static CustomInterceptor customInterceptor = new CustomInterceptor();

public static void testCreate() {
    final Session session = sessionFactory.openSession(customInterceptor);
    Transaction transaction = session.beginTransaction();
    Master master = (Master) session.load(Master.class, 1L);
    Entity entity = new Entity();
    entity.setName("newOne");
    entity.setMaster(master);
    Child child = new Child();
    child.setKey(1001);
    child.setParent(entity);
    entity.getChildren().add(child);
    NEW_ID = (Integer) session.save(entity);
    transaction.commit();
    session.close();
}
The logs indicate the successful creation
[main] INFO  com.interceptor.TestIntercept  - main: calling testCreate
172  [main] INFO  com.interceptor.CustomInterceptor  - afterTransactionBegin: Ca
lled for transaction org.hibernate.transaction.JDBCTransaction@19836ed
219  [main] INFO  com.interceptor.CustomInterceptor  - getEntityName: name for e
ntity com.interceptor.Entity@145c859
219  [main] INFO  com.interceptor.CustomInterceptor  - onSave: Saving object com
.interceptor.Entity@145c859 with id null
219  [main] INFO  com.interceptor.CustomInterceptor  - onSave: Object Details ar
e as below: 
1703 [main] INFO  com.interceptor.CustomInterceptor  - onSave: propertyName : na
me ,type :  org.hibernate.type.StringType@785d65 , state : newOne
1703 [main] INFO  com.interceptor.CustomInterceptor  - getEntity: Returns fully 
loaded cached entity with name  com.interceptor.Master and id 1
1703 [main] INFO  com.interceptor.CustomInterceptor  - beforeTransactionCompleti
on: Called for statement select master0_.ID as ID1_0_, master0_.DATA as DATA1_0_
 from INTERCEPT_ENTITY_MASTER master0_ where master0_.ID=?
Hibernate: 
    select
        master0_.ID as ID1_0_,
        master0_.DATA as DATA1_0_ 
    from
        INTERCEPT_ENTITY_MASTER master0_ 
    where
        master0_.ID=?
1781 [main] INFO  com.interceptor.CustomInterceptor  - getEntity: Returns fully 
loaded cached entity with name  com.interceptor.Master and id 1
1781 [main] INFO  com.interceptor.CustomInterceptor  - instantiate: Instantiatin
g object com.interceptor.Master with id - 1 in mode pojo
1797 [main] INFO  com.interceptor.CustomInterceptor  - onLoad: Attempting to loa
d an object com.interceptor.Master@1b15692 with id 1
1797 [main] INFO  com.interceptor.CustomInterceptor  - onLoad: Object Details ar
e as below: 
1797 [main] INFO  com.interceptor.CustomInterceptor  - onLoad: propertyName : da
ta ,type :  org.hibernate.type.StringType@785d65 ,state : MASTER1
1797 [main] INFO  com.interceptor.CustomInterceptor  - onSave: propertyName : ma
ster ,type :  org.hibernate.type.ManyToOneType(com.interceptor.Master) , state :
 com.interceptor.Master@1b15692
1797 [main] INFO  com.interceptor.CustomInterceptor  - onSave: propertyName : ch
ildren ,type :  org.hibernate.type.SetType(com.interceptor.Entity.children) , st
ate : [com.interceptor.Child@d9660d]
1875 [main] INFO  com.interceptor.CustomInterceptor  - beforeTransactionCompleti
on: Called for statement insert into INTERCEPT_ENTITY (NAME, MASTER_ID) values (
?, ?)
Hibernate: 
    insert 
    into
        INTERCEPT_ENTITY
        (NAME, MASTER_ID) 
    values
        (?, ?)
1891 [main] INFO  com.interceptor.CustomInterceptor  - isTransient: Checking obj
ect for Transient state... com.interceptor.Child@d9660d
1891 [main] INFO  com.interceptor.CustomInterceptor  - onSave: Saving object com
.interceptor.Child@d9660d with id null
1891 [main] INFO  com.interceptor.CustomInterceptor  - onSave: Object Details ar
e as below: 
1891 [main] INFO  com.interceptor.CustomInterceptor  - onSave: propertyName : ke
y ,type :  org.hibernate.type.IntegerType@c9d92c , state : 1001
1906 [main] INFO  com.interceptor.CustomInterceptor  - onSave: propertyName : pa
rent ,type :  org.hibernate.type.ManyToOneType(com.interceptor.Entity) , state :
 com.interceptor.Entity@145c859
1906 [main] INFO  com.interceptor.CustomInterceptor  - beforeTransactionCompleti
on: Called for statement insert into CHILD_ENTITY (`KEY`, ENTITY_ID) values (?, 
?)
Hibernate: 
    insert 
    into
        CHILD_ENTITY
        (`KEY`, ENTITY_ID) 
    values
        (?, ?)
1922 [main] INFO  com.interceptor.CustomInterceptor  - preFlush: List of objects
 to flush... 
1922 [main] INFO  com.interceptor.CustomInterceptor  - preFlush: 1 : com.interce
ptor.Entity@145c859
1922 [main] INFO  com.interceptor.CustomInterceptor  - preFlush: 2 : com.interce
ptor.Master@1b15692
1922 [main] INFO  com.interceptor.CustomInterceptor  - preFlush: 3 : com.interce
ptor.Child@d9660d
1922 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: Detects if obj
ect is dirty com.interceptor.Master@1b15692 with id 1
1922 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: Object Details
 are as below: 
1922 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: propertyName :
 data ,type :  org.hibernate.type.StringType@785d65 , previous state : MASTER1 ,
 current state : MASTER1
1922 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: Detects if obj
ect is dirty com.interceptor.Entity@145c859 with id 3
1922 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: Object Details
 are as below: 
1922 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: propertyName :
 name ,type :  org.hibernate.type.StringType@785d65 , previous state : newOne , 
current state : newOne
1922 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: propertyName :
 master ,type :  org.hibernate.type.ManyToOneType(com.interceptor.Master) , prev
ious state : com.interceptor.Master@1b15692 , current state : com.interceptor.Ma
ster@1b15692
1922 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: propertyName :
 children ,type :  org.hibernate.type.SetType(com.interceptor.Entity.children) ,
 previous state : [com.interceptor.Child@d9660d] , current state : [com.intercep
tor.Child@d9660d]
1969 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: Detects if obj
ect is dirty com.interceptor.Child@d9660d with id 2
1969 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: Object Details
 are as below: 
1969 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: propertyName :
 key ,type :  org.hibernate.type.IntegerType@c9d92c , previous state : 1001 , cu
rrent state : 1001
1969 [main] INFO  com.interceptor.CustomInterceptor  - findDirty: propertyName :
 parent ,type :  org.hibernate.type.ManyToOneType(com.interceptor.Entity) , prev
ious state : com.interceptor.Entity@145c859 , current state : com.interceptor.En
tity@145c859
1969 [main] INFO  com.interceptor.CustomInterceptor  - onCollectionRecreate: Recre
ated collection [com.interceptor.Child@d9660d] for key 3
1969 [main] INFO  com.interceptor.CustomInterceptor  - postFlush: List of object
s that have been flushed... 
1969 [main] INFO  com.interceptor.CustomInterceptor  - postFlush: 1 : com.interc
eptor.Entity@145c859
1969 [main] INFO  com.interceptor.CustomInterceptor  - postFlush: 2 : com.interc
eptor.Master@1b15692
1969 [main] INFO  com.interceptor.CustomInterceptor  - postFlush: 3 : com.interc
eptor.Child@d9660d
1969 [main] INFO  com.interceptor.CustomInterceptor  - beforeTransactionCompleti
on: Called for transaction org.hibernate.transaction.JDBCTransaction@19836ed
1969 [main] INFO  com.interceptor.CustomInterceptor  - afterTransactionCompletio
n: Called for transaction org.hibernate.transaction.JDBCTransaction@19836ed
1984 [main] INFO  com.interceptor.TestIntercept  - main: completed calling testC
reate

No comments:

Post a Comment