PropertyNodesVerifier.java revision 1de396f6df89363169d3a2e61a61fa98d12c1ef8
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        parser.addInterpreter(this);
64        try {
65            parser.parse(is);
66        } finally {
67            if (is != null) {
68                try {
69                    is.close();
70                } catch (IOException e) {
71                }
72            }
73        }
74    }
75
76    @Override
77    public void onEntryStarted() {
78        super.onEntryStarted();
79        AndroidTestCase.assertTrue(mIndex < mPropertyNodesVerifierElemList.size());
80    }
81
82    @Override
83    public void onEntryEnded() {
84        mPropertyNodesVerifierElemList.get(mIndex).verify(getCurrentVNode());
85        super.onEntryEnded();
86        mIndex++;
87    }
88}
89