KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > shared > Reporter


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.shared;
20
21 import java.io.IOException JavaDoc;
22 import java.io.File JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.apache.beehive.netui.tools.testrecorder.shared.xmlbeans.XMLHelper;
27 import org.apache.beehive.netui.tools.testrecorder.server.TestRecorderServlet;
28
29
30 public class Reporter {
31
32     private static final Logger log = Logger.getInstance( TestRecorderServlet.class );
33
34     public static String JavaDoc genDetails( RecordSessionBean bean ) {
35         return formatDetails( bean );
36     }
37
38     public static String JavaDoc genDiffDetails( File JavaDoc diffFile ) throws IOException JavaDoc, SessionXMLException {
39         List JavaDoc results = XMLHelper.getDiffResults( diffFile );
40         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( results.size() * 128 );
41         TestResults testResults = null;
42         List JavaDoc diffList = null;
43         for ( int i = 0; i < results.size(); i++ ) {
44             testResults = (TestResults) results.get( i );
45             diffList = testResults.getDiffResults();
46             String JavaDoc diff = null;
47             if ( i > 0 ) {
48                 sb.append( getLineBreak() );
49             }
50             sb.append( "Uri( " + testResults.getUri() + " )" + getLineBreak() );
51             sb.append( "Test Number( " + testResults.getTestNumber() + " )" + getLineBreak() );
52             if ( testResults.isError() ) {
53                 sb.append( "Test Execution Error Encountered!" );
54             }
55             if ( diffList.size() == 0 ) {
56                 sb.append( "*** no diff info present for this test failure ***" );
57             }
58             else if ( diffList.size() == 1 ) {
59                 diff = (String JavaDoc) diffList.get( 0 );
60                 sb.append( formatDiff( diff ) );
61             }
62             else {
63                 sb.append( "*** ERROR: unexpected diff results ***" );
64             }
65         }
66         return sb.toString();
67     }
68
69     private static String JavaDoc formatDetails( RecordSessionBean bean ) {
70         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
71         // the header information
72
sb.append( "<table>" );
73         sb.append( "<tr><td><b>Test</b></td><td>" );
74         sb.append( bean.getSessionName() );
75         sb.append( "</td></tr>" );
76         sb.append( "<tr><td><b>Description</b></td><td>" );
77         sb.append( bean.getDescription() );
78         sb.append( "</td></tr>" );
79         sb.append( "<tr><td><b>Tester</b></td><td>" );
80         sb.append( bean.getTester() );
81         sb.append( "</td></tr>" );
82         sb.append( "</table>" );
83
84         int cnt = bean.getTestCount();
85         for ( int i = 0; i < cnt; i++ ) {
86             formatRequestDetails( bean.getRequestData( i ), sb );
87         }
88         return sb.toString();
89     }
90
91     private static void formatRequestDetails( RequestData request, StringBuffer JavaDoc sb ) {
92         sb.append( "<hr /><h4>Request " );
93         sb.append( request.getPath() );
94         String JavaDoc buttonAction = null;
95         String JavaDoc params = formatParameterDetails( request, buttonAction );
96         if ( buttonAction != null ) {
97             sb.append( "<br />Button Request" );
98             sb.append( buttonAction );
99         }
100         sb.append( "</h4>" );
101         sb.append( "<h5>Parameters</h5>" );
102         sb.append( params );
103     }
104
105     private static String JavaDoc formatParameterDetails( RequestData request, String JavaDoc buttonAction ) {
106         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( ( 16 * request.getParamCount() ) + 5 );
107         buttonAction = null;
108         if ( request.getParamCount() > 0 ) {
109             sb.append( "<table border=\"1\" cellspacing=\"0\"><tr><th>Name</th><th>Value</th></tr>" );
110             int cnt = request.getParamCount();
111             for ( int i = 0; i < cnt; i++ ) {
112                 if ( request.getParamName( i ).startsWith( "actionOverride:" ) ) {
113                     buttonAction = request.getParamName( i );
114                 }
115                 sb.append( "<tr><td>" );
116                 sb.append( request.getParamName( i ) );
117                 sb.append( "</td><td>" );
118                 sb.append( request.getParamValue( i ) );
119                 sb.append( "</td></tr>" );
120             }
121             sb.append( "</table>" );
122         }
123         else {
124             sb.append( "None" );
125         }
126         return sb.toString();
127     }
128
129     public static String JavaDoc formatDiff( String JavaDoc diff ) {
130         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( diff.length() );
131         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( diff, "\n\r\f" );
132         String JavaDoc line = null;
133         while ( st.hasMoreTokens() ) {
134             line = st.nextToken();
135             if ( line.startsWith( "R:" ) ) {
136                 if ( line.length() == 2 ) {
137                     line = getRecordMarker() + getLineBreak();
138                 }
139                 else {
140                     line = new String JavaDoc( getRecordMarker() + escape( line.substring( 2 ) ) + getLineBreak() );
141                 }
142             }
143             else if ( line.startsWith( "P:" ) ) {
144                 if ( line.length() == 2 ) {
145                     line = getPlaybackMarker() + getLineBreak();
146                 }
147                 else {
148                     line = new String JavaDoc( getPlaybackMarker() + escape( line.substring( 2 ) ) + getLineBreak() );
149                 }
150             }
151             else {
152                 line = escape( line ) + getLineBreak();
153             }
154             sb.append( line );
155         }
156         return sb.toString();
157     }
158
159     private static String JavaDoc getLineBreak() {
160         return "\n";
161     }
162
163     private static String JavaDoc getPlaybackMarker() {
164         return "<b>P:</b>";
165     }
166
167     private static String JavaDoc getRecordMarker() {
168         return "<b>R:</b>";
169     }
170
171     public static String JavaDoc escape( String JavaDoc in ) {
172         char[] c = in.toCharArray();
173         return escape( c, in.length() );
174     }
175     public static String JavaDoc escape( char[] c, int size ) {
176         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
177         for ( int i = 0; i < size; i++ ) {
178             if ( c[i] == '<' ) {
179                 sb.append( "&lt;" );
180                 continue;
181             }
182             sb.append( c[i] );
183         }
184         return sb.toString();
185     }
186
187 }
188
Popular Tags