1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.validator;
18  
19  import static org.easymock.EasyMock.createMock;
20  import static org.easymock.EasyMock.expect;
21  import static org.easymock.EasyMock.replay;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  import static org.seasar.cubby.CubbyConstants.ATTR_MESSAGES_RESOURCE_BUNDLE;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  import java.util.ResourceBundle;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.junit.After;
35  import org.junit.Before;
36  import org.junit.Test;
37  import org.seasar.cubby.action.Action;
38  import org.seasar.cubby.action.ActionErrors;
39  import org.seasar.cubby.controller.MessagesBehaviour;
40  import org.seasar.cubby.controller.impl.DefaultMessagesBehaviour;
41  import org.seasar.cubby.internal.controller.ThreadContext;
42  import org.seasar.cubby.mock.MockActionErrors;
43  import org.seasar.cubby.mock.MockContainerProvider;
44  import org.seasar.cubby.plugin.PluginRegistry;
45  import org.seasar.cubby.plugins.BinderPlugin;
46  import org.seasar.cubby.spi.ContainerProvider;
47  import org.seasar.cubby.spi.container.Container;
48  import org.seasar.cubby.validator.validators.ArrayMaxSizeValidator;
49  import org.seasar.cubby.validator.validators.RequiredValidator;
50  
51  public class FieldValidationRuleTest {
52  
53  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
54  
55  	private ActionErrors errors = new MockActionErrors();
56  
57  	@Before
58  	public void setup() {
59  		final BinderPlugin binderPlugin = new BinderPlugin();
60  		binderPlugin.bind(ContainerProvider.class).toInstance(
61  				new MockContainerProvider(new Container() {
62  
63  					public <T> T lookup(Class<T> type) {
64  						if (type.equals(MessagesBehaviour.class)) {
65  							return type.cast(new DefaultMessagesBehaviour());
66  						}
67  						return null;
68  					}
69  
70  				}));
71  		pluginRegistry.register(binderPlugin);
72  	}
73  
74  	@After
75  	public void teardown() {
76  		pluginRegistry.clear();
77  	}
78  
79  	@Test
80  	public void apply1() throws Exception {
81  		final HttpServletRequest request = createMock(HttpServletRequest.class);
82  		final HttpServletResponse response = createMock(HttpServletResponse.class);
83  		replay(request, response);
84  
85  		ThreadContext.enter(request, response);
86  		try {
87  			Map<String, Object[]> params = new HashMap<String, Object[]>();
88  			params.put("name", new Object[]{"aa"});
89  
90  			ValidationRule rule = new FieldValidationRule("name",
91  					new RequiredValidator(), new ArrayMaxSizeValidator(1));
92  			rule.apply(params, null, errors);
93  			assertTrue(errors.isEmpty());
94  		} finally {
95  			ThreadContext.exit();
96  		}
97  		ThreadContext.remove();
98  	}
99  
100 	@Test
101 	public void apply2() throws Exception {
102 		final HttpServletRequest request = createMock(HttpServletRequest.class);
103 		expect(request.getLocale()).andStubReturn(null);
104 		final ResourceBundle resourceBundle = new DefaultMessagesBehaviour()
105 				.getBundle(null);
106 		expect(request.getAttribute(ATTR_MESSAGES_RESOURCE_BUNDLE))
107 				.andStubReturn(resourceBundle);
108 		final HttpServletResponse response = createMock(HttpServletResponse.class);
109 		replay(request, response);
110 
111 		ThreadContext.enter(request, response);
112 		try {
113 			Map<String, Object[]> params = new HashMap<String, Object[]>();
114 			params.put("name", new Object[]{"aa", "bb"});
115 
116 			ValidationRule rule = new FieldValidationRule("name",
117 					new RequiredValidator(), new ArrayMaxSizeValidator(1));
118 			rule.apply(params, null, errors);
119 			assertFalse(errors.isEmpty());
120 			assertFalse(errors.getFields().get("name").isEmpty());
121 			assertTrue(errors.getIndexedFields().get("name").get(0).isEmpty());
122 		} finally {
123 			ThreadContext.exit();
124 		}
125 		ThreadContext.remove();
126 	}
127 
128 	@Test
129 	public void apply3() throws Exception {
130 		final HttpServletRequest request = createMock(HttpServletRequest.class);
131 		expect(request.getLocale()).andStubReturn(null);
132 		final ResourceBundle resourceBundle = new DefaultMessagesBehaviour()
133 				.getBundle(null);
134 		expect(request.getAttribute(ATTR_MESSAGES_RESOURCE_BUNDLE))
135 				.andStubReturn(resourceBundle);
136 		final HttpServletResponse response = createMock(HttpServletResponse.class);
137 		replay(request, response);
138 
139 		ThreadContext.enter(request, response);
140 		try {
141 			Map<String, Object[]> params = new HashMap<String, Object[]>();
142 
143 			ValidationRule rule = new FieldValidationRule("name",
144 					new RequiredValidator(), new ArrayMaxSizeValidator(1));
145 			rule.apply(params, null, errors);
146 			assertFalse(errors.isEmpty());
147 			assertFalse(errors.getFields().get("name").isEmpty());
148 			assertEquals(1, errors.getIndexedFields().get("name").get(0).size());
149 		} finally {
150 			ThreadContext.exit();
151 		}
152 		ThreadContext.remove();
153 	}
154 
155 	public static class MockAction extends Action {
156 		public String name;
157 	}
158 
159 }