PagerJsonWriter { @Nullable public String write(@Nullable final Pager pager) { if (pager == null) { return null; } final StringWriter writer = new StringWriter(); try { write(writer, pager); } catch (IOException ioe) { Log.w(TAG, ioe.getMessage()); return null; } return writer.toString(); }  @Nullable String write(@Nullable final Pager pager); void write(@NonNull final Writer out,
                      @NonNull final Pager pager);  }
@Test public void testWriteEmptyPager() throws Exception { final Pager pager = new Pager(); final StringWriter writer = new StringWriter(); pagerJsonWriter.write(writer, pager); assertNotNull(writer.toString()); @SuppressWarnings("StringBufferReplaceableByString") final String expectedJsonString = new StringBuilder().append('{') .append("\"id\":") .append(0) .append(",\"size\":") .append(0) .append(",\"position\":") .append(0) .append(",\"history\":[]") .append('}') .toString(); assertEquals(expectedJsonString, writer.toString()); }
@Test public void testWrite() throws Exception { final Pager pager = new Pager(1234L); pager.setSize(5); pager.setPosition(3); pager.getHistory() .add(1); pager.getHistory() .add(4); pager.getHistory() .add(3); pager.getHistory() .add(2); final StringWriter writer = new StringWriter(); pagerJsonWriter.write(writer, pager); assertNotNull(writer.toString()); @SuppressWarnings("StringBufferReplaceableByString") final String expectedJsonString = new StringBuilder().append('{') .append("\"id\":") .append(1234L) .append(",\"size\":") .append(5) .append(",\"position\":") .append(3) .append(",\"history\":[1,4,3,2]") .append('}') .toString(); assertEquals(expectedJsonString, writer.toString()); }