1 26 28 package de.nava.informa.impl.hibernate; 29 30 import java.util.*; 31 import java.util.List ; 32 33 import net.sf.hibernate.Hibernate; 34 import net.sf.hibernate.HibernateException; 35 import net.sf.hibernate.Transaction; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 import de.nava.informa.core.*; 41 import de.nava.informa.core.ChannelIF; 42 import de.nava.informa.utils.InformaHibernateTestCase; 43 44 50 public class TestUnreadItems extends InformaHibernateTestCase { 51 52 private static Log logger = LogFactory.getLog(TestUnreadItems.class); 53 54 public TestUnreadItems(String name) { 55 super("TestUnreadItems", name); 56 } 57 58 public void testUnreadItems() throws Exception { 59 ChannelBuilder builder = new ChannelBuilder(session); 60 int chId = -1; 61 String chanName = "Unread Tester"; 62 Transaction tx = null; 63 try { 64 tx = session.beginTransaction(); 65 ChannelIF channel = builder.createChannel(chanName); 66 channel.setDescription("Test Channel: " + chanName); 67 session.save(channel); 68 int items; 70 for (items = 0; items < 20; items++) { 71 boolean unreadflag = ((items > 0 && items < 5)) 72 || (items > 10 && items < 15); 73 String desc = unreadflag ? "UnreadItem" : "ReadItem"; 74 ItemIF anItem = builder.createItem(channel, "Item: " + items, desc, null); 75 anItem.setUnRead(unreadflag); 76 } 77 session.save(channel); 78 chId = (int) channel.getId(); 79 tx.commit(); 80 } 81 catch (HibernateException he) { 82 logger.warn("trying to rollback the transaction"); 83 if (tx != null) tx.rollback(); 84 throw he; 85 } 86 assertTrue("No valid channel created.", chId >= 0); 87 88 try { 90 logger.info("Searching for channel " + chId); 91 List result = session.find("from Channel as ch where ch.id = ?", 92 new Integer ((int) chId), Hibernate.INTEGER); 93 assertEquals(1, result.size()); 94 ChannelIF c = (ChannelIF) result.iterator().next(); 95 logger.info("retrieved channel --> " + c); 96 97 Iterator itemsIter = c.getItems().iterator(); 99 while (itemsIter.hasNext()) { 100 ItemIF anItem = (ItemIF) itemsIter.next(); 101 if (anItem.getUnRead()) 102 assertTrue("Item marked as Unread isn't supposed to be Unread", 103 anItem.getDescription().compareTo("UnreadItem") == 0); 104 if (!anItem.getUnRead()) 105 assertTrue("Item marked as Read isn't supposed to be Read", anItem 106 .getDescription().compareTo("ReadItem") == 0); 107 } 108 } catch (HibernateException he) { 109 logger.warn("Error while querying for channel"); 110 throw he; 111 } 112 } 113 114 } 115 | Popular Tags |