KickJava   Java API By Example, From Geeks To Geeks.

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


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.RequestData;
22 import org.apache.beehive.netui.tools.testrecorder.shared.DiffFailedException;
23 import org.apache.beehive.netui.tools.testrecorder.shared.NVPair;
24 import org.apache.beehive.netui.tools.testrecorder.shared.ResponseData;
25 import org.apache.beehive.netui.tools.testrecorder.shared.Logger;
26 import org.apache.beehive.netui.tools.testrecorder.shared.TestResults;
27
28 /**
29  * User: ozzy
30  * Date: Jul 9, 2004
31  * Time: 10:10:18 AM
32  */

33 public abstract class AbstractDiffEngine implements DiffEngine {
34
35     private static final Logger log = Logger.getInstance( AbstractDiffEngine.class );
36
37     public TestResults diff( RequestData recordRequest, ResponseData recordResponse, RequestData playbackRequest,
38             ResponseData playbackResponse, TestResults results ) throws DiffFailedException {
39         results = diff( recordRequest, playbackRequest, results );
40         results = diff( recordResponse, playbackResponse, results );
41         return results;
42     }
43
44     public abstract TestResults responseBodyDiff( String JavaDoc record, String JavaDoc playback, TestResults results )
45             throws DiffFailedException;
46
47     public TestResults diff( RequestData record, RequestData playback,
48             TestResults results ) throws DiffFailedException {
49         if ( !record.getProtocol().equals( playback.getProtocol() ) ) {
50             results.addDiffResult( "record request protocol( " + record.getProtocol() +
51                     " ) does not match playback request protocol( " +
52                     playback.getProtocol() + " )" );
53         }
54         
55         // Note that we do NOT compare the recorded request path with the playback request path, because the Servlet
56
// spec doesn't say that they must be equal. Specifically, this can break when the container itself interprets
57
// a welcome-page request.
58

59         if ( !record.getMethod().equals( playback.getMethod() ) ) {
60             results.addDiffResult( "record request method( " + record.getMethod() +
61                     " ) does not match playback request method( " +
62                     playback.getMethod() + " )" );
63         }
64         results = diffParams( record.getParameters(), playback.getParameters(), results );
65         return results;
66     }
67
68     public TestResults diffParams( NVPair[] record, NVPair[] playback,
69             TestResults results ) throws DiffFailedException {
70         // assumption: params are sorted.
71
NVPair recPair = null;
72         NVPair playPair = null;
73         int i = 0;
74         boolean match = false;
75         if ( record != null ) {
76             for ( i = 0; i < record.length; i++, match = false ) {
77                 recPair = record[i];
78                 if ( playback != null && i < playback.length ) {
79                     playPair = playback[i];
80                     if ( recPair.equals( playPair ) ) {
81                         match = true;
82                     }
83                 }
84                 else {
85                     // match stays false
86
playPair = null;
87                 }
88                 if ( !match ) {
89                     results.addDiffResult( "record request parameter( " + recPair +
90                             " ) does not match playback request parameter( " +
91                             playPair + " )" );
92                 }
93             }
94         }
95         if ( playback != null ) {
96             // see if more playback params exist
97
recPair = null;
98             for ( ; i < playback.length; i++ ) {
99                 playPair = playback[i];
100                 results.addDiffResult( "record request parameter( " + recPair +
101                         " ) does not match playback request parameter( " +
102                         playPair + " )" );
103             }
104         }
105         return results;
106     }
107
108     public TestResults diff( ResponseData record, ResponseData playback, TestResults results )
109             throws DiffFailedException {
110         if ( record.getStatusCode() != playback.getStatusCode() ) {
111             results.addDiffResult( "record response status code( " + record.getStatusCode() +
112                     " ) does not match playback response status code( " +
113                     playback.getStatusCode() + " )" );
114         }
115         results = responseBodyDiff( record.getNormalizedBody(), playback.getNormalizedBody(), results );
116         return results;
117     }
118
119 }
120
Popular Tags