• 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.util.Properties;
28 
29 /**
30  * Simply does .readLine of each file into string buffers and then String.equals().
31  * @author Shane_Curcuru@lotus.com
32  * @version $Id$
33  */
34 public class SimpleFileCheckService implements CheckService
35 {
36 
37     /**
38      * Compare two objects for equivalence, and return appropriate result.
39      *
40      * @param logger to dump any output messages to
41      * @param actual (current) File to check
42      * @param reference (gold, or expected) File to check against
43      * @param description of what you're checking
44      * @param msg comment to log out with this test point
45      * @param id ID tag to log out with this test point
46      * @return Logger.*_RESULT code denoting status; each method may
47      * define it's own meanings for pass, fail, ambiguous, etc.
48      */
check(Logger logger, Object actual, Object reference, String msg, String id)49     public int check(Logger logger, Object actual, Object reference,
50                      String msg, String id)
51     {
52 
53         if (!((actual instanceof File) & (reference instanceof File)))
54         {
55 
56             // Must have File objects to continue
57             logger.checkErr(msg + " :check() objects were not Files", id);
58 
59             return Logger.ERRR_RESULT;
60         }
61 
62         String fVal1 = readFileIntoString(logger, (File) actual);
63 
64         // Fail if Actual file doesn't exist
65         if (fVal1 == null)
66         {
67             logger.checkFail(msg + " :Actual file null", id);
68 
69             return Logger.FAIL_RESULT;
70         }
71 
72         String fVal2 = readFileIntoString(logger, (File) reference);
73 
74         // Ambiguous if gold or reference file doesn't exist
75         if (fVal2 == null)
76         {
77             logger.checkAmbiguous(msg + " :Gold file null", id);
78 
79             return Logger.AMBG_RESULT;
80         }
81 
82         // Pass if they're equal, fail otherwise
83         if (fVal1.equals(fVal2))
84         {
85             logger.checkPass(msg, id);
86 
87             return Logger.PASS_RESULT;
88         }
89         else
90         {
91             logger.checkFail(msg, id);
92 
93             return Logger.FAIL_RESULT;
94         }
95     }
96 
97     /**
98      * Compare two objects for equivalence, and return appropriate result.
99      *
100      * @param logger to dump any output messages to
101      * @param actual (current) File to check
102      * @param reference (gold, or expected) File to check against
103      * @param description of what you're checking
104      * @param msg comment to log out with this test point
105      * @return Logger.*_RESULT code denoting status; each method may
106      * define it's own meanings for pass, fail, ambiguous, etc.
107      */
check(Logger logger, Object actual, Object reference, String msg)108     public int check(Logger logger, Object actual, Object reference,
109                      String msg)
110     {
111         return check(logger, actual, reference, msg, null);
112     }
113 
114     /**
115      * Read text file into string line-by-line.
116      * @param logger to dump any messages to
117      * @param f File object to read
118      * @return String of file's contents
119      */
readFileIntoString(Logger logger, File f)120     private String readFileIntoString(Logger logger, File f)
121     {
122 
123         StringBuffer sb = new StringBuffer();
124 
125         try
126         {
127             FileReader fr = new FileReader(f);
128             BufferedReader br = new BufferedReader(fr);
129 
130             for (;;)
131             {
132                 String inbuf = br.readLine();
133 
134                 if (inbuf == null)
135                     break;
136 
137                 sb.append(inbuf);
138             }
139         }
140         catch (Exception e)
141         {
142             if (logger != null)
143             {
144                 logger.logMsg(Logger.ERRORMSG, "SimpleFileCheckService(" + f.getPath()
145                                      + ") threw:" + e.toString());
146             }
147             else
148                 System.err.println("SimpleFileCheckService(" + f.getPath()
149                                    + ") threw:" + e.toString());
150 
151             return null;
152         }
153 
154         return sb.toString();
155     }
156 
157     /**
158      * Gets extended information about the last checkFiles call: NONE AVAILABLE.
159      * @return null, since we don't support this
160      */
getExtendedInfo()161     public String getExtendedInfo()
162     {
163         return null;
164     }
165 
166     /**
167      * Allows the user to set specific attributes on the testing
168      * utility or it's underlying product object under test.
169      *
170      * No-op; this class does not have any supported attributes.
171      *
172      * @param name The name of the attribute.
173      * @param value The value of the attribute.
174      * @throws IllegalArgumentException thrown if the underlying
175      * implementation doesn't recognize the attribute and wants to
176      * inform the user of this fact.
177      */
setAttribute(String name, Object value)178     public void setAttribute(String name, Object value)
179         throws IllegalArgumentException
180     {
181         /* no-op */
182     }
183 
184     /**
185      * Allows the user to set specific attributes on the testing
186      * utility or it's underlying product object under test.
187      *
188      * No-op; this class does not have any supported attributes.
189      *
190      * @param attrs Props of various name, value attrs.
191      */
applyAttributes(Properties attrs)192     public void applyAttributes(Properties attrs)
193     {
194         /* no-op */
195     }
196 
197     /**
198      * Allows the user to retrieve specific attributes on the testing
199      * utility or it's underlying product object under test.
200      *
201      * @param name The name of the attribute.
202      * @return null, no attributes supported.
203      * @throws IllegalArgumentException thrown if the underlying
204      * implementation doesn't recognize the attribute and wants to
205      * inform the user of this fact.
206      */
getAttribute(String name)207     public Object getAttribute(String name)
208         throws IllegalArgumentException
209     {
210         return null;
211     }
212 
213     /**
214      * Description of what this testing utility does.
215      *
216      * @return String description of extension
217      */
getDescription()218     public String getDescription()
219     {
220         return ("Reads in text files line-by-line as strings (ignoring newlines) and does String.equals()");
221     }
222 
223 }  // end of class SimpleFileCheckService
224 
225