ContentFactory { public boolean equalsTo(Content c) { try { if (!c.isAvailable()) return false; if (isTooLong()) return false; if (getLength() != c.getBytes().length) return false; return Arrays.equals(getBytes(), c.getBytes()); } catch (IOException e) { return false; } }  Content createContent(Storage s); boolean equalsTo(Content c); static final int MAX_CONTENT_LENGTH; }
@Test public void testEqualsTo() { ContentFactory f = new MyContentFactory(new byte[]{1}); assertTrue(f.equalsTo(new ByteContent(new byte[]{1}))); assertFalse(f.equalsTo(new ByteContent(new byte[]{2}))); assertFalse(f.equalsTo(new UnavailableContent())); }
@Test public void testDoesNotEqualsToIfTooLong() { ContentFactory f = new MyContentFactory(new byte[ContentFactory.MAX_CONTENT_LENGTH + 1]); assertFalse(f.equalsTo(new ByteContent(new byte[]{1}))); assertFalse(f.equalsTo(new UnavailableContent())); }
@Test public void testDoesNotEqualsToIfExceptionOccurs() { ContentFactory f = new MyContentFactory(new byte[1]) { @Override public long getLength() throws IOException { throw new IOException(); } }; assertFalse(f.equalsTo(new ByteContent(new byte[]{1}))); f = new MyContentFactory(new byte[1]) { @Override public byte[] getBytes() throws IOException { throw new IOException(); } }; assertFalse(f.equalsTo(new ByteContent(new byte[]{1}))); }