From 0b30e21e1f557b937e717d6d769cc367ad98e603 Mon Sep 17 00:00:00 2001 From: depryf Date: Mon, 8 Jul 2024 09:33:53 -0400 Subject: [PATCH] Fixed exception comparing versions --- src/main/java/com/imsweb/naaccrxml/SpecificationVersion.java | 5 +++++ .../java/com/imsweb/naaccrxml/SpecificationVersionTest.java | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/com/imsweb/naaccrxml/SpecificationVersion.java b/src/main/java/com/imsweb/naaccrxml/SpecificationVersion.java index 73e0971..eaa4bd4 100644 --- a/src/main/java/com/imsweb/naaccrxml/SpecificationVersion.java +++ b/src/main/java/com/imsweb/naaccrxml/SpecificationVersion.java @@ -3,6 +3,8 @@ */ package com.imsweb.naaccrxml; +import java.util.Objects; + import org.apache.commons.lang3.StringUtils; public final class SpecificationVersion { @@ -46,6 +48,9 @@ public static boolean isSpecificationSupported(String spec) { * @return negative integer if first version is smaller, 0 if they are the same, positive integer otherwise */ public static int compareSpecifications(String spec1, String spec2) { + if (StringUtils.isBlank(spec1) || !isSpecificationSupported(spec1) || StringUtils.isBlank(spec2) || !isSpecificationSupported(spec2)) + return Objects.compare(spec1, spec2, String::compareTo); + String[] parts1 = StringUtils.split(spec1, '.'); Integer major1 = Integer.valueOf(parts1[0]); Integer minor1 = Integer.valueOf(parts1[1]); diff --git a/src/test/java/com/imsweb/naaccrxml/SpecificationVersionTest.java b/src/test/java/com/imsweb/naaccrxml/SpecificationVersionTest.java index b387a5d..b436302 100644 --- a/src/test/java/com/imsweb/naaccrxml/SpecificationVersionTest.java +++ b/src/test/java/com/imsweb/naaccrxml/SpecificationVersionTest.java @@ -44,6 +44,10 @@ public void testCompareVersions() { Assert.assertEquals(1, SpecificationVersion.compareSpecifications("2.1", "1.0")); Assert.assertEquals(-1, SpecificationVersion.compareSpecifications("1.2", "2.1")); Assert.assertEquals(1, SpecificationVersion.compareSpecifications("2.1", "1.2")); + + Assert.assertEquals(2, SpecificationVersion.compareSpecifications("1.0", "1")); + Assert.assertEquals(-2, SpecificationVersion.compareSpecifications("1", "1.0")); + Assert.assertEquals(0, SpecificationVersion.compareSpecifications("1", "1")); } @Test