001: package de.nava.informa.impl.hibernate;
002:
003: //
004: // Informa -- RSS Library for Java
005: // Copyright (c) 2002, 2003 by Niko Schmuck
006: //
007: // Niko Schmuck
008: // http://sourceforge.net/projects/informa
009: // mailto:niko_schmuck@users.sourceforge.net
010: //
011: // This library is free software.
012: //
013: // You may redistribute it and/or modify it under the terms of the GNU
014: // Lesser General Public License as published by the Free Software Foundation.
015: //
016: // Version 2.1 of the license should be included with this distribution in
017: // the file LICENSE. If the license is not included with this distribution,
018: // you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
019: // or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
020: // MA 02139 USA.
021: //
022: // This library is distributed in the hope that it will be useful,
023: // but WITHOUT ANY WARRANTY; without even the implied waranty of
024: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
025: // Lesser General Public License for more details.
026: //
027:
028: // $Id: TestChannelGroup.java,v 1.6 2006/01/03 00:30:39 niko_schmuck Exp $
029:
030: import java.net.URL;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.hibernate.HibernateException;
035: import org.hibernate.Transaction;
036:
037: import de.nava.informa.core.ChannelGroupIF;
038: import de.nava.informa.core.ChannelIF;
039: import de.nava.informa.utils.InformaHibernateTestCase;
040:
041: /**
042: * Test for making channel groups persistent while using the hibernate
043: * mapping backend.
044: *
045: * @author Niko Schmuck
046: */
047: public class TestChannelGroup extends InformaHibernateTestCase {
048:
049: private static Log logger = LogFactory
050: .getLog(TestChannelGroup.class);
051:
052: public TestChannelGroup(String name) {
053: super ("TestChannelGroup", name);
054: }
055:
056: public void testChannelGroups() throws Exception {
057: ChannelBuilder builder = new ChannelBuilder(session);
058: Transaction tx = null;
059: int chId = -1;
060: int chGrpId = -1;
061: // our test objects
062: ChannelIF channel;
063: ChannelGroupIF grp1;
064: // -- first create a channel with a category assigned
065: try {
066: tx = session.beginTransaction();
067: String chanName = "Foo Test Channel";
068: channel = builder.createChannel(chanName);
069: channel.setDescription("Test Channel: " + chanName);
070: channel.setLocation(new URL(
071: "http://nava.de/test/channelFoo"));
072: session.saveOrUpdate(channel);
073: grp1 = builder.createChannelGroup("group A");
074: grp1.add(channel);
075: session.saveOrUpdate(grp1);
076: tx.commit();
077: chId = (int) channel.getId();
078: chGrpId = (int) grp1.getId();
079: } catch (HibernateException he) {
080: logger.warn("trying to rollback the transaction");
081: if (tx != null)
082: tx.rollback();
083: throw he;
084: }
085: assertTrue("No valid channel created.", chId >= 0);
086: assertTrue("No valid channel group created.", chGrpId >= 0);
087: // -- try to retrieve channel and the assigned category
088: try {
089: logger.info("Searching for channel group " + chGrpId);
090: Object result = session.get(ChannelGroup.class, new Long(
091: chGrpId));
092: assertNotNull(result);
093: ChannelGroupIF cg = (ChannelGroupIF) result;
094: logger.info("retrieved channel group --> " + cg);
095: assertEquals(1, cg.getAll().size());
096: ChannelIF c = (ChannelIF) cg.getAll().iterator().next();
097: assertEquals("Foo Test Channel", c.getTitle());
098: assertNull(cg.getParent());
099: } catch (HibernateException he) {
100: logger.warn("Error while querying for channel");
101: throw he;
102: }
103: // --- delete test objects
104: try {
105: tx = session.beginTransaction();
106: session.delete(grp1);
107: session.delete(channel);
108: tx.commit();
109: } catch (HibernateException he) {
110: logger.warn("trying to rollback the transaction");
111: if (tx != null)
112: tx.rollback();
113: throw he;
114: }
115: }
116:
117: }
|