1 package org.hibernate.ce.auction.test; 2 3 import org.hibernate.ce.auction.persistence.*; 4 import org.hibernate.ce.auction.model.*; 5 6 import javax.persistence.*; 7 import java.util.*; 8 import java.math.BigDecimal ; 9 10 17 public class Ejb3Test extends junit.framework.TestCase { 18 19 Category cars; 21 Category carsLuxury; 22 Category carsSUV; 23 24 User u1; 25 User u2; 26 User u3; 27 28 Item auctionOne; 29 Item auctionTwo; 30 Item auctionThree; 31 Item auctionFour; 32 33 34 protected void runTest() throws Throwable { 35 try { 36 System.out.println("Running test..."); 37 super.runTest(); 38 } catch (Throwable e) { 39 Ejb3Util.rollbackTransaction(); 40 throw e; 41 } finally{ 42 Ejb3Util.closeEM(); 43 } 44 } 45 46 47 protected void initData() { 48 49 EntityManager em = Ejb3Util.getCurrentEM(); 50 Ejb3Util.beginTransaction(); 51 52 cars = new Category("Cars"); 54 carsLuxury = new Category("Luxury Cars"); 55 cars.addChildCategory(carsLuxury); 56 carsSUV = new Category("SUVs"); 57 cars.addChildCategory(carsSUV); 58 em.persist(cars); 59 60 u1 = new User("Christian", "Bauer", "turin", "abc123", "christian@hibernate.org"); 62 u1.setAddress(new Address("Foo", "12345", "Bar")); 63 u1.setAdmin(true); 64 u2= new User("Gavin", "King", "gavin", "abc123", "gavin@hibernate.org"); 65 u2.setAddress(new Address("Foo", "12345", "Bar")); 66 u3= new User("Max", "Andersen", "max", "abc123", "max@hibernate.org"); 67 u3.setAddress(new Address("Foo", "12345", "Bar")); 68 em.persist(u1); 69 em.persist(u2); 70 em.persist(u3); 71 72 BillingDetails ccOne = new CreditCard("Christian Bauer", u1, "1234567890", 74 CreditCardType.MASTERCARD, "10", "2005"); 75 BillingDetails accOne = new BankAccount("Christian Bauer", u1, "234234234234", 76 "FooBar Rich Bank", "foobar123foobaz"); 77 u1.addBillingDetails(ccOne); 78 u1.addBillingDetails(accOne); 79 80 Calendar inThreeDays = GregorianCalendar.getInstance(); 82 inThreeDays.roll(Calendar.DAY_OF_YEAR, 3); 83 Calendar inFiveDays = GregorianCalendar.getInstance(); 84 inFiveDays.roll(Calendar.DAY_OF_YEAR, 5); 85 Calendar nextWeek = GregorianCalendar.getInstance(); 86 nextWeek.roll(Calendar.WEEK_OF_YEAR, true); 87 88 auctionOne = new Item("Item One", "An item in the carsLuxury category.", 89 u2, 90 new MonetaryAmount(new BigDecimal ("1.99"), Currency.getInstance(Locale.US)), 91 new MonetaryAmount(new BigDecimal ("50.33"), Currency.getInstance(Locale.US)), 92 new Date(), inThreeDays.getTime()); 93 auctionOne.getImages().add("imagefiledupe1.jpg"); 94 auctionOne.getImages().add("imagefiledupe1.jpg"); 95 auctionOne.getImages().add("imagefile2.jpg"); 96 auctionOne.setPendingForApproval(); 97 auctionOne.approve(u1); 98 em.persist(auctionOne); 99 new CategorizedItem(u1.getUsername(), carsLuxury, auctionOne); 100 101 auctionTwo = new Item("Item Two", "Another item in the carsLuxury category.", 102 u2, 103 new MonetaryAmount(new BigDecimal ("2.22"), Currency.getInstance(Locale.US)), 104 new MonetaryAmount(new BigDecimal ("100.88"), Currency.getInstance(Locale.US)), 105 new Date(), inFiveDays.getTime()); 106 em.persist(auctionTwo); 107 new CategorizedItem(u1.getUsername(), carsLuxury, auctionTwo); 108 109 auctionThree = new Item("Item Three", "Don't drive SUVs.", 110 u2, 111 new MonetaryAmount(new BigDecimal ("3.11"), Currency.getInstance(Locale.US)), 112 new MonetaryAmount(new BigDecimal ("300.55"), Currency.getInstance(Locale.US)), 113 new Date(), inThreeDays.getTime()); 114 em.persist(auctionThree); 115 new CategorizedItem(u1.getUsername(), carsSUV, auctionThree); 116 117 auctionFour = new Item("Item Four", "Really, not even luxury SUVs.", 118 u1, 119 new MonetaryAmount(new BigDecimal ("4.55"), Currency.getInstance(Locale.US)), 120 new MonetaryAmount(new BigDecimal ("40.99"), Currency.getInstance(Locale.US)), 121 new Date(), nextWeek.getTime()); 122 em.persist(auctionFour); 123 new CategorizedItem(u1.getUsername(), carsLuxury, auctionFour); 124 new CategorizedItem(u1.getUsername(), carsSUV, auctionFour); 125 126 Bid bidOne1 = new Bid(new MonetaryAmount(new BigDecimal ("12.12"), Currency.getInstance(Locale.US)), 128 auctionOne, u3); 129 org.hibernate.ce.auction.model.Bid bidOne2 = new Bid(new MonetaryAmount(new BigDecimal ("13.13"), Currency.getInstance(Locale.US)), 130 auctionOne, u1); 131 Bid bidOne3 = new Bid(new MonetaryAmount(new BigDecimal ("14.14"), Currency.getInstance(Locale.US)), 132 auctionOne, u3); 133 134 auctionOne.addBid(bidOne1); 135 auctionOne.addBid(bidOne2); 136 auctionOne.addBid(bidOne3); 137 138 auctionOne.setSuccessfulBid(bidOne3); 140 141 Comment commentOne = new Comment(Rating.EXCELLENT, "This is Excellent.", u3, auctionOne); 143 Comment commentTwo = new Comment(Rating.LOW, "This is very Low.", u1, auctionThree); 144 em.persist(commentOne); 145 em.persist(commentTwo); 146 147 Ejb3Util.commitTransaction(); 148 Ejb3Util.closeEM(); 149 150 } 151 152 public void testEjbQl() { 153 154 initData(); 155 156 EntityManager em = Ejb3Util.getCurrentEM(); 157 Ejb3Util.beginTransaction(); 158 159 Query q = em.createQuery("select i.description from Item i where i.initialPrice.value > :amount"); 160 q.setParameter("amount", new BigDecimal ("2.50")); 161 List result = q.getResultList(); 162 assertTrue(result.size() == 2); 163 164 Ejb3Util.commitTransaction(); 165 Ejb3Util.closeEM(); 166 } 167 168 169 } 170 | Popular Tags |