Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract some useful classes and simplify Glide.with().using() usage #101

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion checkstyle_suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
<suppress files=".*/library/src/androidTest/.*" checks="[a-zA-Z0-9]*"/>
<suppress files=".*[/\\]library[/\\]src[/\\]androidTest[/\\].*" checks="[a-zA-Z0-9]*"/>
</suppressions>

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import java.io.InputStream;

/**
* Fetches an {@link InputStream} using the okhttp library.
*/
public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private final OkHttpClient client;
private final GlideUrl url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Tests if {@link EngineKey} {@link Object#hashCode() hashCode} and {@link Object#equals(Object) equals}
* and SHA-1 disk cache key are different on any difference in ID or existence of a certain workflow part.
* Also checking whether the equals method is symmetric.
*
* @see #assertDifferent
*/
public class EngineKeyTest {
private Harness harness;

Expand Down Expand Up @@ -59,122 +67,180 @@ public void testIsIdenticalWithSameArguments() {
}

@Test
public void testDiffersIfIdDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
public void testDiffersIfIdDiffers() throws Exception {
EngineKey first = harness.build();
harness.id = harness.id + "2";
EngineKey second = harness.build();

assertNotSame(first, second);
assertDifferent(first, second);
}

@Test
public void testDiffersIfHeightDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
public void testDiffersIfHeightDiffers() throws Exception {
EngineKey first = harness.build();
harness.height += 1;
EngineKey second = harness.build();

assertNotSame(first, second);
assertDifferent(first, second);
}

@Test
public void testDiffersIfWidthDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
public void testDiffersIfWidthDiffers() throws Exception {
EngineKey first = harness.build();
harness.width += 1;
EngineKey second = harness.build();

assertNotSame(first, second);
assertDifferent(first, second);
}

@Test
public void testDiffersIfTransformationDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
public void testDiffersIfTransformationDiffers() throws Exception {
String id = "transformation";
when(harness.transformation.getId()).thenReturn(id);
EngineKey first = harness.build();
harness.transformation = mock(Transformation.class);
when(harness.transformation.getId()).thenReturn(id + "2");
EngineKey second = harness.build();

assertNotSame(first, second);
assertDifferent(first, second);
}

@Test
public void testDiffersIfCacheDecoderDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
public void testDiffersIfTransformationMissing() throws Exception {
EngineKey first = harness.build();
harness.transformation = null;
EngineKey second = harness.build();

assertDifferent(first, second);
}

@Test
public void testDiffersIfCacheDecoderDiffers() throws Exception {
String id = "cacheDecoder";
when(harness.cacheDecoder.getId()).thenReturn(id);
EngineKey first = harness.build();
harness.cacheDecoder = mock(ResourceDecoder.class);
when(harness.cacheDecoder.getId()).thenReturn(id + "2");
EngineKey second = harness.build();

assertNotSame(first, second);
assertDifferent(first, second);
}

@Test
public void testDiffersIfDecoderDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
public void testDiffersIfCacheDecoderMissing() throws Exception {
EngineKey first = harness.build();
harness.cacheDecoder = null;
EngineKey second = harness.build();

assertDifferent(first, second);
}

@Test
public void testDiffersIfDecoderDiffers() throws Exception {
String id = "decoder";
when(harness.decoder.getId()).thenReturn(id);
EngineKey first = harness.build();
harness.decoder = mock(ResourceDecoder.class);
when(harness.decoder.getId()).thenReturn(id + "2");
EngineKey second = harness.build();

assertNotSame(first, second);
assertDifferent(first, second);
}

@Test
public void testDiffersIfEncoderDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
public void testDiffersIfDecoderMissing() throws Exception {
EngineKey first = harness.build();
harness.decoder = null;
EngineKey second = harness.build();

assertDifferent(first, second);
}

@Test
public void testDiffersIfEncoderDiffers() throws Exception {
String id = "encoder";
when(harness.encoder.getId()).thenReturn(id);
EngineKey first = harness.build();
harness.encoder = mock(ResourceEncoder.class);
when(harness.encoder.getId()).thenReturn(id + "2");
EngineKey second = harness.build();

assertNotSame(first, second);
assertDifferent(first, second);
}

@Test
public void testDiffersIfEncoderMissing() throws Exception {
EngineKey first = harness.build();
harness.encoder = null;
EngineKey second = harness.build();

assertDifferent(first, second);
}

@Test
public void testDiffersWhenTranscoderDiffers() throws NoSuchAlgorithmException, UnsupportedEncodingException {
public void testDiffersWhenTranscoderDiffers() throws Exception {
String id = "transcoder";
when(harness.transcoder.getId()).thenReturn(id);
EngineKey first = harness.build();
harness.transcoder = mock(ResourceTranscoder.class);
when(harness.transcoder.getId()).thenReturn(id + "2");
EngineKey second = harness.build();

// The transcoder doesn't affect the cached data, so we don't expect the key digests to updated differently even
// though the transcoder id isn't the same.
assertNotSame(first, second, false);
// The transcoder doesn't affect the cached data,
// so we don't expect the key digests to updated differently even though the transcoder id isn't the same.
assertDifferent(first, second, false);
assertDifferent(second, first, false);
}

@Test
public void testDiffersWhenSourceEncoderDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
public void testDiffersIfTranscoderMissing() throws Exception {
EngineKey first = harness.build();
harness.transcoder = null;
EngineKey second = harness.build();

assertDifferent(first, second, false);
assertDifferent(second, first, false);
}

@Test
public void testDiffersWhenSourceEncoderDiffers() throws Exception {
String id = "sourceEncoder";
when(harness.sourceEncoder.getId()).thenReturn(id);
EngineKey first = harness.build();
harness.sourceEncoder = mock(Encoder.class);
when(harness.sourceEncoder.getId()).thenReturn(id + "2");
EngineKey second = harness.build();

assertNotSame(first, second);
assertDifferent(first, second);
}

private static void assertNotSame(EngineKey first, EngineKey second)
throws UnsupportedEncodingException, NoSuchAlgorithmException {
assertNotSame(first, second, true);
@Test
public void testDiffersIfSourceEncoderMissing() throws Exception {
EngineKey first = harness.build();
harness.sourceEncoder = null;
EngineKey second = harness.build();

assertDifferent(first, second);
}

private static void assertNotSame(EngineKey first, EngineKey second, boolean diskCacheDiffers)
private static void assertDifferent(EngineKey first, EngineKey second)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
assertFalse(first.equals(second));
assertTrue(first.hashCode() != second.hashCode());
assertDifferent(first, second, true);
assertDifferent(second, first, true);
}

private static void assertDifferent(EngineKey first, EngineKey second, boolean diskCacheDiffers)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
assertNotEquals(first, second);
assertNotEquals(first.hashCode(), second.hashCode());

if (diskCacheDiffers) {
MessageDigest firstDigest = MessageDigest.getInstance("SHA-1");
first.updateDiskCacheKey(firstDigest);
MessageDigest secondDigest = MessageDigest.getInstance("SHA-1");
second.updateDiskCacheKey(secondDigest);

assertFalse(Arrays.equals(firstDigest.digest(), secondDigest.digest()));
assertThat(firstDigest.digest(), not(equalTo(secondDigest.digest())));
}
}
}
}
Loading