source: java2python/geniuswebtranslator/geniuswebtest/geniusweb/issuevalue/DiscreteValueSetTest.java@ 805

Last change on this file since 805 was 805, checked in by wouter, 6 months ago

#278 fixing @NonNull annotations

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