1a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee/* Copyright (C) 2014 The Android Open Source Project
2a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee *
3a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * Licensed under the Apache License, Version 2.0 (the "License");
4a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * you may not use this file except in compliance with the License.
5a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * You may obtain a copy of the License at
6a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee *
7a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee *      http://www.apache.org/licenses/LICENSE-2.0
8a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee *
9a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * Unless required by applicable law or agreed to in writing, software
10a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * distributed under the License is distributed on an "AS IS" BASIS,
11a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * See the License for the specific language governing permissions and
13a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * limitations under the License.
14a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee */
15a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
16a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Leepackage com.android.exchange.utility;
17a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
18a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Leeimport android.test.suitebuilder.annotation.SmallTest;
19a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
20a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Leeimport org.apache.http.message.BasicHeader;
21a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
22a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Leeimport junit.framework.TestCase;
23a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
24a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Leeimport java.io.ByteArrayInputStream;
25a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Leeimport java.io.IOException;
26a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Leeimport java.util.Arrays;
27a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
28a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee/**
29a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * Test for {@link WbxmlResponseLogger}.
30a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee * You can run this entire test case with:
31a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee *   runtest -c com.android.exchange.utility.WbxmlResponseLoggerTests exchange
32a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee */
33a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee@SmallTest
34a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Leepublic class WbxmlResponseLoggerTests extends TestCase {
35a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    private static final byte testArray[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
36a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x11,};
37a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
38a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    public void testShouldLogResponseTooBig() {
39a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final long contentSize = WbxmlResponseLogger.MAX_LENGTH + 1;
40a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        assertEquals(false, WbxmlResponseLogger.shouldLogResponse(contentSize));
41a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    }
42a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
43a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    public void testShouldLogResponseSmallEnough() {
44a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final long contentSize = WbxmlResponseLogger.MAX_LENGTH - 1;
45a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        assertEquals(true, WbxmlResponseLogger.shouldLogResponse(contentSize));
46a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    }
47a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
48a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    public void testProcessContentEncoding() {
49a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final String encoding = "US-ASCII";
50a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final BasicHeader header = new BasicHeader("content-encoding", encoding);
51a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final String outputEncoding = WbxmlResponseLogger.processContentEncoding(header);
52a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        assertEquals(true, encoding.equals(outputEncoding));
53a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    }
54a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
55a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    public void testProcessContentEncodingNullHeader() {
56a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final String encoding = "UTF-8";
57a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final String outputEncoding = WbxmlResponseLogger.processContentEncoding(null);
58a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        assertEquals(true, encoding.equals(outputEncoding));
59a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    }
60a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
61a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    public void testProcessContentEncodingNullValue() {
62a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final String encoding = "UTF-8";
63a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final BasicHeader header = new BasicHeader("content-encoding", null);
64a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final String outputEncoding = WbxmlResponseLogger.processContentEncoding(header);
65a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        assertEquals(true, encoding.equals(outputEncoding));
66a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    }
67a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
68a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    public void testGetContentAsByteArraySingleBatch() throws IOException {
69a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final ByteArrayInputStream bis = new ByteArrayInputStream(testArray);
70a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final byte outputBytes[] = WbxmlResponseLogger.getContentAsByteArray(bis,
71a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee            testArray.length);
72a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        assertEquals(true, Arrays.equals(testArray, outputBytes));
73a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    }
74a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee
75a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    public void testGetContentAsByteArrayMultipleBatches() throws IOException {
76a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final ByteArrayInputStream bis = new ByteArrayInputStream(testArray);
77a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        // If we cut the batch size to be half the length of testArray, we force
78a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        // 2 batches of processing.
79a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        final byte outputBytes[] = WbxmlResponseLogger.getContentAsByteArray(bis,
80a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee                testArray.length / 2);
81a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee        assertEquals(true, Arrays.equals(testArray, outputBytes));
82a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee    }
83a5fd5a343a8698978a88c7d8907de19a07fce2abAnthony Lee}
84