Debian Codegraph @

src/test/java/ezvcard/parameter/EnumParameterListTest.java

raw symbols SCIP
1 package ezvcard.parameter;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.fail;
5
6 import java.util.Arrays;
7 import java.util.List;
8
9 import org.junit.Test;
10
11 /*
12 Copyright (c) 2012-2023, Michael Angstadt
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17
18 1. Redistributions of source code must retain the above copyright notice, this
19 list of conditions and the following disclaimer.
20 2. Redistributions in binary form must reproduce the above copyright notice,
21 this list of conditions and the following disclaimer in the documentation
22 and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
28 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /**
37 * @author Michael Angstadt
38 */
39 public class EnumParameterListTest {
40 private final String NAME = "NUM";
41
42 @Test
43 public void get_set() {
44 VCardParameters parameters = new VCardParameters();
45 List<VCardParameterImpl> list = parameters.new EnumParameterList<VCardParameterImpl>(NAME) {
46 @Override
47 protected VCardParameterImpl _asObject(String value) throws Exception {
48 if ("0".equals(value)) {
49 return VCardParameterImpl.ZERO;
50 }
51 if ("1".equals(value)) {
52 return VCardParameterImpl.ONE;
53 }
54 if ("2".equals(value)) {
55 return VCardParameterImpl.TWO;
56 }
57 throw new NumberFormatException();
58 }
59 };
60
61 assertEquals(Arrays.asList(), list);
62 assertEquals(Arrays.asList(), parameters.get(NAME));
63
64 list.add(VCardParameterImpl.ZERO);
65 assertEquals(Arrays.asList(VCardParameterImpl.ZERO), list);
66 assertEquals(Arrays.asList("0"), parameters.get(NAME));
67
68 parameters.put(NAME, "1");
69 assertEquals(Arrays.asList(VCardParameterImpl.ZERO, VCardParameterImpl.ONE), list);
70 assertEquals(Arrays.asList("0", "1"), parameters.get(NAME));
71
72 list.clear();
73 assertEquals(Arrays.asList(), list);
74 assertEquals(Arrays.asList(), parameters.get(NAME));
75
76 list.add(VCardParameterImpl.ZERO);
77 assertEquals(Arrays.asList(VCardParameterImpl.ZERO), list);
78 assertEquals(Arrays.asList("0"), parameters.get(NAME));
79
80 parameters.removeAll(NAME);
81 assertEquals(Arrays.asList(), list);
82 assertEquals(Arrays.asList(), parameters.get(NAME));
83
84 parameters.put(NAME, "3");
85 try {
86 list.get(0);
87 fail();
88 } catch (IllegalStateException e) {
89 assertEquals(e.getCause().getClass(), NumberFormatException.class);
90 }
91 }
92
93 private static class VCardParameterImpl extends VCardParameter {
94 public static final VCardParameterImpl ZERO = new VCardParameterImpl("0");
95 public static final VCardParameterImpl ONE = new VCardParameterImpl("1");
96 public static final VCardParameterImpl TWO = new VCardParameterImpl("2");
97
98 public VCardParameterImpl(String value) {
99 super(value);
100 }
101 }
102 }