PagerJsonReader { @Nullable public Pager read(@Nullable final String json) { if (TextUtils.isEmpty(json)) { return null; } try { return read(new StringReader(json)); } catch (IOException ioe) { Log.w(TAG, ioe.getMessage()); } return null; }  @Nullable Pager read(@Nullable final String json); @NonNull Pager read(@NonNull final Reader in);  }
@Test public void testReadEmptyPager() throws Exception { @SuppressWarnings("StringBufferReplaceableByString") final String jsonString = new StringBuilder().append('{') .append("\"id\":") .append(0) .append(",\"size\":") .append(0) .append(",\"position\":") .append(0) .append(",\"history\":[]") .append('}') .toString(); final StringReader reader = new StringReader(jsonString); final Pager pager = pagerJsonReader.read(reader); assertNotNull(pager); assertEquals(0, pager.getId()); assertEquals(0, pager.getSize()); assertEquals(0, pager.getPosition()); assertTrue(pager.getHistory() .isEmpty()); }
@Test public void testRead() throws Exception { @SuppressWarnings("StringBufferReplaceableByString") final String jsonString = new StringBuilder().append('{') .append("\"id\":") .append(1234L) .append(",\"size\":") .append(5) .append(",\"position\":") .append(3) .append(",\"history\":[1,4,3,2]") .append('}') .toString(); final StringReader reader = new StringReader(jsonString); final Pager pager = pagerJsonReader.read(reader); assertNotNull(pager); assertEquals(1234L, pager.getId()); assertEquals(5, pager.getSize()); assertEquals(3, pager.getPosition()); assertEquals(4, pager.getHistory() .size()); assertEquals(Integer.valueOf(2), pager.getHistory() .pollLast()); assertEquals(Integer.valueOf(3), pager.getHistory() .pollLast()); assertEquals(Integer.valueOf(4), pager.getHistory() .pollLast()); assertEquals(Integer.valueOf(1), pager.getHistory() .pollLast()); }