|
14 | 14 |
|
15 | 15 | package com.fasterxml.jackson.dataformat.ion;
|
16 | 16 |
|
17 |
| -import com.fasterxml.jackson.core.JsonParser; |
18 |
| -import com.fasterxml.jackson.core.JsonToken; |
19 |
| -import com.fasterxml.jackson.core.StreamReadCapability; |
| 17 | +import com.fasterxml.jackson.core.*; |
20 | 18 |
|
21 | 19 | import org.junit.Assert;
|
22 | 20 |
|
|
28 | 26 | import java.io.IOException;
|
29 | 27 | import java.math.BigDecimal;
|
30 | 28 | import java.math.BigInteger;
|
| 29 | +import java.util.List; |
| 30 | + |
31 | 31 | import org.junit.Test;
|
32 | 32 |
|
| 33 | +import static org.junit.Assert.assertEquals; |
| 34 | + |
33 | 35 | @SuppressWarnings("resource")
|
34 | 36 | public class IonParserTest
|
35 | 37 | {
|
@@ -125,4 +127,109 @@ public void testParserCapabilities() throws Exception {
|
125 | 127 | }
|
126 | 128 | }
|
127 | 129 |
|
| 130 | + |
| 131 | + @Test(expected = JsonParseException.class) |
| 132 | + public void testIonExceptionIsWrapped() throws IOException |
| 133 | + { |
| 134 | + IonFactory f = new IonFactory(); |
| 135 | + try (IonParser parser = (IonParser) f.createParser("[ 12, true ) ]")) { |
| 136 | + assertEquals(JsonToken.START_ARRAY, parser.nextToken()); |
| 137 | + assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextValue()); |
| 138 | + assertEquals(12, parser.getIntValue()); |
| 139 | + assertEquals(JsonToken.VALUE_TRUE, parser.nextValue()); |
| 140 | + parser.nextValue(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Test(expected = JsonParseException.class) |
| 145 | + public void testUnknownSymbolExceptionForValueIsWrapped() throws IOException |
| 146 | + { |
| 147 | + IonFactory f = new IonFactory(); |
| 148 | + try (IonParser parser = (IonParser) f.createParser("[ 12, $99 ]")) { |
| 149 | + assertEquals(JsonToken.START_ARRAY, parser.nextToken()); |
| 150 | + assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextValue()); |
| 151 | + assertEquals(12, parser.getIntValue()); |
| 152 | + assertEquals(JsonToken.VALUE_STRING, parser.nextValue()); |
| 153 | + parser.getValueAsString(); // Should encounter unknown symbol and fail |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Test(expected = JsonParseException.class) |
| 158 | + public void testUnknownSymbolExceptionForFieldNameIsWrapped() throws IOException |
| 159 | + { |
| 160 | + IonFactory f = new IonFactory(); |
| 161 | + try (IonParser parser = (IonParser) f.createParser("{ a: 1, $99: 2 }")) { |
| 162 | + assertEquals(JsonToken.START_OBJECT, parser.nextToken()); |
| 163 | + assertEquals(JsonToken.FIELD_NAME, parser.nextToken()); |
| 164 | + assertEquals("a", parser.getCurrentName()); |
| 165 | + assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextValue()); |
| 166 | + assertEquals(1, parser.getIntValue()); |
| 167 | + parser.nextValue(); // Should encounter unknown symbol and fail |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Test(expected = JsonParseException.class) |
| 172 | + public void testUnknownSymbolExceptionForAnnotationIsWrapped() throws IOException |
| 173 | + { |
| 174 | + IonFactory f = new IonFactory(); |
| 175 | + try (IonParser parser = (IonParser) f.createParser("{ a: $99::1 }")) { |
| 176 | + assertEquals(JsonToken.START_OBJECT, parser.nextToken()); |
| 177 | + assertEquals(JsonToken.FIELD_NAME, parser.nextToken()); |
| 178 | + assertEquals("a", parser.getCurrentName()); |
| 179 | + assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextValue()); |
| 180 | + assertEquals(1, parser.getIntValue()); |
| 181 | + parser.getTypeAnnotations(); // Should encounter unknown symbol and fail |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + static class FooBar { |
| 186 | + public int foo; |
| 187 | + public List<Object> bar; |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + public static class IonExceptionTests { |
| 192 | + IonObjectMapper mapper = new IonObjectMapper(); |
| 193 | + |
| 194 | + // throws JsonMapperException, depends on IonParser.nextToken() |
| 195 | + // mapper.reader().readValue("{ foo: \"boo\", bar: (1 null.string 2e0 3d0 { ) }", FooBar.class) |
| 196 | + |
| 197 | + // throws JsonMapperException, depends on IonParser.nextToken(), IonObjectMapper._readMapAndClose() |
| 198 | + // mapper.readValue("{ foo: \"boo\", bar: (1 null.string 2e0 3d0 { ) }", FooBar.class) |
| 199 | + |
| 200 | + |
| 201 | + // FooBar fb = mapper.readValue("{ foo: 1, bar: ( ] ) }", FooBar.class); |
| 202 | + // FooBar fb = mapper.readValue("{ foo: 1, bar: ( ] ) }", FooBar.class); |
| 203 | + |
| 204 | + |
| 205 | + @Test(expected = JacksonException.class) |
| 206 | + public void testIonObjectMapper1() throws IOException { |
| 207 | + mapper.readValue("{ foo: 1, bar: ( ] ) }", FooBar.class); |
| 208 | + } |
| 209 | + |
| 210 | + @Test(expected = JacksonException.class) |
| 211 | + public void testIonObjectMapper2() throws IOException { |
| 212 | + mapper.readValue("{ foo: 1, bar: (1 null.string 2e0 3d0 { ) }", FooBar.class); |
| 213 | + } |
| 214 | + |
| 215 | + @Test(expected = JacksonException.class) |
| 216 | + public void testIonObjectMapper3() throws IOException { |
| 217 | + mapper.readValue("{ foo: 1, bar: ( ) ] }", FooBar.class); |
| 218 | + } |
| 219 | + |
| 220 | + @Test(expected = JacksonException.class) |
| 221 | + public void testIonObjectMapper4() throws IOException { |
| 222 | + mapper.reader().readValue("{ foo: 1, bar: ( ] ) }", FooBar.class); |
| 223 | + } |
| 224 | + |
| 225 | + @Test(expected = JacksonException.class) |
| 226 | + public void testIonObjectMapper5() throws IOException { |
| 227 | + mapper.reader().readValue("{ foo: 1, bar: (1 null.string 2e0 3d0 { ) }", FooBar.class); |
| 228 | + } |
| 229 | + |
| 230 | + @Test(expected = JacksonException.class) |
| 231 | + public void testIonObjectMapper6() throws IOException { |
| 232 | + mapper.reader().readValue("{ foo: 1, bar: ( ) ] }", FooBar.class); |
| 233 | + } |
| 234 | + } |
128 | 235 | }
|
0 commit comments