KickJava   Java API By Example, From Geeks To Geeks.

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


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 // $Id: TestUnreadItems.java,v 1.4 2004/06/28 19:14:30 niko_schmuck Exp $
27

28 package de.nava.informa.impl.hibernate;
29
30 import java.util.*;
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.*;
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 TestUnreadItems extends InformaHibernateTestCase {
51
52   private static Log logger = LogFactory.getLog(TestUnreadItems.class);
53
54   public TestUnreadItems(String JavaDoc name) {
55     super("TestUnreadItems", name);
56   }
57
58   public void testUnreadItems() throws Exception JavaDoc {
59     ChannelBuilder builder = new ChannelBuilder(session);
60     int chId = -1;
61     String JavaDoc chanName = "Unread Tester";
62     Transaction tx = null;
63     try {
64       tx = session.beginTransaction();
65       ChannelIF channel = builder.createChannel(chanName);
66       channel.setDescription("Test Channel: " + chanName);
67       session.save(channel);
68       // Add items, with some marked read and some marked unread
69
int items;
70       for (items = 0; items < 20; items++) {
71         boolean unreadflag = ((items > 0 && items < 5))
72           || (items > 10 && items < 15);
73         String JavaDoc desc = unreadflag ? "UnreadItem" : "ReadItem";
74         ItemIF anItem = builder.createItem(channel, "Item: " + items, desc, null);
75         anItem.setUnRead(unreadflag);
76       }
77       session.save(channel);
78       chId = (int) channel.getId();
79       tx.commit();
80     }
81     catch (HibernateException he) {
82       logger.warn("trying to rollback the transaction");
83       if (tx != null) tx.rollback();
84       throw he;
85     }
86     assertTrue("No valid channel created.", chId >= 0);
87
88     // -- try to retrieve channel and check the unread statuses
89
try {
90       logger.info("Searching for channel " + chId);
91       List JavaDoc result = session.find("from Channel as ch where ch.id = ?",
92           new Integer JavaDoc((int) chId), Hibernate.INTEGER);
93       assertEquals(1, result.size());
94       ChannelIF c = (ChannelIF) result.iterator().next();
95       logger.info("retrieved channel --> " + c);
96
97       // now check unread settings
98
Iterator itemsIter = c.getItems().iterator();
99       while (itemsIter.hasNext()) {
100         ItemIF anItem = (ItemIF) itemsIter.next();
101         if (anItem.getUnRead())
102             assertTrue("Item marked as Unread isn't supposed to be Unread",
103                 anItem.getDescription().compareTo("UnreadItem") == 0);
104         if (!anItem.getUnRead())
105             assertTrue("Item marked as Read isn't supposed to be Read", anItem
106                 .getDescription().compareTo("ReadItem") == 0);
107       }
108     } catch (HibernateException he) {
109       logger.warn("Error while querying for channel");
110       throw he;
111     }
112   }
113
114 }
115
Popular Tags