1 26 27 29 package de.nava.informa.impl.hibernate; 30 31 import java.io.File ; 32 import java.net.URL ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import net.sf.hibernate.Hibernate; 37 import net.sf.hibernate.HibernateException; 38 import net.sf.hibernate.Transaction; 39 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 import de.nava.informa.core.ChannelIF; 44 import de.nava.informa.core.ItemIF; 45 import de.nava.informa.parsers.FeedParser; 46 import de.nava.informa.utils.InformaHibernateTestCase; 47 48 54 public class TestCreateChannels extends InformaHibernateTestCase { 55 56 private static Log logger = LogFactory.getLog(TestCreateChannels.class); 57 58 public TestCreateChannels(String name) { 59 super("TestCreateChannels", name); 60 } 61 62 public void testCreateChannelItems() throws Exception { 63 ChannelBuilder builder = new ChannelBuilder(session); 64 Transaction tx = null; 65 int chId = -1; 66 try { 68 tx = session.beginTransaction(); 69 String chanName = "Foo Test Channel"; 70 ChannelIF channel = builder.createChannel(chanName); 71 channel.setDescription("Test Channel: " + chanName); 72 channel.setLocation(new URL ("http://www.nava.de/channelTest")); 73 session.saveOrUpdate(channel); 74 ItemIF item1 = builder.createItem(channel, "Item 1 for " + chanName, 75 "First in line", new URL ("http://www.sf.net")); 76 session.saveOrUpdate(item1); 77 ItemIF item2 = builder.createItem(channel, "Item 2 for " + chanName, 78 "Second in line", new URL ("http://www.sf.net")); 79 session.saveOrUpdate(item2); 80 session.saveOrUpdate(channel); 81 tx.commit(); 82 chId = (int) channel.getId(); 83 } 84 catch (HibernateException he) { 85 logger.warn("trying to rollback the transaction"); 86 if (tx != null) tx.rollback(); 87 throw he; 88 } 89 assertTrue("No valid channel created.", chId >= 0); 90 try { 92 logger.info("Searching for channel " + chId); 93 List result = session.find("from Channel as ch where ch.id = ?", 94 new Integer ((int) chId), Hibernate.INTEGER); 95 assertEquals(1, result.size()); 96 ChannelIF c = (ChannelIF) result.iterator().next(); 97 logger.info("retrieved channel --> " + c); 98 assertEquals(2, c.getItems().size()); 99 Iterator it_items = c.getItems().iterator(); 100 while (it_items.hasNext()) { 101 ItemIF item = (ItemIF) it_items.next(); 102 logger.info(" * " + item); 103 assertEquals(c, item.getChannel()); 104 } 105 } 106 catch (HibernateException he) { 107 logger.warn("Error while querying for channel"); 108 throw he; 109 } 110 } 111 112 public void testCreatePersistentChannelsFromFeed() throws Exception { 113 ChannelBuilder builder = new ChannelBuilder(session); 114 Transaction tx = null; 115 try { 116 tx = session.beginTransaction(); 117 File inpFile = new File (getDataDir(), "xmlhack-0.91.xml"); 118 ChannelIF channel = FeedParser.parse(builder, inpFile); 119 session.save(channel); 120 tx.commit(); 121 assertEquals(6, channel.getItems().size()); 122 } 123 catch (HibernateException he) { 124 logger.warn("trying to rollback the transaction"); 125 if (tx != null) tx.rollback(); 126 throw he; 127 } 128 } 129 130 public void testCreatePersistentChannel() throws Exception { 131 ChannelBuilder builder = new ChannelBuilder(session); 132 Transaction tx = null; 133 try { 134 logger.info("start new hibernate transaction"); 135 tx = session.beginTransaction(); 136 ChannelIF chA = builder.createChannel("Channel A"); 137 chA.setLocation(new URL ("http://test.org/A")); 138 long chA_id = chA.getId(); 139 chA.setDescription("test channel for hibernate backend"); 140 logger.info("created chA: " + chA); 141 ItemIF itA = builder.createItem(chA, "Simple item", "oh what a desc", 142 new URL ("http://www.sf.net/")); 143 logger.info("created itA: " + itA); 144 session.save(chA); 145 logger.info("saved chA"); 146 tx.commit(); 147 logger.info("transaction commited"); 148 assertEquals(chA_id, chA.getId()); 149 } 150 catch (HibernateException he) { 151 logger.warn("trying to rollback the transaction"); 152 if (tx != null) tx.rollback(); 153 throw he; 154 } 155 } 156 157 } 158 | Popular Tags |