LazyListImpl extends ArrayList<User> implements ListAccess<User> { public User[] load(int index, int length) throws Exception, IllegalArgumentException { if (index < 0 || length < 0) throw new IllegalArgumentException("The index value and the length value cannot be negative"); if (index + length > size()) throw new IllegalArgumentException("The sum of the index and the length cannot be greater than the list size"); User[] users = new User[length]; for (int i = 0; i < length; i++) { users[i] = get(index + i); } return users; }  User[] load(int index, int length); int getSize();  }
@Test public void testLoad() throws Exception { try { list.load(-1, 5); fail("An IllegalArgumentException is expected"); } catch (IllegalArgumentException e) { } try { list.load(0, -1); fail("An IllegalArgumentException is expected"); } catch (IllegalArgumentException e) { } try { list.load(0, 6); fail("An IllegalArgumentException is expected"); } catch (IllegalArgumentException e) { } try { list.load(1, 5); fail("An IllegalArgumentException is expected"); } catch (IllegalArgumentException e) { } User[] users = list.load(0, 5); assertNotNull(users); assertEquals(5, users.length); int startIndex = 1; for (User user : users) { assertNotNull(user); assertEquals("foo" + startIndex++, user.getUserName()); } users = list.load(0, 3); assertNotNull(users); assertEquals(3, users.length); startIndex = 1; for (User user : users) { assertNotNull(user); assertEquals("foo" + startIndex++, user.getUserName()); } users = list.load(2, 3); assertNotNull(users); assertEquals(3, users.length); startIndex = 3; for (User user : users) { assertNotNull(user); assertEquals("foo" + startIndex++, user.getUserName()); } }