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

Last change on this file since 889 was 860, checked in by wouter, 5 months ago

#264 small fixes

File size: 4.5 KB
RevLine 
[595]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;
[605]13import java.util.NoSuchElementException;
[595]14
[860]15import org.eclipse.jdt.annotation.NonNull;
[595]16import org.junit.Test;
17
18import com.fasterxml.jackson.core.JsonParseException;
19import com.fasterxml.jackson.core.JsonProcessingException;
20import com.fasterxml.jackson.databind.JsonMappingException;
21import com.fasterxml.jackson.databind.ObjectMapper;
22
23public class DiscreteValueSetTest {
24
[860]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
[595]31 .asList(B, A);
32
[860]33 private static final @NonNull DiscreteValueSet set = new DiscreteValueSet(
34 values);
35 private static final @NonNull DiscreteValueSet setReverse = new DiscreteValueSet(
[595]36 valuesReverse);
[860]37 private static final @NonNull String asString = "{\"values\":[\"a\",\"b\"]}";
[595]38 // private static final String asString = "{\"DiscreteValueSet
39 // \":[\"a\",\"b\"]}";
[860]40 private static final @NonNull String asString1 = "[\"a\",\"b\"]";
[595]41
42 // SPECIAL. note that the "numbers" have no leading "=" and therefore are
43 // parsed as discrete values.
[860]44 private static final @NonNull String discretenumbers = "{\"values\":[\"0\",\"1\"]}";
45 private static final @NonNull ObjectMapper jackson = new ObjectMapper();
[595]46
47 @Test
48 public void testSerialize() throws JsonProcessingException {
49
[860]50 final @NonNull String jsonstr = jackson.writeValueAsString(set);
[732]51 //System.out.println(json);
[786]52 assertEquals(asString, jsonstr);
[595]53 }
54
55 @Test
56 public void testDeserialize()
57 throws JsonParseException, JsonMappingException, IOException {
[860]58 final @NonNull DiscreteValueSet p = jackson.readValue(asString,
[595]59 DiscreteValueSet.class);
[732]60 //System.out.println(p);
[595]61 assertEquals(set, p);
62 }
63
64 @Test
65 public void testDeserializeNumberValues()
66 throws JsonParseException, JsonMappingException, IOException {
[860]67 final @NonNull DiscreteValueSet p = jackson.readValue(discretenumbers,
[595]68 DiscreteValueSet.class);
[732]69 //System.out.println(p);
[595]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() {
[860]100 assertTrue(values.equals(set.getValues())
101 || valuesReverse.equals(set.getValues()));
[595]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")));
[805]110 assertFalse(set.contains(new DiscreteValue("21112")));
111 assertFalse(set.contains(new NumberValue("21112")));
[595]112 }
113
114 @Test
115 public void iteratorTest1() {
116 Iterator<DiscreteValue> it = set.iterator();
117 assertEquals(A, it.next());
118 assertEquals(B, it.next());
[605]119 assertThrows(() -> it.next(), NoSuchElementException.class); // check that there is a next.
[595]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());
[605]128 assertThrows(() -> it.next(), NoSuchElementException.class); // check that there is a next.
[595]129
130 }
131
[605]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
[595]151}
Note: See TracBrowser for help on using the repository browser.