Skip to content

Commit 83ba102

Browse files
committedOct 1, 2019
Distinguish between missing and non-handling ModelLoaders in exceptions.
Fixes b/141615023.
1 parent 291af10 commit 83ba102

File tree

3 files changed

+135
-6
lines changed

3 files changed

+135
-6
lines changed
 

‎library/src/main/java/com/bumptech/glide/Registry.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,7 @@ public <X> DataRewinder<X> getRewinder(@NonNull X data) {
582582

583583
@NonNull
584584
public <Model> List<ModelLoader<Model, ?>> getModelLoaders(@NonNull Model model) {
585-
List<ModelLoader<Model, ?>> result = modelLoaderRegistry.getModelLoaders(model);
586-
if (result.isEmpty()) {
587-
throw new NoModelLoaderAvailableException(model);
588-
}
589-
return result;
585+
return modelLoaderRegistry.getModelLoaders(model);
590586
}
591587

592588
@NonNull
@@ -605,8 +601,18 @@ public List<ImageHeaderParser> getImageHeaderParsers() {
605601
// Never serialized by Glide.
606602
@SuppressWarnings("serial")
607603
public static class NoModelLoaderAvailableException extends MissingComponentException {
604+
608605
public NoModelLoaderAvailableException(@NonNull Object model) {
609-
super("Failed to find any ModelLoaders for model: " + model);
606+
super("Failed to find any ModelLoaders registered for model class: " + model.getClass());
607+
}
608+
609+
public <M> NoModelLoaderAvailableException(
610+
@NonNull M model, @NonNull List<ModelLoader<M, ?>> matchingButNotHandlingModelLoaders) {
611+
super(
612+
"Found ModelLoaders for model class: "
613+
+ matchingButNotHandlingModelLoaders
614+
+ ", but none that handle this specific model instance: "
615+
+ model);
610616
}
611617

612618
public NoModelLoaderAvailableException(

‎library/src/main/java/com/bumptech/glide/load/model/ModelLoaderRegistry.java

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import androidx.annotation.NonNull;
44
import androidx.annotation.Nullable;
55
import androidx.core.util.Pools.Pool;
6+
import com.bumptech.glide.Registry.NoModelLoaderAvailableException;
67
import com.bumptech.glide.util.Synthetic;
78
import java.util.ArrayList;
89
import java.util.Collections;
@@ -72,6 +73,9 @@ private <Model, Data> void tearDown(
7273
@NonNull
7374
public <A> List<ModelLoader<A, ?>> getModelLoaders(@NonNull A model) {
7475
List<ModelLoader<A, ?>> modelLoaders = getModelLoadersForClass(getClass(model));
76+
if (modelLoaders.isEmpty()) {
77+
throw new NoModelLoaderAvailableException(model);
78+
}
7579
int size = modelLoaders.size();
7680
boolean isEmpty = true;
7781
List<ModelLoader<A, ?>> filteredLoaders = Collections.emptyList();
@@ -86,6 +90,9 @@ private <Model, Data> void tearDown(
8690
filteredLoaders.add(loader);
8791
}
8892
}
93+
if (filteredLoaders.isEmpty()) {
94+
throw new NoModelLoaderAvailableException(model, modelLoaders);
95+
}
8996
return filteredLoaders;
9097
}
9198

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.bumptech.glide.load.model;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
import static org.junit.Assert.assertThrows;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.when;
7+
8+
import androidx.annotation.NonNull;
9+
import com.bumptech.glide.Registry.NoModelLoaderAvailableException;
10+
import com.bumptech.glide.util.pool.FactoryPools;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.junit.function.ThrowingRunnable;
14+
import org.junit.runner.RunWith;
15+
import org.robolectric.RobolectricTestRunner;
16+
17+
@RunWith(RobolectricTestRunner.class)
18+
public class ModelLoaderRegistryTest {
19+
private static final String MOCK_MODEL_LOADER_NAME = "MockModelLoader";
20+
21+
private ModelLoaderRegistry registry;
22+
23+
@Before
24+
public void setUp() {
25+
registry = new ModelLoaderRegistry(FactoryPools.<Throwable>threadSafeList());
26+
}
27+
28+
@Test
29+
public void getModelLoaders_withNoRegisteredModelLoader_throws() {
30+
final Object model = new Object();
31+
NoModelLoaderAvailableException thrown =
32+
assertThrows(
33+
NoModelLoaderAvailableException.class,
34+
new ThrowingRunnable() {
35+
@Override
36+
public void run() {
37+
registry.getModelLoaders(model);
38+
}
39+
});
40+
41+
assertThat(thrown)
42+
.hasMessageThat()
43+
.contains(
44+
"Failed to find any ModelLoaders registered for model class: " + model.getClass());
45+
}
46+
47+
@Test
48+
public void getModelLoaders_withRegisteredModelLoader_thatDoesNotHandleModelInstance_throws() {
49+
final Object model = new Object();
50+
final ModelLoader<Object, Object> modelLoader = mockModelLoader();
51+
when(modelLoader.handles(model)).thenReturn(false);
52+
appendModelLoader(modelLoader);
53+
54+
NoModelLoaderAvailableException thrown =
55+
assertThrows(
56+
NoModelLoaderAvailableException.class,
57+
new ThrowingRunnable() {
58+
@Override
59+
public void run() {
60+
registry.getModelLoaders(model);
61+
}
62+
});
63+
64+
assertThat(thrown)
65+
.hasMessageThat()
66+
.contains(
67+
"Found ModelLoaders for model class: [MockModelLoader], but none that handle this specific model instance: java.lang.Object");
68+
}
69+
70+
@Test
71+
public void getModelLoaders_withRegisteredModelLoader_handlesModel_returnsModelLoader() {
72+
final Object model = new Object();
73+
final ModelLoader<Object, Object> modelLoader = mockModelLoader();
74+
when(modelLoader.handles(model)).thenReturn(true);
75+
appendModelLoader(modelLoader);
76+
77+
assertThat(registry.getModelLoaders(model)).containsExactly(modelLoader);
78+
}
79+
80+
@Test
81+
public void
82+
getModelLoaders_withRegisteredModelLoaders_onlyOneHandlesModel_returnsHandlingModelLoader() {
83+
final Object model = new Object();
84+
85+
ModelLoader<Object, Object> handlingModelLoader = mockModelLoader();
86+
when(handlingModelLoader.handles(model)).thenReturn(true);
87+
appendModelLoader(handlingModelLoader);
88+
89+
ModelLoader<Object, Object> notHandlingModelLoader = mockModelLoader();
90+
when(notHandlingModelLoader.handles(model)).thenReturn(false);
91+
appendModelLoader(notHandlingModelLoader);
92+
93+
assertThat(registry.getModelLoaders(model)).containsExactly(handlingModelLoader);
94+
}
95+
96+
private void appendModelLoader(final ModelLoader<Object, Object> modelLoader) {
97+
registry.append(
98+
Object.class,
99+
Object.class,
100+
new ModelLoaderFactory<Object, Object>() {
101+
@NonNull
102+
@Override
103+
public ModelLoader<Object, Object> build(@NonNull MultiModelLoaderFactory multiFactory) {
104+
return modelLoader;
105+
}
106+
107+
@Override
108+
public void teardown() {}
109+
});
110+
}
111+
112+
@SuppressWarnings("unchecked")
113+
private static ModelLoader<Object, Object> mockModelLoader() {
114+
return mock(ModelLoader.class, MOCK_MODEL_LOADER_NAME);
115+
}
116+
}

0 commit comments

Comments
 (0)
Please sign in to comment.