Coverage Report - org.apache.tapestry5.internal.test.TestableRequestImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
TestableRequestImpl
64%
37/58
62%
10/16
0
 
 1  
 // Copyright 2007, 2008, 2009 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.internal.test;
 16  
 
 17  
 import org.apache.tapestry5.ioc.annotations.Inject;
 18  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 19  
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 20  
 import org.apache.tapestry5.services.Session;
 21  
 
 22  
 import java.util.*;
 23  
 
 24  
 public class TestableRequestImpl implements TestableRequest
 25  
 {
 26  
     private final String contextPath;
 27  
 
 28  54
     private final Map<String, Object> parameters = CollectionFactory.newMap();
 29  
 
 30  54
     private final Map<String, Object> attributes = CollectionFactory.newMap();
 31  
 
 32  
     private Session session;
 33  
 
 34  54
     private String path = "/";
 35  
 
 36  54
     private Locale locale = Locale.getDefault();
 37  
 
 38  
     @Inject
 39  
     public TestableRequestImpl()
 40  
     {
 41  46
         this("/foo");
 42  46
     }
 43  
 
 44  
     public TestableRequestImpl(String contextPath)
 45  54
     {
 46  54
         this.contextPath = contextPath;
 47  54
     }
 48  
 
 49  
     public TestableRequest clear()
 50  
     {
 51  72
         parameters.clear();
 52  
 
 53  72
         return this;
 54  
     }
 55  
 
 56  
     public TestableRequest setPath(String path)
 57  
     {
 58  72
         this.path = path;
 59  
 
 60  72
         return this;
 61  
     }
 62  
 
 63  
     public TestableRequest setLocale(Locale locale)
 64  
     {
 65  52
         this.locale = locale;
 66  
 
 67  52
         return this;
 68  
     }
 69  
 
 70  
     public TestableRequest loadParameter(String parameterName, String parameterValue)
 71  
     {
 72  8
         Object existing = parameters.get(parameterName);
 73  
 
 74  8
         if (existing == null)
 75  
         {
 76  8
             parameters.put(parameterName, parameterValue);
 77  8
             return this;
 78  
         }
 79  
 
 80  0
         if (existing instanceof List)
 81  
         {
 82  0
             ((List) existing).add(parameterValue);
 83  0
             return this;
 84  
         }
 85  
 
 86  
         // Convert from a single String to a List of Strings.
 87  
 
 88  0
         List list = new ArrayList();
 89  0
         list.add(existing);
 90  0
         list.add(parameterValue);
 91  
 
 92  0
         parameters.put(parameterName, list);
 93  
 
 94  0
         return this;
 95  
     }
 96  
 
 97  
     public TestableRequest overrideParameter(String parameterName, String parameterValue)
 98  
     {
 99  10
         parameters.put(parameterName, parameterValue);
 100  
 
 101  10
         return this;
 102  
     }
 103  
 
 104  
     public long getDateHeader(String name)
 105  
     {
 106  0
         return 0;
 107  
     }
 108  
 
 109  
     /**
 110  
      * Returns null.
 111  
      */
 112  
     public String getHeader(String name)
 113  
     {
 114  0
         return null;
 115  
     }
 116  
 
 117  
     /**
 118  
      * Returns an empty list.
 119  
      */
 120  
     public List<String> getHeaderNames()
 121  
     {
 122  0
         return Collections.emptyList();
 123  
     }
 124  
 
 125  
     public Locale getLocale()
 126  
     {
 127  72
         return locale;
 128  
     }
 129  
 
 130  
     public List<String> getParameterNames()
 131  
     {
 132  0
         return InternalUtils.sortedKeys(parameters);
 133  
     }
 134  
 
 135  
     public String[] getParameters(String name)
 136  
     {
 137  8
         Object value = parameters.get(name);
 138  
 
 139  8
         if (value == null) return null;
 140  
 
 141  8
         if (value instanceof String)
 142  8
             return new String[] { (String) value };
 143  
 
 144  0
         List list = (List) value;
 145  
 
 146  0
         return (String[]) list.toArray(new String[list.size()]);
 147  
     }
 148  
 
 149  
     public String getPath()
 150  
     {
 151  422
         return path;
 152  
     }
 153  
 
 154  
     public String getContextPath()
 155  
     {
 156  334
         return contextPath;
 157  
     }
 158  
 
 159  
     public String getParameter(String name)
 160  
     {
 161  60
         Object value = parameters.get(name);
 162  
 
 163  60
         if (value == null || value instanceof String) return (String) value;
 164  
 
 165  0
         List<String> list = (List<String>) value;
 166  
 
 167  0
         return list.get(0);
 168  
     }
 169  
 
 170  
     public Session getSession(boolean create)
 171  
     {
 172  118
         if (!create) return session;
 173  
 
 174  62
         if (session == null) session = new PageTesterSession();
 175  
 
 176  62
         return session;
 177  
     }
 178  
 
 179  
     public void setEncoding(String requestEncoding)
 180  
     {
 181  0
     }
 182  
 
 183  
     /**
 184  
      * Always returns false. If you need to test Ajax functionality, you need to be using Selenium.
 185  
      */
 186  
     public boolean isXHR()
 187  
     {
 188  58
         return false;
 189  
     }
 190  
 
 191  
     public boolean isSecure()
 192  
     {
 193  146
         return false;
 194  
     }
 195  
 
 196  
     /**
 197  
      * Always returns true.
 198  
      */
 199  
     public boolean isRequestedSessionIdValid()
 200  
     {
 201  0
         return true;
 202  
     }
 203  
 
 204  
     public Object getAttribute(String name)
 205  
     {
 206  60
         return attributes.get(name);
 207  
     }
 208  
 
 209  
     public void setAttribute(String name, Object value)
 210  
     {
 211  0
         attributes.put(name, value);
 212  0
     }
 213  
 
 214  
     /**
 215  
      * Returns "localhost" which is sufficient for testing purposes.
 216  
      */
 217  
     public String getServerName()
 218  
     {
 219  0
         return "localhost";
 220  
     }
 221  
 
 222  
     /**
 223  
      * Always returns POST, to keep the Form component happy.
 224  
      */
 225  
     public String getMethod()
 226  
     {
 227  8
         return "POST";
 228  
     }
 229  
 }