1 26 28 package de.nava.informa.impl.hibernate; 29 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.sql.Connection ; 33 import java.sql.SQLException ; 34 import java.util.Iterator ; 35 import java.util.List ; 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 56 public class TestInformaPersistence extends InformaTestCase 57 implements ChannelObserverIF { 58 59 SessionHandler handler; 60 Connection conn; 61 ChannelBuilder builder; 62 ChannelRegistry channelRegistry; 63 64 private static Log log = LogFactory.getLog(TestInformaPersistence.class); 65 66 public TestInformaPersistence(String testname) { 67 super("TestInformaPersistence", testname); 68 } 69 70 public void setUp() throws HibernateException, SQLException , 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 , ChannelBuilderException { 81 if (conn != null) 82 conn.close(); 83 if (builder != null) 84 builder.close(); 85 handler = null; 86 } 87 88 90 public void testChannelRegistry() { 91 99 100 Thread t = new Thread () { 101 public void run() { 102 try { 103 Thread.sleep(3000); 104 } catch (InterruptedException e) { 105 log.warn("Interrupted while running testcase: " + e); 106 } 107 } 108 }; 109 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 121 private Channel lookupAndCreateChannel(String label, String 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 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 (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 e) { 153 e.printStackTrace(); 154 } catch (ChannelBuilderException e1) { 155 e1.printStackTrace(); 156 } 157 return aChannel; 158 } 159 } 160 161 private Channel locateChannel(String xmlURL) { 162 synchronized (builder) { 163 Channel aChannel = null; 164 Transaction tx = null; 165 try { 166 builder.beginTransaction(); 167 Session sess = builder.getSession(); 168 List 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) { aChannel = (Channel) channels.get(channels.size() - 1); 174 } 175 builder.endTransaction(); 176 } catch (Exception e) { 177 e.printStackTrace(); 178 } 179 return aChannel; 180 } 181 } 182 183 185 private void verifyChannel(String 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 (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 e) { 196 e.printStackTrace(); 197 } 198 } 199 200 private void logChannel(Channel aChan) { 201 String logthis = "Channel: " + aChan + ":"; 202 Iterator 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 214 private int createChanWithItems(String chanName, int count) throws Exception { 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 ("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 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 |