KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > test > TestPersistency


1 /*
2  * Created on 28-feb-03
3  *
4  * To change this generated comment go to
5  * Window>Preferences>Java>Code Generation>Code Template
6  */

7 package com.genimen.djeneric.test;
8
9 import java.sql.ResultSet JavaDoc;
10 import java.util.Date JavaDoc;
11
12 import junit.framework.Assert;
13 import junit.framework.TestCase;
14
15 import com.genimen.djeneric.repository.DjPersistenceManager;
16 import com.genimen.djeneric.repository.DjPersistenceManagerFactory;
17 import com.genimen.djeneric.repository.rdbms.RdbmsSession;
18 import com.genimen.djeneric.repository.rdbms.SqlStatement;
19 import com.genimen.djeneric.test.strong.Customer;
20 import com.genimen.djeneric.test.strong.Order;
21 import com.genimen.djeneric.test.strong.Orderline;
22 import com.genimen.djeneric.test.strong.Product;
23 import com.genimen.djeneric.test.strong.ProductQbe;
24 import com.genimen.djeneric.test.strong.TestSession;
25 import com.genimen.djeneric.test.strong.impl.TestPersistenceManager;
26 import com.genimen.djeneric.util.DjLogger;
27
28 /**
29  * @author Wido Riezebos
30  */

31 public class TestPersistency extends TestCase
32 {
33
34   /**
35    * Constructor for CreateSession.
36    * @param arg0
37    */

38   public TestPersistency(String JavaDoc arg0)
39   {
40     super(arg0);
41   }
42
43   public void testPersistency()
44   {
45     try
46     {
47       DjPersistenceManagerFactory factory = new DjPersistenceManagerFactory(getClass()
48           .getResourceAsStream("repositories.xml"));
49
50       TestPersistenceManager mgr = new TestPersistenceManager(factory.getRepositoryDescriptor("test"));
51
52       mgr.setCurrentContext("dflt");
53
54       TestSession session = mgr.createTestSession();
55
56       int RECCOUNT = 25;
57
58       for (int i = 0; i < RECCOUNT; i++)
59       {
60         Product prod = session.createProduct();
61         prod.setDescription(("Description of product " + i).getBytes());
62         prod.setName("prod" + i);
63         prod.setProductNumber(i);
64       }
65
66       for (int i = 0; i < RECCOUNT; i++)
67       {
68         Customer customer = session.createCustomer();
69         customer.setName("Customer " + i);
70         customer.setCity("City of customer " + i);
71         customer.setStreet("Street of customer " + i);
72         customer.setZipCode("Zip " + i);
73
74         for (int j = 0; j < RECCOUNT; j++)
75         {
76           Order order = session.createOrder();
77           order.setCustomer(customer);
78           order.setOrderdate(new Date JavaDoc());
79           order.setOrdernumber(i * 1000 + j);
80           for (int p = 0; p < 10; p++)
81           {
82             Orderline ol = session.createOrderline();
83             ol.setLine(p + 1);
84             ol.setQuantity(10 + 2 * p);
85
86             ProductQbe qbe = session.createProductQbe();
87             qbe.setName("prod" + j);
88
89             Product[] theProd = session.getProductSet(qbe);
90             Assert.assertEquals(1, theProd.length);
91
92             ol.setProduct(theProd[0]);
93             order.addToOrderlines(ol);
94           }
95         }
96
97       }
98       session.commit();
99       session.close();
100
101       RdbmsSession sess = (RdbmsSession) mgr.createSession();
102       SqlStatement stmt = sess.getSqlStatement("select count(*) cnt from customers");
103       ResultSet JavaDoc rs = stmt.executeQuery();
104       rs.next();
105       Assert.assertEquals(RECCOUNT, rs.getInt("cnt"));
106       rs.close();
107       stmt.close();
108       sess.close();
109       mgr.close();
110     }
111     catch (Exception JavaDoc e)
112     {
113       DjLogger.log(e);
114       Assert.fail(e.getMessage());
115     }
116   }
117
118   protected void setUp() throws Exception JavaDoc
119   {
120     DjPersistenceManagerFactory factory = new DjPersistenceManagerFactory(getClass()
121         .getResourceAsStream("repositories.xml"));
122     DjPersistenceManager mgr = factory.createInstance("test");
123
124     mgr.setCurrentContext("dflt");
125
126     RdbmsSession session = (RdbmsSession) mgr.createSession();
127     session.getSqlStatement("delete from orderlines").executeUpdate();
128     session.getSqlStatement("delete from products").executeUpdate();
129     session.getSqlStatement("delete from orders").executeUpdate();
130     session.getSqlStatement("delete from customers").executeUpdate();
131     session.commit();
132     session.close();
133     mgr.close();
134   }
135
136   protected void tearDown() throws Exception JavaDoc
137   {
138     setUp();
139     super.tearDown();
140   }
141
142 }
Popular Tags