super("TestChannelCategory", name);
}
public void testChannelCategories() throws Exception {
ChannelBuilder builder = new ChannelBuilder(session);
Transaction tx = null;
int chId = -1;
// our test objects
ChannelIF channel;
CategoryIF cat1, cat2;
// -- first create a channel with a category assigned
try {
tx = session.beginTransaction();
// create channel
String chanName = "Foo Test Channel";
channel = builder.createChannel(chanName);
channel.setDescription("Test Channel: " + chanName);
session.saveOrUpdate(channel);
// create cat1
cat1 = builder.createCategory(null, "Root Cat");
session.saveOrUpdate(cat1);
// create cat2
cat2 = builder.createCategory(cat1, "Agent_A");
session.saveOrUpdate(cat2);
channel.addCategory(cat2);
session.saveOrUpdate(channel);
tx.commit();
chId = (int) channel.getId();
}
catch (HibernateException he) {
logger.warn("trying to rollback the transaction");
if (tx != null) tx.rollback();
throw he;
}
assertTrue("No valid channel created.", chId >= 0);
// -- try to retrieve channel and the assigned category
try {
logger.info("Searching for channel " + chId);
Object result = session.get(Channel.class, new Long(chId));
assertNotNull(result);
ChannelIF c = (ChannelIF) result;
logger.info("retrieved channel --> " + c);
assertEquals(1, c.getCategories().size());
CategoryIF cat = (CategoryIF) c.getCategories().iterator().next();
assertEquals("Agent_A", cat.getTitle());
assertNotNull(cat.getParent());
assertEquals("Root Cat", cat.getParent().getTitle());
}
catch (HibernateException he) {
logger.warn("Error while querying for channel");
throw he;
}
// -- delete test objects
try {
tx = session.beginTransaction();
session.delete(cat1);
session.delete(cat2);
session.delete(channel);
tx.commit();
}
catch (HibernateException he) {
logger.warn("trying to rollback the transaction");
if (tx != null) tx.rollback();
throw he;
}
}