@Test void serialization_unknown_yield_type_should_throw_an_exception() { MethodBehavior mb = newMethodBehavior("org.bar.A#foo()Z"); mb.addYield(new CustomMethodYield(mb)); mb.completed(); IllegalStateException e = assertThrows(IllegalStateException.class, () -> gson.toJson(mb)); assertThat(e).hasMessage("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); }
private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; }
MethodBehaviorJsonAdapter implements JsonSerializer<MethodBehavior>, JsonDeserializer<MethodBehavior> { private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; } }
MethodBehaviorJsonAdapter implements JsonSerializer<MethodBehavior>, JsonDeserializer<MethodBehavior> { private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; } private  MethodBehaviorJsonAdapter(); }
MethodBehaviorJsonAdapter implements JsonSerializer<MethodBehavior>, JsonDeserializer<MethodBehavior> { private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; } private  MethodBehaviorJsonAdapter(); static Gson gson(); @Override MethodBehavior deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(MethodBehavior src, Type typeOfSrc, JsonSerializationContext context); }
MethodBehaviorJsonAdapter implements JsonSerializer<MethodBehavior>, JsonDeserializer<MethodBehavior> { private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; } private  MethodBehaviorJsonAdapter(); static Gson gson(); @Override MethodBehavior deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(MethodBehavior src, Type typeOfSrc, JsonSerializationContext context);  }