[595] | 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;
|
---|
[605] | 13 | import java.util.NoSuchElementException;
|
---|
[595] | 14 |
|
---|
| 15 | import org.junit.Test;
|
---|
| 16 |
|
---|
| 17 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 18 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 19 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 20 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 21 |
|
---|
| 22 | public 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 |
|
---|
[786] | 47 | String jsonstr = jackson.writeValueAsString(set);
|
---|
[732] | 48 | //System.out.println(json);
|
---|
[786] | 49 | assertEquals(asString, jsonstr);
|
---|
[595] | 50 | }
|
---|
| 51 |
|
---|
| 52 | @Test
|
---|
| 53 | public void testDeserialize()
|
---|
| 54 | throws JsonParseException, JsonMappingException, IOException {
|
---|
| 55 | DiscreteValueSet p = jackson.readValue(asString,
|
---|
| 56 | DiscreteValueSet.class);
|
---|
[732] | 57 | //System.out.println(p);
|
---|
[595] | 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);
|
---|
[732] | 66 | //System.out.println(p);
|
---|
[595] | 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")));
|
---|
[805] | 106 | assertFalse(set.contains(new DiscreteValue("21112")));
|
---|
| 107 | assertFalse(set.contains(new NumberValue("21112")));
|
---|
[595] | 108 | }
|
---|
| 109 |
|
---|
| 110 | @Test
|
---|
| 111 | public void iteratorTest1() {
|
---|
| 112 | Iterator<DiscreteValue> it = set.iterator();
|
---|
| 113 | assertEquals(A, it.next());
|
---|
| 114 | assertEquals(B, it.next());
|
---|
[605] | 115 | assertThrows(() -> it.next(), NoSuchElementException.class); // check that there is a next.
|
---|
[595] | 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());
|
---|
[605] | 124 | assertThrows(() -> it.next(), NoSuchElementException.class); // check that there is a next.
|
---|
[595] | 125 |
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[605] | 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 |
|
---|
[595] | 147 | }
|
---|