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 */ 017 018 package org.apache.commons.configuration.web; 019 020 import static org.junit.Assert.assertEquals; 021 022 import java.util.HashMap; 023 import java.util.List; 024 import java.util.Map; 025 026 import javax.servlet.ServletRequest; 027 028 import org.apache.commons.configuration.AbstractConfiguration; 029 import org.apache.commons.configuration.BaseConfiguration; 030 import org.apache.commons.configuration.Configuration; 031 import org.apache.commons.configuration.ConfigurationMap; 032 import org.apache.commons.configuration.TestAbstractConfiguration; 033 import org.junit.Test; 034 035 import com.mockobjects.servlet.MockHttpServletRequest; 036 037 /** 038 * Test case for the {@link ServletRequestConfiguration} class. 039 * 040 * @author Emmanuel Bourg 041 * @version $Id: TestServletRequestConfiguration.java 1222465 2011-12-22 21:32:56Z oheger $ 042 */ 043 public class TestServletRequestConfiguration extends TestAbstractConfiguration 044 { 045 @Override 046 protected AbstractConfiguration getConfiguration() 047 { 048 final Configuration configuration = new BaseConfiguration(); 049 ((BaseConfiguration) configuration).setListDelimiter('\0'); 050 configuration.setProperty("key1", "value1"); 051 configuration.setProperty("key2", "value2"); 052 configuration.addProperty("list", "value1"); 053 configuration.addProperty("list", "value2"); 054 configuration.addProperty("listesc", "value1\\,value2"); 055 056 return createConfiguration(configuration); 057 } 058 059 @Override 060 protected AbstractConfiguration getEmptyConfiguration() 061 { 062 ServletRequest request = new MockHttpServletRequest() 063 { 064 @Override 065 public String getParameter(String key) 066 { 067 return null; 068 } 069 070 @Override 071 public Map<?, ?> getParameterMap() 072 { 073 return new HashMap<Object, Object>(); 074 } 075 }; 076 077 return new ServletRequestConfiguration(request); 078 } 079 080 /** 081 * Returns a new servlet request configuration that is backed by the passed 082 * in configuration. 083 * 084 * @param base the configuration with the underlying values 085 * @return the servlet request configuration 086 */ 087 private ServletRequestConfiguration createConfiguration(final Configuration base) 088 { 089 ServletRequest request = new MockHttpServletRequest() 090 { 091 @Override 092 public String[] getParameterValues(String key) 093 { 094 return base.getStringArray(key); 095 } 096 097 @Override 098 public Map<?, ?> getParameterMap() 099 { 100 return new ConfigurationMap(base); 101 } 102 }; 103 104 return new ServletRequestConfiguration(request); 105 } 106 107 @Override 108 @Test(expected = UnsupportedOperationException.class) 109 public void testAddPropertyDirect() 110 { 111 super.testAddPropertyDirect(); 112 } 113 114 @Override 115 @Test(expected = UnsupportedOperationException.class) 116 public void testClearProperty() 117 { 118 super.testClearProperty(); 119 } 120 121 /** 122 * Tests a list with elements that contain an escaped list delimiter. 123 */ 124 @Test 125 public void testListWithEscapedElements() 126 { 127 String[] values = { "test1", "test2\\,test3", "test4\\,test5" }; 128 String listKey = "test.list"; 129 130 BaseConfiguration config = new BaseConfiguration(); 131 config.setListDelimiter('\0'); 132 config.addProperty(listKey, values); 133 134 assertEquals("Wrong number of list elements", values.length, config.getList(listKey).size()); 135 136 Configuration c = createConfiguration(config); 137 List<?> v = c.getList(listKey); 138 139 assertEquals("Wrong number of elements in list", values.length, v.size()); 140 141 for (int i = 0; i < values.length; i++) 142 { 143 assertEquals("Wrong value at index " + i, values[i].replaceAll("\\\\", ""), v.get(i)); 144 } 145 } 146 }