### Question:
MessageTagParser implements TagParser { @Override public byte[] replaceTagByContent(TaggedResponseWrapper response) throws UnsupportedEncodingException{ String encoding = response.getCharacterEncoding(); Locale locale = response.getLocale(); StringBuffer out = new StringBuffer(new String(response.getByteArray(), encoding)); Matcher matcher = MESSAGE_TAG_PATTERN.matcher(out); while (matcher.find()) { if (matcher.groupCount() == 1) { String key = matcher.group(1).trim(); out.replace(matcher.start(), matcher.end(), getStringByKey(key, locale)); matcher.reset(); } } return out.toString().getBytes(encoding); }  @Override byte[] replaceTagByContent(TaggedResponseWrapper response);  }### Answer:
@Test public void replaceTagByContentShouldReplaceContentByCorrectMessage() throws Exception { when(wrapper.getByteArray()).thenReturn("test <jcommune:message>code1</jcommune:message>".getBytes()); when(wrapper.getLocale()).thenReturn(Locale.ENGLISH); byte[] result = parser.replaceTagByContent(wrapper); assertEquals(new String(result), "test message1"); }
@Test public void replaceTagByContentShouldCorrectlyProcessCodesWithDots() throws Exception { when(wrapper.getByteArray()).thenReturn("test <jcommune:message>code.2</jcommune:message>".getBytes()); when(wrapper.getLocale()).thenReturn(Locale.ENGLISH); byte[] result = parser.replaceTagByContent(wrapper); assertEquals(new String(result), "test message2"); }
@Test public void replaceTagByContentShouldCorrectlyProcessCodesWithIllegalCharacters() throws Exception { String code = "code.2/&?"; when(wrapper.getByteArray()).thenReturn(("test <jcommune:message>" + code + "</jcommune:message>").getBytes()); when(wrapper.getLocale()).thenReturn(Locale.ENGLISH); byte[] result = parser.replaceTagByContent(wrapper); assertEquals(new String(result), "test " + code); }
@Test public void replaceTagByContentShouldUseEnglishAsDefaultIfTranslateNotFound() throws Exception { when(wrapper.getByteArray()).thenReturn("test <jcommune:message>code3</jcommune:message>".getBytes()); when(wrapper.getLocale()).thenReturn(Locale.forLanguageTag("ru")); byte[] result = parser.replaceTagByContent(wrapper); assertEquals(new String(result), "test message3"); }
@Test public void replaceTagByContentShouldUseCodeIfTranslateAnsDefaultsNotFound() throws Exception { when(wrapper.getByteArray()).thenReturn("test <jcommune:message>code.000</jcommune:message>".getBytes()); when(wrapper.getLocale()).thenReturn(Locale.ENGLISH); byte[] result = parser.replaceTagByContent(wrapper); assertEquals(new String(result), "test code.000"); }
@Test public void testParseShouldNotModifyIncomingBufferAndReturnFalseIfNoMatchesFound() throws Exception { String response = "test string"; when(wrapper.getByteArray()).thenReturn(response.getBytes()); when(wrapper.getLocale()).thenReturn(Locale.ENGLISH); byte[] result = parser.replaceTagByContent(wrapper); assertEquals(new String(result), response); }