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 java.util.Enumeration;
021: import java.util.List;
022:
023: import javax.servlet.ServletRequest;
024:
025: import com.mockobjects.servlet.MockHttpServletRequest;
026: import org.apache.commons.collections.iterators.IteratorEnumeration;
027: import org.apache.commons.configuration.AbstractConfiguration;
028: import org.apache.commons.configuration.BaseConfiguration;
029: import org.apache.commons.configuration.Configuration;
030: import org.apache.commons.configuration.TestAbstractConfiguration;
031: import org.apache.commons.lang.StringUtils;
032:
033: /**
034: * Test case for the {@link ServletRequestConfiguration} class.
035: *
036: * @author Emmanuel Bourg
037: * @version $Revision: 515306 $, $Date: 2007-03-06 22:15:00 +0100 (Di, 06 Mrz 2007) $
038: */
039: public class TestServletRequestConfiguration extends
040: TestAbstractConfiguration {
041: protected AbstractConfiguration getConfiguration() {
042: final Configuration configuration = new BaseConfiguration();
043: ((BaseConfiguration) configuration).setListDelimiter('\0');
044: configuration.setProperty("key1", "value1");
045: configuration.setProperty("key2", "value2");
046: configuration.addProperty("list", "value1");
047: configuration.addProperty("list", "value2");
048: configuration.addProperty("listesc", "value1\\,value2");
049:
050: return createConfiguration(configuration);
051: }
052:
053: protected AbstractConfiguration getEmptyConfiguration() {
054: final Configuration configuration = new BaseConfiguration();
055:
056: ServletRequest request = new MockHttpServletRequest() {
057: public String getParameter(String key) {
058: return null;
059: }
060:
061: public Enumeration getParameterNames() {
062: return new IteratorEnumeration(configuration.getKeys());
063: }
064: };
065:
066: return new ServletRequestConfiguration(request);
067: }
068:
069: /**
070: * Returns a new servlet request configuration that is backed by the passed
071: * in configuration.
072: *
073: * @param base the configuration with the underlying values
074: * @return the servlet request configuration
075: */
076: private ServletRequestConfiguration createConfiguration(
077: final Configuration base) {
078: ServletRequest request = new MockHttpServletRequest() {
079: public String[] getParameterValues(String key) {
080: return base.getStringArray(key);
081: }
082:
083: public Enumeration getParameterNames() {
084: return new IteratorEnumeration(base.getKeys());
085: }
086: };
087:
088: return new ServletRequestConfiguration(request);
089: }
090:
091: public void testAddPropertyDirect() {
092: try {
093: super .testAddPropertyDirect();
094: fail("addPropertyDirect should throw an UnsupportedException");
095: } catch (UnsupportedOperationException e) {
096: // ok
097: }
098: }
099:
100: public void testClearProperty() {
101: try {
102: super .testClearProperty();
103: fail("testClearProperty should throw an UnsupportedException");
104: } catch (UnsupportedOperationException e) {
105: // ok
106: }
107: }
108:
109: /**
110: * Tests a list with elements that contain an escaped list delimiter.
111: */
112: public void testListWithEscapedElements() {
113: String[] values = { "test1", "test2\\,test3", "test4\\,test5" };
114: final String listKey = "test.list";
115: BaseConfiguration config = new BaseConfiguration();
116: config.setListDelimiter('\0');
117: config.addProperty(listKey, values);
118: assertEquals("Wrong number of list elements", values.length,
119: config.getList(listKey).size());
120: Configuration c = createConfiguration(config);
121: List v = c.getList(listKey);
122: assertEquals("Wrong number of elements in list", values.length,
123: v.size());
124: for (int i = 0; i < values.length; i++) {
125: assertEquals("Wrong value at index " + i, StringUtils
126: .replace(values[i], "\\", StringUtils.EMPTY), v
127: .get(i));
128: }
129: }
130: }
|