KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.hibernate.ce.auction.test;
2
3 import junit.framework.*;
4 import junit.textui.TestRunner;
5 import org.hibernate.ce.auction.dao.*;
6 import org.hibernate.ce.auction.exceptions.*;
7 import org.hibernate.ce.auction.model.*;
8 import org.hibernate.ce.auction.persistence.HibernateUtil;
9
10 import java.math.BigDecimal JavaDoc;
11 import java.util.*;
12
13 public class ItemTest extends TestCaseWithData {
14
15     // ********************************************************** //
16

17     public void testItemData() throws Exception JavaDoc {
18         initData();
19
20         ItemDAO itemDAO = new ItemDAO();
21
22         Item a1 = itemDAO.getItemById(auctionOne.getId(), false);
23         assertEquals(a1.getInitialPrice(),
24                      new MonetaryAmount(new BigDecimal JavaDoc("1.99"), Currency.getInstance(Locale.US)));
25
26         HibernateUtil.commitTransaction();
27         HibernateUtil.closeSession();
28     }
29
30     // ********************************************************** //
31

32     public void testPlaceBid() throws Exception JavaDoc {
33         initData();
34
35         // TODO: Test doesn't consider currency properly!
36

37         ItemDAO itemDAO = new ItemDAO();
38         UserDAO userDAO = new UserDAO();
39
40         Bid currentMaxBid = itemDAO.getMaxBid(auctionTwo.getId());
41         Bid currentMinBid = itemDAO.getMinBid(auctionTwo.getId());
42         Item a2 = itemDAO.getItemById(auctionTwo.getId(), true);
43
44         // Fail, auction is not active yet
45
try {
46             BigDecimal JavaDoc bidAmount = new BigDecimal JavaDoc("99.99");
47             MonetaryAmount newAmount = new MonetaryAmount(bidAmount, Currency.getInstance("USD"));
48             a2.placeBid(userDAO.getUserById(u3.getId(), false),
49                                     newAmount,
50                                     currentMaxBid,
51                                     currentMinBid);
52         } catch (BusinessException success) {}
53
54         // Fail, user isn't an admin
55
try {
56             a2.approve(u3);
57         } catch (PermissionException success) {}
58
59         // Success, set active
60
a2.setPendingForApproval();
61         a2.approve(u1);
62
63         // Success, place a bid
64
try {
65             BigDecimal JavaDoc bidAmount = new BigDecimal JavaDoc("100.00");
66             MonetaryAmount newAmount = new MonetaryAmount(bidAmount, Currency.getInstance("USD"));
67             a2.placeBid(userDAO.getUserById(u3.getId(), false),
68                                     newAmount,
69                                     currentMaxBid,
70                                     currentMinBid);
71
72         } catch (BusinessException failure) {
73             throw failure;
74         }
75
76         // Fail, bid amount is too low
77
try {
78             BigDecimal JavaDoc bidAmount = new BigDecimal JavaDoc("99.99");
79             MonetaryAmount newAmount = new MonetaryAmount(bidAmount, Currency.getInstance("USD"));
80             a2.placeBid(userDAO.getUserById(u3.getId(), false),
81                                     newAmount,
82                                     currentMaxBid,
83                                     currentMinBid);
84         } catch (BusinessException success) {}
85
86         // TODO: Implement test for auction dates...
87

88         HibernateUtil.commitTransaction();
89         HibernateUtil.closeSession();
90
91     }
92
93     // ********************************************************** //
94

95     public ItemTest(String JavaDoc x) {
96         super(x);
97     }
98
99     public static Test suite() {
100         return new TestSuite(ItemTest.class);
101     }
102
103     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
104         TestRunner.run( suite() );
105     }
106
107 }
108
Popular Tags