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

Last change on this file since 749 was 732, checked in by wouter, 12 months ago

#169 unittests are actually translating and running! They do not succeed but that's a different matter

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 json = jackson.writeValueAsString(set);
48 //System.out.println(json);
49 assertEquals(asString, json);
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 testConstructor2() {
102 assertEquals(set, new DiscreteValueSet(A, B));
103 }
104
105 @Test
106 public void containsTest() {
107 assertTrue(set.contains(A));
108 assertTrue(set.contains(A1));
109 assertTrue(set.contains(B));
110 assertFalse(set.contains(new DiscreteValue("c")));
111 assertFalse(set.contains(null));
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}
Note: See TracBrowser for help on using the repository browser.