blob: e23a9cded61828adbed21973dc7933f9851eb213 [file] [log] [blame]
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +09001/*
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 Miyakawa69831d92010-09-02 10:39:00 -070016package android.pim.vcard.test_utils;
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090017
18import android.pim.vcard.VCardConfig;
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070019import android.test.AndroidTestCase;
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090020import android.text.TextUtils;
21
22import junit.framework.TestCase;
23
24import java.util.ArrayList;
25import java.util.List;
26
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070027public class LineVerifierElem {
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090028 private final List<String> mExpectedLineList = new ArrayList<String>();
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070029 private final int mVCardType;
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090030
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070031 public LineVerifierElem(AndroidTestCase androidTestCase, int vcardType) {
32 mVCardType = vcardType;
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090033 }
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 Miyakawa69831d92010-09-02 10:39:00 -070057 TestCase.fail("Multiple \"BEGIN:VCARD\" line found");
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090058 } else {
59 beginExists = true;
60 continue;
61 }
62 } else if ("END:VCARD".equalsIgnoreCase(line)) {
63 if (endExists) {
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070064 TestCase.fail("Multiple \"END:VCARD\" line found");
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090065 } else {
66 endExists = true;
67 continue;
68 }
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070069 } else if ((VCardConfig.isVersion21(mVCardType) ? "VERSION:2.1" :
70 (VCardConfig.isVersion30(mVCardType) ? "VERSION:3.0" :
71 "VERSION:4.0")).equalsIgnoreCase(line)) {
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090072 if (versionExists) {
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070073 TestCase.fail("Multiple VERSION line + found");
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090074 } else {
75 versionExists = true;
76 continue;
77 }
78 }
79
80 if (!beginExists) {
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070081 TestCase.fail("Property other than BEGIN came before BEGIN property: " + line);
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090082 } else if (endExists) {
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070083 TestCase.fail("Property other than END came after END property: " + line);
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090084 }
85
86 final int index = mExpectedLineList.indexOf(line);
87 if (index >= 0) {
88 mExpectedLineList.remove(index);
89 } else {
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070090 TestCase.fail("Unexpected line: " + line);
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090091 }
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 Miyakawa69831d92010-09-02 10:39:00 -0700101 TestCase.fail("Expected line(s) not found:" + buffer.toString());
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +0900102 }
103 }
104}