KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > server > LineDiffEngine


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
26 import java.io.StringReader JavaDoc;
27
28 /**
29  * User: ozzy
30  * Date: Jul 9, 2004
31  * Time: 10:07:32 AM
32  */

33 public class LineDiffEngine extends AbstractDiffEngine {
34
35     private static final Logger log = Logger.getInstance( LineDiffEngine.class );
36
37     private static final String JavaDoc REGEX_MARKER = "REGEX:";
38
39     public TestResults responseBodyDiff( String JavaDoc record, String JavaDoc playback, TestResults results )
40             throws DiffFailedException {
41         try {
42             LineNumberReader JavaDoc recReader = new LineNumberReader JavaDoc( new StringReader JavaDoc( record ) );
43             LineNumberReader JavaDoc playReader = new LineNumberReader JavaDoc( new StringReader JavaDoc( playback ) );
44             String JavaDoc recLine = null;
45             String JavaDoc 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 JavaDoc ex ) {
82             String JavaDoc 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