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