001// Copyright 2007, 2009, 2012 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.test; 016 017import com.thoughtworks.selenium.CommandProcessor; 018 019/** 020 * A wrapper around a standard command processor that adds additional exception reporting when a 021 * failure occurs. 022 */ 023public class ErrorReportingCommandProcessor implements CommandProcessor 024{ 025 private final CommandProcessor delegate; 026 027 private final ErrorReporter errorReporter; 028 029 public ErrorReportingCommandProcessor(CommandProcessor delegate, ErrorReporter errorReporter) 030 { 031 this.delegate = delegate; 032 this.errorReporter = errorReporter; 033 } 034 035 private static final String BORDER = "**********************************************************************"; 036 037 private void reportError(String command, String[] args, RuntimeException ex) 038 { 039 StringBuilder builder = new StringBuilder(); 040 041 builder.append("Selenium failure processing command "); 042 builder.append(command); 043 builder.append('('); 044 045 for (int i = 0; i < args.length; i++) 046 { 047 if (i > 0) 048 builder.append(", "); 049 builder.append('"'); 050 builder.append(args[i]); 051 builder.append('"'); 052 } 053 054 builder.append("): "); 055 builder.append(ex.toString()); 056 057 try 058 { 059 String logs = delegate.getString("retrieveLastRemoteControlLogs", new String[]{}); 060 061 if (logs != null && logs.length() > 0) 062 { 063 064 builder.append('\n'); 065 builder.append(BORDER); 066 builder.append('\n'); 067 068 builder.append(logs); 069 } 070 071 } catch (Exception ex2) 072 { 073 // Skip the logs. 074 } 075 076 077 String report = builder.toString(); 078 079 System.err.println(BORDER); 080 System.err.println(report); 081 System.err.println(BORDER); 082 083 errorReporter.writeErrorReport(report); 084 } 085 086 @Override 087 public String doCommand(String command, String[] args) 088 { 089 try 090 { 091 return delegate.doCommand(command, args); 092 } catch (RuntimeException ex) 093 { 094 reportError(command, args, ex); 095 throw ex; 096 } 097 } 098 099 @Override 100 public boolean getBoolean(String string, String[] strings) 101 { 102 try 103 { 104 return delegate.getBoolean(string, strings); 105 } catch (RuntimeException ex) 106 { 107 reportError(string, strings, ex); 108 throw ex; 109 } 110 } 111 112 @Override 113 public boolean[] getBooleanArray(String string, String[] strings) 114 { 115 try 116 { 117 return delegate.getBooleanArray(string, strings); 118 } catch (RuntimeException ex) 119 { 120 reportError(string, strings, ex); 121 throw ex; 122 } 123 } 124 125 @Override 126 public Number getNumber(String string, String[] strings) 127 { 128 try 129 { 130 return delegate.getNumber(string, strings); 131 } catch (RuntimeException ex) 132 { 133 reportError(string, strings, ex); 134 throw ex; 135 } 136 } 137 138 @Override 139 public Number[] getNumberArray(String string, String[] strings) 140 { 141 try 142 { 143 return delegate.getNumberArray(string, strings); 144 } catch (RuntimeException ex) 145 { 146 reportError(string, strings, ex); 147 throw ex; 148 } 149 } 150 151 @Override 152 public String getString(String string, String[] strings) 153 { 154 try 155 { 156 return delegate.getString(string, strings); 157 } catch (RuntimeException ex) 158 { 159 reportError(string, strings, ex); 160 throw ex; 161 } 162 } 163 164 @Override 165 public String[] getStringArray(String string, String[] strings) 166 { 167 try 168 { 169 return delegate.getStringArray(string, strings); 170 } catch (RuntimeException ex) 171 { 172 reportError(string, strings, ex); 173 throw ex; 174 } 175 } 176 177 @Override 178 public void start() 179 { 180 delegate.start(); 181 } 182 183 @Override 184 public void stop() 185 { 186 delegate.stop(); 187 } 188 189 /** 190 * @since 5.1.0.0 191 */ 192 @Override 193 public String getRemoteControlServerLocation() 194 { 195 return delegate.getRemoteControlServerLocation(); 196 } 197 198 /** 199 * @since 5.1.0.0 200 */ 201 @Override 202 public void setExtensionJs(String extensionJs) 203 { 204 delegate.setExtensionJs(extensionJs); 205 } 206 207 /** 208 * @since 5.1.0.0 209 */ 210 @Override 211 public void start(String optionsString) 212 { 213 delegate.start(optionsString); 214 } 215 216 /** 217 * @since 5.1.0.0 218 */ 219 @Override 220 public void start(Object optionsObject) 221 { 222 delegate.start(optionsObject); 223 } 224}