@Test public void testNonoptionalFields() throws JsonProtocolModelParseException, JsonProtocolParseException { SeveralTypesWithSomethingParser parser; { List<Class<?>> types = new ArrayList<Class<?>>(3); types.add(TypeWithNullableLong.class); types.add(TypeWithSomething.class); types.add(Something.class); parser = createDynamicParser(SeveralTypesWithSomethingParser.class, types); } // First couple of checks that parser does work { JSONObject json = parseJson("{'val': null}"); TypeWithNullableLong val = parser.parseTypeWithNullableLong(json); assertNull(val.val()); } { JSONObject json = parseJson("{'data': {} }"); TypeWithSomething val = parser.parseTypeWithSomething(json); assertNotNull(val.data()); } JSONObject emptyJson = parseJson("{}"); { try { TypeWithNullableLong val = parser.parseTypeWithNullableLong(emptyJson); val.val(); fail(); } catch (Exception e) { // expected } } { try { TypeWithSomething val = parser.parseTypeWithSomething(emptyJson); fail(); } catch (Exception e) { // expected } } }
@Test public void testNullLongValue() throws JsonProtocolModelParseException, JsonProtocolParseException { TypeWithNullableLongParser parser = createDynamicParser(TypeWithNullableLongParser.class, TypeWithNullableLong.class); { JSONObject json = parseJson("{'val': 2 }"); TypeWithNullableLong val = parser.parseTypeWithNullableLong(json); assertEquals(Long.valueOf(2), val.val()); } { JSONObject json = parseJson("{'val': null }"); TypeWithNullableLong val = parser.parseTypeWithNullableLong(json); assertNull(val.val()); } }