@Test public void testTransactionIdInvalid() { Transaction trx = new Transaction(null, 10D, "test", null); BeanPropertyBindingResult result = new BeanPropertyBindingResult(trx, "transaction"); ValidationUtils.invokeValidator(validator, trx, result); assertThat(result.hasErrors(), equalTo(true)); assertThat(result.getErrorCount(), equalTo(1)); }
public void testNew() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request = new MockHttpServletRequest("GET", "/workCodeForm.html"); MockHttpServletResponse response = new MockHttpServletResponse(); simpleDispatcherServlet.service(request, response); ModelAndView mv = child.handleRequest(request, response); Map m = mv.getModel(); log.debug("m = " + m); WorkCodeFormBacking backing = (WorkCodeFormBacking) m.get("workCodeFormBacking"); log.debug(Constants.toReflexString(backing)); MockPageContext ctx = new MockPageContext(); Iterator i = m.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); ctx.setAttribute(key, m.get(key)); } RequestContext r = new RequestContext(request, m); BindStatus b = r.getBindStatus("workCodeFormBacking.workCode.description"); log.debug("expression = " + b.getExpression()); log.debug("value = " + b.getValue()); String description = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.workCode.description}", String.class, ctx); // request.addParameter(name, description); String parttime = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.workCode.parttime}", String.class, ctx); String code = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.workCode.code}", String.class, ctx); String id = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.workCode.id}", String.class, ctx); String save = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.save}", String.class, ctx); log.debug("description = " + description); log.debug("parttime = " + parttime); log.debug("code = " + code); log.debug("id = " + id); log.debug("save = " + save); HashMap newMap = new HashMap(); newMap.put( r.getBindStatus("workCodeFormBacking.workCode.description").getExpression(), description); newMap.put(r.getBindStatus("workCodeFormBacking.workCode.parttime").getExpression(), parttime); newMap.put(r.getBindStatus("workCodeFormBacking.workCode.code").getExpression(), code); newMap.put(r.getBindStatus("workCodeFormBacking.workCode.id").getExpression(), id); newMap.put(r.getBindStatus("workCodeFormBacking.save").getExpression(), "save"); request = new MockHttpServletRequest("POST", "/workCodeForm.html"); i = newMap.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); log.debug("add parameter " + key + " " + newMap.get(key)); request.addParameter(key, (String) newMap.get(key)); } mv = child.handleRequest(request, response); m = mv.getModel(); log.debug("m = " + m); BeanPropertyBindingResult br = (BeanPropertyBindingResult) m.get("org.springframework.validation.BindingResult.workCodeFormBacking"); log.debug("field errors " + br.getFieldErrorCount()); log.debug("errors " + br.getErrorCount()); assertTrue(br.getFieldError("workCode.code") != null); assertTrue(br.getFieldError("workCode.description") != null); backing = (WorkCodeFormBacking) m.get("workCodeFormBacking"); log.debug(response.getErrorMessage()); log.debug(Constants.toReflexString(backing)); log.debug("view = " + mv.getViewName()); assertTrue("workCodeForm".equals(mv.getViewName())); }