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 */
16package com.android.vcard.tests.testutils;
17
18import android.test.AndroidTestCase;
19import android.text.TextUtils;
20
21import com.android.vcard.VCardConfig;
22
23import junit.framework.TestCase;
24
25import java.util.ArrayList;
26import java.util.List;
27
28public class LineVerifierElem {
29    private final List<String> mExpectedLineList = new ArrayList<String>();
30    private final int mVCardType;
31
32    public LineVerifierElem(AndroidTestCase androidTestCase, int vcardType) {
33        mVCardType = vcardType;
34    }
35
36    public LineVerifierElem addExpected(final String line) {
37        if (!TextUtils.isEmpty(line)) {
38            mExpectedLineList.add(line);
39        }
40        return this;
41    }
42
43    public void verify(final String vcard) {
44        final String[] lineArray = vcard.split("\\r?\\n");
45        final int length = lineArray.length;
46        boolean beginExists = false;
47        boolean endExists = false;
48        boolean versionExists = false;
49
50        for (int i = 0; i < length; i++) {
51            final String line = lineArray[i];
52            if (TextUtils.isEmpty(line)) {
53                continue;
54            }
55
56            if ("BEGIN:VCARD".equalsIgnoreCase(line)) {
57                if (beginExists) {
58                    TestCase.fail("Multiple \"BEGIN:VCARD\" line found");
59                } else {
60                    beginExists = true;
61                    continue;
62                }
63            } else if ("END:VCARD".equalsIgnoreCase(line)) {
64                if (endExists) {
65                    TestCase.fail("Multiple \"END:VCARD\" line found");
66                } else {
67                    endExists = true;
68                    continue;
69                }
70            } else if ((VCardConfig.isVersion21(mVCardType) ? "VERSION:2.1" :
71                (VCardConfig.isVersion30(mVCardType) ? "VERSION:3.0" :
72                    "VERSION:4.0")).equalsIgnoreCase(line)) {
73                if (versionExists) {
74                    TestCase.fail("Multiple VERSION line + found");
75                } else {
76                    versionExists = true;
77                    continue;
78                }
79            }
80
81            if (!beginExists) {
82                TestCase.fail("Property other than BEGIN came before BEGIN property: " + line);
83            } else if (endExists) {
84                TestCase.fail("Property other than END came after END property: " + line);
85            }
86
87            final int index = mExpectedLineList.indexOf(line);
88            if (index >= 0) {
89                mExpectedLineList.remove(index);
90            } else {
91                TestCase.fail("Unexpected line: " + line);
92            }
93        }
94
95        if (!mExpectedLineList.isEmpty()) {
96            StringBuffer buffer = new StringBuffer();
97            for (String expectedLine : mExpectedLineList) {
98                buffer.append(expectedLine);
99                buffer.append("\n");
100            }
101
102            TestCase.fail("Expected line(s) not found:" + buffer.toString());
103        }
104    }
105}
106