@Test public void testFormatChildParent() throws Exception { final int firstDetailsNumber = 23; final int secondDetailsNumber = 478; when(random.nextInt()).thenReturn(Integer.valueOf(firstDetailsNumber), Integer.valueOf(secondDetailsNumber)); final AbstractParentRecordStructureMember childParent = mock(AbstractParentRecordStructureMember.class); final String childParentFormatted = "i am the formatted child of a parent"; final String childParentName = "child_parent_name"; when(childParent.getName()).thenReturn(childParentName); final AbstractParentRecordStructureMember parent = mock(AbstractParentRecordStructureMember.class); final String parentName = "parent"; final String parentFormatted = "i am the formatted parent"; when(parent.getChildMemberCount()).thenReturn(Integer.valueOf(1)); when(parent.getChildMember(0)).thenReturn(childParent); when(parent.getName()).thenReturn(parentName); final StringWriter firstWriter = mock(StringWriter.class); when(firstWriter.toString()).thenReturn(parentFormatted); final StringWriter secondWriter = mock(StringWriter.class); when(secondWriter.toString()).thenReturn(childParentFormatted); whenNew(StringWriter.class).withNoArguments().thenReturn(firstWriter, secondWriter); final VelocityContext firstContext = mock(VelocityContext.class); final VelocityContext secondContext = mock(VelocityContext.class); whenNew(VelocityContext.class).withNoArguments().thenReturn(firstContext, secondContext); assertThat(formatter.format(parent)).isEqualTo(parentFormatted + childParentFormatted); verify(firstContext).put("member", parent); verify(firstContext).put("detailsDivName", parentName + firstDetailsNumber); verify(template).merge(firstContext, firstWriter); verify(secondContext).put("member", childParent); verify(secondContext).put("detailsDivName", childParentName + secondDetailsNumber); verify(template).merge(secondContext, secondWriter); }
@Override public String format(final AbstractParentRecordStructureMember member) throws MavenReportException { final VelocityContext context = new VelocityContext(); context.put("member", member); context.put("detailsDivName", member.getName() + random.nextInt()); final StringWriter writer = new StringWriter(); try { template.merge(context, writer); } catch (final Exception e) { throw new MavenReportException("Failed to merge template.", e); } final StringBuilder listBuilder = new StringBuilder(writer.toString()); for (int i = 0, size = member.getChildMemberCount(); i < size; i++) { final RecordStructureMember childMember = member.getChildMember(i); if (childMember instanceof RecordStructureField) { listBuilder.append(fieldFormatter.format((RecordStructureField) childMember)); } else { listBuilder.append(format((AbstractParentRecordStructureMember) childMember)); } } return listBuilder.toString(); }
ParentalMemberFormatter implements MemberFormatter<AbstractParentRecordStructureMember> { @Override public String format(final AbstractParentRecordStructureMember member) throws MavenReportException { final VelocityContext context = new VelocityContext(); context.put("member", member); context.put("detailsDivName", member.getName() + random.nextInt()); final StringWriter writer = new StringWriter(); try { template.merge(context, writer); } catch (final Exception e) { throw new MavenReportException("Failed to merge template.", e); } final StringBuilder listBuilder = new StringBuilder(writer.toString()); for (int i = 0, size = member.getChildMemberCount(); i < size; i++) { final RecordStructureMember childMember = member.getChildMember(i); if (childMember instanceof RecordStructureField) { listBuilder.append(fieldFormatter.format((RecordStructureField) childMember)); } else { listBuilder.append(format((AbstractParentRecordStructureMember) childMember)); } } return listBuilder.toString(); } }
ParentalMemberFormatter implements MemberFormatter<AbstractParentRecordStructureMember> { @Override public String format(final AbstractParentRecordStructureMember member) throws MavenReportException { final VelocityContext context = new VelocityContext(); context.put("member", member); context.put("detailsDivName", member.getName() + random.nextInt()); final StringWriter writer = new StringWriter(); try { template.merge(context, writer); } catch (final Exception e) { throw new MavenReportException("Failed to merge template.", e); } final StringBuilder listBuilder = new StringBuilder(writer.toString()); for (int i = 0, size = member.getChildMemberCount(); i < size; i++) { final RecordStructureMember childMember = member.getChildMember(i); if (childMember instanceof RecordStructureField) { listBuilder.append(fieldFormatter.format((RecordStructureField) childMember)); } else { listBuilder.append(format((AbstractParentRecordStructureMember) childMember)); } } return listBuilder.toString(); } ParentalMemberFormatter(final VelocityEngine engine);  ParentalMemberFormatter(final FieldFormatter formatter, final Template template); }
ParentalMemberFormatter implements MemberFormatter<AbstractParentRecordStructureMember> { @Override public String format(final AbstractParentRecordStructureMember member) throws MavenReportException { final VelocityContext context = new VelocityContext(); context.put("member", member); context.put("detailsDivName", member.getName() + random.nextInt()); final StringWriter writer = new StringWriter(); try { template.merge(context, writer); } catch (final Exception e) { throw new MavenReportException("Failed to merge template.", e); } final StringBuilder listBuilder = new StringBuilder(writer.toString()); for (int i = 0, size = member.getChildMemberCount(); i < size; i++) { final RecordStructureMember childMember = member.getChildMember(i); if (childMember instanceof RecordStructureField) { listBuilder.append(fieldFormatter.format((RecordStructureField) childMember)); } else { listBuilder.append(format((AbstractParentRecordStructureMember) childMember)); } } return listBuilder.toString(); } ParentalMemberFormatter(final VelocityEngine engine);  ParentalMemberFormatter(final FieldFormatter formatter, final Template template); @Override String format(final AbstractParentRecordStructureMember member); }
ParentalMemberFormatter implements MemberFormatter<AbstractParentRecordStructureMember> { @Override public String format(final AbstractParentRecordStructureMember member) throws MavenReportException { final VelocityContext context = new VelocityContext(); context.put("member", member); context.put("detailsDivName", member.getName() + random.nextInt()); final StringWriter writer = new StringWriter(); try { template.merge(context, writer); } catch (final Exception e) { throw new MavenReportException("Failed to merge template.", e); } final StringBuilder listBuilder = new StringBuilder(writer.toString()); for (int i = 0, size = member.getChildMemberCount(); i < size; i++) { final RecordStructureMember childMember = member.getChildMember(i); if (childMember instanceof RecordStructureField) { listBuilder.append(fieldFormatter.format((RecordStructureField) childMember)); } else { listBuilder.append(format((AbstractParentRecordStructureMember) childMember)); } } return listBuilder.toString(); } ParentalMemberFormatter(final VelocityEngine engine);  ParentalMemberFormatter(final FieldFormatter formatter, final Template template); @Override String format(final AbstractParentRecordStructureMember member);  }