1 18 19 package org.apache.beehive.netui.tools.testrecorder.shared; 20 21 import java.io.IOException ; 22 import java.io.File ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 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 genDetails( RecordSessionBean bean ) { 35 return formatDetails( bean ); 36 } 37 38 public static String genDiffDetails( File diffFile ) throws IOException , SessionXMLException { 39 List results = XMLHelper.getDiffResults( diffFile ); 40 StringBuffer sb = new StringBuffer ( results.size() * 128 ); 41 TestResults testResults = null; 42 List diffList = null; 43 for ( int i = 0; i < results.size(); i++ ) { 44 testResults = (TestResults) results.get( i ); 45 diffList = testResults.getDiffResults(); 46 String 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 ) 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 formatDetails( RecordSessionBean bean ) { 70 StringBuffer sb = new StringBuffer (); 71 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 sb ) { 92 sb.append( "<hr /><h4>Request " ); 93 sb.append( request.getPath() ); 94 String buttonAction = null; 95 String 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 formatParameterDetails( RequestData request, String buttonAction ) { 106 StringBuffer sb = new StringBuffer ( ( 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 formatDiff( String diff ) { 130 StringBuffer sb = new StringBuffer ( diff.length() ); 131 StringTokenizer st = new StringTokenizer ( diff, "\n\r\f" ); 132 String 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 ( 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 ( 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 getLineBreak() { 160 return "\n"; 161 } 162 163 private static String getPlaybackMarker() { 164 return "<b>P:</b>"; 165 } 166 167 private static String getRecordMarker() { 168 return "<b>R:</b>"; 169 } 170 171 public static String escape( String in ) { 172 char[] c = in.toCharArray(); 173 return escape( c, in.length() ); 174 } 175 public static String escape( char[] c, int size ) { 176 StringBuffer sb = new StringBuffer (); 177 for ( int i = 0; i < size; i++ ) { 178 if ( c[i] == '<' ) { 179 sb.append( "<" ); 180 continue; 181 } 182 sb.append( c[i] ); 183 } 184 return sb.toString(); 185 } 186 187 } 188 | Popular Tags |