• 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.FileInputStream;
27 import java.io.InputStreamReader;
28 import java.util.Properties;
29 
30 /**
31  * Simply does .readLine of each file into string buffers and then String.equals().
32  * @author Paul_Dick@us.ibm.com
33  * @version $Id$
34  */
35 public class LinebyLineCheckService implements CheckService
36 {
37 
38     /**
39      * Compare two objects for equivalence, and return appropriate result.
40      *
41      * @param logger to dump any output messages to
42      * @param actual (current) File to check
43      * @param reference (gold, or expected) File to check against
44      * @param description of what you're checking
45      * @param msg comment to log out with this test point
46      * @param id ID tag to log out with this test point
47      * @return Logger.*_RESULT code denoting status; each method may
48      * define it's own meanings for pass, fail, ambiguous, etc.
49      */
check(Logger logger, Object actual, Object expected, String msg, String id)50     public int check(Logger logger, Object actual, Object expected,
51                      String msg, String id)
52     {
53         if (!((actual instanceof File) & (expected instanceof File)))
54         {
55             // Must have File objects to continue
56             logger.checkErr(msg + " :check() objects were not Files", id);
57             return Logger.ERRR_RESULT;
58         }
59 
60 		if (doCompare(logger, (File) actual, (File) expected))
61         {
62             logger.checkPass(msg, id);
63             return Logger.PASS_RESULT;
64         }
65         else
66         {
67             logger.checkFail(msg, id);
68             return Logger.FAIL_RESULT;
69         }
70     }
71 
72     /**
73      * Compare two objects for equivalence, and return appropriate result.
74      *
75      * @param logger to dump any output messages to
76      * @param actual (current) File to check
77      * @param reference (gold, or expected) File to check against
78      * @param description of what you're checking
79      * @param msg comment to log out with this test point
80      * @return Logger.*_RESULT code denoting status; each method may
81      * define it's own meanings for pass, fail, ambiguous, etc.
82      */
check(Logger logger, Object actual, Object reference, String msg)83     public int check(Logger logger, Object actual, Object reference,
84                      String msg)
85     {
86         return check(logger, actual, reference, msg, null);
87     }
88 
89     /**
90      * Read text files line-by-line and do comparison
91      * @param logger to dump any messages to
92      * @param act File object to read
93      * @param exp File object to read
94      * @return True or False based on comparison
95      */
doCompare(Logger logger, File act, File exp)96     private boolean doCompare(Logger logger, File act, File exp)
97     {
98         StringBuffer sb_act = new StringBuffer();
99 		StringBuffer sb_exp = new StringBuffer();
100 
101         try
102         {
103             FileInputStream in_act = new FileInputStream(act);
104             FileInputStream in_exp = new FileInputStream(exp);
105 			// Create necessary I/O objects
106             InputStreamReader fra = new InputStreamReader(in_act, "UTF-8");
107                        InputStreamReader fre = new InputStreamReader(in_exp, "UTF-8");
108 
109             BufferedReader bra = new BufferedReader(fra);
110 			BufferedReader bre = new BufferedReader(fre);
111 
112 			int line = 0;	// Line number to report in case of failure
113             for (;;)
114             {
115 				String inbufe = bre.readLine();	 // read files, line by line
116 				String inbufa = bra.readLine();
117 				line += 1;						 // Keep track of line number
118 
119 				if (inbufe == null)		// Is expected done?
120 				{
121 					if (inbufa == null)	// If so, is actual done?
122 					{
123 						break;			// If so, we're done!!
124 					}
125 					else				// If not report error
126 					{
127 						logger.logArbitrary(logger.FAILSONLY, "Actual("+line+"): " + inbufa);
128 						logger.logArbitrary(logger.FAILSONLY, "Expect("+line+"): " + inbufe);
129 						return false;
130 					}
131 				}
132 				else
133 				{						// Still data to compare
134 				    if ( !(inbufa.equals(inbufe)) )
135 				    {
136 						logger.logArbitrary(logger.FAILSONLY, "Actual("+line+"): " + inbufa);
137 						logger.logArbitrary(logger.FAILSONLY, "Expect("+line+"): " + inbufe);
138 						return false;
139 				    }
140 				}
141             }
142         }
143         catch (Exception e)				//  Report any file I/O exceptions
144         {
145             if (logger != null)
146             {
147                 logger.logMsg(Logger.ERRORMSG, "LinebyLineCheckService() threw:" +
148                 				e.toString());
149             }
150             else
151                 System.err.println("LinebyLineCheckService() threw:" + e.toString());
152 
153             return false;
154         }
155         return true;
156     }
157 
158     /**
159      * Gets extended information about the last checkFiles call: NONE AVAILABLE.
160      * @return null, since we don't support this
161      */
getExtendedInfo()162     public String getExtendedInfo()
163     {
164         return null;
165     }
166 
167     /**
168      * Allows the user to set specific attributes on the testing
169      * utility or it's underlying product object under test.
170      *
171      * No-op; this class does not have any supported attributes.
172      *
173      * @param name The name of the attribute.
174      * @param value The value of the attribute.
175      * @throws IllegalArgumentException thrown if the underlying
176      * implementation doesn't recognize the attribute and wants to
177      * inform the user of this fact.
178      */
setAttribute(String name, Object value)179     public void setAttribute(String name, Object value)
180         throws IllegalArgumentException
181     {
182         /* no-op */
183     }
184 
185     /**
186      * Allows the user to set specific attributes on the testing
187      * utility or it's underlying product object under test.
188      *
189      * No-op; this class does not have any supported attributes.
190      *
191      * @param attrs Props of various name, value attrs.
192      */
applyAttributes(Properties attrs)193     public void applyAttributes(Properties attrs)
194     {
195         /* no-op */
196     }
197 
198     /**
199      * Allows the user to retrieve specific attributes on the testing
200      * utility or it's underlying product object under test.
201      *
202      * @param name The name of the attribute.
203      * @return null, no attributes supported.
204      * @throws IllegalArgumentException thrown if the underlying
205      * implementation doesn't recognize the attribute and wants to
206      * inform the user of this fact.
207      */
getAttribute(String name)208     public Object getAttribute(String name)
209         throws IllegalArgumentException
210     {
211         return null;
212     }
213 
214     /**
215      * Description of what this testing utility does.
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 LinebyLineCheckService
224