Fix for decimal separator during type conversion

- fix prevents situation when decimal
separator for default locale
is set to comma instead of dot.

Change-Id: I667682e4ed5e1d69e23ac2e82922cca024d1c467
This commit is contained in:
Lukasz Zajaczkowski 2015-09-25 08:18:05 +00:00
parent fc30e21e5c
commit a9934846d5
2 changed files with 74 additions and 1 deletions

View File

@ -18,14 +18,18 @@ package monasca.common.model.alarm;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import com.fasterxml.jackson.annotation.JsonCreator;
import monasca.common.model.alarm.AlarmExpressionLexer;
import monasca.common.model.alarm.AlarmExpressionParser;
import monasca.common.model.metric.MetricDefinition;
@ -46,7 +50,12 @@ public class AlarmSubExpression implements Serializable {
private int periods;
// Use a DecimalFormatter for threshold because the standard double format starts using scientific notation when
// threshold is very large and that scientific notation can't be parsed when recreating the SubExpression
private static final DecimalFormat formatter = new DecimalFormat("0.0##############");
private static final DecimalFormat formatter;
static {
formatter = new DecimalFormat("0.0##############");
// Prevents situation when decimal separator for default locale is different then dot.
formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.US));
}
public AlarmSubExpression(AggregateFunction function, MetricDefinition metricDefinition,
AlarmOperator operator, double threshold, int period, int periods) {

View File

@ -0,0 +1,64 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package monasca.common.model.alarm;
import static org.testng.Assert.assertEquals;
import java.util.List;
import java.util.Locale;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import monasca.common.model.metric.MetricDefinition;
/**
* Checks if conversion from decimal to string is Locale independent.
*
* @author lukasz.zajaczkowski@ts.fujitsu.com
*
*/
@Test
public class AlarmSubExpressionLocaleTest {
private static final String EXPECTED_EXPRESSION = "min(hpcs.compute{instance_id=5, metric_name=cpu, device=1}) < 1.2";
public void shouldBeLocaleIndependent() {
List<Locale> localeList = Lists.newArrayList(
Locale.GERMAN, Locale.CHINA, Locale.FRANCE, Locale.JAPAN, Locale.CANADA, Locale.KOREA
);
for (Locale locale : localeList) {
Locale.setDefault(locale);
AlarmSubExpression alarmSubExpression =
new AlarmSubExpression(AggregateFunction.MIN, new MetricDefinition("hpcs.compute", ImmutableMap.<String, String>builder()
.put("instance_id", "5").put("metric_name", "cpu").put("device", "1").build()), AlarmOperator.LT, 1.2, 60, 1);
assertEquals(alarmSubExpression.getExpression(), EXPECTED_EXPRESSION, "Not correct expression for locale " + locale.getDisplayName());
}
}
public void shouldWorkWithDefaultLocale() {
Locale.setDefault(Locale.US);
AlarmSubExpression alarmSubExpression =
new AlarmSubExpression(AggregateFunction.MIN, new MetricDefinition("hpcs.compute", ImmutableMap.<String, String>builder()
.put("instance_id", "5").put("metric_name", "cpu").put("device", "1").build()), AlarmOperator.LT, 1.2, 60, 1);
assertEquals(alarmSubExpression.getExpression(), EXPECTED_EXPRESSION, "Not correct expression for default locale");
}
}