Simon Hunt
Committed by Gerrit Code Review

ONOS-3320: PropertyPanelTest fails in non-English locale.

- Added formatter() instance method that can be overridden by unit test.

Change-Id: I38ee045edee852c95e27bead46800fb5dad4cf88
......@@ -18,7 +18,7 @@ package org.onosproject.ui.topo;
import com.google.common.collect.Sets;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
......@@ -28,7 +28,7 @@ import java.util.Set;
*/
public class PropertyPanel {
private static final DecimalFormat DF0 = new DecimalFormat("#,###");
private static final NumberFormat NF = NumberFormat.getInstance();
private String title;
private String typeId;
......@@ -49,6 +49,24 @@ public class PropertyPanel {
}
/**
* Returns a number formatter to use for formatting integer and long
* property values.
* <p>
* This default implementation uses a formatter for the default
* locale. For example:
* <pre>
* Locale.ENGLISH : 1000 -&gt; "1,000"
* Locale.FRENCH : 1000 -&gt; "1 000"
* Locale.GERMAN : 1000 -&gt; "1.000"
* </pre>
*
* @return the number formatter
*/
protected NumberFormat formatter() {
return NF;
}
/**
* Adds an ID field to the panel data, to be included in
* the returned JSON data to the client.
*
......@@ -80,7 +98,7 @@ public class PropertyPanel {
* @return self, for chaining
*/
public PropertyPanel addProp(String key, int value) {
properties.add(new Prop(key, DF0.format(value)));
properties.add(new Prop(key, formatter().format(value)));
return this;
}
......@@ -92,7 +110,7 @@ public class PropertyPanel {
* @return self, for chaining
*/
public PropertyPanel addProp(String key, long value) {
properties.add(new Prop(key, DF0.format(value)));
properties.add(new Prop(key, formatter().format(value)));
return this;
}
......
......@@ -20,17 +20,38 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.onosproject.ui.topo.PropertyPanel.Prop;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link PropertyPanel}.
*/
public class PropertyPanelTest {
// Modified property panel subclass to use ENGLISH locale formatter so
// we know formatted numbers will use comma for the thousand separator.
private static final class EnglishPropertyPanel extends PropertyPanel {
private static final NumberFormat ENGLISH_FORMATTER =
NumberFormat.getInstance(Locale.ENGLISH);
public EnglishPropertyPanel(String title, String typeId) {
super(title, typeId);
}
@Override
protected NumberFormat formatter() {
return ENGLISH_FORMATTER;
}
}
private static final String TITLE_ORIG = "Original Title";
private static final String TYPE_ORIG = "Original type ID";
private static final String TITLE_NEW = "New Title";
......@@ -76,7 +97,7 @@ public class PropertyPanelTest {
@Test
public void basic() {
pp = new PropertyPanel(TITLE_ORIG, TYPE_ORIG);
pp = new EnglishPropertyPanel(TITLE_ORIG, TYPE_ORIG);
assertEquals("wrong title", TITLE_ORIG, pp.title());
assertEquals("wrong type", TYPE_ORIG, pp.typeId());
assertNull("id?", pp.id());
......