Skip to content

Commit

Permalink
Switch to a proper builder for MockRetrofit.
Browse files Browse the repository at this point in the history
This allows passing an explicit executor for test cases.
  • Loading branch information
JakeWharton committed Dec 21, 2015
1 parent 73b88a4 commit 4cd675d
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.concurrent.ExecutorService;
import retrofit2.Call;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;
Expand All @@ -34,11 +35,14 @@
public final class BehaviorDelegate<T> {
private final Retrofit retrofit;
private final NetworkBehavior behavior;
private final ExecutorService executor;
private final Class<T> service;

BehaviorDelegate(Retrofit retrofit, NetworkBehavior behavior, Class<T> service) {
BehaviorDelegate(Retrofit retrofit, NetworkBehavior behavior, ExecutorService executor,
Class<T> service) {
this.retrofit = retrofit;
this.behavior = behavior;
this.executor = executor;
this.service = service;
}

Expand All @@ -48,8 +52,7 @@ public T returningResponse(Object response) {

@SuppressWarnings("unchecked") // Single-interface proxy creation guarded by parameter safety.
public T returning(Call<?> call) {
final Call<?> behaviorCall =
new BehaviorCall<>(behavior, retrofit.client().getDispatcher().getExecutorService(), call);
final Call<?> behaviorCall = new BehaviorCall<>(behavior, executor, call);
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class[] { service },
new InvocationHandler() {
@Override
Expand Down
50 changes: 40 additions & 10 deletions retrofit-mock/src/main/java/retrofit2/mock/MockRetrofit.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@
*/
package retrofit2.mock;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import retrofit2.Retrofit;

public final class MockRetrofit {
public static MockRetrofit create(Retrofit retrofit) {
return create(retrofit, NetworkBehavior.create());
}

public static MockRetrofit create(Retrofit retrofit, NetworkBehavior behavior) {
return new MockRetrofit(retrofit, behavior);
}

private final Retrofit retrofit;
private final NetworkBehavior behavior;
private final ExecutorService executor;

public MockRetrofit(Retrofit retrofit, NetworkBehavior behavior) {
MockRetrofit(Retrofit retrofit, NetworkBehavior behavior, ExecutorService executor) {
this.retrofit = retrofit;
this.behavior = behavior;
this.executor = executor;
}

public Retrofit retrofit() {
Expand All @@ -42,8 +39,41 @@ public NetworkBehavior networkBehavior() {
return behavior;
}

public Executor backgroundExecutor() {
return executor;
}

@SuppressWarnings("unchecked") // Single-interface proxy creation guarded by parameter safety.
public <T> BehaviorDelegate<T> create(Class<T> service) {
return new BehaviorDelegate<>(retrofit, behavior, service);
return new BehaviorDelegate<>(retrofit, behavior, executor, service);
}

public static final class Builder {
private final Retrofit retrofit;
private NetworkBehavior behavior;
private ExecutorService executor;

public Builder(Retrofit retrofit) {
if (retrofit == null) throw new NullPointerException("retrofit == null");
this.retrofit = retrofit;
}

public Builder networkBehavior(NetworkBehavior behavior) {
if (behavior == null) throw new NullPointerException("behavior == null");
this.behavior = behavior;
return this;
}

public Builder backgroundExecutor(ExecutorService executor) {
if (executor == null) throw new NullPointerException("executor == null");
this.executor = executor;
return this;
}

public MockRetrofit build() {
if (behavior == null) behavior = NetworkBehavior.create();
if (executor == null) executor = Executors.newCachedThreadPool();
return new MockRetrofit(retrofit, behavior, executor);
}
}
}
Loading

0 comments on commit 4cd675d

Please sign in to comment.