KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > impl > hibernate > TestInformaPersistence


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002, 2003 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26 // $Id: TestInformaPersistence.java,v 1.6 2004/05/15 18:20:56 niko_schmuck Exp $
27

28 package de.nava.informa.impl.hibernate;
29
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.sql.Connection JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36
37 import net.sf.hibernate.Hibernate;
38 import net.sf.hibernate.HibernateException;
39 import net.sf.hibernate.Session;
40 import net.sf.hibernate.Transaction;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 import de.nava.informa.core.ChannelBuilderException;
46 import de.nava.informa.core.ChannelIF;
47 import de.nava.informa.core.ChannelObserverIF;
48 import de.nava.informa.core.ItemIF;
49 import de.nava.informa.utils.ChannelRegistry;
50 import de.nava.informa.utils.InformaTestCase;
51
52 /**
53  * TestInformaPersistence
54  *
55  */

56 public class TestInformaPersistence extends InformaTestCase
57   implements ChannelObserverIF {
58
59   SessionHandler handler;
60   Connection JavaDoc conn;
61   ChannelBuilder builder;
62   ChannelRegistry channelRegistry;
63
64   private static Log log = LogFactory.getLog(TestInformaPersistence.class);
65
66   public TestInformaPersistence(String JavaDoc testname) {
67     super("TestInformaPersistence", testname);
68   }
69
70   public void setUp() throws HibernateException, SQLException JavaDoc,
71                                           ChannelBuilderException {
72     handler = SessionHandler.getInstance();
73     handler.getSession();
74     builder = new ChannelBuilder(handler);
75     builder.beginTransaction();
76     channelRegistry = new ChannelRegistry(builder);
77     builder.endTransaction();
78   }
79
80   public void tearDown() throws SQLException JavaDoc, ChannelBuilderException {
81     if (conn != null)
82       conn.close();
83     if (builder != null)
84       builder.close();
85     handler = null;
86   }
87
88   // ==================================================================
89

90   public void testChannelRegistry() {
91     /* these tests now work correctly
92                 int chanA = createChanWithItems("Fee channel", 3);
93                 int chanB = createChanWithItems("Fie channel", 12);
94                 int chanC = createChanWithItems("Foe channel", 44);
95                 verifyChannel(chanA);
96                 verifyChannel(chanB);
97                 verifyChannel(chanC);
98     */

99
100     Thread JavaDoc t = new Thread JavaDoc() {
101       public void run() {
102         try {
103           Thread.sleep(3000);
104         } catch (InterruptedException JavaDoc e) {
105           log.warn("Interrupted while running testcase: " + e);
106         }
107       }
108     };
109     // TODO: investigate more about multi-threaded JUnit tests (GroboUtils?)
110
t.start();
111     lookupAndCreateChannel("Joho",
112                            "http://www.hyperorg.com/blogger/index.rdf");
113     lookupAndCreateChannel("Bitwaste",
114                            "http://www.bitwaste.com/wasted-bits/index.cgi/index.rss");
115     lookupAndCreateChannel("AllConsuming",
116                            "http://www.allconsuming.net/xml/recent_consumption.rss.xml");
117   }
118
119   // ==================================================================
120

121   private Channel lookupAndCreateChannel(String JavaDoc label, String JavaDoc url) {
122     log.info("Looking up channel: " + label);
123     Channel achan = locateChannel(url);
124     if (achan != null) {
125       verifyChannel(label, achan.getIntId());
126     } else {
127       log.info(label + " not yet located.");
128     }
129     achan = getChannel(url, achan);
130     verifyChannel(label + " that was created:", achan.getIntId());
131     return achan;
132   }
133
134   private Channel getChannel(String JavaDoc string, Channel existing) {
135     log.info("Getting channel: " + string);
136     synchronized (builder) {
137       Channel aChannel = null;
138       Transaction tx = null;
139       try {
140         builder.beginTransaction();
141         if (existing == null) {
142           aChannel =
143             (Channel) channelRegistry.addChannel(new URL JavaDoc(string), 30, false);
144         } else {
145           aChannel = (Channel) channelRegistry.addChannel(existing, false, 30);
146         }
147         int chanid = aChannel.getIntId();
148         log.info("Got channel: " + aChannel + "(id =" + chanid + ")");
149         aChannel.addObserver(this);
150         builder.endTransaction();
151         channelRegistry.activateChannel(aChannel, 60);
152       } catch (MalformedURLException JavaDoc e) {
153         e.printStackTrace();
154       } catch (ChannelBuilderException e1) {
155         e1.printStackTrace();
156       }
157       return aChannel;
158     }
159   }
160
161   private Channel locateChannel(String JavaDoc xmlURL) {
162     synchronized (builder) {
163       Channel aChannel = null;
164       Transaction tx = null;
165       try {
166         builder.beginTransaction();
167         Session sess = builder.getSession();
168         List JavaDoc channels =
169           sess.find("from Channel as chan where chan.locationString = ?",
170                     xmlURL, Hibernate.STRING);
171         log.info("***locateChannel for " + xmlURL + " produced these: " + channels);
172         if (channels.size() >= 1) { // for now just take the last one
173
aChannel = (Channel) channels.get(channels.size() - 1);
174         }
175         builder.endTransaction();
176       } catch (Exception JavaDoc e) {
177         e.printStackTrace();
178       }
179       return aChannel;
180     }
181   }
182
183   // TODO: split this out into InformaHibernateTestCase for better reuse
184

185   private void verifyChannel(String JavaDoc label, int chan_id) {
186     try {
187       Session session = handler.getSession();
188       log.info(label + chan_id);
189       Channel aChan = (Channel) session.load(Channel.class, new Integer JavaDoc(chan_id));
190       assertEquals((long) chan_id, aChan.getId());
191       assertNotNull(aChan.getTitle());
192       logChannel(aChan);
193       session.flush();
194       session.close();
195     } catch (Exception JavaDoc e) {
196       e.printStackTrace();
197     }
198   }
199
200   private void logChannel(Channel aChan) {
201     String JavaDoc logthis = "Channel: " + aChan + ":";
202     Iterator JavaDoc items = aChan.getItems().iterator();
203     while (items.hasNext()) {
204       Item item = (Item) items.next();
205       logthis = logthis + ":" + item;
206     }
207     log.info(logthis);
208   }
209
210   // --------------------------------------------------------------------------
211
// currently unused
212
// --------------------------------------------------------------------------
213

214   private int createChanWithItems(String JavaDoc chanName, int count) throws Exception JavaDoc {
215     synchronized (builder) {
216       int chan_id = -1;
217       try {
218         builder.beginTransaction();
219         log.info("createChanWithItems starting...");
220         Channel channel = (Channel) builder.createChannel(chanName);
221         chan_id = channel.getIntId();
222         channel.setDescription("Test Channel: " + chanName + " ID = " + chan_id);
223         log.info("created " + chanName + " ID = " + chan_id);
224         log.info("saved " + chanName);
225
226         for (int i = 0; i < count; i++) {
227           Item item1 =
228             (Item) builder.createItem(channel, "Item " + i + " for " + chanName,
229                                       "A wonderful description!",
230                                       new URL JavaDoc("http://www.sf.net/"));
231         }
232         logChannel(channel);
233         builder.endTransaction();
234         log.info("transaction commited. CHAN ID = " + chan_id);
235       } catch (ChannelBuilderException he) {
236         he.printStackTrace();
237         throw he;
238       }
239       return chan_id;
240     }
241   }
242
243   // ----------------------------------------------------------------------
244
// Implementation of ChannelObserverIF
245
// ----------------------------------------------------------------------
246

247   public void itemAdded(ItemIF newItem) {
248     log.info("Added item " + newItem);
249   }
250
251   public void channelRetrieved(ChannelIF channel) {
252     log.info("Added channel " + channel);
253   }
254
255 }
256
Popular Tags