KickJava   Java API By Example, From Geeks To Geeks.

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


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: InformaTestCase.java,v 1.14 2004/05/21 11:26:54 niko_schmuck Exp $
28

29 package de.nava.informa.utils;
30
31 import java.io.File JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import junit.framework.TestCase;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import de.nava.informa.core.ChannelIF;
40 import de.nava.informa.core.ItemIF;
41
42 /**
43  * Base class for unit tests of the informa library. It provides some
44  * convenience methods for easy data file retrieval.
45  *
46  * @author Niko Schmuck
47  */

48 public class InformaTestCase extends TestCase {
49
50   private static Log logger = LogFactory.getLog(InformaTestCase.class);
51   
52   protected final static String JavaDoc FS = System.getProperty("file.separator");
53   protected static String JavaDoc dataDir;
54   protected static String JavaDoc indexDir;
55   protected static String JavaDoc outputDir;
56   protected static String JavaDoc baselineDir;
57
58   protected String JavaDoc testcase_name;
59   protected String JavaDoc method_name;
60   
61   static {
62     // fall-back mechanism to get the base directory
63
String JavaDoc baseDir = System.getProperty("INFORMA_HOME");
64     logger.debug("retrieving property INFORMA_HOME: " + baseDir);
65     if (baseDir == null) {
66       baseDir = System.getProperty("basedir");
67       logger.debug("retrieving property basedir: " + baseDir);
68     }
69     if (baseDir == null) {
70       baseDir = System.getProperty("user.dir");
71       logger.debug("retrieving property user.dir: " + baseDir);
72     }
73     // calculate other dirs from this
74
String JavaDoc writeDir = baseDir + FS + "build" + FS + "test" + FS + "data";
75     indexDir = writeDir + FS + "index";
76     outputDir = writeDir + FS + "out";
77     // the output directory is only used to write files generated by test
78
// cases and may therefore not exist yet
79
File JavaDoc out = new File JavaDoc(outputDir);
80     if (!out.exists()) {
81       out.mkdirs();
82     }
83     // Those directories are only used in read-only mode
84
String JavaDoc refDir = baseDir + FS + "test";
85     dataDir = refDir + FS + "data";
86     baselineDir = refDir + FS + "baseline";
87   }
88   
89
90   /**
91    * Constructor for a new informa specific test case. The base directory
92    * is calculated from one of the following system properties (order matters):
93    * <code>INFORMA_HOME, basedir, user.dir</code>
94    *
95    * @param name the name of the test case (read method, thanks JUnit)
96    */

97   public InformaTestCase(String JavaDoc testcase_name, String JavaDoc method_name) {
98     super(method_name);
99     this.method_name = method_name;
100     this.testcase_name = testcase_name;
101   }
102
103   /**
104    * @return The directory from which we can retrieve test relevant data.
105    */

106   public static String JavaDoc getDataDir() {
107     return dataDir;
108   }
109   
110   /**
111    * @return The directory to store the full-text index in.
112    */

113   public static String JavaDoc getIndexDir() {
114     return indexDir;
115   }
116
117   /**
118    * @return The directory where our test might want to write a produced
119    * file to.
120    */

121   public static String JavaDoc getOutputDir() {
122     return outputDir;
123   }
124
125   /**
126    * @return The directory containing the gold files to compare the
127    * files generated by the current test runs with.
128    */

129   public static String JavaDoc getBaselineDir() {
130     return baselineDir;
131   }
132
133   /**
134    * Returns the name of the testcase inclusive the class.
135    */

136   public String JavaDoc getName() {
137     return testcase_name + "." + method_name;
138   }
139   
140   // =====================================================================
141
// internal helper methods
142
// =====================================================================
143

144   protected ItemIF searchForItem(ChannelIF chn, String JavaDoc itmTitle) {
145     ItemIF lookup_item = null;
146     Iterator JavaDoc it = chn.getItems().iterator();
147     while (it.hasNext()) {
148       ItemIF item = (ItemIF) it.next();
149       assertNotNull("Item has no title", item.getTitle());
150       assertNotNull("Item has no link", item.getLink());
151       // logger.debug("title: <" + item.getTitle() + ">");
152
if (item.getTitle().startsWith(itmTitle)) {
153         lookup_item = item;
154         break;
155       }
156     }
157     return lookup_item;
158   }
159
160 }
161
Popular Tags