1 | package geniusweb.issuevalue;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.math.BigInteger;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Iterator;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.NoSuchElementException;
|
---|
14 |
|
---|
15 | import org.eclipse.jdt.annotation.NonNull;
|
---|
16 | import org.junit.Test;
|
---|
17 |
|
---|
18 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
19 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
20 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
21 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
22 |
|
---|
23 | public class DiscreteValueSetTest {
|
---|
24 |
|
---|
25 | private static final @NonNull DiscreteValue A = new DiscreteValue("a");
|
---|
26 | private static final @NonNull DiscreteValue A1 = new DiscreteValue("a");
|
---|
27 | private static final @NonNull DiscreteValue B = new DiscreteValue("b");
|
---|
28 | private final static @NonNull List<DiscreteValue> values = Arrays.asList(A,
|
---|
29 | B);
|
---|
30 | private final static @NonNull Collection<DiscreteValue> valuesReverse = Arrays
|
---|
31 | .asList(B, A);
|
---|
32 |
|
---|
33 | private static final @NonNull DiscreteValueSet set = new DiscreteValueSet(
|
---|
34 | values);
|
---|
35 | private static final @NonNull DiscreteValueSet setReverse = new DiscreteValueSet(
|
---|
36 | valuesReverse);
|
---|
37 | private static final @NonNull String asString = "{\"values\":[\"a\",\"b\"]}";
|
---|
38 | // private static final String asString = "{\"DiscreteValueSet
|
---|
39 | // \":[\"a\",\"b\"]}";
|
---|
40 | private static final @NonNull String asString1 = "[\"a\",\"b\"]";
|
---|
41 |
|
---|
42 | // SPECIAL. note that the "numbers" have no leading "=" and therefore are
|
---|
43 | // parsed as discrete values.
|
---|
44 | private static final @NonNull String discretenumbers = "{\"values\":[\"0\",\"1\"]}";
|
---|
45 | private static final @NonNull ObjectMapper jackson = new ObjectMapper();
|
---|
46 |
|
---|
47 | @Test
|
---|
48 | public void testSerialize() throws JsonProcessingException {
|
---|
49 |
|
---|
50 | final @NonNull String jsonstr = jackson.writeValueAsString(set);
|
---|
51 | //System.out.println(json);
|
---|
52 | assertEquals(asString, jsonstr);
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Test
|
---|
56 | public void testDeserialize()
|
---|
57 | throws JsonParseException, JsonMappingException, IOException {
|
---|
58 | final @NonNull DiscreteValueSet p = jackson.readValue(asString,
|
---|
59 | DiscreteValueSet.class);
|
---|
60 | //System.out.println(p);
|
---|
61 | assertEquals(set, p);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Test
|
---|
65 | public void testDeserializeNumberValues()
|
---|
66 | throws JsonParseException, JsonMappingException, IOException {
|
---|
67 | final @NonNull DiscreteValueSet p = jackson.readValue(discretenumbers,
|
---|
68 | DiscreteValueSet.class);
|
---|
69 | //System.out.println(p);
|
---|
70 | assertEquals("0", p.get(BigInteger.ZERO).getValue()); // not the number
|
---|
71 | // but string
|
---|
72 | assertEquals("1", p.get(BigInteger.ONE).getValue());
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Test
|
---|
76 | public void testEquality() {
|
---|
77 | assertEquals(set, setReverse);
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Test
|
---|
81 | public void testHashCodeInsensitiveToOrder() {
|
---|
82 | assertEquals(set.hashCode(), setReverse.hashCode());
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Test
|
---|
86 | public void testSize() {
|
---|
87 | assertEquals(new BigInteger("2"), set.size());
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Test
|
---|
91 | public void testGet() {
|
---|
92 | assertEquals(A, set.get(0));
|
---|
93 | assertEquals(B, set.get(1));
|
---|
94 | assertEquals(A, set.get(BigInteger.ZERO));
|
---|
95 | assertEquals(B, set.get(BigInteger.ONE));
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Test
|
---|
99 | public void getValuesTest() {
|
---|
100 | assertTrue(values.equals(set.getValues())
|
---|
101 | || valuesReverse.equals(set.getValues()));
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Test
|
---|
105 | public void containsTest() {
|
---|
106 | assertTrue(set.contains(A));
|
---|
107 | assertTrue(set.contains(A1));
|
---|
108 | assertTrue(set.contains(B));
|
---|
109 | assertFalse(set.contains(new DiscreteValue("c")));
|
---|
110 | assertFalse(set.contains(new DiscreteValue("21112")));
|
---|
111 | assertFalse(set.contains(new NumberValue("21112")));
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Test
|
---|
115 | public void iteratorTest1() {
|
---|
116 | Iterator<DiscreteValue> it = set.iterator();
|
---|
117 | assertEquals(A, it.next());
|
---|
118 | assertEquals(B, it.next());
|
---|
119 | assertThrows(() -> it.next(), NoSuchElementException.class); // check that there is a next.
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Test
|
---|
124 | public void iteratorTest2() {
|
---|
125 | Iterator<DiscreteValue> it = setReverse.iterator();
|
---|
126 | assertEquals(B, it.next());
|
---|
127 | assertEquals(A, it.next());
|
---|
128 | assertThrows(() -> it.next(), NoSuchElementException.class); // check that there is a next.
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * assert that f throws exception of type e
|
---|
134 | *
|
---|
135 | * @param f the function to be called, wrapped as {@link Runnable}
|
---|
136 | * @param e the {@link Class} of expected exception
|
---|
137 | */
|
---|
138 | private static void assertThrows(Runnable f, Class<? extends Throwable> e) {
|
---|
139 | try {
|
---|
140 | f.run();
|
---|
141 | throw new AssertionError("Expected " + e.getSimpleName()
|
---|
142 | + " but nothing was thrown");
|
---|
143 | } catch (Throwable actuale) {
|
---|
144 | if (actuale.getClass() != e) {
|
---|
145 | throw new AssertionError("Expected " + e.getSimpleName()
|
---|
146 | + " but was " + actuale.getClass());
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|