1 26 27 29 package de.nava.informa.impl.hibernate; 30 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.CategoryIF; 41 import de.nava.informa.core.ChannelIF; 42 import de.nava.informa.utils.InformaHibernateTestCase; 43 44 50 public class TestChannelCategory extends InformaHibernateTestCase { 51 52 private static Log logger = LogFactory.getLog(TestChannelCategory.class); 53 54 public TestChannelCategory(String name) { 55 super("TestChannelCategory", name); 56 } 57 58 public void testChannelCategories() throws Exception { 59 ChannelBuilder builder = new ChannelBuilder(session); 60 Transaction tx = null; 61 int chId = -1; 62 ChannelIF channel; 64 CategoryIF cat1, cat2; 65 try { 67 tx = session.beginTransaction(); 68 String chanName = "Foo Test Channel"; 70 channel = builder.createChannel(chanName); 71 channel.setDescription("Test Channel: " + chanName); 72 session.saveOrUpdate(channel); 73 cat1 = builder.createCategory(null, "Root Cat"); 75 session.saveOrUpdate(cat1); 76 cat2 = builder.createCategory(cat1, "Agent_A"); 78 session.saveOrUpdate(cat2); 79 channel.addCategory(cat2); 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(1, c.getCategories().size()); 99 CategoryIF cat = (CategoryIF) c.getCategories().iterator().next(); 100 assertEquals("Agent_A", cat.getTitle()); 101 assertNotNull(cat.getParent()); 102 assertEquals("Root Cat", cat.getParent().getTitle()); 103 } 104 catch (HibernateException he) { 105 logger.warn("Error while querying for channel"); 106 throw he; 107 } 108 try { 110 tx = session.beginTransaction(); 111 session.delete(cat1); 112 session.delete(cat2); 113 session.delete(channel); 114 tx.commit(); 115 } 116 catch (HibernateException he) { 117 logger.warn("trying to rollback the transaction"); 118 if (tx != null) tx.rollback(); 119 throw he; 120 } 121 } 122 123 } 124 | Popular Tags |