Skip to content

Commit 157dbc7

Browse files
committed
Add simulate network error for unit testing.
1 parent d5bb3c4 commit 157dbc7

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/com/androidquery/callback/AbstractAjaxCallback.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public abstract class AbstractAjaxCallback<T, K> implements Runnable{
113113
private static int NETWORK_POOL = 4;
114114
private static boolean GZIP = true;
115115
private static boolean REUSE_CLIENT = true;
116+
private static boolean SIMULATE_ERROR = false;
116117

117118
private Class<T> type;
118119
private Reference<Object> whandler;
@@ -194,6 +195,15 @@ public static void setGZip(boolean gzip){
194195
GZIP = gzip;
195196
}
196197

198+
/**
199+
* Set to true to simulate network error.
200+
*
201+
* @param error
202+
*/
203+
public static void setSimulateError(boolean error){
204+
SIMULATE_ERROR = error;
205+
}
206+
197207
/**
198208
* Sets the default static transformer. This transformer should be stateless.
199209
* If state is required, use the AjaxCallback.transformer() or AQuery.transformer().
@@ -1593,6 +1603,10 @@ private void httpDo(HttpUriRequest hr, String url, Map<String, String> headers,
15931603
throw new IOException("Aborted");
15941604
}
15951605

1606+
if(SIMULATE_ERROR){
1607+
throw new IOException("Simulated Error");
1608+
}
1609+
15961610
HttpResponse response = null;
15971611

15981612
try{

tests/src/com/androidquery/test/AQueryAsyncTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,4 +1716,34 @@ public void testRetryOk() {
17161716

17171717

17181718
}
1719+
1720+
public void testAjaxSimulateError() {
1721+
1722+
AjaxCallback.setSimulateError(true);
1723+
1724+
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
1725+
1726+
AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>(){
1727+
1728+
@Override
1729+
public void callback(String url, JSONObject jo, AjaxStatus status) {
1730+
1731+
done(url, jo, status);
1732+
1733+
}
1734+
1735+
};
1736+
1737+
1738+
aq.ajax(url, JSONObject.class, cb);
1739+
1740+
waitAsync();
1741+
1742+
JSONObject jo = (JSONObject) result;
1743+
1744+
assertNull(jo);
1745+
assertNotNull(status);
1746+
assertEquals(AjaxStatus.NETWORK_ERROR, status.getCode());
1747+
1748+
}
17191749
}

tests/src/com/androidquery/test/AbstractTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44

55
import com.androidquery.AQuery;
6+
import com.androidquery.callback.AjaxCallback;
67
import com.androidquery.callback.BitmapAjaxCallback;
78
import com.androidquery.util.AQUtility;
89

@@ -26,6 +27,7 @@ public AbstractTest(Class cls){
2627
protected void setUp() throws Exception {
2728
super.setUp();
2829
aq = new AQuery(getActivity());
30+
AjaxCallback.setSimulateError(false);
2931
AQUtility.debug("new act", getActivity() + ":" + getActivity().isFinishing());
3032
}
3133

0 commit comments

Comments
 (0)