KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > TestChannelRegistry


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: TestChannelRegistry.java,v 1.12 2004/03/09 23:42:19 niko_schmuck Exp $
27

28 package de.nava.informa.utils;
29
30 import java.io.File JavaDoc;
31 import java.util.Date JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import de.nava.informa.core.ChannelIF;
37 import de.nava.informa.impl.basic.ChannelBuilder;
38
39 public class TestChannelRegistry extends InformaTestCase {
40
41   private static Log logger = LogFactory.getLog(InformaTestCase.class);
42
43   public TestChannelRegistry(String JavaDoc name) {
44     super("TestChannelRegistry", name);
45   }
46
47   public void XXXtestCreate() throws Exception JavaDoc {
48     ChannelRegistry reg = new ChannelRegistry(new ChannelBuilder());
49     // first channel
50
File JavaDoc inpFile = new File JavaDoc(getDataDir(), "xmlhack-0.91.xml");
51     ChannelIF chA = reg.addChannel(inpFile.toURL(), 2, true);
52     Date JavaDoc dateA = chA.getLastUpdated();
53     assertNull("channel shouldn't be parsed now", dateA);
54     // second channel
55
inpFile = new File JavaDoc(getDataDir(), "pro-linux.rdf");
56     ChannelIF chB = reg.addChannel(inpFile.toURL(), 2, true);
57     // third channel
58
inpFile = new File JavaDoc(getDataDir(), "snipsnap-org.rss");
59     ChannelIF chC = reg.addChannel(inpFile.toURL(), 2, true);
60     // some basic assertions
61
assertEquals("channel exists", 3, reg.getChannels().size());
62     assertTrue("channel A", reg.getChannels().contains(chA));
63     assertTrue("channel B", reg.getChannels().contains(chB));
64     assertTrue("channel C", reg.getChannels().contains(chC));
65     logger.info("starting to sleep ...");
66     try {
67       Thread.sleep(5000);
68     } catch (InterruptedException JavaDoc e) {
69       logger.warn("Interrupted waiting thread");
70     }
71     logger.info("... stopped sleeping");
72     // check that they are still active
73
assertTrue("channel A active", reg.isActiveChannel(chA));
74     assertTrue("channel B active", reg.isActiveChannel(chB));
75     assertTrue("channel C active", reg.isActiveChannel(chC));
76     // verify update
77
assertNotNull("channel should have been updated in the meantime",
78                   chA.getLastUpdated());
79   }
80
81   public void testParseProblem() throws Exception JavaDoc {
82     ChannelRegistry reg = new ChannelRegistry(new ChannelBuilder());
83     reg.setAcceptNrOfErrors(1);
84     // first channel
85
File JavaDoc inpFile = new File JavaDoc(getDataDir(), "xmlhack-0.91.xml");
86     File JavaDoc chFile = new File JavaDoc(getOutputDir(), "xmlhack-0.91.xml");
87     synchronized(chFile) {
88       FileUtils.copyFile(inpFile, chFile);
89     }
90     ChannelIF chA = reg.addChannel(chFile.toURL(), 2 /* secs */, true);
91     // be sure channel is read in
92
try {
93       Thread.sleep(2500);
94     } catch (InterruptedException JavaDoc e) {
95       logger.warn("Interrupted waiting thread");
96     }
97     // some basic assertions
98
assertEquals("channel exists", 1, reg.getChannels().size());
99     assertTrue("channel A", reg.getChannels().contains(chA));
100     UpdateChannelInfo info = reg.getUpdateInfo(chA);
101     assertTrue("channel A active", reg.isActiveChannel(chA));
102     assertNull("no exception", info.getLastException());
103     assertEquals("NrProblems", 0, info.getNrProblemsOccurred());
104     logger.info("deleting channel file");
105     // simulate defect by deleting channel file
106
synchronized(chFile) {
107       chFile.delete();
108     }
109     logger.info("starting to sleep ...");
110     try {
111       // while we are sleep a new update should detect the lack of the file
112
Thread.sleep(2500);
113     } catch (InterruptedException JavaDoc e) {
114       logger.warn("Interrupted waiting thread");
115     }
116     logger.info("... stopped sleeping");
117     // check that it's not any longer active
118
info = reg.getUpdateInfo(chA);
119     assertTrue("channel A should be deactive", !reg.isActiveChannel(chA));
120     logger.debug("exception: " + info.getLastException());
121     assertNotNull("Exception", info.getLastException());
122     assertEquals("NrProblems", 1, info.getNrProblemsOccurred());
123   }
124
125 }
126
Popular Tags