1 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 20 public class HibernateAnnotationIntegrationTest extends TestCase { 21 public void testApply() throws Exception { 22 PersistentClass classMapping = getCfg().getClassMapping( Address.class.getName() ); 23 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 { 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 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 [] getMappings() { 73 return new Class [] { 74 Address.class 75 }; 76 } 77 78 public HibernateAnnotationIntegrationTest(String x) { 79 super(x); 80 } 81 } 82 | Popular Tags |