From 9318c5ce82f3ab592a9325424f245cfceaf31271 Mon Sep 17 00:00:00 2001 From: Kristian Lund Date: Sat, 14 Nov 2015 17:38:09 +0100 Subject: [PATCH] Solving locale issue upstream by adding locale variable --- .../org/scoverage/OverallCheckTask.groovy | 6 +++++- .../org/scoverage/OverallCheckTaskTest.groovy | 19 +++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/groovy/org/scoverage/OverallCheckTask.groovy b/src/main/groovy/org/scoverage/OverallCheckTask.groovy index 9cf0d1a..f56d0ad 100644 --- a/src/main/groovy/org/scoverage/OverallCheckTask.groovy +++ b/src/main/groovy/org/scoverage/OverallCheckTask.groovy @@ -46,7 +46,11 @@ class OverallCheckTask extends DefaultTask { /** Set if want to change default from 'reportDir' in scoverage extension. */ File reportDir + /** Overwrite to test for a specific locale. */ + Locale locale + protected XmlParser parser + protected DecimalFormat df = new DecimalFormat("#.##") OverallCheckTask() { @@ -73,7 +77,7 @@ class OverallCheckTask extends DefaultTask { try { Node xml = parser.parse(reportFile) - NumberFormat nf = NumberFormat.getInstance(); + NumberFormat nf = NumberFormat.getInstance(locale == null ? Locale.getDefault() : locale); Double coverageValue = nf.parse(xml.attribute(coverageType.paramName) as String).doubleValue(); Double overallRate = coverageType.normalize(coverageValue) def difference = (minimumRate - overallRate) diff --git a/src/test/groovy/org/scoverage/OverallCheckTaskTest.groovy b/src/test/groovy/org/scoverage/OverallCheckTaskTest.groovy index 5ba4abf..aeb3389 100644 --- a/src/test/groovy/org/scoverage/OverallCheckTaskTest.groovy +++ b/src/test/groovy/org/scoverage/OverallCheckTaskTest.groovy @@ -13,6 +13,9 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.ExpectedException +import javax.swing.text.NumberFormatter +import java.text.NumberFormat + /** * Copied from the Internet, just to check if we have correct exception thrown. */ @@ -41,17 +44,6 @@ class CauseMatcher extends TypeSafeMatcher { } class OverallCheckTaskTest { - private static Locale defaultLocale - @BeforeClass - public static void setup() { - defaultLocale = Locale.getDefault() - Locale.setDefault(Locale.US) - } - - @AfterClass - public static void tearDown() { - Locale.setDefault(defaultLocale) - } @Rule public ExpectedException expectedException = ExpectedException.none() @@ -60,6 +52,7 @@ class OverallCheckTaskTest { Project project = ProjectBuilder.builder().build() project.plugins.apply(ScoveragePlugin) project.tasks.create('bob', OverallCheckTask) { + locale = Locale.US minimumRate = coverageRate reportDir = new File('src/test/resources') coverageType = type @@ -74,6 +67,7 @@ class OverallCheckTaskTest { Project project = ProjectBuilder.builder().build() project.plugins.apply(ScoveragePlugin) project.tasks.create('bob', OverallCheckTask) { + locale = Locale.US minimumRate = 1.0 reportDir = new File('src/test/nothingthere') coverageType = CoverageType.Line @@ -114,9 +108,10 @@ class OverallCheckTaskTest { @Test void failsWhenStatementRateIsBelowTarget() { Project project = projectForRate(1, CoverageType.Statement) + NumberFormat nf = NumberFormat.getInstance() expectedException.expectCause(new CauseMatcher( GradleException.class, - OverallCheckTask.errorMsg("33.33", "100", CoverageType.Statement) + OverallCheckTask.errorMsg(nf.format(new Double(33.33)), "100", CoverageType.Statement) )) project.tasks.bob.execute() }