⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandcontrollertests.java

📁 struts+spring 源码 希望能给大家带来帮助
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		BindException errors = (BindException)mav.getModel().get(ERRORS_KEY);
		assertEquals("There should be no errors", 0, errors.getErrorCount());
	}
	
	public void testWithCustomDateEditor() throws Exception {
		final DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		TestController tc = new TestController() {
			protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) {
				binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
			}
		};
		MockRenderRequest request = new MockRenderRequest();		
		MockRenderResponse response = new MockRenderResponse();
		String name = "test";
		int age = 30;
		request.addParameter("name", name);
		request.addParameter("age", "" + age);
		String dateString = "07-03-2006";
		Date expectedDate = dateFormat.parse(dateString);
		request.addParameter("date", dateString);
		ModelAndView mav = tc.handleRenderRequest(request, response);
		TestBean command = (TestBean)mav.getModel().get(tc.getCommandName());
		assertEquals(name, command.getName());
		assertEquals(age, command.getAge());
		assertEquals(expectedDate, command.getDate());
		BindException errors = (BindException)mav.getModel().get(ERRORS_KEY);
		assertEquals("There should be no errors", 0, errors.getErrorCount());
	}

	public void testWithCustomDateEditorEmptyNotAllowed() throws Exception {
		final DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		TestController tc = new TestController() {
			protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) {
				binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
			}
		};
		MockRenderRequest request = new MockRenderRequest();		
		MockRenderResponse response = new MockRenderResponse();
		String name = "test";
		int age = 30;
		request.addParameter("name", name);
		request.addParameter("age", "" + age);
		String emptyString = "";
		request.addParameter("date", emptyString);
		ModelAndView mav = tc.handleRenderRequest(request, response);
		TestBean command = (TestBean)mav.getModel().get(tc.getCommandName());
		assertEquals(name, command.getName());
		assertEquals(age, command.getAge());
		BindException errors = (BindException)mav.getModel().get(ERRORS_KEY);
		assertEquals("There should be 1 error", 1, errors.getErrorCount());		
		assertNotNull(errors.getFieldError("date"));
		assertEquals("typeMismatch", errors.getFieldError("date").getCode());
		assertEquals(emptyString, errors.getFieldError("date").getRejectedValue());
	}

	public void testWithCustomDateEditorEmptyAllowed() throws Exception {
		final DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		TestController tc = new TestController() {
			protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) {
				binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
			}
		};
		MockRenderRequest request = new MockRenderRequest();		
		MockRenderResponse response = new MockRenderResponse();
		String name = "test";
		int age = 30;
		request.addParameter("name", name);
		request.addParameter("age", "" + age);
		String dateString = "";
		request.addParameter("date", dateString);
		ModelAndView mav = tc.handleRenderRequest(request, response);
		TestBean command = (TestBean)mav.getModel().get(tc.getCommandName());
		assertEquals(name, command.getName());
		assertEquals(age, command.getAge());
		BindException errors = (BindException)mav.getModel().get(ERRORS_KEY);
		assertEquals("There should be 0 errors", 0, errors.getErrorCount());		
		assertNull("date should be null", command.getDate());
	}
	
	public void testNestedBindingWithPropertyEditor() throws Exception {
		TestController tc = new TestController() {
			protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) {
				binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
					public void setAsText(String text) throws IllegalArgumentException {
						setValue(new TestBean(text));
					}
				});
			}
		};
		MockRenderRequest request = new MockRenderRequest();		
		MockRenderResponse response = new MockRenderResponse();
		String name = "test";
		String spouseName = "testSpouse";
		int age = 30;
		int spouseAge = 31;
		request.addParameter("name", name);
		request.addParameter("age", "" + age);
		request.addParameter("spouse", spouseName);
		request.addParameter("spouse.age", "" + spouseAge);
		ModelAndView mav = tc.handleRenderRequest(request, response);
		TestBean command = (TestBean)mav.getModel().get(tc.getCommandName());
		assertEquals(name, command.getName());
		assertEquals(age, command.getAge());
		assertNotNull(command.getSpouse());
		assertEquals(spouseName, command.getSpouse().getName());
		assertEquals(spouseAge, command.getSpouse().getAge());
		BindException errors = (BindException)mav.getModel().get(ERRORS_KEY);
		assertEquals("There should be no errors", 0, errors.getErrorCount());
	}
	
	public void testWithValidatorNotSupportingCommandClass() throws Exception {
		Validator v = new Validator() {
			public boolean supports(Class c) {
				return false;
			}
			public void validate(Object o, Errors e) {}
		};
		TestController tc = new TestController();
		tc.setValidator(v);
		MockRenderRequest request = new MockRenderRequest();
		MockRenderResponse response = new MockRenderResponse();
		try {
			tc.handleRenderRequest(request, response);
			fail("Should have thrown IllegalArgumentException");
		}
		catch(IllegalArgumentException e) {
			// expected
		}
	}

	public void testWithValidatorAddingGlobalError() throws Exception {
		final String errorCode = "someCode";
		final String defaultMessage = "validation error!";
		TestController tc = new TestController();
		tc.setValidator(new Validator() {
			public boolean supports(Class c) {
				return TestBean.class.isAssignableFrom(c);
			}
			public void validate(Object o, Errors e) {
				e.reject(errorCode, defaultMessage);
			}
		});
		MockRenderRequest request = new MockRenderRequest();
		MockRenderResponse response = new MockRenderResponse();
		ModelAndView mav = tc.handleRenderRequest(request, response);
		BindException errors = (BindException)mav.getModel().get(ERRORS_KEY);
		assertEquals("There should be 1 error", 1, errors.getErrorCount());
		ObjectError error = errors.getGlobalError();
		assertEquals(error.getCode(), errorCode);
		assertEquals(error.getDefaultMessage(), defaultMessage);
	}

	public void testWithValidatorAndNullFieldError() throws Exception {
		final String errorCode = "someCode";
		final String defaultMessage = "validation error!";
		TestController tc = new TestController();
		tc.setValidator(new Validator() {
			public boolean supports(Class c) {
				return TestBean.class.isAssignableFrom(c);
			}
			public void validate(Object o, Errors e) {
				ValidationUtils.rejectIfEmpty(e, "name", errorCode, defaultMessage);
			}
		});
		MockRenderRequest request = new MockRenderRequest();
		int age = 32;
		request.setParameter("age", "" + age);
		MockRenderResponse response = new MockRenderResponse();
		ModelAndView mav = tc.handleRenderRequest(request, response);
		TestBean command = (TestBean)mav.getModel().get(tc.getCommandName());
		assertNull("name should be null", command.getName());
		assertEquals(age, command.getAge());
		BindException errors = (BindException)mav.getModel().get(ERRORS_KEY);
		assertEquals("There should be 1 error", 1, errors.getErrorCount());
		FieldError error = errors.getFieldError("name");
		assertEquals(error.getCode(), errorCode);
		assertEquals(error.getDefaultMessage(), defaultMessage);
	}

	public void testWithValidatorAndWhitespaceFieldError() throws Exception {
		final String errorCode = "someCode";
		final String defaultMessage = "validation error!";
		TestController tc = new TestController();
		tc.setValidator(new Validator() {
			public boolean supports(Class c) {
				return TestBean.class.isAssignableFrom(c);
			}
			public void validate(Object o, Errors e) {
				ValidationUtils.rejectIfEmptyOrWhitespace(e, "name", errorCode, defaultMessage);
			}
		});
		MockRenderRequest request = new MockRenderRequest();
		int age = 32;
		String whitespace = "  \t  ";
		request.setParameter("age", "" + age);
		request.setParameter("name", whitespace);
		MockRenderResponse response = new MockRenderResponse();
		ModelAndView mav = tc.handleRenderRequest(request, response);
		TestBean command = (TestBean)mav.getModel().get(tc.getCommandName());
		assertTrue(command.getName().equals(whitespace));
		assertEquals(age, command.getAge());
		BindException errors = (BindException)mav.getModel().get(ERRORS_KEY);
		assertEquals("There should be 1 error", 1, errors.getErrorCount());
		FieldError error = errors.getFieldError("name");
		assertEquals("rejected value should contain whitespace", whitespace, error.getRejectedValue());
		assertEquals(error.getCode(), errorCode);
		assertEquals(error.getDefaultMessage(), defaultMessage);
	}

	private static class TestController extends AbstractCommandController {

		private TestController() {
			super(TestBean.class, "testBean");
		}
		
		protected void handleAction(ActionRequest request, ActionResponse response, Object command, BindException errors) {
			((TestBean)command).setJedi(true);
		}

		protected ModelAndView handleRender(RenderRequest request, RenderResponse response, Object command, BindException errors) {
			assertNotNull(command);
			assertNotNull(errors);
			Map model = new HashMap();
			model.put(getCommandName(), command);
			model.put(ERRORS_KEY, errors);
			return new ModelAndView(request.getContextPath() + "-view", model);
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -