1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.filter;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Enumeration;
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  
25  import org.seasar.cubby.CubbyConstants;
26  import org.seasar.cubby.action.Action;
27  import org.seasar.framework.mock.servlet.MockHttpServletRequestImpl;
28  import org.seasar.framework.mock.servlet.MockServletContextImpl;
29  
30  /**
31   * 
32   * @author agata
33   */
34  public class CubbyHttpServletRequestWrapperTest extends TestCase {
35  
36  	@Override
37  	protected void setUp() throws Exception {
38  		super.setUp();
39  	}
40  
41  	public void testGetAttributeNames() throws Exception {
42  		MockServletContextImpl context = new MockServletContextImpl("/cubby");
43  		MockHttpServletRequestImpl request = new MockHttpServletRequestImpl(
44  				context, "servlet");
45  		request.setAttribute("a1", "a1");
46  		Collection<String> origNames = toCollection(request.getAttributeNames());
47  		assertTrue("追加済みの属性", origNames.contains("a1"));
48  		assertFalse("存在しない属性", origNames.contains("a2"));
49  		assertFalse("ラップ後の追加の属性", origNames
50  				.contains(CubbyConstants.ATTR_CONTEXT_PATH));
51  		assertFalse("ラップ後の追加の属性", origNames
52  				.contains(CubbyConstants.ATTR_MESSAGES));
53  		assertFalse("ラップ後の追加の属性", origNames
54  				.contains(CubbyConstants.ATTR_ACTION));
55  
56  		CubbyHttpServletRequestWrapper wrapper = new CubbyHttpServletRequestWrapper(
57  				request);
58  		Action action = new HogeAction();
59  		wrapper.setAttribute(CubbyConstants.ATTR_ACTION, action);
60  
61  		Collection<String> wrappedNames = toCollection(wrapper
62  				.getAttributeNames());
63  		assertTrue("追加済みの属性", origNames.contains("a1"));
64  		assertFalse("存在しない属性", origNames.contains("a2"));
65  		assertTrue("ラップ後の追加の属性", wrappedNames
66  				.contains(CubbyConstants.ATTR_CONTEXT_PATH));
67  		assertTrue("ラップ後の追加の属性", wrappedNames
68  				.contains(CubbyConstants.ATTR_MESSAGES));
69  		assertTrue("ラップ後の追加の属性", wrappedNames
70  				.contains(CubbyConstants.ATTR_ACTION));
71  	}
72  
73  	private Collection<String> toCollection(Enumeration<?> attributeNames) {
74  		List<String> names = new ArrayList<String>();
75  		while (attributeNames.hasMoreElements()) {
76  			names.add((String) attributeNames.nextElement());
77  		}
78  		return names;
79  	}
80  
81  	class HogeAction extends Action {
82  	}
83  }