diff options
author | Ficus Kirkpatrick <ficus@android.com> | 2014-11-29 09:32:54 -0800 |
---|---|---|
committer | Ficus Kirkpatrick <ficus@android.com> | 2014-12-05 15:46:44 -0800 |
commit | b9b8dc3d98fb1a8c3f02c2c2fcc18cbd344c05cb (patch) | |
tree | 1f4a33c24f608d79781de54e5d1ff5f3072fa261 /src/test/java/com/android/volley/mock | |
parent | 008e0cc8e51ef9e91110d91d4d662d0d86b252a1 (diff) | |
download | volley-b9b8dc3d98fb1a8c3f02c2c2fcc18cbd344c05cb.tar.gz |
Migrate from Gradle to Maven.
- Restructure source to src/{main,test} style
- Add pom.xml and update Android.mk
- Migrate all tests to JUnit4 and Robolectric
- RequestQueueTest is currently @Ignored as fixing it will
involve more extensive refactoring.
- Main library still builds in Gradle; tests do not
Change-Id: I1edc53bb1a54f64d3e806e4572901295ef63e2ca
Diffstat (limited to 'src/test/java/com/android/volley/mock')
9 files changed, 789 insertions, 0 deletions
diff --git a/src/test/java/com/android/volley/mock/MockCache.java b/src/test/java/com/android/volley/mock/MockCache.java new file mode 100644 index 0000000..85a4607 --- /dev/null +++ b/src/test/java/com/android/volley/mock/MockCache.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.mock; + +import com.android.volley.Cache; + +public class MockCache implements Cache { + + public boolean clearCalled = false; + @Override + public void clear() { + clearCalled = true; + } + + public boolean getCalled = false; + private Entry mFakeEntry = null; + + public void setEntryToReturn(Entry entry) { + mFakeEntry = entry; + } + + @Override + public Entry get(String key) { + getCalled = true; + return mFakeEntry; + } + + public boolean putCalled = false; + public String keyPut = null; + public Entry entryPut = null; + + @Override + public void put(String key, Entry entry) { + putCalled = true; + keyPut = key; + entryPut = entry; + } + + @Override + public void invalidate(String key, boolean fullExpire) { + } + + @Override + public void remove(String key) { + } + + @Override + public void initialize() { + } + +} diff --git a/src/test/java/com/android/volley/mock/MockHttpClient.java b/src/test/java/com/android/volley/mock/MockHttpClient.java new file mode 100644 index 0000000..c2a36bc --- /dev/null +++ b/src/test/java/com/android/volley/mock/MockHttpClient.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.mock; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.ProtocolVersion; +import org.apache.http.StatusLine; +import org.apache.http.client.HttpClient; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.message.BasicStatusLine; +import org.apache.http.params.HttpParams; +import org.apache.http.protocol.HttpContext; + + +public class MockHttpClient implements HttpClient { + private int mStatusCode = HttpStatus.SC_OK; + private HttpEntity mResponseEntity = null; + + public void setResponseData(HttpEntity entity) { + mStatusCode = HttpStatus.SC_OK; + mResponseEntity = entity; + } + + public void setErrorCode(int statusCode) { + if (statusCode == HttpStatus.SC_OK) { + throw new IllegalArgumentException("statusCode cannot be 200 for an error"); + } + mStatusCode = statusCode; + } + + public HttpUriRequest requestExecuted = null; + + // This is the only one we actually use. + @Override + public HttpResponse execute(HttpUriRequest request, HttpContext context) { + requestExecuted = request; + StatusLine statusLine = new BasicStatusLine( + new ProtocolVersion("HTTP", 1, 1), mStatusCode, ""); + HttpResponse response = new BasicHttpResponse(statusLine); + response.setEntity(mResponseEntity); + + return response; + } + + + // Unimplemented methods ahoy + + @Override + public HttpResponse execute(HttpUriRequest request) { + throw new UnsupportedOperationException(); + } + + @Override + public HttpResponse execute(HttpHost target, HttpRequest request) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1) { + throw new UnsupportedOperationException(); + } + + @Override + public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1, HttpContext arg2) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2, + HttpContext arg3) { + throw new UnsupportedOperationException(); + } + + @Override + public ClientConnectionManager getConnectionManager() { + throw new UnsupportedOperationException(); + } + + @Override + public HttpParams getParams() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/test/java/com/android/volley/mock/MockHttpStack.java b/src/test/java/com/android/volley/mock/MockHttpStack.java new file mode 100644 index 0000000..9594fde --- /dev/null +++ b/src/test/java/com/android/volley/mock/MockHttpStack.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.mock; + +import com.android.volley.AuthFailureError; +import com.android.volley.Request; +import com.android.volley.toolbox.HttpStack; + +import org.apache.http.HttpResponse; + +import java.util.HashMap; +import java.util.Map; + +public class MockHttpStack implements HttpStack { + + private HttpResponse mResponseToReturn; + + private String mLastUrl; + + private Map<String, String> mLastHeaders; + + private byte[] mLastPostBody; + + public String getLastUrl() { + return mLastUrl; + } + + public Map<String, String> getLastHeaders() { + return mLastHeaders; + } + + public byte[] getLastPostBody() { + return mLastPostBody; + } + + public void setResponseToReturn(HttpResponse response) { + mResponseToReturn = response; + } + + @Override + public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) + throws AuthFailureError { + mLastUrl = request.getUrl(); + mLastHeaders = new HashMap<String, String>(); + if (request.getHeaders() != null) { + mLastHeaders.putAll(request.getHeaders()); + } + if (additionalHeaders != null) { + mLastHeaders.putAll(additionalHeaders); + } + try { + mLastPostBody = request.getBody(); + } catch (AuthFailureError e) { + mLastPostBody = null; + } + return mResponseToReturn; + } +} diff --git a/src/test/java/com/android/volley/mock/MockHttpURLConnection.java b/src/test/java/com/android/volley/mock/MockHttpURLConnection.java new file mode 100644 index 0000000..efa3a21 --- /dev/null +++ b/src/test/java/com/android/volley/mock/MockHttpURLConnection.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.mock; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class MockHttpURLConnection extends HttpURLConnection { + + private boolean mDoOutput; + private String mRequestMethod; + private OutputStream mOutputStream; + + public MockHttpURLConnection() throws MalformedURLException { + super(new URL("http://foo.com")); + mDoOutput = false; + mRequestMethod = "GET"; + mOutputStream = new ByteArrayOutputStream(); + } + + @Override + public void setDoOutput(boolean flag) { + mDoOutput = flag; + } + + @Override + public boolean getDoOutput() { + return mDoOutput; + } + + @Override + public void setRequestMethod(String method) { + mRequestMethod = method; + } + + @Override + public String getRequestMethod() { + return mRequestMethod; + } + + @Override + public OutputStream getOutputStream() { + return mOutputStream; + } + + @Override + public void disconnect() { + } + + @Override + public boolean usingProxy() { + return false; + } + + @Override + public void connect() throws IOException { + } + +} diff --git a/src/test/java/com/android/volley/mock/MockNetwork.java b/src/test/java/com/android/volley/mock/MockNetwork.java new file mode 100644 index 0000000..207ec63 --- /dev/null +++ b/src/test/java/com/android/volley/mock/MockNetwork.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.mock; + +import com.android.volley.Network; +import com.android.volley.NetworkResponse; +import com.android.volley.Request; +import com.android.volley.ServerError; +import com.android.volley.VolleyError; + +public class MockNetwork implements Network { + public final static int ALWAYS_THROW_EXCEPTIONS = -1; + + private int mNumExceptionsToThrow = 0; + private byte[] mDataToReturn = null; + + /** + * @param numExceptionsToThrow number of times to throw an exception or + * {@link #ALWAYS_THROW_EXCEPTIONS} + */ + public void setNumExceptionsToThrow(int numExceptionsToThrow) { + mNumExceptionsToThrow = numExceptionsToThrow; + } + + public void setDataToReturn(byte[] data) { + mDataToReturn = data; + } + + public Request<?> requestHandled = null; + + @Override + public NetworkResponse performRequest(Request<?> request) throws VolleyError { + if (mNumExceptionsToThrow > 0 || mNumExceptionsToThrow == ALWAYS_THROW_EXCEPTIONS) { + if (mNumExceptionsToThrow != ALWAYS_THROW_EXCEPTIONS) { + mNumExceptionsToThrow--; + } + throw new ServerError(); + } + + requestHandled = request; + return new NetworkResponse(mDataToReturn); + } + +} diff --git a/src/test/java/com/android/volley/mock/MockRequest.java b/src/test/java/com/android/volley/mock/MockRequest.java new file mode 100644 index 0000000..9815ea8 --- /dev/null +++ b/src/test/java/com/android/volley/mock/MockRequest.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.mock; + +import com.android.volley.NetworkResponse; +import com.android.volley.Request; +import com.android.volley.Response; +import com.android.volley.Response.ErrorListener; +import com.android.volley.VolleyError; +import com.android.volley.utils.CacheTestUtils; + +import java.util.HashMap; +import java.util.Map; + +public class MockRequest extends Request<byte[]> { + public MockRequest() { + super(Request.Method.GET, "http://foo.com", null); + } + + public MockRequest(String url, ErrorListener listener) { + super(Request.Method.GET, url, listener); + } + + private Map<String, String> mPostParams = new HashMap<String, String>(); + + public void setPostParams(Map<String, String> postParams) { + mPostParams = postParams; + } + + @Override + public Map<String, String> getPostParams() { + return mPostParams; + } + + private String mCacheKey = super.getCacheKey(); + + public void setCacheKey(String cacheKey) { + mCacheKey = cacheKey; + } + + @Override + public String getCacheKey() { + return mCacheKey; + } + + public boolean deliverResponse_called = false; + public boolean parseResponse_called = false; + + @Override + protected void deliverResponse(byte[] response) { + deliverResponse_called = true; + } + + public boolean deliverError_called = false; + + @Override + public void deliverError(VolleyError error) { + super.deliverError(error); + deliverError_called = true; + } + + public boolean cancel_called = false; + + @Override + public void cancel() { + cancel_called = true; + super.cancel(); + } + + private Priority mPriority = super.getPriority(); + + public void setPriority(Priority priority) { + mPriority = priority; + } + + @Override + public Priority getPriority() { + return mPriority; + } + + @Override + protected Response<byte[]> parseNetworkResponse(NetworkResponse response) { + parseResponse_called = true; + return Response.success(response.data, CacheTestUtils.makeRandomCacheEntry(response.data)); + } + +} diff --git a/src/test/java/com/android/volley/mock/MockResponseDelivery.java b/src/test/java/com/android/volley/mock/MockResponseDelivery.java new file mode 100644 index 0000000..4dbfd5c --- /dev/null +++ b/src/test/java/com/android/volley/mock/MockResponseDelivery.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.mock; + +import com.android.volley.Request; +import com.android.volley.Response; +import com.android.volley.ResponseDelivery; +import com.android.volley.VolleyError; + +public class MockResponseDelivery implements ResponseDelivery { + + public boolean postResponse_called = false; + public boolean postError_called = false; + + public boolean wasEitherResponseCalled() { + return postResponse_called || postError_called; + } + + public Response<?> responsePosted = null; + @Override + public void postResponse(Request<?> request, Response<?> response) { + postResponse_called = true; + responsePosted = response; + } + + @Override + public void postResponse(Request<?> request, Response<?> response, Runnable runnable) { + postResponse_called = true; + responsePosted = response; + runnable.run(); + } + + @Override + public void postError(Request<?> request, VolleyError error) { + postError_called = true; + } +} diff --git a/src/test/java/com/android/volley/mock/TestRequest.java b/src/test/java/com/android/volley/mock/TestRequest.java new file mode 100644 index 0000000..dfc4dc1 --- /dev/null +++ b/src/test/java/com/android/volley/mock/TestRequest.java @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.mock; + +import com.android.volley.NetworkResponse; +import com.android.volley.Request; +import com.android.volley.Response; + +import java.util.HashMap; +import java.util.Map; + +public class TestRequest { + private static final String TEST_URL = "http://foo.com"; + + /** Base Request class for testing allowing both the deprecated and new constructor. */ + private static class Base extends Request<byte[]> { + @SuppressWarnings("deprecation") + public Base(String url, Response.ErrorListener listener) { + super(url, listener); + } + + public Base(int method, String url, Response.ErrorListener listener) { + super(method, url, listener); + } + + @Override + protected Response<byte[]> parseNetworkResponse(NetworkResponse response) { + return null; + } + + @Override + protected void deliverResponse(byte[] response) { + } + } + + /** Test example of a GET request in the deprecated style. */ + public static class DeprecatedGet extends Base { + public DeprecatedGet() { + super(TEST_URL, null); + } + } + + /** Test example of a POST request in the deprecated style. */ + public static class DeprecatedPost extends Base { + private Map<String, String> mPostParams; + + public DeprecatedPost() { + super(TEST_URL, null); + mPostParams = new HashMap<String, String>(); + mPostParams.put("requestpost", "foo"); + } + + @Override + protected Map<String, String> getPostParams() { + return mPostParams; + } + } + + /** Test example of a GET request in the new style. */ + public static class Get extends Base { + public Get() { + super(Method.GET, TEST_URL, null); + } + } + + /** + * Test example of a POST request in the new style. In the new style, it is possible + * to have a POST with no body. + */ + public static class Post extends Base { + public Post() { + super(Method.POST, TEST_URL, null); + } + } + + /** Test example of a POST request in the new style with a body. */ + public static class PostWithBody extends Post { + private Map<String, String> mParams; + + public PostWithBody() { + mParams = new HashMap<String, String>(); + mParams.put("testKey", "testValue"); + } + + @Override + public Map<String, String> getParams() { + return mParams; + } + } + + /** + * Test example of a PUT request in the new style. In the new style, it is possible to have a + * PUT with no body. + */ + public static class Put extends Base { + public Put() { + super(Method.PUT, TEST_URL, null); + } + } + + /** Test example of a PUT request in the new style with a body. */ + public static class PutWithBody extends Put { + private Map<String, String> mParams = new HashMap<String, String>(); + + public PutWithBody() { + mParams = new HashMap<String, String>(); + mParams.put("testKey", "testValue"); + } + + @Override + public Map<String, String> getParams() { + return mParams; + } + } + + /** Test example of a DELETE request in the new style. */ + public static class Delete extends Base { + public Delete() { + super(Method.DELETE, TEST_URL, null); + } + } + + /** Test example of a HEAD request in the new style. */ + public static class Head extends Base { + public Head() { + super(Method.HEAD, TEST_URL, null); + } + } + + /** Test example of a OPTIONS request in the new style. */ + public static class Options extends Base { + public Options() { + super(Method.OPTIONS, TEST_URL, null); + } + } + + /** Test example of a TRACE request in the new style. */ + public static class Trace extends Base { + public Trace() { + super(Method.TRACE, TEST_URL, null); + } + } + + /** Test example of a PATCH request in the new style. */ + public static class Patch extends Base { + public Patch() { + super(Method.PATCH, TEST_URL, null); + } + } + + /** Test example of a PATCH request in the new style with a body. */ + public static class PatchWithBody extends Patch { + private Map<String, String> mParams = new HashMap<String, String>(); + + public PatchWithBody() { + mParams = new HashMap<String, String>(); + mParams.put("testKey", "testValue"); + } + + @Override + public Map<String, String> getParams() { + return mParams; + } + } +} diff --git a/src/test/java/com/android/volley/mock/WaitableQueue.java b/src/test/java/com/android/volley/mock/WaitableQueue.java new file mode 100644 index 0000000..079bbf5 --- /dev/null +++ b/src/test/java/com/android/volley/mock/WaitableQueue.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.volley.mock; + +import com.android.volley.NetworkResponse; +import com.android.volley.Request; +import com.android.volley.Response; + +import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +// TODO: the name of this class sucks +@SuppressWarnings("serial") +public class WaitableQueue extends PriorityBlockingQueue<Request<?>> { + private final Request<?> mStopRequest = new MagicStopRequest(); + private final Semaphore mStopEvent = new Semaphore(0); + + // TODO: this isn't really "until empty" it's "until next call to take() after empty" + public void waitUntilEmpty(long timeoutMillis) + throws TimeoutException, InterruptedException { + add(mStopRequest); + if (!mStopEvent.tryAcquire(timeoutMillis, TimeUnit.MILLISECONDS)) { + throw new TimeoutException(); + } + } + + @Override + public Request<?> take() throws InterruptedException { + Request<?> item = super.take(); + if (item == mStopRequest) { + mStopEvent.release(); + return take(); + } + return item; + } + + private static class MagicStopRequest extends Request<Object> { + public MagicStopRequest() { + super(Request.Method.GET, "", null); + } + + @Override + public Priority getPriority() { + return Priority.LOW; + } + + @Override + protected Response<Object> parseNetworkResponse(NetworkResponse response) { + return null; + } + + @Override + protected void deliverResponse(Object response) { + } + } +} |