ContentFactory { public Content createContent(Storage s) { try { if (isTooLong()) return new UnavailableContent(); return s.storeContent(getBytes()); } catch (IOException e) { return new UnavailableContent(); } }  Content createContent(Storage s); boolean equalsTo(Content c); static final int MAX_CONTENT_LENGTH; }
@Test public void testReturningUnavailableContentIfTooLong() { ContentFactory f = new MyContentFactory(new byte[ContentFactory.MAX_CONTENT_LENGTH + 1]); Content c = f.createContent(s); assertFalse(c.isAvailable()); }
@Test public void testDoesNotAskForContentIfTooLong() { final boolean[] isCalled = new boolean[1]; ContentFactory f = new ContentFactory() { @Override public byte[] getBytes() { isCalled[0] = true; return null; } public long getLength() { return ContentFactory.MAX_CONTENT_LENGTH + 1; } }; f.createContent(s); assertFalse(isCalled[0]); }
@Test public void testReturningUnavailableContentIfIOExceptionOccursOnGettingLength() { ContentFactory f = new MyContentFactory(new byte[1]) { @Override public long getLength() throws IOException { throw new IOException(); } }; Content c = f.createContent(s); assertFalse(c.isAvailable()); }
@Test public void testReturningUnavailableContentIfIOExceptionOccursOnGettingBytes() { ContentFactory f = new MyContentFactory(new byte[1]) { @Override public byte[] getBytes() throws IOException { throw new IOException(); } }; Content c = f.createContent(s); assertFalse(c.isAvailable()); }