summaryrefslogtreecommitdiffstats
path: root/tests/AndroidTests/src/com/android/unit_tests/vcard/PropertyNodesVerifier.java
blob: 0afad490b383efc075d069ea810ef3ba607f2de5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.unit_tests.vcard;

import android.content.ContentValues;
import android.pim.vcard.VCardConfig;
import android.pim.vcard.VCardParser;
import android.pim.vcard.VCardParser_V21;
import android.pim.vcard.VCardParser_V30;
import android.pim.vcard.exception.VCardException;
import android.test.AndroidTestCase;

import junit.framework.TestCase;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/* package */ class PropertyNodesVerifier extends VNodeBuilder {
    private final List<PropertyNodesVerifierElem> mPropertyNodesVerifierElemList;
    private final AndroidTestCase mAndroidTestCase;
    private int mIndex;

    public PropertyNodesVerifier(AndroidTestCase testCase) {
        mPropertyNodesVerifierElemList = new ArrayList<PropertyNodesVerifierElem>();
        mAndroidTestCase = testCase;
    }

    public PropertyNodesVerifierElem addPropertyNodesVerifierElem() {
        PropertyNodesVerifierElem elem = new PropertyNodesVerifierElem(mAndroidTestCase);
        mPropertyNodesVerifierElemList.add(elem);
        return elem;
    }

    public void verify(int resId, int vCardType)
            throws IOException, VCardException {
        verify(mAndroidTestCase.getContext().getResources().openRawResource(resId), vCardType);
    }

    public void verify(int resId, int vCardType, final VCardParser vCardParser)
            throws IOException, VCardException {
        verify(mAndroidTestCase.getContext().getResources().openRawResource(resId),
                vCardType, vCardParser);
    }

    public void verify(InputStream is, int vCardType) throws IOException, VCardException {
        final VCardParser vCardParser;
        if (VCardConfig.isV30(vCardType)) {
            vCardParser = new VCardParser_V30(true);  // Use StrictParsing.
        } else {
            vCardParser = new VCardParser_V21();
        }
        verify(is, vCardType, vCardParser);
    }

    public void verify(InputStream is, int vCardType, final VCardParser vCardParser)
            throws IOException, VCardException {
        try {
            vCardParser.parse(is, this);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public void endEntry() {
        super.endEntry();
        mAndroidTestCase.assertTrue(mIndex < mPropertyNodesVerifierElemList.size());
        mAndroidTestCase.assertTrue(mIndex < vNodeList.size());
        mPropertyNodesVerifierElemList.get(mIndex).verify(vNodeList.get(mIndex));
        mIndex++;
    }
}

/**
 * Utility class which verifies input VNode.
 *
 * This class first checks whether each propertyNode in the VNode is in the
 * "ordered expected property list".
 * If the node does not exist in the "ordered list", the class refers to
 * "unorderd expected property set" and checks the node is expected somewhere.
 */
/* package */ class PropertyNodesVerifierElem {
    public static class TypeSet extends HashSet<String> {
        public TypeSet(String ... array) {
            super(Arrays.asList(array));
        }
    }

    public static class GroupSet extends HashSet<String> {
        public GroupSet(String ... array) {
            super(Arrays.asList(array));
        }
    }

    private final HashMap<String, List<PropertyNode>> mOrderedNodeMap;
    // Intentionally use ArrayList instead of Set, assuming there may be more than one
    // exactly same objects.
    private final ArrayList<PropertyNode> mUnorderedNodeList;
    private final TestCase mTestCase;

    public PropertyNodesVerifierElem(TestCase testCase) {
        mOrderedNodeMap = new HashMap<String, List<PropertyNode>>();
        mUnorderedNodeList = new ArrayList<PropertyNode>();
        mTestCase = testCase;
    }

    // WithOrder

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(String propName, String propValue) {
        return addExpectedNodeWithOrder(propName, propValue, null, null, null, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(
            String propName, String propValue, ContentValues contentValues) {
        return addExpectedNodeWithOrder(propName, propValue, null,
                null, contentValues, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(
            String propName, List<String> propValueList, ContentValues contentValues) {
        return addExpectedNodeWithOrder(propName, null, propValueList,
                null, contentValues, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(
            String propName, String propValue, List<String> propValueList) {
        return addExpectedNodeWithOrder(propName, propValue, propValueList, null,
                null, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(
            String propName, List<String> propValueList) {
        final String propValue = concatinateListWithSemiColon(propValueList);
        return addExpectedNodeWithOrder(propName, propValue.toString(), propValueList,
                null, null, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(String propName, String propValue,
            TypeSet paramMap_TYPE) {
        return addExpectedNodeWithOrder(propName, propValue, null,
                null, null, paramMap_TYPE, null);
    }

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(String propName,
            List<String> propValueList, TypeSet paramMap_TYPE) {
        return addExpectedNodeWithOrder(propName, null, propValueList, null, null,
                paramMap_TYPE, null);
    }

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(String propName, String propValue,
            ContentValues paramMap, TypeSet paramMap_TYPE) {
        return addExpectedNodeWithOrder(propName, propValue, null, null,
                paramMap, paramMap_TYPE, null);
    }

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(String propName, String propValue,
            List<String> propValueList, TypeSet paramMap_TYPE) {
        return addExpectedNodeWithOrder(propName, propValue, propValueList, null, null,
                paramMap_TYPE, null);
    }

    public PropertyNodesVerifierElem addExpectedNodeWithOrder(String propName, String propValue,
            List<String> propValueList, byte[] propValue_bytes,
            ContentValues paramMap, TypeSet paramMap_TYPE, GroupSet propGroupSet) {
        if (propValue == null && propValueList != null) {
            propValue = concatinateListWithSemiColon(propValueList);
        }
        PropertyNode propertyNode = new PropertyNode(propName,
                propValue, propValueList, propValue_bytes,
                paramMap, paramMap_TYPE, propGroupSet);
        List<PropertyNode> expectedNodeList = mOrderedNodeMap.get(propName);
        if (expectedNodeList == null) {
            expectedNodeList = new ArrayList<PropertyNode>();
            mOrderedNodeMap.put(propName, expectedNodeList);
        }
        expectedNodeList.add(propertyNode);
        return this;
    }

    // WithoutOrder

    public PropertyNodesVerifierElem addExpectedNode(String propName, String propValue) {
        return addExpectedNode(propName, propValue, null, null, null, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNode(String propName, String propValue,
            ContentValues contentValues) {
        return addExpectedNode(propName, propValue, null, null, contentValues, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNode(String propName,
            List<String> propValueList, ContentValues contentValues) {
        return addExpectedNode(propName, null,
                propValueList, null, contentValues, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNode(String propName, String propValue,
            List<String> propValueList) {
        return addExpectedNode(propName, propValue, propValueList, null, null, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNode(String propName,
            List<String> propValueList) {
        return addExpectedNode(propName, null, propValueList,
                null, null, null, null);
    }

    public PropertyNodesVerifierElem addExpectedNode(String propName, String propValue,
            TypeSet paramMap_TYPE) {
        return addExpectedNode(propName, propValue, null, null, null, paramMap_TYPE, null);
    }

    public PropertyNodesVerifierElem addExpectedNode(String propName,
            List<String> propValueList, TypeSet paramMap_TYPE) {
        final String propValue = concatinateListWithSemiColon(propValueList);
        return addExpectedNode(propName, propValue, propValueList, null, null,
                paramMap_TYPE, null);
    }

    public PropertyNodesVerifierElem addExpectedNode(String propName, String propValue,
            List<String> propValueList, TypeSet paramMap_TYPE) {
        return addExpectedNode(propName, propValue, propValueList, null, null,
                paramMap_TYPE, null);
    }

    public PropertyNodesVerifierElem addExpectedNode(String propName, String propValue,
            ContentValues paramMap, TypeSet paramMap_TYPE) {
        return addExpectedNode(propName, propValue, null, null,
                paramMap, paramMap_TYPE, null);
    }

    public PropertyNodesVerifierElem addExpectedNode(String propName, String propValue,
            List<String> propValueList, byte[] propValue_bytes,
            ContentValues paramMap, TypeSet paramMap_TYPE, GroupSet propGroupSet) {
        if (propValue == null && propValueList != null) {
            propValue = concatinateListWithSemiColon(propValueList);
        }
        mUnorderedNodeList.add(new PropertyNode(propName, propValue,
                propValueList, propValue_bytes, paramMap, paramMap_TYPE, propGroupSet));
        return this;
    }

    public void verify(VNode vnode) {
        for (PropertyNode actualNode : vnode.propList) {
            verifyNode(actualNode.propName, actualNode);
        }
        if (!mOrderedNodeMap.isEmpty() || !mUnorderedNodeList.isEmpty()) {
            List<String> expectedProps = new ArrayList<String>();
            for (List<PropertyNode> nodes : mOrderedNodeMap.values()) {
                for (PropertyNode node : nodes) {
                    if (!expectedProps.contains(node.propName)) {
                        expectedProps.add(node.propName);
                    }
                }
            }
            for (PropertyNode node : mUnorderedNodeList) {
                if (!expectedProps.contains(node.propName)) {
                    expectedProps.add(node.propName);
                }
            }
            mTestCase.fail("Expected property " + Arrays.toString(expectedProps.toArray())
                    + " was not found.");
        }
    }

    private void verifyNode(final String propName, final PropertyNode actualNode) {
        List<PropertyNode> expectedNodeList = mOrderedNodeMap.get(propName);
        final int size = (expectedNodeList != null ? expectedNodeList.size() : 0);
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                PropertyNode expectedNode = expectedNodeList.get(i);
                List<PropertyNode> expectedButDifferentValueList = new ArrayList<PropertyNode>();
                if (expectedNode.propName.equals(propName)) {
                    if (expectedNode.equals(actualNode)) {
                        expectedNodeList.remove(i);
                        if (expectedNodeList.size() == 0) {
                            mOrderedNodeMap.remove(propName);
                        }
                        return;
                    } else {
                        expectedButDifferentValueList.add(expectedNode);
                    }
                }

                // "actualNode" is not in ordered expected list.
                // Try looking over unordered expected list.
                if (tryFoundExpectedNodeFromUnorderedList(actualNode,
                        expectedButDifferentValueList)) {
                    return;
                }

                if (!expectedButDifferentValueList.isEmpty()) {
                    // Same propName exists but with different value(s).
                    failWithExpectedNodeList(propName, actualNode,
                            expectedButDifferentValueList);
                } else {
                    // There's no expected node with same propName.
                    mTestCase.fail("Unexpected property \"" + propName + "\" exists.");
                }
            }
        } else {
            List<PropertyNode> expectedButDifferentValueList =
                new ArrayList<PropertyNode>();
            if (tryFoundExpectedNodeFromUnorderedList(actualNode, expectedButDifferentValueList)) {
                return;
            } else {
                if (!expectedButDifferentValueList.isEmpty()) {
                    // Same propName exists but with different value(s).
                    failWithExpectedNodeList(propName, actualNode,
                            expectedButDifferentValueList);
                } else {
                    // There's no expected node with same propName.
                    mTestCase.fail("Unexpected property \"" + propName + "\" exists.");
                }
            }
        }
    }

    private String concatinateListWithSemiColon(List<String> array) {
        StringBuffer buffer = new StringBuffer();
        boolean first = true;
        for (String propValueElem : array) {
            if (first) {
                first = false;
            } else {
                buffer.append(';');
            }
            buffer.append(propValueElem);
        }

        return buffer.toString();
    }

    private boolean tryFoundExpectedNodeFromUnorderedList(PropertyNode actualNode,
            List<PropertyNode> expectedButDifferentValueList) {
        final String propName = actualNode.propName;
        int unorderedListSize = mUnorderedNodeList.size();
        for (int i = 0; i < unorderedListSize; i++) {
            PropertyNode unorderedExpectedNode = mUnorderedNodeList.get(i);
            if (unorderedExpectedNode.propName.equals(propName)) {
                if (unorderedExpectedNode.equals(actualNode)) {
                    mUnorderedNodeList.remove(i);
                    return true;
                }
                expectedButDifferentValueList.add(unorderedExpectedNode);
            }
        }
        return false;
    }

    private void failWithExpectedNodeList(String propName, PropertyNode actualNode,
            List<PropertyNode> expectedNodeList) {
        StringBuilder builder = new StringBuilder();
        for (PropertyNode expectedNode : expectedNodeList) {
            builder.append("expected: ");
            builder.append(expectedNode.toString());
            builder.append("\n");
        }
        mTestCase.fail("Property \"" + propName + "\" has wrong value.\n"
                + builder.toString()
                + "  actual: " + actualNode.toString());
    }
}