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; ...@@ -18,7 +18,7 @@ package org.onosproject.ui.topo;
18 18
19 import com.google.common.collect.Sets; 19 import com.google.common.collect.Sets;
20 20
21 -import java.text.DecimalFormat; 21 +import java.text.NumberFormat;
22 import java.util.ArrayList; 22 import java.util.ArrayList;
23 import java.util.List; 23 import java.util.List;
24 import java.util.Set; 24 import java.util.Set;
...@@ -28,7 +28,7 @@ import java.util.Set; ...@@ -28,7 +28,7 @@ import java.util.Set;
28 */ 28 */
29 public class PropertyPanel { 29 public class PropertyPanel {
30 30
31 - private static final DecimalFormat DF0 = new DecimalFormat("#,###"); 31 + private static final NumberFormat NF = NumberFormat.getInstance();
32 32
33 private String title; 33 private String title;
34 private String typeId; 34 private String typeId;
...@@ -49,6 +49,24 @@ public class PropertyPanel { ...@@ -49,6 +49,24 @@ public class PropertyPanel {
49 } 49 }
50 50
51 /** 51 /**
52 + * Returns a number formatter to use for formatting integer and long
53 + * property values.
54 + * <p>
55 + * This default implementation uses a formatter for the default
56 + * locale. For example:
57 + * <pre>
58 + * Locale.ENGLISH : 1000 -&gt; "1,000"
59 + * Locale.FRENCH : 1000 -&gt; "1 000"
60 + * Locale.GERMAN : 1000 -&gt; "1.000"
61 + * </pre>
62 + *
63 + * @return the number formatter
64 + */
65 + protected NumberFormat formatter() {
66 + return NF;
67 + }
68 +
69 + /**
52 * Adds an ID field to the panel data, to be included in 70 * Adds an ID field to the panel data, to be included in
53 * the returned JSON data to the client. 71 * the returned JSON data to the client.
54 * 72 *
...@@ -80,7 +98,7 @@ public class PropertyPanel { ...@@ -80,7 +98,7 @@ public class PropertyPanel {
80 * @return self, for chaining 98 * @return self, for chaining
81 */ 99 */
82 public PropertyPanel addProp(String key, int value) { 100 public PropertyPanel addProp(String key, int value) {
83 - properties.add(new Prop(key, DF0.format(value))); 101 + properties.add(new Prop(key, formatter().format(value)));
84 return this; 102 return this;
85 } 103 }
86 104
...@@ -92,7 +110,7 @@ public class PropertyPanel { ...@@ -92,7 +110,7 @@ public class PropertyPanel {
92 * @return self, for chaining 110 * @return self, for chaining
93 */ 111 */
94 public PropertyPanel addProp(String key, long value) { 112 public PropertyPanel addProp(String key, long value) {
95 - properties.add(new Prop(key, DF0.format(value))); 113 + properties.add(new Prop(key, formatter().format(value)));
96 return this; 114 return this;
97 } 115 }
98 116
......
...@@ -20,17 +20,38 @@ import org.junit.BeforeClass; ...@@ -20,17 +20,38 @@ import org.junit.BeforeClass;
20 import org.junit.Test; 20 import org.junit.Test;
21 import org.onosproject.ui.topo.PropertyPanel.Prop; 21 import org.onosproject.ui.topo.PropertyPanel.Prop;
22 22
23 +import java.text.NumberFormat;
23 import java.util.HashMap; 24 import java.util.HashMap;
24 import java.util.Iterator; 25 import java.util.Iterator;
26 +import java.util.Locale;
25 import java.util.Map; 27 import java.util.Map;
26 28
27 -import static org.junit.Assert.*; 29 +import static org.junit.Assert.assertEquals;
30 +import static org.junit.Assert.assertFalse;
31 +import static org.junit.Assert.assertNull;
32 +import static org.junit.Assert.fail;
28 33
29 /** 34 /**
30 * Unit tests for {@link PropertyPanel}. 35 * Unit tests for {@link PropertyPanel}.
31 */ 36 */
32 public class PropertyPanelTest { 37 public class PropertyPanelTest {
33 38
39 + // Modified property panel subclass to use ENGLISH locale formatter so
40 + // we know formatted numbers will use comma for the thousand separator.
41 + private static final class EnglishPropertyPanel extends PropertyPanel {
42 + private static final NumberFormat ENGLISH_FORMATTER =
43 + NumberFormat.getInstance(Locale.ENGLISH);
44 +
45 + public EnglishPropertyPanel(String title, String typeId) {
46 + super(title, typeId);
47 + }
48 +
49 + @Override
50 + protected NumberFormat formatter() {
51 + return ENGLISH_FORMATTER;
52 + }
53 + }
54 +
34 private static final String TITLE_ORIG = "Original Title"; 55 private static final String TITLE_ORIG = "Original Title";
35 private static final String TYPE_ORIG = "Original type ID"; 56 private static final String TYPE_ORIG = "Original type ID";
36 private static final String TITLE_NEW = "New Title"; 57 private static final String TITLE_NEW = "New Title";
...@@ -76,7 +97,7 @@ public class PropertyPanelTest { ...@@ -76,7 +97,7 @@ public class PropertyPanelTest {
76 97
77 @Test 98 @Test
78 public void basic() { 99 public void basic() {
79 - pp = new PropertyPanel(TITLE_ORIG, TYPE_ORIG); 100 + pp = new EnglishPropertyPanel(TITLE_ORIG, TYPE_ORIG);
80 assertEquals("wrong title", TITLE_ORIG, pp.title()); 101 assertEquals("wrong title", TITLE_ORIG, pp.title());
81 assertEquals("wrong type", TYPE_ORIG, pp.typeId()); 102 assertEquals("wrong type", TYPE_ORIG, pp.typeId());
82 assertNull("id?", pp.id()); 103 assertNull("id?", pp.id());
......