Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21478 rajender 1
/*
2
 * Copyright (C) 2011 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.android.volley.toolbox;
18
 
19
import com.android.volley.NetworkResponse;
20
import com.android.volley.Response;
21
 
22
import org.json.JSONArray;
23
import org.json.JSONObject;
24
import org.junit.Test;
25
import org.junit.runner.RunWith;
26
import org.robolectric.RobolectricTestRunner;
27
 
28
import java.nio.charset.Charset;
29
import java.util.HashMap;
30
import java.util.Map;
31
 
32
import static org.junit.Assert.assertEquals;
33
import static org.junit.Assert.assertNotNull;
34
import static org.junit.Assert.assertTrue;
35
 
36
@RunWith(RobolectricTestRunner.class)
37
public class JsonRequestCharsetTest {
38
 
39
    /**
40
     * String in Czech - "Retezec v cestine."
41
     */
42
    private static final String TEXT_VALUE = "\u0158et\u011bzec v \u010de\u0161tin\u011b.";
43
    private static final String TEXT_NAME = "text";
44
    private static final int TEXT_INDEX = 0;
45
 
46
    /**
47
     * Copyright symbol has different encoding in utf-8 and ISO-8859-1,
48
     * and it doesn't exists in ISO-8859-2
49
     */
50
    private static final String COPY_VALUE = "\u00a9";
51
    private static final String COPY_NAME = "copyright";
52
    private static final int COPY_INDEX = 1;
53
 
54
    @Test public void defaultCharsetJsonObject() throws Exception {
55
        // UTF-8 is default charset for JSON
56
        byte[] data = jsonObjectString().getBytes(Charset.forName("UTF-8"));
57
        NetworkResponse network = new NetworkResponse(data);
58
        JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
59
        Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
60
 
61
        assertNotNull(objectResponse);
62
        assertTrue(objectResponse.isSuccess());
63
        assertEquals(TEXT_VALUE, objectResponse.result.getString(TEXT_NAME));
64
        assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
65
    }
66
 
67
    @Test public void defaultCharsetJsonArray() throws Exception {
68
        // UTF-8 is default charset for JSON
69
        byte[] data = jsonArrayString().getBytes(Charset.forName("UTF-8"));
70
        NetworkResponse network = new NetworkResponse(data);
71
        JsonArrayRequest arrayRequest = new JsonArrayRequest("", null, null);
72
        Response<JSONArray> arrayResponse = arrayRequest.parseNetworkResponse(network);
73
 
74
        assertNotNull(arrayResponse);
75
        assertTrue(arrayResponse.isSuccess());
76
        assertEquals(TEXT_VALUE, arrayResponse.result.getString(TEXT_INDEX));
77
        assertEquals(COPY_VALUE, arrayResponse.result.getString(COPY_INDEX));
78
    }
79
 
80
    @Test public void specifiedCharsetJsonObject() throws Exception {
81
        byte[] data = jsonObjectString().getBytes(Charset.forName("ISO-8859-1"));
82
        Map<String, String> headers = new HashMap<String, String>();
83
        headers.put("Content-Type", "application/json; charset=iso-8859-1");
84
        NetworkResponse network = new NetworkResponse(data, headers);
85
        JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
86
        Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
87
 
88
        assertNotNull(objectResponse);
89
        assertTrue(objectResponse.isSuccess());
90
        //don't check the text in Czech, ISO-8859-1 doesn't support some Czech characters
91
        assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
92
    }
93
 
94
    @Test public void specifiedCharsetJsonArray() throws Exception {
95
        byte[] data = jsonArrayString().getBytes(Charset.forName("ISO-8859-2"));
96
        Map<String, String> headers = new HashMap<String, String>();
97
        headers.put("Content-Type", "application/json; charset=iso-8859-2");
98
        NetworkResponse network = new NetworkResponse(data, headers);
99
        JsonArrayRequest arrayRequest = new JsonArrayRequest("", null, null);
100
        Response<JSONArray> arrayResponse = arrayRequest.parseNetworkResponse(network);
101
 
102
        assertNotNull(arrayResponse);
103
        assertTrue(arrayResponse.isSuccess());
104
        assertEquals(TEXT_VALUE, arrayResponse.result.getString(TEXT_INDEX));
105
        // don't check the copyright symbol, ISO-8859-2 doesn't have it, but it has Czech characters
106
    }
107
 
108
    private static String jsonObjectString() throws Exception {
109
        JSONObject json = new JSONObject().put(TEXT_NAME, TEXT_VALUE).put(COPY_NAME, COPY_VALUE);
110
        return json.toString();
111
    }
112
 
113
    private static String jsonArrayString() throws Exception {
114
        JSONArray json = new JSONArray().put(TEXT_INDEX, TEXT_VALUE).put(COPY_INDEX, COPY_VALUE);
115
        return json.toString();
116
    }
117
}