KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > test > TestCaseWithData


1 package org.hibernate.ce.auction.test;
2
3 import org.hibernate.ce.auction.model.*;
4 import org.hibernate.ce.auction.persistence.HibernateUtil;
5 import org.hibernate.ce.auction.dao.*;
6
7 import java.math.BigDecimal JavaDoc;
8 import java.util.*;
9 /**
10  * No actual test, but only test data initialization.
11  *
12  * @author Christian Bauer <christian@hibernate.org>
13  */

14 public abstract class TestCaseWithData extends TestCase {
15
16     // Keep references to domain objects
17
Category cars;
18     Category carsLuxury;
19     Category carsSUV;
20
21     User u1;
22     User u2;
23     User u3;
24
25     Item auctionOne;
26     Item auctionTwo;
27     Item auctionThree;
28     Item auctionFour;
29
30     // ********************************************************** //
31

32     /**
33      * Create test data for our domain model.
34      *
35      * @throws Exception
36      */

37     protected void initData() throws Exception JavaDoc {
38
39         // Prepare DAOS
40
CategoryDAO catDAO = new CategoryDAO();
41         UserDAO userDAO = new UserDAO();
42         ItemDAO itemDAO = new ItemDAO();
43         CommentDAO commentDAO = new CommentDAO();
44
45         // Categories
46
cars = new Category("Cars");
47         carsLuxury = new Category("Luxury Cars");
48         cars.addChildCategory(carsLuxury);
49         carsSUV = new Category("SUVs");
50         cars.addChildCategory(carsSUV);
51         catDAO.makePersistent(cars);
52
53         // Users
54
u1 = new User("Christian", "Bauer", "turin", "abc123", "christian@hibernate.org");
55         u1.setAddress(new Address("Foo", "12345", "Bar"));
56         u1.setAdmin(true);
57         u2= new User("Gavin", "King", "gavin", "abc123", "gavin@hibernate.org");
58         u2.setAddress(new Address("Foo", "12345", "Bar"));
59         u3= new User("Max", "Andersen", "max", "abc123", "max@hibernate.org");
60         u3.setAddress(new Address("Foo", "12345", "Bar"));
61         userDAO.makePersistent(u1);
62         userDAO.makePersistent(u2);
63         userDAO.makePersistent(u3);
64
65         // BillingDetails
66
BillingDetails ccOne = new CreditCard("Christian Bauer", u1, "1234567890",
67                                                 CreditCardType.MASTERCARD, "10", "2005");
68         BillingDetails accOne = new BankAccount("Christian Bauer", u1, "234234234234",
69                                                 "FooBar Rich Bank", "foobar123foobaz");
70         u1.addBillingDetails(ccOne);
71         u1.addBillingDetails(accOne);
72
73         // Items
74
Calendar inThreeDays = GregorianCalendar.getInstance();
75         inThreeDays.roll(Calendar.DAY_OF_YEAR, 3);
76         Calendar inFiveDays = GregorianCalendar.getInstance();
77         inFiveDays.roll(Calendar.DAY_OF_YEAR, 5);
78         Calendar nextWeek = GregorianCalendar.getInstance();
79         nextWeek.roll(Calendar.WEEK_OF_YEAR, true);
80
81         auctionOne = new Item("Item One", "An item in the carsLuxury category.",
82                 u2,
83                 new MonetaryAmount(new BigDecimal JavaDoc("1.99"), Currency.getInstance(Locale.US)),
84                 new MonetaryAmount(new BigDecimal JavaDoc("50.33"), Currency.getInstance(Locale.US)),
85                 new Date(), inThreeDays.getTime());
86         auctionOne.getImages().add("imagefiledupe1.jpg");
87         auctionOne.getImages().add("imagefiledupe1.jpg");
88         auctionOne.getImages().add("imagefile2.jpg");
89         auctionOne.setPendingForApproval();
90         auctionOne.approve(u1);
91         itemDAO.makePersistent(auctionOne);
92         new CategorizedItem(u1.getUsername(), carsLuxury, auctionOne);
93
94         auctionTwo = new Item("Item Two", "Another item in the carsLuxury category.",
95                 u2,
96                 new MonetaryAmount(new BigDecimal JavaDoc("2.22"), Currency.getInstance(Locale.US)),
97                 new MonetaryAmount(new BigDecimal JavaDoc("100.88"), Currency.getInstance(Locale.US)),
98                 new Date(), inFiveDays.getTime());
99         itemDAO.makePersistent(auctionTwo);
100         new CategorizedItem(u1.getUsername(), carsLuxury, auctionTwo);
101
102         auctionThree = new Item("Item Three", "Don't drive SUVs.",
103                 u2,
104                 new MonetaryAmount(new BigDecimal JavaDoc("3.11"), Currency.getInstance(Locale.US)),
105                 new MonetaryAmount(new BigDecimal JavaDoc("300.55"), Currency.getInstance(Locale.US)),
106                 new Date(), inThreeDays.getTime());
107         itemDAO.makePersistent(auctionThree);
108         new CategorizedItem(u1.getUsername(), carsSUV, auctionThree);
109
110         auctionFour = new Item("Item Four", "Really, not even luxury SUVs.",
111                 u1,
112                 new MonetaryAmount(new BigDecimal JavaDoc("4.55"), Currency.getInstance(Locale.US)),
113                 new MonetaryAmount(new BigDecimal JavaDoc("40.99"), Currency.getInstance(Locale.US)),
114                 new Date(), nextWeek.getTime());
115         itemDAO.makePersistent(auctionFour);
116         new CategorizedItem(u1.getUsername(), carsLuxury, auctionFour);
117         new CategorizedItem(u1.getUsername(), carsSUV, auctionFour);
118
119         // Bids
120
Bid bidOne1 = new Bid(new MonetaryAmount(new BigDecimal JavaDoc("12.12"), Currency.getInstance(Locale.US)),
121                 auctionOne, u3);
122         org.hibernate.ce.auction.model.Bid bidOne2 = new Bid(new MonetaryAmount(new BigDecimal JavaDoc("13.13"), Currency.getInstance(Locale.US)),
123                 auctionOne, u1);
124         Bid bidOne3 = new Bid(new MonetaryAmount(new BigDecimal JavaDoc("14.14"), Currency.getInstance(Locale.US)),
125                 auctionOne, u3);
126
127         auctionOne.addBid(bidOne1);
128         auctionOne.addBid(bidOne2);
129         auctionOne.addBid(bidOne3);
130
131         // Successful Bid
132
auctionOne.setSuccessfulBid(bidOne3);
133
134         // Comments
135
Comment commentOne = new Comment(Rating.EXCELLENT, "This is Excellent.", u3, auctionOne);
136         Comment commentTwo = new Comment(Rating.LOW, "This is very Low.", u1, auctionThree);
137         commentDAO.makePersistent(commentOne);
138         commentDAO.makePersistent(commentTwo);
139
140         HibernateUtil.commitTransaction();
141         HibernateUtil.closeSession();
142
143     }
144
145     // ********************************************************** //
146

147     public TestCaseWithData(String JavaDoc x) {
148         super(x);
149     }
150
151 }
152
Popular Tags