Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 16 | package android.pim.vcard.test_utils; |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 17 | |
| 18 | import android.pim.vcard.VCardConfig; |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 19 | import android.test.AndroidTestCase; |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 20 | import android.text.TextUtils; |
| 21 | |
| 22 | import junit.framework.TestCase; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 27 | public class LineVerifierElem { |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 28 | private final List<String> mExpectedLineList = new ArrayList<String>(); |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 29 | private final int mVCardType; |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 30 | |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 31 | public LineVerifierElem(AndroidTestCase androidTestCase, int vcardType) { |
| 32 | mVCardType = vcardType; |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | public LineVerifierElem addExpected(final String line) { |
| 36 | if (!TextUtils.isEmpty(line)) { |
| 37 | mExpectedLineList.add(line); |
| 38 | } |
| 39 | return this; |
| 40 | } |
| 41 | |
| 42 | public void verify(final String vcard) { |
| 43 | final String[] lineArray = vcard.split("\\r?\\n"); |
| 44 | final int length = lineArray.length; |
| 45 | boolean beginExists = false; |
| 46 | boolean endExists = false; |
| 47 | boolean versionExists = false; |
| 48 | |
| 49 | for (int i = 0; i < length; i++) { |
| 50 | final String line = lineArray[i]; |
| 51 | if (TextUtils.isEmpty(line)) { |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | if ("BEGIN:VCARD".equalsIgnoreCase(line)) { |
| 56 | if (beginExists) { |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 57 | TestCase.fail("Multiple \"BEGIN:VCARD\" line found"); |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 58 | } else { |
| 59 | beginExists = true; |
| 60 | continue; |
| 61 | } |
| 62 | } else if ("END:VCARD".equalsIgnoreCase(line)) { |
| 63 | if (endExists) { |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 64 | TestCase.fail("Multiple \"END:VCARD\" line found"); |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 65 | } else { |
| 66 | endExists = true; |
| 67 | continue; |
| 68 | } |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 69 | } else if ((VCardConfig.isVersion21(mVCardType) ? "VERSION:2.1" : |
| 70 | (VCardConfig.isVersion30(mVCardType) ? "VERSION:3.0" : |
| 71 | "VERSION:4.0")).equalsIgnoreCase(line)) { |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 72 | if (versionExists) { |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 73 | TestCase.fail("Multiple VERSION line + found"); |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 74 | } else { |
| 75 | versionExists = true; |
| 76 | continue; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (!beginExists) { |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 81 | TestCase.fail("Property other than BEGIN came before BEGIN property: " + line); |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 82 | } else if (endExists) { |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 83 | TestCase.fail("Property other than END came after END property: " + line); |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | final int index = mExpectedLineList.indexOf(line); |
| 87 | if (index >= 0) { |
| 88 | mExpectedLineList.remove(index); |
| 89 | } else { |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 90 | TestCase.fail("Unexpected line: " + line); |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | if (!mExpectedLineList.isEmpty()) { |
| 95 | StringBuffer buffer = new StringBuffer(); |
| 96 | for (String expectedLine : mExpectedLineList) { |
| 97 | buffer.append(expectedLine); |
| 98 | buffer.append("\n"); |
| 99 | } |
| 100 | |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 101 | TestCase.fail("Expected line(s) not found:" + buffer.toString()); |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |