001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.mock; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024import java.util.Set; 025 026import org.apache.wicket.request.IRequestParameters; 027import org.apache.wicket.request.IWritableRequestParameters; 028import org.apache.wicket.util.string.StringValue; 029 030/** 031 * Mutable mock implementation of {@link IRequestParameters}. 032 * 033 * @author Matej Knopp 034 */ 035public class MockRequestParameters implements IWritableRequestParameters 036{ 037 private final Map<String, List<StringValue>> parameters = new HashMap<String, List<StringValue>>(); 038 039 @Override 040 public Set<String> getParameterNames() 041 { 042 return Collections.unmodifiableSet(parameters.keySet()); 043 } 044 045 @Override 046 public StringValue getParameterValue(String name) 047 { 048 List<StringValue> values = parameters.get(name); 049 return (values != null && !values.isEmpty()) ? values.get(0) 050 : StringValue.valueOf((String)null); 051 } 052 053 @Override 054 public List<StringValue> getParameterValues(String name) 055 { 056 List<StringValue> values = parameters.get(name); 057 return values != null ? Collections.unmodifiableList(values) : null; 058 } 059 060 @Override 061 public void setParameterValues(String name, List<StringValue> values) 062 { 063 parameters.put(name, values); 064 } 065 066 067 /** 068 * Sets value for given key. 069 * 070 * @param name 071 * @param value 072 */ 073 public void setParameterValue(String name, String value) 074 { 075 List<StringValue> list = new ArrayList<StringValue>(1); 076 list.add(StringValue.valueOf(value)); 077 parameters.put(name, list); 078 } 079 080 /** 081 * Adds value for given key. 082 * 083 * @param name 084 * @param value 085 */ 086 public void addParameterValue(String name, String value) 087 { 088 List<StringValue> list = parameters.get(name); 089 if (list == null) 090 { 091 list = new ArrayList<StringValue>(1); 092 parameters.put(name, list); 093 } 094 list.add(StringValue.valueOf(value)); 095 } 096 097 @Override 098 public void reset() 099 { 100 parameters.clear(); 101 } 102 103}