[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")));
|
---|
| 106 | assertFalse(set.contains(null));
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | @Test
|
---|
| 110 | public void iteratorTest1() {
|
---|
| 111 | Iterator<DiscreteValue> it = set.iterator();
|
---|
| 112 | assertEquals(A, it.next());
|
---|
| 113 | assertEquals(B, it.next());
|
---|
[605] | 114 | assertThrows(() -> it.next(), NoSuchElementException.class); // check that there is a next.
|
---|
[595] | 115 |
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | @Test
|
---|
| 119 | public void iteratorTest2() {
|
---|
| 120 | Iterator<DiscreteValue> it = setReverse.iterator();
|
---|
| 121 | assertEquals(B, it.next());
|
---|
| 122 | assertEquals(A, it.next());
|
---|
[605] | 123 | assertThrows(() -> it.next(), NoSuchElementException.class); // check that there is a next.
|
---|
[595] | 124 |
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[605] | 127 | /**
|
---|
| 128 | * assert that f throws exception of type e
|
---|
| 129 | *
|
---|
| 130 | * @param f the function to be called, wrapped as {@link Runnable}
|
---|
| 131 | * @param e the {@link Class} of expected exception
|
---|
| 132 | */
|
---|
| 133 | private static void assertThrows(Runnable f, Class<? extends Throwable> e) {
|
---|
| 134 | try {
|
---|
| 135 | f.run();
|
---|
| 136 | throw new AssertionError("Expected " + e.getSimpleName()
|
---|
| 137 | + " but nothing was thrown");
|
---|
| 138 | } catch (Throwable actuale) {
|
---|
| 139 | if (actuale.getClass() != e) {
|
---|
| 140 | throw new AssertionError("Expected " + e.getSimpleName()
|
---|
| 141 | + " but was " + actuale.getClass());
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[595] | 146 | }
|
---|