KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > mediator > TestHibernateMediator


1 package org.bsf.smartValueObject.mediator;
2
3 import junit.framework.TestCase;
4
5 import java.util.*;
6 import java.sql.Connection JavaDoc;
7 import java.sql.DriverManager JavaDoc;
8 import java.sql.Statement JavaDoc;
9
10 import org.bsf.smartValueObject.demo.CompanyVO;
11 import org.bsf.smartValueObject.demo.SubsidiaryVO;
12 import org.bsf.smartValueObject.demo.DemoConfig;
13
14 /**
15  * Testcase Hibernate Mediator
16  *
17  */

18 public class TestHibernateMediator extends TestCase {
19     private Mediator mediator;
20     private Date date;
21     private final String JavaDoc createCompanySql = "CREATE TABLE company(" +
22             "id IDENTITY," +
23             "name VARCHAR(30)," +
24             "creationDate DATE" +
25             ");";
26     private final String JavaDoc createSubsidiarySql = "CREATE TABLE subsidiary(" +
27             "id IDENTITY," +
28             "name VARCHAR(30)," +
29             "workforce INT," +
30             "companyId INT" +
31             ");";
32     private final String JavaDoc destroyCompanySql = "DROP TABLE company IF EXISTS;";
33     private final String JavaDoc destroySubsidiarySql = "DROP TABLE subsidiary IF EXISTS;";
34
35     public void setUp() throws Exception JavaDoc {
36         createTestDb();
37         mediator = new HibernateMediator(new DemoConfig());
38     }
39
40     public void tearDown() throws Exception JavaDoc {
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 JavaDoc) 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 JavaDoc buildUserGraph() {
67         CompanyVO comp = new CompanyVO();
68         comp.setName( "The big big company" );
69
70         // set time to actual date, 0:00:00
71
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 helper methods /////////////
99
private Connection JavaDoc getConnection() throws Exception JavaDoc {
100         String JavaDoc driver = "org.hsqldb.jdbcDriver";
101         Class.forName(driver).newInstance();
102         String JavaDoc url = "jdbc:hsqldb:";
103         String JavaDoc database = "test";
104         String JavaDoc user = "sa";
105         String JavaDoc password = "";
106         Connection JavaDoc conn = DriverManager.getConnection(
107                 url + database,
108                 user,
109                 password);
110         return conn;
111     }
112
113     /**
114      * Creates test database.
115      * @throws Exception
116      */

117     private void createTestDb() throws Exception JavaDoc {
118         Connection JavaDoc conn = getConnection();
119         Statement JavaDoc s = conn.createStatement();
120         s.execute(createCompanySql);
121         s.close();
122         s = conn.createStatement();
123         s.execute(createSubsidiarySql);
124         s.close();
125     }
126
127     /**
128      * Destroys database.
129      * @throws Exception
130      */

131     private void destroyTestDb() throws Exception JavaDoc {
132         Connection JavaDoc conn = getConnection();
133         Statement JavaDoc 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