VariableUtil { public static String getExpandedMessage(PageInfo page, String msg) { String s = msg; int idx; while ((idx = s.indexOf(PAGE_NUMBER_WITH_FORMAT)) >= 0) { final int endidx = s.indexOf('#', idx + PAGE_NUMBER_WITH_FORMAT.length()); if (endidx < 0) { break; } final String fmt = s.substring(idx + PAGE_NUMBER_WITH_FORMAT.length(), endidx); final StringBuilder sb = new StringBuilder(s); String value; if (page == null) { final StringBuilder blanks = new StringBuilder(fmt.length()); blanks.setLength(fmt.length()); for (int i = 0; i < blanks.length(); i++) { blanks.setCharAt(i, '0'); } value = blanks.toString(); } else { final NumberFormat nf = new DecimalFormat(fmt); value = nf.format(page.getPageNumber()); } sb.replace(idx, endidx + 1, value); s = sb.toString(); } if (page == null) { s = replace(s, PAGE_NUMBER, "000"); s = replace(s, FORMATTED_PAGE_NUMBER, "000"); } else { s = replace(s, PAGE_NUMBER, Integer.toString(page.getPageNumber())); s = replace(s, FORMATTED_PAGE_NUMBER, page.getPageNumberString()); } return s; }  static String getExpandedMessage(PageInfo page, String msg);  }
@Test public void getExpandedMessageNoPage() { System.out.println("getExpandedMessage"); PageInfo page = null; String msg = "#page-number#"; String expResult = "000"; String result = VariableUtil.getExpandedMessage(page, msg); assertEquals(expResult, result); }
@Test public void getExpandedMessageWithPage() { System.out.println("getExpandedMessage"); PageInfo page = new PageInfo(42, "42"); String msg = "#page-number#"; String expResult = "42"; String result = VariableUtil.getExpandedMessage(page, msg); assertEquals(expResult, result); }
@Test public void getExpandedMessageWithPageFormat() { System.out.println("getExpandedMessage"); PageInfo page = new PageInfo(42, "42"); String msg = "Hallo Welt #page-number:0000#"; String expResult = "Hallo Welt 0042"; String result = VariableUtil.getExpandedMessage(page, msg); assertEquals(expResult, result); }