1 package org.bsf.smartValueObject.mediator; 2 3 import junit.framework.TestCase; 4 5 import java.util.*; 6 import java.sql.Connection ; 7 import java.sql.DriverManager ; 8 import java.sql.Statement ; 9 10 import org.bsf.smartValueObject.demo.CompanyVO; 11 import org.bsf.smartValueObject.demo.SubsidiaryVO; 12 import org.bsf.smartValueObject.demo.DemoConfig; 13 14 18 public class TestHibernateMediator extends TestCase { 19 private Mediator mediator; 20 private Date date; 21 private final String createCompanySql = "CREATE TABLE company(" + 22 "id IDENTITY," + 23 "name VARCHAR(30)," + 24 "creationDate DATE" + 25 ");"; 26 private final String createSubsidiarySql = "CREATE TABLE subsidiary(" + 27 "id IDENTITY," + 28 "name VARCHAR(30)," + 29 "workforce INT," + 30 "companyId INT" + 31 ");"; 32 private final String destroyCompanySql = "DROP TABLE company IF EXISTS;"; 33 private final String destroySubsidiarySql = "DROP TABLE subsidiary IF EXISTS;"; 34 35 public void setUp() throws Exception { 36 createTestDb(); 37 mediator = new HibernateMediator(new DemoConfig()); 38 } 39 40 public void tearDown() throws Exception { 41 destroyTestDb(); 42 } 43 44 public void testUpdate() throws MediatorException { 45 ChangeSummary cs = mediator.updateGraph(buildUserGraph()); 46 CompanyVO company = new CompanyVO(); 47 company.setId((Long ) cs.getKey()); 48 CompanyVO result = (CompanyVO) mediator.getGraph(company); 49 50 assertTrue(result != null); 51 assertEquals("The big big company", result.getName()); 52 53 assertEquals(date, result.getCreationDate()); 54 assertEquals(3, result.getSubsidiaries().size()); 55 56 mediator.deleteGraph(result); 57 58 result = (CompanyVO) mediator.getGraph(company); 59 assertTrue(result == null); 60 } 61 62 public void testConcurrency() throws MediatorException { 63 64 } 65 66 private Object buildUserGraph() { 67 CompanyVO comp = new CompanyVO(); 68 comp.setName( "The big big company" ); 69 70 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "Europe/Paris" ) ); 72 cal.set( Calendar.HOUR_OF_DAY, 0 ); 73 cal.set( Calendar.MINUTE, 0 ); 74 cal.set( Calendar.SECOND, 0 ); 75 cal.set( Calendar.MILLISECOND, 0 ); 76 77 date = cal.getTime(); 78 comp.setCreationDate( date ); 79 80 SubsidiaryVO sub = new SubsidiaryVO(); 81 sub.setName( "Paris" ); 82 sub.setWorkforce( 2 ); 83 comp.addSubsidiary( sub ); 84 85 sub = new SubsidiaryVO(); 86 sub.setName( "London" ); 87 sub.setWorkforce( 1 ); 88 comp.addSubsidiary( sub ); 89 90 sub = new SubsidiaryVO(); 91 sub.setName( "Berlin" ); 92 sub.setWorkforce( 1 ); 93 comp.addSubsidiary( sub ); 94 95 return comp; 96 } 97 98 private Connection getConnection() throws Exception { 100 String driver = "org.hsqldb.jdbcDriver"; 101 Class.forName(driver).newInstance(); 102 String url = "jdbc:hsqldb:"; 103 String database = "test"; 104 String user = "sa"; 105 String password = ""; 106 Connection conn = DriverManager.getConnection( 107 url + database, 108 user, 109 password); 110 return conn; 111 } 112 113 117 private void createTestDb() throws Exception { 118 Connection conn = getConnection(); 119 Statement s = conn.createStatement(); 120 s.execute(createCompanySql); 121 s.close(); 122 s = conn.createStatement(); 123 s.execute(createSubsidiarySql); 124 s.close(); 125 } 126 127 131 private void destroyTestDb() throws Exception { 132 Connection conn = getConnection(); 133 Statement s = conn.createStatement(); 134 s.execute(destroyCompanySql); 135 s.close(); 136 s = conn.createStatement(); 137 s.execute(destroySubsidiarySql); 138 s.close(); 139 } 140 } 141 | Popular Tags |