Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/core/gov/aps/jca/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

package gov.aps.jca;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Administrative class to keep track of the version number of the context
* implementations.
Expand Down Expand Up @@ -103,6 +106,56 @@ public Version(String productName, String implementationLangugage, String versio
this.productName = productName;
this.implementationLanguage = implementationLangugage;
this.version = version;
parseDeprecatedVersionFields(version);
}

private static final Pattern LEGACY_DEV_WITH_MAINT_PATTERN =
Pattern.compile("^\\s*[vV]?(\\d+)\\.(\\d+)\\.(\\d+)[dD](\\d+).*$");

private static final Pattern LEGACY_DEV_PATTERN =
Pattern.compile("^\\s*[vV]?(\\d+)\\.(\\d+)[dD](\\d+).*$");

private static final Pattern NUMERIC_CORE_PATTERN =
Pattern.compile("^\\s*[vV]?(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?.*$");

private void parseDeprecatedVersionFields(String rawVersion)
{
majorVersion = 0;
minorVersion = 0;
maintenanceVersion = 0;
developmentVersion = 0;

if (rawVersion == null)
return;

Matcher matcher = LEGACY_DEV_WITH_MAINT_PATTERN.matcher(rawVersion);
if (matcher.matches())
{
majorVersion = Integer.parseInt(matcher.group(1));
minorVersion = Integer.parseInt(matcher.group(2));
maintenanceVersion = Integer.parseInt(matcher.group(3));
developmentVersion = Integer.parseInt(matcher.group(4));
return;
}

matcher = LEGACY_DEV_PATTERN.matcher(rawVersion);
if (matcher.matches())
{
majorVersion = Integer.parseInt(matcher.group(1));
minorVersion = Integer.parseInt(matcher.group(2));
developmentVersion = Integer.parseInt(matcher.group(3));
return;
}

matcher = NUMERIC_CORE_PATTERN.matcher(rawVersion);
if (matcher.matches())
{
majorVersion = Integer.parseInt(matcher.group(1));
if (matcher.group(2) != null)
minorVersion = Integer.parseInt(matcher.group(2));
if (matcher.group(3) != null)
maintenanceVersion = Integer.parseInt(matcher.group(3));
}
}


Expand Down Expand Up @@ -232,4 +285,4 @@ public String toString()
{
return getLongVersionString();
}
}
}
48 changes: 48 additions & 0 deletions test/gov/aps/jca/VersionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gov.aps.jca;

import org.junit.Assert;
import org.junit.Test;

public class VersionTest {

@Test
public void parsesNumericVersionWithQualifier() {
Version version = new Version("JCA", "Java", "2.4.12-SNAPSHOT");

Assert.assertEquals(2, version.getMajorVersion());
Assert.assertEquals(4, version.getMinorVersion());
Assert.assertEquals(12, version.getMaintenanceVersion());
Assert.assertEquals(0, version.getDevelopmentVersion());
}

@Test
public void parsesLegacyDevelopmentVersion() {
Version version = new Version("JCA", "Java", "1.2D3");

Assert.assertEquals(1, version.getMajorVersion());
Assert.assertEquals(2, version.getMinorVersion());
Assert.assertEquals(0, version.getMaintenanceVersion());
Assert.assertEquals(3, version.getDevelopmentVersion());
}

@Test
public void parsesLegacyDevelopmentVersionWithMaintenance() {
Version version = new Version("JCA", "Java", "1.2.3D4");

Assert.assertEquals(1, version.getMajorVersion());
Assert.assertEquals(2, version.getMinorVersion());
Assert.assertEquals(3, version.getMaintenanceVersion());
Assert.assertEquals(4, version.getDevelopmentVersion());
}

@Test
public void defaultsToZerosOnInvalidVersion() {
Version version = new Version("JCA", "Java", "abc");

Assert.assertEquals(0, version.getMajorVersion());
Assert.assertEquals(0, version.getMinorVersion());
Assert.assertEquals(0, version.getMaintenanceVersion());
Assert.assertEquals(0, version.getDevelopmentVersion());
}
}

Loading