1 18 19 package org.apache.beehive.netui.tools.testrecorder.server; 20 21 import org.apache.beehive.netui.tools.testrecorder.shared.DiffFailedException; 22 import org.apache.beehive.netui.tools.testrecorder.shared.Logger; 23 import org.apache.beehive.netui.tools.testrecorder.shared.TestResults; 24 25 import java.io.LineNumberReader ; 26 import java.io.StringReader ; 27 28 33 public class LineDiffEngine extends AbstractDiffEngine { 34 35 private static final Logger log = Logger.getInstance( LineDiffEngine.class ); 36 37 private static final String REGEX_MARKER = "REGEX:"; 38 39 public TestResults responseBodyDiff( String record, String playback, TestResults results ) 40 throws DiffFailedException { 41 try { 42 LineNumberReader recReader = new LineNumberReader ( new StringReader ( record ) ); 43 LineNumberReader playReader = new LineNumberReader ( new StringReader ( playback ) ); 44 String recLine = null; 45 String playLine = null; 46 boolean match; 47 while ( ( recLine = recReader.readLine() ) != null ) { 48 playLine = playReader.readLine(); 49 if ( playLine != null ) { 50 playLine = playLine.trim(); 51 recLine = recLine.trim(); 52 if ( recLine.startsWith( REGEX_MARKER ) ) { 53 match = playLine.matches( recLine.substring( REGEX_MARKER.length() ).trim() ); 54 } 55 else { 56 match = recLine.equals( playLine ); 57 } 58 } 59 else { 60 match = false; 61 } 62 63 if ( !match ) { 64 results.addDiffResult( "Line (" + recReader.getLineNumber() + 65 ") does not match. \n" + 66 "R: " + recLine + "\n" + 67 "P: " + playLine + "\n" ); 68 } 69 } 70 if ( log.isDebugEnabled() ) { 71 log.debug( "**** record reader has no more lines ****" ); 72 } 73 while ( ( playLine = playReader.readLine() ) != null ) { 74 match = false; 75 results.addDiffResult( "Line (" + playReader.getLineNumber() + 76 ") does not match.\n" + 77 "R: " + recLine + "\n" + 78 "P: " + playLine + "\n" ); 79 } 80 } 81 catch ( Exception ex ) { 82 String msg = "diff failed, exception( " + ex.getMessage() + " )"; 83 log.error( msg, ex ); 84 throw new DiffFailedException( msg, ex ); 85 } 86 return results; 87 } 88 89 } 90 | Popular Tags |