### Question:
LeftPadder implements Padder { public String pad(String data, int maxLength) throws ISOException { StringBuilder padded = new StringBuilder(maxLength); int len = data.length(); if (len > maxLength) { throw new ISOException("Data is too long. Max = " + maxLength); } else { for (int i = maxLength - len; i > 0; i--) { padded.append(pad); } padded.append(data); } return padded.toString(); } LeftPadder(char pad); String pad(String data, int maxLength); String unpad(String paddedData); static final LeftPadder ZERO_PADDER; }### Answer:
@Test public void testPaddingNeeded() throws Exception { assertEquals("000123", padder.pad("123", 6)); }
@Test public void testNoPaddingNeeded() throws Exception { assertEquals("123", padder.pad("123", 3)); }
@Test public void testPadLengthTooShort() throws Exception { try { padder.pad("123", 2); fail("Padding a string longer than the available width should throw an exception"); } catch (Exception asIExpected) { } }