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