KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestCreateChannels.java,v 1.12 2004/06/28 19:14:30 niko_schmuck Exp $
28

29 package de.nava.informa.impl.hibernate;
30
31 import java.io.File JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 import net.sf.hibernate.Hibernate;
37 import net.sf.hibernate.HibernateException;
38 import net.sf.hibernate.Transaction;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 import de.nava.informa.core.ChannelIF;
44 import de.nava.informa.core.ItemIF;
45 import de.nava.informa.parsers.FeedParser;
46 import de.nava.informa.utils.InformaHibernateTestCase;
47
48 /**
49  * Class demonstrating the use of the hibernate backend to persist the
50  * channel object model to a relational database.
51  *
52  * @author Niko Schmuck
53  */

54 public class TestCreateChannels extends InformaHibernateTestCase {
55
56   private static Log logger = LogFactory.getLog(TestCreateChannels.class);
57
58   public TestCreateChannels(String JavaDoc name) {
59     super("TestCreateChannels", name);
60   }
61
62   public void testCreateChannelItems() throws Exception JavaDoc {
63     ChannelBuilder builder = new ChannelBuilder(session);
64     Transaction tx = null;
65     int chId = -1;
66     // -- first create a channel with two news items
67
try {
68       tx = session.beginTransaction();
69       String JavaDoc chanName = "Foo Test Channel";
70       ChannelIF channel = builder.createChannel(chanName);
71       channel.setDescription("Test Channel: " + chanName);
72       channel.setLocation(new URL JavaDoc("http://www.nava.de/channelTest"));
73       session.saveOrUpdate(channel);
74       ItemIF item1 = builder.createItem(channel, "Item 1 for " + chanName,
75                                         "First in line", new URL JavaDoc("http://www.sf.net"));
76       session.saveOrUpdate(item1);
77       ItemIF item2 = builder.createItem(channel, "Item 2 for " + chanName,
78                                         "Second in line", new URL JavaDoc("http://www.sf.net"));
79       session.saveOrUpdate(item2);
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 children
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(2, c.getItems().size());
99       Iterator JavaDoc it_items = c.getItems().iterator();
100       while (it_items.hasNext()) {
101         ItemIF item = (ItemIF) it_items.next();
102         logger.info(" * " + item);
103         assertEquals(c, item.getChannel());
104       }
105     }
106     catch (HibernateException he) {
107       logger.warn("Error while querying for channel");
108       throw he;
109     }
110   }
111
112   public void testCreatePersistentChannelsFromFeed() throws Exception JavaDoc {
113     ChannelBuilder builder = new ChannelBuilder(session);
114     Transaction tx = null;
115     try {
116       tx = session.beginTransaction();
117       File JavaDoc inpFile = new File JavaDoc(getDataDir(), "xmlhack-0.91.xml");
118       ChannelIF channel = FeedParser.parse(builder, inpFile);
119       session.save(channel);
120       tx.commit();
121       assertEquals(6, channel.getItems().size());
122     }
123     catch (HibernateException he) {
124       logger.warn("trying to rollback the transaction");
125       if (tx != null) tx.rollback();
126       throw he;
127     }
128   }
129
130   public void testCreatePersistentChannel() throws Exception JavaDoc {
131     ChannelBuilder builder = new ChannelBuilder(session);
132     Transaction tx = null;
133     try {
134       logger.info("start new hibernate transaction");
135       tx = session.beginTransaction();
136       ChannelIF chA = builder.createChannel("Channel A");
137       chA.setLocation(new URL JavaDoc("http://test.org/A"));
138       long chA_id = chA.getId();
139       chA.setDescription("test channel for hibernate backend");
140       logger.info("created chA: " + chA);
141       ItemIF itA = builder.createItem(chA, "Simple item", "oh what a desc",
142                                       new URL JavaDoc("http://www.sf.net/"));
143       logger.info("created itA: " + itA);
144       session.save(chA);
145       logger.info("saved chA");
146       tx.commit();
147       logger.info("transaction commited");
148       assertEquals(chA_id, chA.getId());
149     }
150     catch (HibernateException he) {
151       logger.warn("trying to rollback the transaction");
152       if (tx != null) tx.rollback();
153       throw he;
154     }
155   }
156
157 }
158
Popular Tags