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: InformaTestCase.java,v 1.14 2004/05/21 11:26:54 niko_schmuck Exp $
027:
028: package de.nava.informa.utils;
029:
030: import java.io.File;
031: import java.util.Iterator;
032:
033: import junit.framework.TestCase;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: import de.nava.informa.core.ChannelIF;
039: import de.nava.informa.core.ItemIF;
040:
041: /**
042: * Base class for unit tests of the informa library. It provides some
043: * convenience methods for easy data file retrieval.
044: *
045: * @author Niko Schmuck
046: */
047: public class InformaTestCase extends TestCase {
048:
049: private static Log logger = LogFactory
050: .getLog(InformaTestCase.class);
051:
052: protected final static String FS = System
053: .getProperty("file.separator");
054: protected static String dataDir;
055: protected static String indexDir;
056: protected static String outputDir;
057: protected static String baselineDir;
058:
059: protected String testcase_name;
060: protected String method_name;
061:
062: static {
063: // fall-back mechanism to get the base directory
064: String baseDir = System.getProperty("INFORMA_HOME");
065: logger.debug("retrieving property INFORMA_HOME: " + baseDir);
066: if (baseDir == null) {
067: baseDir = System.getProperty("basedir");
068: logger.debug("retrieving property basedir: " + baseDir);
069: }
070: if (baseDir == null) {
071: baseDir = System.getProperty("user.dir");
072: logger.debug("retrieving property user.dir: " + baseDir);
073: }
074: // calculate other dirs from this
075: String writeDir = baseDir + FS + "build" + FS + "test" + FS
076: + "data";
077: indexDir = writeDir + FS + "index";
078: outputDir = writeDir + FS + "out";
079: // the output directory is only used to write files generated by test
080: // cases and may therefore not exist yet
081: File out = new File(outputDir);
082: if (!out.exists()) {
083: out.mkdirs();
084: }
085: // Those directories are only used in read-only mode
086: String refDir = baseDir + FS + "test";
087: dataDir = refDir + FS + "data";
088: baselineDir = refDir + FS + "baseline";
089: }
090:
091: /**
092: * Constructor for a new informa specific test case. The base directory
093: * is calculated from one of the following system properties (order matters):
094: * <code>INFORMA_HOME, basedir, user.dir</code>
095: *
096: * @param name the name of the test case (read method, thanks JUnit)
097: */
098: public InformaTestCase(String testcase_name, String method_name) {
099: super (method_name);
100: this .method_name = method_name;
101: this .testcase_name = testcase_name;
102: }
103:
104: /**
105: * @return The directory from which we can retrieve test relevant data.
106: */
107: public static String getDataDir() {
108: return dataDir;
109: }
110:
111: /**
112: * @return The directory to store the full-text index in.
113: */
114: public static String getIndexDir() {
115: return indexDir;
116: }
117:
118: /**
119: * @return The directory where our test might want to write a produced
120: * file to.
121: */
122: public static String getOutputDir() {
123: return outputDir;
124: }
125:
126: /**
127: * @return The directory containing the gold files to compare the
128: * files generated by the current test runs with.
129: */
130: public static String getBaselineDir() {
131: return baselineDir;
132: }
133:
134: /**
135: * Returns the name of the testcase inclusive the class.
136: */
137: public String getName() {
138: return testcase_name + "." + method_name;
139: }
140:
141: // =====================================================================
142: // internal helper methods
143: // =====================================================================
144:
145: protected ItemIF searchForItem(ChannelIF chn, String itmTitle) {
146: ItemIF lookup_item = null;
147: Iterator it = chn.getItems().iterator();
148: while (it.hasNext()) {
149: ItemIF item = (ItemIF) it.next();
150: assertNotNull("Item has no title", item.getTitle());
151: assertNotNull("Item has no link", item.getLink());
152: // logger.debug("title: <" + item.getTitle() + ">");
153: if (item.getTitle().startsWith(itmTitle)) {
154: lookup_item = item;
155: break;
156: }
157: }
158: return lookup_item;
159: }
160:
161: }
|