001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.hibernate.eg;
018:
019: import java.util.ArrayList;
020: import java.util.Date;
021:
022: import org.compass.core.CompassHits;
023: import org.compass.core.CompassSession;
024: import org.compass.core.CompassTransaction;
025: import org.compass.gps.device.hibernate.dep.Hibernate3GpsDevice;
026: import org.hibernate.Session;
027: import org.hibernate.SessionFactory;
028: import org.hibernate.Transaction;
029: import org.hibernate.cfg.Configuration;
030: import org.hibernate.cfg.Environment;
031:
032: /**
033: * @author kimchy
034: */
035: public class Hibernate3GpsDeviceTests extends
036: AbstractHibernateGpsDeviceTests {
037:
038: private SessionFactory sessionFactory;
039:
040: private AuctionItem mainItem;
041:
042: private User mainBidder;
043:
044: private User userToDelete;
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048:
049: Configuration conf = new Configuration()
050: .configure(
051: "/org/compass/gps/device/hibernate/eg/hibernate3.cfg.xml")
052: .setProperty(Environment.HBM2DDL_AUTO, "create");
053: sessionFactory = conf.buildSessionFactory();
054:
055: Hibernate3GpsDevice device = new Hibernate3GpsDevice();
056: device.setSessionFactory(sessionFactory);
057: device.setName("hibernateDevice");
058: compassGps.addGpsDevice(device);
059: compassGps.start();
060:
061: // set up the initial set of data
062: Session s = sessionFactory.openSession();
063: Transaction tx = s.beginTransaction();
064:
065: userToDelete = new User();
066: userToDelete.setUserName("deleteme");
067: userToDelete.setName(new Name("delete", null, "me"));
068: s.save(userToDelete);
069:
070: User seller = new User();
071: seller.setUserName("oldirty");
072: seller.setName(new Name("ol' dirty", null, "bastard"));
073: seller.setEmail("oldirty@hibernate.org");
074: seller.setAuctions(new ArrayList());
075: s.save(seller);
076: User bidder1 = new User();
077: bidder1.setUserName("1E1");
078: bidder1.setName(new Name("oney", new Character('1'), "one"));
079: bidder1.setEmail("oney@hibernate.org");
080: bidder1.setBids(new ArrayList());
081: s.save(bidder1);
082: User bidder2 = new User();
083: bidder2.setUserName("izi");
084: bidder2.setName(new Name("iz", null, "inizi"));
085: bidder2.setEmail("izi@hibernate.org");
086: bidder2.setBids(new ArrayList());
087: s.save(bidder2);
088:
089: for (int i = 0; i < 3; i++) {
090: AuctionItem item = new AuctionItem();
091: item.setDescription("auction item " + i);
092: item.setEnds(new Date());
093: item.setBids(new ArrayList());
094: item.setSeller(seller);
095: item.setCondition(i * 3 + 2);
096: for (int j = 0; j < i; j++) {
097:
098: Bid bid = new Bid();
099: bid.setBidder(bidder1);
100: bid.setAmount(j);
101: bid.setDatetime(new Date());
102: bid.setItem(item);
103: item.getBids().add(bid);
104: bidder1.getBids().add(bid);
105:
106: Bid bid2 = new Bid();
107: bid2.setBidder(bidder2);
108: bid2.setAmount(j + 0.5f);
109: bid2.setDatetime(new Date());
110: bid2.setItem(item);
111: item.getBids().add(bid2);
112: bidder2.getBids().add(bid2);
113: }
114: seller.getAuctions().add(item);
115: mainItem = item;
116: }
117: mainBidder = bidder2;
118:
119: BuyNow buyNow = new BuyNow();
120: buyNow.setAmount(1.0f);
121: buyNow.setDatetime(new Date());
122: buyNow.setBidder(mainBidder);
123: buyNow.setItem(mainItem);
124: mainBidder.getBids().add(buyNow);
125: mainItem.getBids().add(buyNow);
126:
127: tx.commit();
128: s.close();
129: }
130:
131: protected void tearDown() throws Exception {
132: sessionFactory.close();
133: compassGps.stop();
134: super .tearDown();
135: }
136:
137: public void testSetUpData() throws Exception {
138: CompassSession sess = mirrorCompass.openSession();
139: CompassTransaction tr = sess.beginTransaction();
140:
141: CompassHits users = sess.find("bastard");
142: assertEquals(1, users.getLength());
143:
144: tr.commit();
145: }
146:
147: public void testSimpleDelete() throws Exception {
148: // find all the bids
149: CompassSession sess = mirrorCompass.openSession();
150: CompassTransaction tr = sess.beginTransaction();
151: User user = (User) sess.get(User.class, userToDelete.getId());
152: assertNotNull(user);
153:
154: // delete all the bids using hibernate
155: Session hibSess = sessionFactory.openSession();
156: Transaction hibTrans = hibSess.beginTransaction();
157: user = (User) hibSess.load(User.class, userToDelete.getId());
158: hibSess.delete(user);
159: hibTrans.commit();
160: hibSess.close();
161:
162: // check that it was reflected in compass
163: user = (User) sess.get(User.class, userToDelete.getId());
164: assertNull(user);
165: tr.commit();
166: sess.close();
167: }
168:
169: public void testSimpleUpdate() throws Exception {
170: CompassSession sess = mirrorCompass.openSession();
171: CompassTransaction tr = sess.beginTransaction();
172: CompassHits users = sess.find("bastard");
173: assertEquals(1, users.getLength());
174:
175: Session hibSess = sessionFactory.openSession();
176: Transaction hibTrans = hibSess.beginTransaction();
177: User user = (User) hibSess.load(User.class, ((User) users
178: .data(0)).getId());
179: user.getName().setLastName("snow");
180: hibTrans.commit();
181: hibSess.close();
182:
183: users = sess.find("bastard");
184: assertEquals(0, users.getLength());
185: users = sess.find("snow");
186: assertEquals(1, users.getLength());
187: tr.commit();
188: sess.close();
189: }
190:
191: public void testReindex() throws Exception {
192: compassGps.index();
193: CompassSession sess = mirrorCompass.openSession();
194: CompassTransaction tr = sess.beginTransaction();
195:
196: CompassHits users = sess.find("bastard");
197: assertEquals(1, users.getLength());
198:
199: tr.commit();
200: }
201: }
|