/retrofit/src/test/java/retrofit/RestMethodInfoTest.java

https://gitlab.com/vicidroiddev/retrofit · Java · 230 lines · 190 code · 37 blank · 3 comment · 1 complexity · 71811c9bb5516c905c713a1ae1c3617c MD5 · raw file

  1. // Copyright 2013 Square, Inc.
  2. package retrofit;
  3. import com.google.gson.reflect.TypeToken;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Type;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import org.junit.Test;
  10. import retrofit.client.Response;
  11. import retrofit.http.GET;
  12. import retrofit.http.Query;
  13. import retrofit.http.Streaming;
  14. import rx.Observable;
  15. import static org.assertj.core.api.Assertions.assertThat;
  16. @SuppressWarnings("unused") // Lots of unused parameters for example code.
  17. public class RestMethodInfoTest {
  18. @Test public void pathParameterParsing() throws Exception {
  19. expectParams("/");
  20. expectParams("/foo");
  21. expectParams("/foo/bar");
  22. expectParams("/foo/bar/{}");
  23. expectParams("/foo/bar/{taco}", "taco");
  24. expectParams("/foo/bar/{t}", "t");
  25. expectParams("/foo/bar/{!!!}/"); // Invalid parameter.
  26. expectParams("/foo/bar/{}/{taco}", "taco");
  27. expectParams("/foo/bar/{taco}/or/{burrito}", "taco", "burrito");
  28. expectParams("/foo/bar/{taco}/or/{taco}", "taco");
  29. expectParams("/foo/bar/{taco-shell}", "taco-shell");
  30. expectParams("/foo/bar/{taco_shell}", "taco_shell");
  31. expectParams("/foo/bar/{sha256}", "sha256");
  32. expectParams("/foo/bar/{TACO}", "TACO");
  33. expectParams("/foo/bar/{taco}/{tAco}/{taCo}", "taco", "tAco", "taCo");
  34. expectParams("/foo/bar/{1}"); // Invalid parameter, name cannot start with digit.
  35. }
  36. private static void expectParams(String path, String... expected) {
  37. Set<String> calculated = RestMethodInfo.parsePathParameters(path);
  38. assertThat(calculated).hasSize(expected.length);
  39. if (expected.length > 0) {
  40. assertThat(calculated).containsExactly(expected);
  41. }
  42. }
  43. @Test public void concreteCallbackTypes() {
  44. class Example {
  45. @GET("/foo") void a(ResponseCallback cb) {
  46. }
  47. }
  48. Method method = TestingUtils.onlyMethod(Example.class);
  49. RestMethodInfo methodInfo = new RestMethodInfo(method);
  50. assertThat(methodInfo.isSynchronous).isFalse();
  51. assertThat(methodInfo.responseObjectType).isEqualTo(Response.class);
  52. }
  53. @Test public void concreteCallbackTypesWithParams() {
  54. class Example {
  55. @GET("/foo") void a(@Query("id") String id, ResponseCallback cb) {
  56. }
  57. }
  58. Method method = TestingUtils.onlyMethod(Example.class);
  59. RestMethodInfo methodInfo = new RestMethodInfo(method);
  60. assertThat(methodInfo.isSynchronous).isFalse();
  61. assertThat(methodInfo.responseObjectType).isEqualTo(Response.class);
  62. }
  63. @Test public void genericCallbackTypes() {
  64. class Example {
  65. @GET("/foo") void a(Callback<Response> cb) {
  66. }
  67. }
  68. Method method = TestingUtils.onlyMethod(Example.class);
  69. RestMethodInfo methodInfo = new RestMethodInfo(method);
  70. assertThat(methodInfo.isSynchronous).isFalse();
  71. assertThat(methodInfo.responseObjectType).isEqualTo(Response.class);
  72. }
  73. @Test public void genericCallbackTypesWithParams() {
  74. class Example {
  75. @GET("/foo") void a(@Query("id") String id, Callback<Response> c) {
  76. }
  77. }
  78. Method method = TestingUtils.onlyMethod(Example.class);
  79. RestMethodInfo methodInfo = new RestMethodInfo(method);
  80. assertThat(methodInfo.isSynchronous).isFalse();
  81. assertThat(methodInfo.responseObjectType).isEqualTo(Response.class);
  82. }
  83. @Test public void wildcardGenericCallbackTypes() {
  84. class Example {
  85. @GET("/foo") void a(Callback<? extends Response> c) {
  86. }
  87. }
  88. Method method = TestingUtils.onlyMethod(Example.class);
  89. RestMethodInfo methodInfo = new RestMethodInfo(method);
  90. assertThat(methodInfo.isSynchronous).isFalse();
  91. assertThat(methodInfo.responseObjectType).isEqualTo(Response.class);
  92. }
  93. @Test public void genericCallbackWithGenericType() {
  94. class Example {
  95. @GET("/foo") void a(Callback<List<String>> c) {
  96. }
  97. }
  98. Method method = TestingUtils.onlyMethod(Example.class);
  99. RestMethodInfo methodInfo = new RestMethodInfo(method);
  100. assertThat(methodInfo.isSynchronous).isFalse();
  101. Type expected = new TypeToken<List<String>>() {}.getType();
  102. assertThat(methodInfo.responseObjectType).isEqualTo(expected);
  103. }
  104. // RestMethodInfo reconstructs this type from MultimapCallback<String, Set<Long>>. It contains
  105. // a little of everything: a parameterized type, a generic array, and a wildcard.
  106. private static Map<? extends String, Set<Long>[]> extendingGenericCallbackType;
  107. @Test public void extendingGenericCallback() throws Exception {
  108. class Example {
  109. @GET("/foo") void a(MultimapCallback<String, Set<Long>> callback) {
  110. }
  111. }
  112. Method method = TestingUtils.onlyMethod(Example.class);
  113. RestMethodInfo methodInfo = new RestMethodInfo(method);
  114. assertThat(methodInfo.isSynchronous).isFalse();
  115. assertThat(methodInfo.responseObjectType).isEqualTo(
  116. RestMethodInfoTest.class.getDeclaredField("extendingGenericCallbackType").getGenericType());
  117. }
  118. @Test public void synchronousResponse() {
  119. class Example {
  120. @GET("/foo") Response a() {
  121. return null;
  122. }
  123. }
  124. Method method = TestingUtils.onlyMethod(Example.class);
  125. RestMethodInfo methodInfo = new RestMethodInfo(method);
  126. assertThat(methodInfo.isSynchronous).isTrue();
  127. assertThat(methodInfo.responseObjectType).isEqualTo(Response.class);
  128. }
  129. @Test public void synchronousGenericResponse() {
  130. class Example {
  131. @GET("/foo") List<String> a() {
  132. return null;
  133. }
  134. }
  135. Method method = TestingUtils.onlyMethod(Example.class);
  136. RestMethodInfo methodInfo = new RestMethodInfo(method);
  137. assertThat(methodInfo.isSynchronous).isTrue();
  138. Type expected = new TypeToken<List<String>>() {}.getType();
  139. assertThat(methodInfo.responseObjectType).isEqualTo(expected);
  140. }
  141. @Test public void streamingResponse() {
  142. class Example {
  143. @GET("/foo") @Streaming Response a() {
  144. return null;
  145. }
  146. }
  147. Method method = TestingUtils.onlyMethod(Example.class);
  148. RestMethodInfo methodInfo = new RestMethodInfo(method);
  149. methodInfo.init();
  150. assertThat(methodInfo.isStreaming).isTrue();
  151. assertThat(methodInfo.responseObjectType).isEqualTo(Response.class);
  152. }
  153. @Test public void streamingResponseWithCallback() {
  154. class Example {
  155. @GET("/foo") @Streaming void a(Callback<Response> callback) {
  156. }
  157. }
  158. Method method = TestingUtils.onlyMethod(Example.class);
  159. RestMethodInfo methodInfo = new RestMethodInfo(method);
  160. methodInfo.init();
  161. assertThat(methodInfo.isStreaming).isTrue();
  162. assertThat(methodInfo.responseObjectType).isEqualTo(Response.class);
  163. }
  164. @Test public void observableResponse() {
  165. class Example {
  166. @GET("/foo") Observable<Response> a() {
  167. return null;
  168. }
  169. }
  170. Method method = TestingUtils.onlyMethod(Example.class);
  171. RestMethodInfo methodInfo = new RestMethodInfo(method);
  172. assertThat(methodInfo.isSynchronous).isFalse();
  173. assertThat(methodInfo.isObservable).isTrue();
  174. assertThat(methodInfo.responseObjectType).isEqualTo(Response.class);
  175. }
  176. @Test public void observableGenericResponse() {
  177. class Example {
  178. @GET("/foo") Observable<List<String>> a() {
  179. return null;
  180. }
  181. }
  182. Method method = TestingUtils.onlyMethod(Example.class);
  183. RestMethodInfo methodInfo = new RestMethodInfo(method);
  184. assertThat(methodInfo.isSynchronous).isFalse();
  185. assertThat(methodInfo.isObservable).isTrue();
  186. Type expected = new TypeToken<List<String>>() {}.getType();
  187. assertThat(methodInfo.responseObjectType).isEqualTo(expected);
  188. }
  189. private static interface ResponseCallback extends Callback<Response> {
  190. }
  191. private static interface MultimapCallback<K, V> extends Callback<Map<? extends K, V[]>> {
  192. }
  193. }