| 1 | package ezvcard.property.asserter; |
| 2 | |
| 3 | import static java.util.Arrays.asList; |
| 4 | import static org.junit.Assert.assertEquals; |
| 5 | import static org.junit.Assert.assertFalse; |
| 6 | |
| 7 | import java.util.Iterator; |
| 8 | import java.util.List; |
| 9 | |
| 10 | import ezvcard.property.VCardProperty; |
| 11 | |
| 12 | /* |
| 13 | Copyright (c) 2012-2023, Michael Angstadt |
| 14 | All rights reserved. |
| 15 | |
| 16 | Redistribution and use in source and binary forms, with or without |
| 17 | modification, are permitted provided that the following conditions are met: |
| 18 | |
| 19 | 1. Redistributions of source code must retain the above copyright notice, this |
| 20 | list of conditions and the following disclaimer. |
| 21 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 22 | this list of conditions and the following disclaimer in the documentation |
| 23 | and/or other materials provided with the distribution. |
| 24 | |
| 25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 26 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 27 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 29 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 30 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 32 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | |
| 36 | The views and conclusions contained in the software and documentation are those |
| 37 | of the authors and should not be interpreted as representing official policies, |
| 38 | either expressed or implied, of the FreeBSD Project. |
| 39 | */ |
| 40 | |
| 41 | /** |
| 42 | * @author Michael Angstadt |
| 43 | */ |
| 44 | public abstract class PropertyImplAsserter<T, P extends VCardProperty> { |
| 45 | @SuppressWarnings("unchecked") |
| 46 | protected final T this_ = (T) this; |
| 47 | private final Iterator<P> it; |
| 48 | private final VCardAsserter asserter; |
| 49 | |
| 50 | protected P expected; |
| 51 | |
| 52 | public PropertyImplAsserter(List<P> list, VCardAsserter asserter) { |
| 53 | it = list.iterator(); |
| 54 | this.asserter = asserter; |
| 55 | expected = _newInstance(); |
| 56 | } |
| 57 | |
| 58 | public T expected(P expected) { |
| 59 | this.expected = expected; |
| 60 | return this_; |
| 61 | } |
| 62 | |
| 63 | public T group(String group) { |
| 64 | expected.setGroup(group); |
| 65 | return this_; |
| 66 | } |
| 67 | |
| 68 | public T param(String name, String... values) { |
| 69 | expected.getParameters().putAll(name, asList(values)); |
| 70 | return this_; |
| 71 | } |
| 72 | |
| 73 | public T next() { |
| 74 | P actual = it.next(); |
| 75 | assertEquals(expected, actual); |
| 76 | asserter.incPropertiesChecked(); |
| 77 | |
| 78 | expected = _newInstance(); |
| 79 | return this_; |
| 80 | } |
| 81 | |
| 82 | public void noMore() { |
| 83 | next(); |
| 84 | assertFalse(it.hasNext()); |
| 85 | } |
| 86 | |
| 87 | protected abstract P _newInstance(); |
| 88 | } |