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 public String doCommand(String command, String[] args) 087 { 088 try 089 { 090 return delegate.doCommand(command, args); 091 } catch (RuntimeException ex) 092 { 093 reportError(command, args, ex); 094 throw ex; 095 } 096 } 097 098 public boolean getBoolean(String string, String[] strings) 099 { 100 try 101 { 102 return delegate.getBoolean(string, strings); 103 } catch (RuntimeException ex) 104 { 105 reportError(string, strings, ex); 106 throw ex; 107 } 108 } 109 110 public boolean[] getBooleanArray(String string, String[] strings) 111 { 112 try 113 { 114 return delegate.getBooleanArray(string, strings); 115 } catch (RuntimeException ex) 116 { 117 reportError(string, strings, ex); 118 throw ex; 119 } 120 } 121 122 public Number getNumber(String string, String[] strings) 123 { 124 try 125 { 126 return delegate.getNumber(string, strings); 127 } catch (RuntimeException ex) 128 { 129 reportError(string, strings, ex); 130 throw ex; 131 } 132 } 133 134 public Number[] getNumberArray(String string, String[] strings) 135 { 136 try 137 { 138 return delegate.getNumberArray(string, strings); 139 } catch (RuntimeException ex) 140 { 141 reportError(string, strings, ex); 142 throw ex; 143 } 144 } 145 146 public String getString(String string, String[] strings) 147 { 148 try 149 { 150 return delegate.getString(string, strings); 151 } catch (RuntimeException ex) 152 { 153 reportError(string, strings, ex); 154 throw ex; 155 } 156 } 157 158 public String[] getStringArray(String string, String[] strings) 159 { 160 try 161 { 162 return delegate.getStringArray(string, strings); 163 } catch (RuntimeException ex) 164 { 165 reportError(string, strings, ex); 166 throw ex; 167 } 168 } 169 170 public void start() 171 { 172 delegate.start(); 173 } 174 175 public void stop() 176 { 177 delegate.stop(); 178 } 179 180 /** 181 * @since 5.1.0.0 182 */ 183 public String getRemoteControlServerLocation() 184 { 185 return delegate.getRemoteControlServerLocation(); 186 } 187 188 /** 189 * @since 5.1.0.0 190 */ 191 public void setExtensionJs(String extensionJs) 192 { 193 delegate.setExtensionJs(extensionJs); 194 } 195 196 /** 197 * @since 5.1.0.0 198 */ 199 public void start(String optionsString) 200 { 201 delegate.start(optionsString); 202 } 203 204 /** 205 * @since 5.1.0.0 206 */ 207 public void start(Object optionsObject) 208 { 209 delegate.start(optionsObject); 210 } 211}