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 |
|
---|
14 | import org.junit.Test;
|
---|
15 |
|
---|
16 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
17 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
18 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
19 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
20 |
|
---|
21 | public class DiscreteValueSetTest {
|
---|
22 |
|
---|
23 | private static final DiscreteValue A = new DiscreteValue("a");
|
---|
24 | private static final DiscreteValue A1 = new DiscreteValue("a");
|
---|
25 | private static final DiscreteValue B = new DiscreteValue("b");
|
---|
26 | private final static List<DiscreteValue> values = Arrays.asList(A, B);
|
---|
27 | private final static Collection<DiscreteValue> valuesReverse = Arrays
|
---|
28 | .asList(B, A);
|
---|
29 |
|
---|
30 | private static final DiscreteValueSet set = new DiscreteValueSet(values);
|
---|
31 | private static final DiscreteValueSet setReverse = new DiscreteValueSet(
|
---|
32 | valuesReverse);
|
---|
33 | private static final String asString = "{\"values\":[\"a\",\"b\"]}";
|
---|
34 | // private static final String asString = "{\"DiscreteValueSet
|
---|
35 | // \":[\"a\",\"b\"]}";
|
---|
36 | private static final String asString1 = "[\"a\",\"b\"]";
|
---|
37 |
|
---|
38 | // SPECIAL. note that the "numbers" have no leading "=" and therefore are
|
---|
39 | // parsed as discrete values.
|
---|
40 | private static final String discretenumbers = "{\"values\":[\"0\",\"1\"]}";
|
---|
41 | private static final ObjectMapper jackson = new ObjectMapper();
|
---|
42 |
|
---|
43 | @Test
|
---|
44 | public void testSerialize() throws JsonProcessingException {
|
---|
45 |
|
---|
46 | String json = jackson.writeValueAsString(set);
|
---|
47 | System.out.println(json);
|
---|
48 | assertEquals(asString, json);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Test
|
---|
52 | public void testDeserialize()
|
---|
53 | throws JsonParseException, JsonMappingException, IOException {
|
---|
54 | DiscreteValueSet p = jackson.readValue(asString,
|
---|
55 | DiscreteValueSet.class);
|
---|
56 | System.out.println(p);
|
---|
57 | assertEquals(set, p);
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Test
|
---|
61 | public void testDeserializeNumberValues()
|
---|
62 | throws JsonParseException, JsonMappingException, IOException {
|
---|
63 | DiscreteValueSet p = jackson.readValue(discretenumbers,
|
---|
64 | DiscreteValueSet.class);
|
---|
65 | System.out.println(p);
|
---|
66 | assertEquals("0", p.get(BigInteger.ZERO).getValue()); // not the number
|
---|
67 | // but string
|
---|
68 | assertEquals("1", p.get(BigInteger.ONE).getValue());
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Test
|
---|
72 | public void testEquality() {
|
---|
73 | assertEquals(set, setReverse);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Test
|
---|
77 | public void testHashCodeInsensitiveToOrder() {
|
---|
78 | assertEquals(set.hashCode(), setReverse.hashCode());
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Test
|
---|
82 | public void testSize() {
|
---|
83 | assertEquals(new BigInteger("2"), set.size());
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Test
|
---|
87 | public void testGet() {
|
---|
88 | assertEquals(A, set.get(0));
|
---|
89 | assertEquals(B, set.get(1));
|
---|
90 | assertEquals(A, set.get(BigInteger.ZERO));
|
---|
91 | assertEquals(B, set.get(BigInteger.ONE));
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Test
|
---|
95 | public void getValuesTest() {
|
---|
96 | assertEquals(values, set.getValues());
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Test
|
---|
100 | public void testConstructor2() {
|
---|
101 | assertEquals(set, new DiscreteValueSet(A, B));
|
---|
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(null));
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Test
|
---|
114 | public void iteratorTest1() {
|
---|
115 | Iterator<DiscreteValue> it = set.iterator();
|
---|
116 | assertTrue(it.hasNext());
|
---|
117 | assertEquals(A, it.next());
|
---|
118 | assertTrue(it.hasNext());
|
---|
119 | assertEquals(B, it.next());
|
---|
120 | assertFalse(it.hasNext());
|
---|
121 |
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Test
|
---|
125 | public void iteratorTest2() {
|
---|
126 | Iterator<DiscreteValue> it = setReverse.iterator();
|
---|
127 | assertTrue(it.hasNext());
|
---|
128 | assertEquals(B, it.next());
|
---|
129 | assertTrue(it.hasNext());
|
---|
130 | assertEquals(A, it.next());
|
---|
131 | assertFalse(it.hasNext());
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | }
|
---|