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