001    // Copyright 2007, 2009 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    
015    package org.apache.tapestry5.test;
016    
017    import 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     */
023    public 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(BORDER);
042            builder.append("\nSelenium failure processing command ");
043            builder.append(command);
044            builder.append("(");
045    
046            for (int i = 0; i < args.length; i++)
047            {
048                if (i > 0)
049                    builder.append(", ");
050                builder.append('"');
051                builder.append(args[i]);
052                builder.append('"');
053            }
054    
055            builder.append("): ");
056            builder.append(ex.toString());
057    
058            builder.append(BORDER);
059    
060            System.err.println(builder.toString());
061    
062            errorReporter.writeErrorReport();
063        }
064    
065        public String doCommand(String command, String[] args)
066        {
067            try
068            {
069                return delegate.doCommand(command, args);
070            }
071            catch (RuntimeException ex)
072            {
073                reportError(command, args, ex);
074                throw ex;
075            }
076        }
077    
078        public boolean getBoolean(String string, String[] strings)
079        {
080            try
081            {
082                return delegate.getBoolean(string, strings);
083            }
084            catch (RuntimeException ex)
085            {
086                reportError(string, strings, ex);
087                throw ex;
088            }
089        }
090    
091        public boolean[] getBooleanArray(String string, String[] strings)
092        {
093            try
094            {
095                return delegate.getBooleanArray(string, strings);
096            }
097            catch (RuntimeException ex)
098            {
099                reportError(string, strings, ex);
100                throw ex;
101            }
102        }
103    
104        public Number getNumber(String string, String[] strings)
105        {
106            try
107            {
108                return delegate.getNumber(string, strings);
109            }
110            catch (RuntimeException ex)
111            {
112                reportError(string, strings, ex);
113                throw ex;
114            }
115        }
116    
117        public Number[] getNumberArray(String string, String[] strings)
118        {
119            try
120            {
121                return delegate.getNumberArray(string, strings);
122            }
123            catch (RuntimeException ex)
124            {
125                reportError(string, strings, ex);
126                throw ex;
127            }
128        }
129    
130        public String getString(String string, String[] strings)
131        {
132            try
133            {
134                return delegate.getString(string, strings);
135            }
136            catch (RuntimeException ex)
137            {
138                reportError(string, strings, ex);
139                throw ex;
140            }
141        }
142    
143        public String[] getStringArray(String string, String[] strings)
144        {
145            try
146            {
147                return delegate.getStringArray(string, strings);
148            }
149            catch (RuntimeException ex)
150            {
151                reportError(string, strings, ex);
152                throw ex;
153            }
154        }
155    
156        public void start()
157        {
158            delegate.start();
159        }
160    
161        public void stop()
162        {
163            delegate.stop();
164        }
165    
166        /**
167         * @since 5.1.0.0
168         */
169        public String getRemoteControlServerLocation()
170        {
171            return delegate.getRemoteControlServerLocation();
172        }
173    
174        /**
175         * @since 5.1.0.0
176         */
177        public void setExtensionJs(String extensionJs)
178        {
179            delegate.setExtensionJs(extensionJs);
180        }
181    
182        /**
183         * @since 5.1.0.0
184         */
185        public void start(String optionsString)
186        {
187            delegate.start(optionsString);
188        }
189    
190        /**
191         * @since 5.1.0.0
192         */
193        public void start(Object optionsObject)
194        {
195            delegate.start(optionsObject);
196        }
197    }