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