PropertyNodesVerifier.java revision 677ef21613a9d35053ec098444832ce4125a847e
1/*
2 * Copyright (C) 2009 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 */
16package com.android.vcard.tests.testutils;
17
18import android.test.AndroidTestCase;
19
20import com.android.vcard.VCardParser;
21import com.android.vcard.VCardUtils;
22import com.android.vcard.exception.VCardException;
23
24import java.io.IOException;
25import java.io.InputStream;
26import java.util.ArrayList;
27import java.util.List;
28
29public class PropertyNodesVerifier extends VNodeBuilder {
30    private final List<PropertyNodesVerifierElem> mPropertyNodesVerifierElemList;
31    private final AndroidTestCase mAndroidTestCase;
32    private int mIndex;
33
34    public PropertyNodesVerifier(AndroidTestCase testCase) {
35        super();
36        mPropertyNodesVerifierElemList = new ArrayList<PropertyNodesVerifierElem>();
37        mAndroidTestCase = testCase;
38    }
39
40    public PropertyNodesVerifierElem addPropertyNodesVerifierElem() {
41        PropertyNodesVerifierElem elem = new PropertyNodesVerifierElem(mAndroidTestCase);
42        mPropertyNodesVerifierElemList.add(elem);
43        return elem;
44    }
45
46    public void verify(int resId, int vcardType) throws IOException, VCardException {
47        verify(mAndroidTestCase.getContext().getResources().openRawResource(resId), vcardType);
48    }
49
50    public void verify(int resId, int vcardType, final VCardParser parser)
51            throws IOException, VCardException {
52        verify(mAndroidTestCase.getContext().getResources().openRawResource(resId),
53                vcardType, parser);
54    }
55
56    public void verify(InputStream is, int vcardType) throws IOException, VCardException {
57        final VCardParser parser = VCardUtils.getAppropriateParser(vcardType);
58        verify(is, vcardType, parser);
59    }
60
61    public void verify(InputStream is, int vcardType, final VCardParser parser)
62            throws IOException, VCardException {
63        try {
64            parser.parse(is, this);
65        } finally {
66            if (is != null) {
67                try {
68                    is.close();
69                } catch (IOException e) {
70                }
71            }
72        }
73    }
74
75    @Override
76    public void endEntry() {
77        super.endEntry();
78        AndroidTestCase.assertTrue(mIndex < mPropertyNodesVerifierElemList.size());
79        AndroidTestCase.assertTrue(mIndex < vNodeList.size());
80        mPropertyNodesVerifierElemList.get(mIndex).verify(vNodeList.get(mIndex));
81        mIndex++;
82    }
83}
84