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.exchange.adapter; 18 19 import android.content.ContentValues; 20 import android.test.AndroidTestCase; 21 import android.test.MoreAsserts; 22 import android.test.suitebuilder.annotation.SmallTest; 23 24 import java.io.ByteArrayInputStream; 25 import java.io.ByteArrayOutputStream; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.io.OutputStream; 29 30 /** You can run this entire test case with: 31 * runtest -c com.android.exchange.adapter.SerializerTests exchange 32 */ 33 @SmallTest 34 public class SerializerTests extends AndroidTestCase { 35 36 private static final byte[] BYTE_ARRAY = new byte[] {1, 2, 3, 4, 5}; 37 private static final int BYTE_ARRAY_LENGTH = 5; 38 private static final String ID = "ID"; 39 private static final String KEY = "Key"; 40 41 // Basic test for use of start, end, tag, data, opaque, and done testSerializer()42 public void testSerializer() throws IOException { 43 ContentValues values = new ContentValues(); 44 // Create a test stream 45 Serializer s = new Serializer(); 46 s.start(Tags.COMPOSE_SEND_MAIL); 47 48 // Test writeStringValue without and with data 49 s.writeStringValue(values, KEY, Tags.COMPOSE_ACCOUNT_ID); 50 values.put(KEY, ID); 51 s.writeStringValue(values, KEY, Tags.COMPOSE_ACCOUNT_ID); 52 53 s.data(Tags.COMPOSE_CLIENT_ID, ID); 54 s.tag(Tags.COMPOSE_SAVE_IN_SENT_ITEMS); 55 s.start(Tags.COMPOSE_MIME); 56 s.opaque(new ByteArrayInputStream(BYTE_ARRAY), BYTE_ARRAY_LENGTH); 57 s.end(); // COMPOSE_MIME 58 s.end(); // COMPOSE_SEND_MAIL 59 s.done(); // DOCUMENT 60 // Get the bytes for the stream 61 byte[] bytes = s.toByteArray(); 62 // These are the expected bytes (self-explanatory) 63 byte[] expectedBytes = new byte[] { 64 3, // Version 1.3 65 1, // unknown or missing public identifier 66 106, // UTF-8 67 0, // String array length 68 Wbxml.SWITCH_PAGE, 69 Tags.COMPOSE, 70 Tags.COMPOSE_SEND_MAIL - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT, 71 Tags.COMPOSE_ACCOUNT_ID - Tags.COMPOSE_PAGE, 72 Tags.COMPOSE_ACCOUNT_ID - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT, 73 Wbxml.STR_I, // 0-terminated string 74 (byte)ID.charAt(0), 75 (byte)ID.charAt(1), 76 0, 77 Wbxml.END, // COMPOSE_ACCOUNT_ID 78 Tags.COMPOSE_CLIENT_ID - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT, 79 Wbxml.STR_I, // 0-terminated string 80 (byte)ID.charAt(0), 81 (byte)ID.charAt(1), 82 0, 83 Wbxml.END, // COMPOSE_CLIENT_ID 84 Tags.COMPOSE_SAVE_IN_SENT_ITEMS - Tags.COMPOSE_PAGE, 85 Tags.COMPOSE_MIME - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT, 86 (byte)Wbxml.OPAQUE, 87 BYTE_ARRAY_LENGTH, 88 BYTE_ARRAY[0], 89 BYTE_ARRAY[1], 90 BYTE_ARRAY[2], 91 BYTE_ARRAY[3], 92 BYTE_ARRAY[4], 93 Wbxml.END, // COMPOSE_MIME 94 Wbxml.END // COMPOSE_SEND_MAIL 95 }; 96 // Make sure we get what's expected 97 MoreAsserts.assertEquals("Serializer mismatch", bytes, expectedBytes); 98 } 99 100 @SmallTest testWriteInteger()101 public void testWriteInteger() throws IOException { 102 OutputStream output = new ByteArrayOutputStream(); 103 Serializer.writeInteger(output, 384); 104 Serializer.writeInteger(output, 0); 105 Serializer.writeInteger(output, -1); 106 } 107 } 108