From cef284495e8df9d4810df8da546049057c6e73a4 Mon Sep 17 00:00:00 2001 From: Octavian Patrascoiu Date: Tue, 23 Jul 2024 11:53:01 +0100 Subject: [PATCH] [#690] Test coverage: Improve sonar quality metrics --- .../gs/dmn/feel/synthesis/FEELToNativeVisitor.java | 10 +++++++--- .../type/JavaTimeKotlinNativeTypeFactory.java | 11 +++++++---- .../tck/serialization/xstream/XStreamMarshaller.java | 11 +++++------ .../MixedItemDefinitionsTransformer.java | 1 - .../dmn/transformation/AbstractTransformerTest.java | 1 + .../InOutCorrectPathsInDecisionsTransformer.java | 8 +++----- .../basic/BasicSignavioDMNToJavaTransformer.java | 2 +- 7 files changed, 24 insertions(+), 20 deletions(-) diff --git a/dmn-core/src/main/java/com/gs/dmn/feel/synthesis/FEELToNativeVisitor.java b/dmn-core/src/main/java/com/gs/dmn/feel/synthesis/FEELToNativeVisitor.java index d0b65076a..8f5ccce38 100644 --- a/dmn-core/src/main/java/com/gs/dmn/feel/synthesis/FEELToNativeVisitor.java +++ b/dmn-core/src/main/java/com/gs/dmn/feel/synthesis/FEELToNativeVisitor.java @@ -761,7 +761,7 @@ protected String makeCondition(String feelOperator, Expression leftOperand protected String makeCondition(String feelOperator, String leftOpd, String rightOpd, NativeOperator javaOperator) { if (javaOperator == null) { - handleError(String.format("Operator '%s' cannot be applied to '%s' and '%s'", feelOperator, leftOpd, rightOpd)); + handleError(makeOperatorErrorMessage(feelOperator, leftOpd, rightOpd)); return null; } else { if (javaOperator.getCardinality() == 2) { @@ -779,7 +779,7 @@ protected String makeCondition(String feelOperator, String leftOpd, String right } } } else { - handleError(String.format("Operator '%s' cannot be applied to '%s' and '%s'", feelOperator, leftOpd, rightOpd)); + handleError(makeOperatorErrorMessage(feelOperator, leftOpd, rightOpd)); return null; } } @@ -790,7 +790,7 @@ protected String listTestOperator(String feelOperatorName, Expression left if (javaOperator != null) { return javaOperator.getName(); } else { - handleError(String.format("Operator '%s' cannot be applied to '%s' and '%s'", feelOperatorName, leftOperand, rightOperand)); + handleError(makeOperatorErrorMessage(feelOperatorName, leftOperand, rightOperand)); return null; } } @@ -814,4 +814,8 @@ protected String rangeGetter(String memberName) { return this.dmnTransformer.getter(memberName); } } + + private static String makeOperatorErrorMessage(String feelOperator, Object leftOpd, Object rightOpd) { + return String.format("Operator '%s' cannot be applied to '%s' and '%s'", feelOperator, leftOpd, rightOpd); + } } diff --git a/dmn-core/src/main/java/com/gs/dmn/feel/synthesis/type/JavaTimeKotlinNativeTypeFactory.java b/dmn-core/src/main/java/com/gs/dmn/feel/synthesis/type/JavaTimeKotlinNativeTypeFactory.java index 4d410a21f..cf23043cc 100644 --- a/dmn-core/src/main/java/com/gs/dmn/feel/synthesis/type/JavaTimeKotlinNativeTypeFactory.java +++ b/dmn-core/src/main/java/com/gs/dmn/feel/synthesis/type/JavaTimeKotlinNativeTypeFactory.java @@ -29,6 +29,9 @@ public class JavaTimeKotlinNativeTypeFactory extends KotlinTypeFactory { private static final Map FEEL_TYPE_TO_JAVA_TYPE = new LinkedHashMap<>(); + + public static final String KOTLIN_ANY = "kotlin.Any"; + static { FEEL_TYPE_TO_JAVA_TYPE.put(ENUMERATION.getName(), String.class.getName()); FEEL_TYPE_TO_JAVA_TYPE.put(YEARS_AND_MONTHS_DURATION.getName(), java.time.temporal.TemporalAmount.class.getName()); @@ -39,8 +42,8 @@ public class JavaTimeKotlinNativeTypeFactory extends KotlinTypeFactory { FEEL_TYPE_TO_JAVA_TYPE.put(STRING.getName(), String.class.getSimpleName()); FEEL_TYPE_TO_JAVA_TYPE.put(BOOLEAN.getName(), Boolean.class.getSimpleName()); FEEL_TYPE_TO_JAVA_TYPE.put(NUMBER.getName(), java.lang.Number.class.getName()); - FEEL_TYPE_TO_JAVA_TYPE.put(ANY.getName(), "kotlin.Any"); - FEEL_TYPE_TO_JAVA_TYPE.put(NULL.getName(), "kotlin.Any"); + FEEL_TYPE_TO_JAVA_TYPE.put(ANY.getName(), KOTLIN_ANY); + FEEL_TYPE_TO_JAVA_TYPE.put(NULL.getName(), KOTLIN_ANY); } private static final Map FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE = new LinkedHashMap<>(); @@ -54,8 +57,8 @@ public class JavaTimeKotlinNativeTypeFactory extends KotlinTypeFactory { FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(STRING.getName(), String.class.getName()); FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(BOOLEAN.getName(), Boolean.class.getName()); FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(NUMBER.getName(), java.lang.Number.class.getName()); - FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(ANY.getName(), "kotlin.Any"); - FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(NULL.getName(), "kotlin.Any"); + FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(ANY.getName(), KOTLIN_ANY); + FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(NULL.getName(), KOTLIN_ANY); } @Override diff --git a/dmn-core/src/main/java/com/gs/dmn/tck/serialization/xstream/XStreamMarshaller.java b/dmn-core/src/main/java/com/gs/dmn/tck/serialization/xstream/XStreamMarshaller.java index 6ea0fc565..b0bc3be48 100644 --- a/dmn-core/src/main/java/com/gs/dmn/tck/serialization/xstream/XStreamMarshaller.java +++ b/dmn-core/src/main/java/com/gs/dmn/tck/serialization/xstream/XStreamMarshaller.java @@ -37,7 +37,6 @@ public class XStreamMarshaller implements TCKMarshaller { private static final Logger LOGGER = LoggerFactory.getLogger(XStreamMarshaller.class); - private static final StaxDriver STAX_DRIVER = new StaxDriver(); private static TCKVersion inferTCKVersion(Reader firstStringReader) { return LATEST; @@ -72,7 +71,7 @@ public TestCases unmarshal(String input, boolean validateSchema) { } return unmarshal(tckVersion, secondStringReader); } catch (Exception e) { - LOGGER.error("Error unmarshalling TCK content from reader.", e); + LOGGER.error("Error unmarshalling TCK content from String.", e); } return null; } @@ -88,7 +87,7 @@ public TestCases unmarshal(File input, boolean validateSchema) { } return unmarshal(tckVersion, secondStringReader); } catch (Exception e) { - LOGGER.error("Error unmarshalling TCK content from reader.", e); + LOGGER.error("Error unmarshalling TCK content from File.", e); } return null; } @@ -104,7 +103,7 @@ public TestCases unmarshal(URL input, boolean validateSchema) { } return unmarshal(tckVersion, secondStringReader); } catch (Exception e) { - LOGGER.error("Error unmarshalling TCK content from reader.", e); + LOGGER.error("Error unmarshalling TCK content from URL.", e); } return null; } @@ -120,7 +119,7 @@ public TestCases unmarshal(InputStream input, boolean validateSchema) { } return unmarshal(tckVersion, secondStringReader); } catch (Exception e) { - LOGGER.error("Error unmarshalling TCK content from reader.", e); + LOGGER.error("Error unmarshalling TCK content from InputStream.", e); } return null; } @@ -131,7 +130,7 @@ public TestCases unmarshal(Reader input, boolean validateSchema) { String xml = buffer.lines().collect(Collectors.joining("\n")); return unmarshal(xml, validateSchema); } catch (Exception e) { - LOGGER.error("Error unmarshalling TCK content from reader.", e); + LOGGER.error("Error unmarshalling TCK content from Reader.", e); } return null; } diff --git a/dmn-core/src/main/java/com/gs/dmn/transformation/MixedItemDefinitionsTransformer.java b/dmn-core/src/main/java/com/gs/dmn/transformation/MixedItemDefinitionsTransformer.java index bacf4ec05..5a6ce1001 100644 --- a/dmn-core/src/main/java/com/gs/dmn/transformation/MixedItemDefinitionsTransformer.java +++ b/dmn-core/src/main/java/com/gs/dmn/transformation/MixedItemDefinitionsTransformer.java @@ -26,7 +26,6 @@ import java.util.List; public class MixedItemDefinitionsTransformer extends SimpleDMNTransformer { - private boolean transformRepository = true; private final Visitor visitor = new MixedItemDefinitionsVisitor<>(this.logger, new NopErrorHandler()); public MixedItemDefinitionsTransformer() { diff --git a/dmn-core/src/test/java/com/gs/dmn/transformation/AbstractTransformerTest.java b/dmn-core/src/test/java/com/gs/dmn/transformation/AbstractTransformerTest.java index a20524085..67b94a68f 100644 --- a/dmn-core/src/test/java/com/gs/dmn/transformation/AbstractTransformerTest.java +++ b/dmn-core/src/test/java/com/gs/dmn/transformation/AbstractTransformerTest.java @@ -89,5 +89,6 @@ protected boolean shouldTransformFile(File inputFile) { @Override protected void transformFile(File child, File root, Path outputPath) { + // NOP transformer for testing } } \ No newline at end of file diff --git a/dmn-signavio/src/main/java/com/gs/dmn/signavio/transformation/InOutCorrectPathsInDecisionsTransformer.java b/dmn-signavio/src/main/java/com/gs/dmn/signavio/transformation/InOutCorrectPathsInDecisionsTransformer.java index 958db4b94..f118ce967 100644 --- a/dmn-signavio/src/main/java/com/gs/dmn/signavio/transformation/InOutCorrectPathsInDecisionsTransformer.java +++ b/dmn-signavio/src/main/java/com/gs/dmn/signavio/transformation/InOutCorrectPathsInDecisionsTransformer.java @@ -24,20 +24,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; +import java.util.ArrayList; +import java.util.List; public class InOutCorrectPathsInDecisionsTransformer extends SimpleDMNTransformer { protected static final Logger LOGGER = LoggerFactory.getLogger(InOutCorrectPathsInDecisionsTransformer.class); - protected final BuildLogger logger; - protected boolean transformRepository = true; - public InOutCorrectPathsInDecisionsTransformer() { this(new Slf4jBuildLogger(LOGGER)); } protected InOutCorrectPathsInDecisionsTransformer(BuildLogger logger) { - this.logger = logger; + super(logger); } @Override diff --git a/dmn-signavio/src/main/java/com/gs/dmn/signavio/transformation/basic/BasicSignavioDMNToJavaTransformer.java b/dmn-signavio/src/main/java/com/gs/dmn/signavio/transformation/basic/BasicSignavioDMNToJavaTransformer.java index b7a73ecee..ad3a42180 100644 --- a/dmn-signavio/src/main/java/com/gs/dmn/signavio/transformation/basic/BasicSignavioDMNToJavaTransformer.java +++ b/dmn-signavio/src/main/java/com/gs/dmn/signavio/transformation/basic/BasicSignavioDMNToJavaTransformer.java @@ -272,7 +272,7 @@ public List annotations(TDRGElement element, List annotationText } catch (Exception e) { LOGGER.error(String.format("Cannot process annotation '%s' for element '%s'", annotationText, element == null ? "" : element.getName())); // Add unevaluated annotation text - annotationStatements.add(String.format("\"%s\"", annotationText.replaceAll("\"", "\\\\\""))); + annotationStatements.add(String.format("\"%s\"", annotationText.replace("\"", "\\\\\""))); } } return annotationStatements;