1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration.web;
19
20 import java.util.Enumeration;
21 import java.util.List;
22
23 import javax.servlet.ServletRequest;
24
25 import com.mockobjects.servlet.MockHttpServletRequest;
26 import org.apache.commons.collections.iterators.IteratorEnumeration;
27 import org.apache.commons.configuration.AbstractConfiguration;
28 import org.apache.commons.configuration.BaseConfiguration;
29 import org.apache.commons.configuration.Configuration;
30 import org.apache.commons.configuration.TestAbstractConfiguration;
31 import org.apache.commons.lang.StringUtils;
32
33 /***
34 * Test case for the {@link ServletRequestConfiguration} class.
35 *
36 * @author Emmanuel Bourg
37 * @version $Revision: 515306 $, $Date: 2007-03-06 22:15:00 +0100 (Di, 06 Mrz 2007) $
38 */
39 public class TestServletRequestConfiguration extends TestAbstractConfiguration
40 {
41 protected AbstractConfiguration getConfiguration()
42 {
43 final Configuration configuration = new BaseConfiguration();
44 ((BaseConfiguration) configuration).setListDelimiter('\0');
45 configuration.setProperty("key1", "value1");
46 configuration.setProperty("key2", "value2");
47 configuration.addProperty("list", "value1");
48 configuration.addProperty("list", "value2");
49 configuration.addProperty("listesc", "value1//,value2");
50
51 return createConfiguration(configuration);
52 }
53
54 protected AbstractConfiguration getEmptyConfiguration()
55 {
56 final Configuration configuration = new BaseConfiguration();
57
58 ServletRequest request = new MockHttpServletRequest()
59 {
60 public String getParameter(String key)
61 {
62 return null;
63 }
64
65 public Enumeration getParameterNames()
66 {
67 return new IteratorEnumeration(configuration.getKeys());
68 }
69 };
70
71 return new ServletRequestConfiguration(request);
72 }
73
74 /***
75 * Returns a new servlet request configuration that is backed by the passed
76 * in configuration.
77 *
78 * @param base the configuration with the underlying values
79 * @return the servlet request configuration
80 */
81 private ServletRequestConfiguration createConfiguration(
82 final Configuration base)
83 {
84 ServletRequest request = new MockHttpServletRequest()
85 {
86 public String[] getParameterValues(String key)
87 {
88 return base.getStringArray(key);
89 }
90
91 public Enumeration getParameterNames()
92 {
93 return new IteratorEnumeration(base.getKeys());
94 }
95 };
96
97 return new ServletRequestConfiguration(request);
98 }
99
100 public void testAddPropertyDirect()
101 {
102 try
103 {
104 super.testAddPropertyDirect();
105 fail("addPropertyDirect should throw an UnsupportedException");
106 }
107 catch (UnsupportedOperationException e)
108 {
109
110 }
111 }
112
113 public void testClearProperty()
114 {
115 try
116 {
117 super.testClearProperty();
118 fail("testClearProperty should throw an UnsupportedException");
119 }
120 catch (UnsupportedOperationException e)
121 {
122
123 }
124 }
125
126 /***
127 * Tests a list with elements that contain an escaped list delimiter.
128 */
129 public void testListWithEscapedElements()
130 {
131 String[] values =
132 { "test1", "test2//,test3", "test4//,test5" };
133 final String listKey = "test.list";
134 BaseConfiguration config = new BaseConfiguration();
135 config.setListDelimiter('\0');
136 config.addProperty(listKey, values);
137 assertEquals("Wrong number of list elements", values.length, config
138 .getList(listKey).size());
139 Configuration c = createConfiguration(config);
140 List v = c.getList(listKey);
141 assertEquals("Wrong number of elements in list", values.length, v
142 .size());
143 for (int i = 0; i < values.length; i++)
144 {
145 assertEquals("Wrong value at index " + i, StringUtils.replace(
146 values[i], "//", StringUtils.EMPTY), v.get(i));
147 }
148 }
149 }