001: //
002: // Informa -- RSS Library for Java
003: // Copyright (c) 2002-2003 by Niko Schmuck
004: //
005: // Niko Schmuck
006: // http://sourceforge.net/projects/informa
007: // mailto:niko_schmuck@users.sourceforge.net
008: //
009: // This library is free software.
010: //
011: // You may redistribute it and/or modify it under the terms of the GNU
012: // Lesser General Public License as published by the Free Software Foundation.
013: //
014: // Version 2.1 of the license should be included with this distribution in
015: // the file LICENSE. If the license is not included with this distribution,
016: // you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
017: // or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
018: // MA 02139 USA.
019: //
020: // This library is distributed in the hope that it will be useful,
021: // but WITHOUT ANY WARRANTY; without even the implied waranty of
022: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
023: // Lesser General Public License for more details.
024: //
025:
026: // $Id: TestChannelRegistry.java,v 1.12 2004/03/09 23:42:19 niko_schmuck Exp $
027:
028: package de.nava.informa.utils;
029:
030: import java.io.File;
031: import java.util.Date;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: import de.nava.informa.core.ChannelIF;
037: import de.nava.informa.impl.basic.ChannelBuilder;
038:
039: public class TestChannelRegistry extends InformaTestCase {
040:
041: private static Log logger = LogFactory
042: .getLog(InformaTestCase.class);
043:
044: public TestChannelRegistry(String name) {
045: super ("TestChannelRegistry", name);
046: }
047:
048: public void XXXtestCreate() throws Exception {
049: ChannelRegistry reg = new ChannelRegistry(new ChannelBuilder());
050: // first channel
051: File inpFile = new File(getDataDir(), "xmlhack-0.91.xml");
052: ChannelIF chA = reg.addChannel(inpFile.toURL(), 2, true);
053: Date dateA = chA.getLastUpdated();
054: assertNull("channel shouldn't be parsed now", dateA);
055: // second channel
056: inpFile = new File(getDataDir(), "pro-linux.rdf");
057: ChannelIF chB = reg.addChannel(inpFile.toURL(), 2, true);
058: // third channel
059: inpFile = new File(getDataDir(), "snipsnap-org.rss");
060: ChannelIF chC = reg.addChannel(inpFile.toURL(), 2, true);
061: // some basic assertions
062: assertEquals("channel exists", 3, reg.getChannels().size());
063: assertTrue("channel A", reg.getChannels().contains(chA));
064: assertTrue("channel B", reg.getChannels().contains(chB));
065: assertTrue("channel C", reg.getChannels().contains(chC));
066: logger.info("starting to sleep ...");
067: try {
068: Thread.sleep(5000);
069: } catch (InterruptedException e) {
070: logger.warn("Interrupted waiting thread");
071: }
072: logger.info("... stopped sleeping");
073: // check that they are still active
074: assertTrue("channel A active", reg.isActiveChannel(chA));
075: assertTrue("channel B active", reg.isActiveChannel(chB));
076: assertTrue("channel C active", reg.isActiveChannel(chC));
077: // verify update
078: assertNotNull(
079: "channel should have been updated in the meantime", chA
080: .getLastUpdated());
081: }
082:
083: public void testParseProblem() throws Exception {
084: ChannelRegistry reg = new ChannelRegistry(new ChannelBuilder());
085: reg.setAcceptNrOfErrors(1);
086: // first channel
087: File inpFile = new File(getDataDir(), "xmlhack-0.91.xml");
088: File chFile = new File(getOutputDir(), "xmlhack-0.91.xml");
089: synchronized (chFile) {
090: FileUtils.copyFile(inpFile, chFile);
091: }
092: ChannelIF chA = reg.addChannel(chFile.toURL(), 2 /* secs */,
093: true);
094: // be sure channel is read in
095: try {
096: Thread.sleep(2500);
097: } catch (InterruptedException e) {
098: logger.warn("Interrupted waiting thread");
099: }
100: // some basic assertions
101: assertEquals("channel exists", 1, reg.getChannels().size());
102: assertTrue("channel A", reg.getChannels().contains(chA));
103: UpdateChannelInfo info = reg.getUpdateInfo(chA);
104: assertTrue("channel A active", reg.isActiveChannel(chA));
105: assertNull("no exception", info.getLastException());
106: assertEquals("NrProblems", 0, info.getNrProblemsOccurred());
107: logger.info("deleting channel file");
108: // simulate defect by deleting channel file
109: synchronized (chFile) {
110: chFile.delete();
111: }
112: logger.info("starting to sleep ...");
113: try {
114: // while we are sleep a new update should detect the lack of the file
115: Thread.sleep(2500);
116: } catch (InterruptedException e) {
117: logger.warn("Interrupted waiting thread");
118: }
119: logger.info("... stopped sleeping");
120: // check that it's not any longer active
121: info = reg.getUpdateInfo(chA);
122: assertTrue("channel A should be deactive", !reg
123: .isActiveChannel(chA));
124: logger.debug("exception: " + info.getLastException());
125: assertNotNull("Exception", info.getLastException());
126: assertEquals("NrProblems", 1, info.getNrProblemsOccurred());
127: }
128:
129: }
|