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