• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id$
20  */
21 
22 package org.apache.qetest;
23 
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.util.Properties;
29 import java.util.Vector;
30 
31 /**
32  * Simple services for getting lists of FileDatalets.
33  *
34  * <p>Provides static worker methods for reading
35  * {@link FileTestletDriver#OPT_FILELIST -fileList} lists
36  * of input/output/gold files and creating lists of
37  * corresponding FileDatalets from them.  We provide the logic
38  * for reading the actual fileList line-by-line; the
39  * {@link FileDatalet} class provides the logic for initializing
40  * itself from a line of text.</p>
41  *
42  * @see FileTestletDriver
43  * @see FileDatalet
44  * @author shane_curcuru@us.ibm.com
45  * @version $Id$
46  */
47 public abstract class FileDataletManager // provide static services only
48 {
49 
50     /** '#' character, comment char in qetest fileList.  */
51     public static final String QETEST_COMMENT_CHAR = "#";
52 
53 
54     /**
55      * Read in a file specifying a list of files to test.
56      *
57      * <p>File format is fixed to be a qetest-style fileList;
58      * optional worker methods may allow other formats.
59      * Essentially we simply read the file line-by-line and
60      * create a FileDatalet from each line.</p>
61      *
62      * @param logger to report problems to
63      * @param fileName String; name of the file
64      * @param desc description; caller's copy changed
65      * @param defaults default properties to potentially add to each datalet
66      * @return Vector of FileDatalets; null if error
67      */
readFileList(Logger logger, String fileName, String desc, Properties defaults)68     public static Vector readFileList(Logger logger, String fileName,
69             String desc, Properties defaults)
70     {
71         // Verify the file is there
72         File f = new File(fileName);
73         if (!f.exists())
74         {
75             logger.logMsg(Logger.ERRORMSG, "readFileList: " + fileName
76                           + " does not exist!");
77             return null;
78         }
79 
80         BufferedReader br = null;
81         String line = null;
82         try
83         {
84             br = new BufferedReader(new FileReader(f));
85             line = br.readLine(); // read just first line
86         }
87         catch (IOException ioe)
88         {
89             logger.logMsg(Logger.ERRORMSG, "readFileList: " + fileName
90                           + " threw: " + ioe.toString());
91             return null;
92         }
93 
94         // Verify the first line
95         if (line == null)
96         {
97             logger.logMsg(Logger.ERRORMSG, "readFileList: " + fileName
98                           + " appears to be blank!");
99             return null;
100         }
101 
102         // Determine which kind of fileList this is
103         Vector vec = null;
104         if (line.startsWith(QETEST_COMMENT_CHAR))
105         {
106             // This is a native qetest style file
107             vec = readQetestFileList(logger, br, line, fileName, desc, defaults);
108         }
109         else
110         {
111             //@todo: add a worker method that allows users to plug
112             //  in their own fileList reader that creates Datalets
113             logger.logMsg(Logger.WARNINGMSG, "readFileList: " + fileName
114                           + " could not determine file type; assuming qetest!");
115             vec = readQetestFileList(logger, br, line, fileName, desc, defaults);
116         }
117 
118         if ((null == vec)
119             || (vec.size() == 0))
120         {
121             logger.logMsg(Logger.ERRORMSG, "readFileList: " + fileName
122                           + " did not have any non-comment lines!");
123             // Explicitly return null so caller knows we had error
124             return null;
125         }
126         return vec;
127     }
128 
129 
130     /**
131      * Read in a qetest fileList specifying a list of files to test.
132      *
133      * <p>File format is pretty simple:</p>
134      * <ul>
135      * <li># first line of comments is copied into desc</li>
136      * <li># beginning a line is a comment</li>
137      * <li># rest of lines are whitespace delimited filenames and options</li>
138      * <li>inputName outName goldName [options...]</li>
139      * <li><b>Note:</b> see {@link FileDatalet} for
140      * details on how the file lines are parsed!</li>
141      * </ul>
142      *
143      * @param logger to report problems to
144      * @param br BufferedReader to read from
145      * @param firstLine already read from br
146      * @param fileName String; name of the file
147      * @param desc to use of this file
148      * @param defaults default properties to potentially add to each datalet
149      * @return Vector of FileDatalets, or null if error
150      */
readQetestFileList(Logger logger, BufferedReader br, String firstLine, String fileName, String desc, Properties defaults)151     protected static Vector readQetestFileList(Logger logger, BufferedReader br,
152                                                String firstLine, String fileName,
153                                                String desc, Properties defaults)
154     {
155         final String ABSOLUTE = "absolute";
156         final String RELATIVE = "relative";
157 
158         Vector vec = new Vector();
159         String line = firstLine;
160         // Check if the first line is a comment
161         if (line.startsWith(QETEST_COMMENT_CHAR))
162         {
163             // Save it as the description
164             desc = line;
165             // Parse the next line
166             try
167             {
168                 line = br.readLine();
169             }
170             catch (IOException ioe)
171             {
172                 logger.logMsg(Logger.ERRORMSG, "readQetestFileList-desc: "
173                               + fileName + " threw: " + ioe.toString());
174                 return null;
175             }
176         }
177 
178         // Load each line into a FileDatalet
179         for (;;)
180         {
181             // Skip any lines beginning with # comment char or that are blank
182             if ((!line.startsWith(QETEST_COMMENT_CHAR)) && (line.length() > 0))
183             {
184                 // Create a Datalet and initialize with the line's
185                 //  contents and default properties
186                 FileDatalet d = new FileDatalet(line, defaults);
187 
188                 // Also pass over the global runId, if set
189                 //@todo this should be standardized and removed;
190                 //  or at least have global constants for it
191                 d.getOptions().put("runId", defaults.getProperty("runId"));
192 
193                 // Add it to our vector
194                 vec.addElement(d);
195             }
196 
197             // Read next line and loop
198             try
199             {
200                 line = br.readLine();
201             }
202             catch (IOException ioe2)
203             {
204                 // Just force us out of the loop; if we've already
205                 //  read part of the file, fine
206                 logger.logMsg(Logger.WARNINGMSG, "readQetestFileList-body: "
207                               + fileName + " threw: " + ioe2.toString());
208                 break;
209             }
210 
211             if (line == null)
212                 break;
213         } // end of for (;;)
214 
215         // Return our Vector of created datalets
216         return vec;
217     }
218 
219 }
220