TransactionTokenTag extends AbstractHtmlElementTag { @Override protected int writeTagContent( final TagWriter tagWriter) throws JspException { HttpServletRequest request = (HttpServletRequest) pageContext .getRequest(); TransactionToken nextToken = (TransactionToken) request.getAttribute( TransactionTokenInterceptor.NEXT_TOKEN_REQUEST_ATTRIBUTE_NAME); if (nextToken != null) { tagWriter.startTag("input"); tagWriter.writeAttribute("type", "hidden"); tagWriter.writeAttribute("name", TransactionTokenInterceptor.TOKEN_REQUEST_PARAMETER); tagWriter.writeAttribute("value", nextToken.getTokenString()); tagWriter.endTag(); } return SKIP_BODY; }    }
@Test public void testWriteTagContentTagWriter01() { TransactionTokenTag tag = new TransactionTokenTag(); PageContext pageContext = mock(PageContext.class); tag.setPageContext(pageContext); HttpServletRequest request = mock(HttpServletRequest.class); StringWriter sw = new StringWriter(); TagWriter tagWriter = new TagWriter(sw); when((HttpServletRequest) pageContext.getRequest()).thenReturn(request); when((TransactionToken) request.getAttribute( TransactionTokenInterceptor.NEXT_TOKEN_REQUEST_ATTRIBUTE_NAME)) .thenReturn(null); int result = 1; try { result = tag.writeTagContent(tagWriter); } catch (JspException e) { fail(); } assertThat(sw.getBuffer().toString(), is("")); assertThat(result, is(0)); }
@Test public void testWriteTagContentTagWriter02() { TransactionTokenTag tag = new TransactionTokenTag(); PageContext pageContext = mock(PageContext.class); tag.setPageContext(pageContext); HttpServletRequest request = mock(HttpServletRequest.class); TransactionToken token = new TransactionToken("tokenName", "tokenkey", "tokenValue"); StringWriter sw = new StringWriter(); TagWriter tagWriter = new TagWriter(sw); when((HttpServletRequest) pageContext.getRequest()).thenReturn(request); when((TransactionToken) request.getAttribute( TransactionTokenInterceptor.NEXT_TOKEN_REQUEST_ATTRIBUTE_NAME)) .thenReturn(token); int result = 1; try { result = tag.writeTagContent(tagWriter); } catch (JspException e) { fail(); } String expected = "<input type=\"hidden\" name=\"" + TransactionTokenInterceptor.TOKEN_REQUEST_PARAMETER + "\" value=\"" + token.getTokenString() + "\"/>"; assertThat(sw.getBuffer().toString(), is(expected)); assertThat(result, is(0)); }
@Test public void testWriteTagContentTagWriter03() { TransactionTokenTag tag = new TransactionTokenTag(); PageContext pageContext = mock(PageContext.class); tag.setPageContext(pageContext); HttpServletRequest request = mock(HttpServletRequest.class); TransactionToken token = new TransactionToken("tokenName", "tokenkey", "tokenValue"); TagWriter tagWriter = mock(TagWriter.class); when((HttpServletRequest) pageContext.getRequest()).thenReturn(request); when((TransactionToken) request.getAttribute( TransactionTokenInterceptor.NEXT_TOKEN_REQUEST_ATTRIBUTE_NAME)) .thenReturn(token); int result = 1; try { doThrow(new JspException()).when(tagWriter).startTag(anyString()); result = tag.writeTagContent(tagWriter); } catch (JspException e) { e.printStackTrace(); } assertThat(result, is(1)); }