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 net.sf.hibernate.Session;
023: import net.sf.hibernate.SessionFactory;
024: import net.sf.hibernate.Transaction;
025: import net.sf.hibernate.cfg.Configuration;
026: import net.sf.hibernate.cfg.Environment;
027: import org.compass.core.CompassHits;
028: import org.compass.core.CompassSession;
029: import org.compass.core.CompassTransaction;
030: import org.compass.gps.device.hibernate.dep.Hibernate2GpsDevice;
031:
032: /**
033: * @author kimchy
034: */
035: public class Hibernate2GpsDeviceTests extends
036: AbstractHibernateGpsDeviceTests {
037:
038: private SessionFactory sessionFactory;
039:
040: private AuctionItem mainItem;
041:
042: private User mainBidder;
043:
044: protected void setUp() throws Exception {
045: super .setUp();
046:
047: Configuration conf = new Configuration()
048: .configure(
049: "/org/compass/gps/device/hibernate/eg/hibernate2.cfg.xml")
050: .setProperty(Environment.HBM2DDL_AUTO, "create");
051:
052: sessionFactory = conf.buildSessionFactory();
053:
054: // set up the initial set of data
055: Session s = sessionFactory.openSession();
056: Transaction tx = s.beginTransaction();
057:
058: User seller = new User();
059: seller.setUserName("oldirty");
060: seller.setName(new Name("ol' dirty", null, "bastard"));
061: seller.setEmail("oldirty@hibernate.org");
062: seller.setAuctions(new ArrayList());
063: s.save(seller);
064: User bidder1 = new User();
065: bidder1.setUserName("1E1");
066: bidder1.setName(new Name("oney", new Character('1'), "one"));
067: bidder1.setEmail("oney@hibernate.org");
068: bidder1.setBids(new ArrayList());
069: s.save(bidder1);
070: User bidder2 = new User();
071: bidder2.setUserName("izi");
072: bidder2.setName(new Name("iz", null, "inizi"));
073: bidder2.setEmail("izi@hibernate.org");
074: bidder2.setBids(new ArrayList());
075: s.save(bidder2);
076:
077: for (int i = 0; i < 3; i++) {
078: AuctionItem item = new AuctionItem();
079: item.setDescription("auction item " + i);
080: item.setEnds(new Date());
081: item.setBids(new ArrayList());
082: item.setSeller(seller);
083: item.setCondition(i * 3 + 2);
084: for (int j = 0; j < i; j++) {
085:
086: Bid bid = new Bid();
087: bid.setBidder(bidder1);
088: bid.setAmount(j);
089: bid.setDatetime(new Date());
090: bid.setItem(item);
091: item.getBids().add(bid);
092: bidder1.getBids().add(bid);
093:
094: Bid bid2 = new Bid();
095: bid2.setBidder(bidder2);
096: bid2.setAmount(j + 0.5f);
097: bid2.setDatetime(new Date());
098: bid2.setItem(item);
099: item.getBids().add(bid2);
100: bidder2.getBids().add(bid2);
101: }
102: seller.getAuctions().add(item);
103: mainItem = item;
104: }
105: mainBidder = bidder2;
106:
107: BuyNow buyNow = new BuyNow();
108: buyNow.setAmount(1.0f);
109: buyNow.setDatetime(new Date());
110: buyNow.setBidder(mainBidder);
111: buyNow.setItem(mainItem);
112: mainBidder.getBids().add(buyNow);
113: mainItem.getBids().add(buyNow);
114:
115: tx.commit();
116: s.close();
117:
118: Hibernate2GpsDevice device = new Hibernate2GpsDevice();
119: device.setName("hibernateDevice");
120: device.setSessionFactory(sessionFactory);
121: compassGps.addGpsDevice(device);
122: compassGps.start();
123: }
124:
125: protected void tearDown() throws Exception {
126: sessionFactory.close();
127: compassGps.stop();
128: super .tearDown();
129: }
130:
131: public void testReindex() throws Exception {
132: compassGps.index();
133: CompassSession sess = mirrorCompass.openSession();
134: CompassTransaction tr = sess.beginTransaction();
135:
136: CompassHits users = sess.find("bastard");
137: assertEquals(1, users.getLength());
138:
139: tr.commit();
140: }
141: }
|