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

📄 commandcontrollertests.java

📁 spring的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			}
		};
		HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
		MockHttpServletResponse response = new MockHttpServletResponse();
		mc.handleRequest(request, response);
		assertTrue("Correct expires header", response.getHeader("Expires").equals(new Long(1)));
		List cacheControl = response.getHeaders("Cache-Control");
		assertTrue("Correct cache control", cacheControl.contains("no-cache"));
		assertTrue("Correct cache control", cacheControl.contains("no-store"));
	}

	public void testCachingWithCustomApplyCacheSecondsCall3() throws Exception {
		TestController mc = new TestController() {
			protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
				applyCacheSeconds(response, -1);
				return super.handle(request, response, command, errors);
			}
		};
		HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
		MockHttpServletResponse response = new MockHttpServletResponse();
		mc.handleRequest(request, response);
		assertTrue("No expires header", response.getHeader("Expires") == null);
		assertTrue("No cache control", response.getHeader("Cache-Control") == null);
	}

	public void testCustomDateEditorWithAllowEmpty() throws Exception {
		final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN);
		TestController mc = new TestController() {
			protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
				binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
			}
		};

		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("date", "1.5.2003");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);
		TestBean tb = (TestBean) mv.getModel().get("command");
		Errors errors = (Errors) mv.getModel().get("errors");
		assertTrue("No field error", !errors.hasFieldErrors("date"));
		assertTrue("Correct date property", df.parse("1.5.2003").equals(tb.getDate()));
		assertTrue("Correct date value", "01.05.2003".equals(errors.getFieldValue("date")));

		request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("date", "");
		response = new MockHttpServletResponse();
		mv = mc.handleRequest(request, response);
		tb = (TestBean) mv.getModel().get("command");
		errors = (Errors) mv.getModel().get("errors");
		assertTrue("No field error", !errors.hasFieldErrors("date"));
		assertTrue("Correct date property", tb.getDate() == null);
		assertTrue("Correct date value", "".equals(errors.getFieldValue("date")));
	}

	public void testCustomDateEditorWithoutAllowEmpty() throws Exception {
		final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN);
		TestController mc = new TestController() {
			protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
				binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
			}
		};

		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("date", "1.5.2003");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);
		TestBean tb = (TestBean) mv.getModel().get("command");
		Errors errors = (Errors) mv.getModel().get("errors");
		assertTrue("No field error", !errors.hasFieldErrors("date"));
		assertTrue("Correct date property", df.parse("1.5.2003").equals(tb.getDate()));
		assertTrue("Correct date value", "01.05.2003".equals(errors.getFieldValue("date")));

		request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("date", "");
		response = new MockHttpServletResponse();
		mv = mc.handleRequest(request, response);
		tb = (TestBean) mv.getModel().get("command");
		errors = (Errors) mv.getModel().get("errors");
		assertTrue("Has field error", errors.hasFieldErrors("date"));
		assertTrue("Correct date property", tb.getDate() != null);
		assertTrue("Correct date value", errors.getFieldValue("date") != null);
	}

	public void testCustomNumberEditorWithAllowEmpty() throws Exception {
		final NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);

		TestController mc = new TestController() {
			protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
				binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
			}
		};

		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("myFloat", "5,1");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);
		TestBean tb = (TestBean) mv.getModel().get("command");
		Errors errors = (Errors) mv.getModel().get("errors");
		assertTrue("No field error", !errors.hasFieldErrors("myFloat"));
		assertTrue("Correct float property", (new Float(5.1)).equals(tb.getMyFloat()));
		assertTrue("Correct float value", "5,1".equals(errors.getFieldValue("myFloat")));

		request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("myFloat", "");
		response = new MockHttpServletResponse();
		mv = mc.handleRequest(request, response);
		tb = (TestBean) mv.getModel().get("command");
		errors = (Errors) mv.getModel().get("errors");
		assertTrue("No field error", !errors.hasFieldErrors("myFloat"));
		assertTrue("Correct float property", tb.getMyFloat() == null);
		assertTrue("Correct float value", "".equals(errors.getFieldValue("myFloat")));
	}

	public void testCustomNumberEditorWithoutAllowEmpty() throws Exception {
		final NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);

		TestController mc = new TestController() {
			protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
				binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, false));
			}
		};

		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("myFloat", "5,1");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);
		TestBean tb = (TestBean) mv.getModel().get("command");
		Errors errors = (Errors) mv.getModel().get("errors");
		assertTrue("No field error", !errors.hasFieldErrors("myFloat"));
		assertTrue("Correct float property", (new Float(5.1)).equals(tb.getMyFloat()));
		assertTrue("Correct float value", "5,1".equals(errors.getFieldValue("myFloat")));

		request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("myFloat", "");
		response = new MockHttpServletResponse();
		mv = mc.handleRequest(request, response);
		tb = (TestBean) mv.getModel().get("command");
		errors = (Errors) mv.getModel().get("errors");
		assertTrue("Has field error", errors.hasFieldErrors("myFloat"));
		assertTrue("Correct float property", tb.getMyFloat() != null);
		assertTrue("Correct float value", errors.getFieldValue("myFloat") != null);
	}

	public void testResetEmptyStringField() throws Exception {
		TestController mc = new TestController();

		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("_name", "visible");
		request.addParameter("name", "test");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);
		TestBean tb = (TestBean) mv.getModel().get("command");
		Errors errors = (Errors) mv.getModel().get("errors");
		assertTrue("Correct name property", "test".equals(tb.getName()));
		assertTrue("Correct name value", "test".equals(errors.getFieldValue("name")));

		request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("_name", "visible");
		request.addParameter("_someNonExistingField", "visible");
		mv = mc.handleRequest(request, response);
		tb = (TestBean) mv.getModel().get("command");
		errors = (Errors) mv.getModel().get("errors");
		assertTrue("Correct name property", tb.getName() == null);
		assertTrue("Correct name value", errors.getFieldValue("name") == null);
	}

	public void testResetEmptyBooleanField() throws Exception {
		TestController mc = new TestController();

		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("_postProcessed", "visible");
		request.addParameter("postProcessed", "true");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);
		TestBean tb = (TestBean) mv.getModel().get("command");
		Errors errors = (Errors) mv.getModel().get("errors");
		assertTrue("Correct postProcessed property", tb.isPostProcessed());
		assertTrue("Correct postProcessed value", Boolean.TRUE.equals(errors.getFieldValue("postProcessed")));

		request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("_postProcessed", "visible");
		mv = mc.handleRequest(request, response);
		tb = (TestBean) mv.getModel().get("command");
		errors = (Errors) mv.getModel().get("errors");
		assertTrue("Correct postProcessed property", !tb.isPostProcessed());
		assertTrue("Correct postProcessed value", Boolean.FALSE.equals(errors.getFieldValue("postProcessed")));
	}

	public void testResetEmptyStringArrayField() throws Exception {
		TestController mc = new TestController();

		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("_stringArray", "visible");
		request.addParameter("stringArray", "value1");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);
		TestBean tb = (TestBean) mv.getModel().get("command");
		assertTrue("Correct stringArray property",
							 tb.getStringArray() != null && "value1".equals(tb.getStringArray()[0]));

		request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("_stringArray", "visible");
		mv = mc.handleRequest(request, response);
		tb = (TestBean) mv.getModel().get("command");
		assertTrue("Correct stringArray property", tb.getStringArray() != null && tb.getStringArray().length == 0);
	}

	public void testResetEmptyFieldsTurnedOff() throws Exception {
		TestController mc = new TestController() {
			protected Object getCommand(HttpServletRequest request) throws Exception {
				return new TestBean("original", 99);
			}
			protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
				binder.setFieldMarkerPrefix(null);
			}
		};

		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("_name", "visible");
		request.addParameter("name", "test");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);
		TestBean tb = (TestBean) mv.getModel().get("command");
		Errors errors = (Errors) mv.getModel().get("errors");
		assertTrue("Correct name property", "test".equals(tb.getName()));
		assertTrue("Correct name value", "test".equals(errors.getFieldValue("name")));

		request = new MockHttpServletRequest("GET", "/welcome.html");
		request.addParameter("_name", "true");
		mv = mc.handleRequest(request, response);
		tb = (TestBean) mv.getModel().get("command");
		errors = (Errors) mv.getModel().get("errors");
		assertTrue("Correct name property", "original".equals(tb.getName()));
		assertTrue("Correct name value", "original".equals(errors.getFieldValue("name")));
	}


	private static class TestController extends AbstractCommandController {
		
		private TestController() {
			super(TestBean.class, "person");
		}
		
		protected ModelAndView handle(HttpServletRequest request,	HttpServletResponse response,	Object command,	BindException errors) {
			Map m = new HashMap();
			assertTrue("Command not null", command != null);
			assertTrue("errors not null", errors != null);
			m.put("errors", errors);
			m.put("command", command);
			return new ModelAndView(request.getServletPath(), m);
		}
	}

}

⌨️ 快捷键说明

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