KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > validator > test > HibernateAnnotationIntegrationTest


1 //$Id: HibernateAnnotationIntegrationTest.java,v 1.1 2005/05/27 08:58:54 epbernard Exp $
2
package org.hibernate.validator.test;
3
4 import org.hibernate.mapping.Column;
5 import org.hibernate.mapping.PersistentClass;
6 import org.hibernate.test.annotations.TestCase;
7 import org.hibernate.cfg.Configuration;
8 import org.hibernate.validator.event.ValidatePreInsertEventListener;
9 import org.hibernate.validator.event.ValidatePreUpdateEventListener;
10 import org.hibernate.validator.InvalidStateException;
11 import org.hibernate.Session;
12 import org.hibernate.Transaction;
13
14 /**
15  * Test the validate framework integration with the Hibernate
16  * metadata binding
17  *
18  * @author Emmanuel Bernard
19  */

20 public class HibernateAnnotationIntegrationTest extends TestCase {
21     public void testApply() throws Exception JavaDoc {
22         PersistentClass classMapping = getCfg().getClassMapping( Address.class.getName() );
23         //new ClassValidator( Address.class, ResourceBundle.getBundle("messages", Locale.ENGLISH) ).apply( classMapping );
24
Column stateColumn = (Column) classMapping.getProperty("state").getColumnIterator().next();
25         assertEquals( stateColumn.getLength(), 3 );
26         Column zipColumn = (Column) classMapping.getProperty("zip").getColumnIterator().next();
27         assertEquals( zipColumn.getLength(), 5 );
28         assertFalse( zipColumn.isNullable() );
29     }
30
31     public void testEvents() throws Exception JavaDoc {
32         Session s;
33         Transaction tx;
34         Address a = new Address();
35         a.setId(12);
36         a.setCountry("Country");
37         a.setLine1("Line 1");
38         a.setZip("nonnumeric");
39         a.setState("NY");
40         s = openSession();
41         tx = s.beginTransaction();
42         try {
43             s.persist(a);
44             tx.commit();
45             fail("bean should have been validated");
46         } catch (InvalidStateException e) {
47             //success
48
assertEquals( 2, e.getInvalidValues().length );
49         } finally {
50             if (tx != null) tx.rollback();
51             s.close();
52         }
53         s = openSession();
54         tx = s.beginTransaction();
55         a.setCountry("Country");
56         a.setLine1("Line 1");
57         a.setZip("434343");
58         a.setState("NY");
59         tx.commit();
60         s.close();
61
62
63
64
65     }
66
67     protected void configure(Configuration cfg) {
68         cfg.getSessionEventListenerConfig().setPreInsertEventListener( new ValidatePreInsertEventListener() );
69         cfg.getSessionEventListenerConfig().setPreUpdateEventListener( new ValidatePreUpdateEventListener() );
70     }
71
72     protected Class JavaDoc[] getMappings() {
73         return new Class JavaDoc[] {
74             Address.class
75         };
76     }
77
78     public HibernateAnnotationIntegrationTest(String JavaDoc x) {
79         super(x);
80     }
81 }
82
Popular Tags