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