1 /* 2 * Copyright (C) 2010 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.emailcommon.mail; 18 19 import android.test.suitebuilder.annotation.SmallTest; 20 21 import com.android.emailcommon.mail.PackedString; 22 23 import junit.framework.TestCase; 24 25 /** 26 * Tests of PackedString 27 * 28 * You can run this entire test case with: 29 * runtest -c com.android.email.mail.PackedStringTests email 30 */ 31 @SmallTest 32 public class PackedStringTests extends TestCase { 33 /** Note: copied from actual class */ 34 private static final char DELIMITER_ELEMENT = '\1'; 35 private static final char DELIMITER_TAG = '\2'; 36 37 // A packed string with tags and values 38 private static final String PACKED_STRING_TAGGED = "val1" + DELIMITER_TAG + "tag1" + 39 DELIMITER_ELEMENT + "val2" + DELIMITER_TAG + "tag2" + 40 DELIMITER_ELEMENT + "val3" + DELIMITER_TAG + "tag3" + 41 DELIMITER_ELEMENT + "val4" + DELIMITER_TAG + "tag4"; 42 43 public void testPackedString() { 44 // Start with a packed string and make sure we can extract the correct Strings 45 PackedString ps = new PackedString(PACKED_STRING_TAGGED); 46 assertEquals("val1", ps.get("tag1")); 47 assertEquals("val2", ps.get("tag2")); 48 assertEquals("val3", ps.get("tag3")); 49 assertEquals("val4", ps.get("tag4")); 50 assertNull(ps.get("tag100")); 51 } 52 53 // test the builder in "create mode" 54 public void testPackedStringBuilderCreate() { 55 PackedString.Builder b = new PackedString.Builder(); 56 b.put("tag1", "value1"); 57 b.put("tag2", "value2"); 58 b.put("tag3", "value3"); 59 b.put("tag4", "value4"); 60 // can't use simple string compare on output, because order not guaranteed 61 // for now, we'll just pump into another one and test results 62 String packedOut = b.toString(); 63 PackedString.Builder b2 = new PackedString.Builder(packedOut); 64 assertEquals("value1", b2.get("tag1")); 65 assertEquals("value2", b2.get("tag2")); 66 assertEquals("value3", b2.get("tag3")); 67 assertEquals("value4", b2.get("tag4")); 68 assertNull(b2.get("tag100")); 69 } 70 71 // test the builder in "edit mode" 72 public void testPackedStringBuilderEdit() { 73 // Start with a Builder based on a non-empty packed string 74 PackedString.Builder b = new PackedString.Builder(PACKED_STRING_TAGGED); 75 // Test readback in-place 76 assertEquals("val1", b.get("tag1")); 77 assertEquals("val2", b.get("tag2")); 78 assertEquals("val3", b.get("tag3")); 79 assertEquals("val4", b.get("tag4")); 80 assertNull(b.get("tag100")); 81 82 // Test modifications in-place 83 b.put("tag2", "TWO"); // edit 84 b.put("tag3", null); // delete 85 b.put("tag5", "value5"); // add 86 // Read-back modifications in place 87 assertEquals("val1", b.get("tag1")); 88 assertEquals("TWO", b.get("tag2")); // edited 89 assertEquals(null, b.get("tag3")); // deleted 90 assertEquals("val4", b.get("tag4")); 91 assertEquals("value5", b.get("tag5")); // added 92 assertNull(b.get("tag100")); 93 94 // Confirm resulting packed string is as-expected 95 String packedOut = b.toString(); 96 PackedString.Builder b2 = new PackedString.Builder(packedOut); 97 assertEquals("val1", b2.get("tag1")); 98 assertEquals("TWO", b2.get("tag2")); 99 assertEquals(null, b2.get("tag3")); 100 assertEquals("val4", b2.get("tag4")); 101 assertEquals("value5", b2.get("tag5")); 102 assertNull(b2.get("tag100")); 103 } 104 } 105