KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > impl > hibernate > TestChannelGroup


1 package de.nava.informa.impl.hibernate;
2
3 //
4
// Informa -- RSS Library for Java
5
// Copyright (c) 2002, 2003 by Niko Schmuck
6
//
7
// Niko Schmuck
8
// http://sourceforge.net/projects/informa
9
// mailto:niko_schmuck@users.sourceforge.net
10
//
11
// This library is free software.
12
//
13
// You may redistribute it and/or modify it under the terms of the GNU
14
// Lesser General Public License as published by the Free Software Foundation.
15
//
16
// Version 2.1 of the license should be included with this distribution in
17
// the file LICENSE. If the license is not included with this distribution,
18
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
19
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
20
// MA 02139 USA.
21
//
22
// This library is distributed in the hope that it will be useful,
23
// but WITHOUT ANY WARRANTY; without even the implied waranty of
24
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25
// Lesser General Public License for more details.
26
//
27

28
29 // $Id: TestChannelGroup.java,v 1.4 2004/06/28 19:14:30 niko_schmuck Exp $
30

31 import java.net.URL JavaDoc;
32 import java.util.List JavaDoc;
33
34 import net.sf.hibernate.Hibernate;
35 import net.sf.hibernate.HibernateException;
36 import net.sf.hibernate.Transaction;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 import de.nava.informa.core.ChannelGroupIF;
42 import de.nava.informa.core.ChannelIF;
43 import de.nava.informa.utils.InformaHibernateTestCase;
44
45 /**
46  * Test for making channel groups persistent while using the hibernate
47  * mapping backend.
48  *
49  * @author Niko Schmuck
50  */

51 public class TestChannelGroup extends InformaHibernateTestCase {
52
53   private static Log logger = LogFactory.getLog(TestChannelGroup.class);
54
55   public TestChannelGroup(String JavaDoc name) {
56     super("TestChannelGroup", name);
57   }
58
59   public void testChannelGroups() throws Exception JavaDoc {
60     ChannelBuilder builder = new ChannelBuilder(session);
61     Transaction tx = null;
62     int chId = -1;
63     int chGrpId = -1;
64     // our test objects
65
ChannelIF channel;
66     ChannelGroupIF grp1;
67     // -- first create a channel with a category assigned
68
try {
69       tx = session.beginTransaction();
70       String JavaDoc chanName = "Foo Test Channel";
71       channel = builder.createChannel(chanName);
72       channel.setDescription("Test Channel: " + chanName);
73       channel.setLocation(new URL JavaDoc("http://nava.de/test/channelFoo"));
74       session.saveOrUpdate(channel);
75       grp1 = builder.createChannelGroup("group A");
76       grp1.add(channel);
77       session.saveOrUpdate(grp1);
78       tx.commit();
79       chId = (int) channel.getId();
80       chGrpId = (int) grp1.getId();
81     }
82     catch (HibernateException he) {
83       logger.warn("trying to rollback the transaction");
84       if (tx != null) tx.rollback();
85       throw he;
86     }
87     assertTrue("No valid channel created.", chId >= 0);
88     assertTrue("No valid channel group created.", chGrpId >= 0);
89     // -- try to retrieve channel and the assigned category
90
try {
91       logger.info("Searching for channel group " + chGrpId);
92       List JavaDoc result = session.find("from ChannelGroup as grp where grp.id = ?",
93                                  new Integer JavaDoc(chGrpId), Hibernate.INTEGER);
94       assertEquals(1, result.size());
95       ChannelGroupIF cg = (ChannelGroupIF) result.iterator().next();
96       logger.info("retrieved channel group --> " + cg);
97       assertEquals(1, cg.getAll().size());
98       ChannelIF c = (ChannelIF) cg.getAll().iterator().next();
99       assertEquals("Foo Test Channel", c.getTitle());
100       assertNull(cg.getParent());
101     }
102     catch (HibernateException he) {
103       logger.warn("Error while querying for channel");
104       throw he;
105     }
106     // --- delete test objects
107
try {
108       tx = session.beginTransaction();
109       session.delete(grp1);
110       session.delete(channel);
111       tx.commit();
112     }
113     catch (HibernateException he) {
114       logger.warn("trying to rollback the transaction");
115       if (tx != null) tx.rollback();
116       throw he;
117     }
118   }
119
120 }
121
Popular Tags