KickJava   Java API By Example, From Geeks To Geeks.

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


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

26
27 // $Id: TestChannelCategory.java,v 1.4 2004/06/28 19:14:30 niko_schmuck Exp $
28

29 package de.nava.informa.impl.hibernate;
30
31 import java.util.List JavaDoc;
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 /**
45  * Test for making channel categories persistent while using the hibernate
46  * mapping backend.
47  *
48  * @author Niko Schmuck
49  */

50 public class TestChannelCategory extends InformaHibernateTestCase {
51
52   private static Log logger = LogFactory.getLog(TestChannelCategory.class);
53
54   public TestChannelCategory(String JavaDoc name) {
55     super("TestChannelCategory", name);
56   }
57
58   public void testChannelCategories() throws Exception JavaDoc {
59     ChannelBuilder builder = new ChannelBuilder(session);
60     Transaction tx = null;
61     int chId = -1;
62     // our test objects
63
ChannelIF channel;
64     CategoryIF cat1, cat2;
65     // -- first create a channel with a category assigned
66
try {
67       tx = session.beginTransaction();
68       // create channel
69
String JavaDoc chanName = "Foo Test Channel";
70       channel = builder.createChannel(chanName);
71       channel.setDescription("Test Channel: " + chanName);
72       session.saveOrUpdate(channel);
73       // create cat1
74
cat1 = builder.createCategory(null, "Root Cat");
75       session.saveOrUpdate(cat1);
76       // create cat2
77
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 to retrieve channel and the assigned category
91
try {
92       logger.info("Searching for channel " + chId);
93       List JavaDoc result = session.find("from Channel as ch where ch.id = ?",
94                                  new Integer JavaDoc((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     // -- delete test objects
109
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