Ticket #20933: 20933.patch

File 20933.patch, 7.4 KB (added by taylor.smock, 4 years ago)
  • src/org/openstreetmap/josm/data/imagery/vectortile/mapbox/Feature.java

    From e784efd4e5d988ba95f0fff14c5178d7cdb67c66 Mon Sep 17 00:00:00 2001
    From: Taylor Smock <tsmock@fb.com>
    Date: Wed, 26 May 2021 13:07:16 -0600
    Subject: [PATCH 1/2] VectorTile: Feature: Always use root locale (stable
     numbers)
    
    This fixes #20933.
    
    Signed-off-by: Taylor Smock <tsmock@fb.com>
    ---
     .../josm/data/imagery/vectortile/mapbox/Feature.java           | 3 ++-
     1 file changed, 2 insertions(+), 1 deletion(-)
    
    diff --git a/src/org/openstreetmap/josm/data/imagery/vectortile/mapbox/Feature.java b/src/org/openstreetmap/josm/data/imagery/vectortile/mapbox/Feature.java
    index 7f4fa1023..9dc07ce73 100644
    a b import java.io.IOException;  
    55import java.text.NumberFormat;
    66import java.util.ArrayList;
    77import java.util.List;
     8import java.util.Locale;
    89
    910import org.openstreetmap.josm.data.osm.TagMap;
    1011import org.openstreetmap.josm.data.protobuf.ProtobufPacked;
    public class Feature {  
    110111            Object value = layer.getValue(number.intValue());
    111112            if (value instanceof Double || value instanceof Float) {
    112113                // reset grouping if the instance is a singleton
    113                 final NumberFormat numberFormat = NumberFormat.getNumberInstance();
     114                final NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.ROOT);
    114115                final boolean grouping = numberFormat.isGroupingUsed();
    115116                try {
    116117                    numberFormat.setGroupingUsed(false);
  • test/unit/org/openstreetmap/josm/data/imagery/vectortile/mapbox/FeatureTest.java

    -- 
    GitLab
    
    
    From bd8b2251deec9d5d335f7b6ab12abb796d3ee68b Mon Sep 17 00:00:00 2001
    From: Taylor Smock <tsmock@fb.com>
    Date: Wed, 26 May 2021 14:40:50 -0600
    Subject: [PATCH 2/2] FeatureTest: Add non-regression test for #20933.
    
    Signed-off-by: Taylor Smock <tsmock@fb.com>
    ---
     .../vectortile/mapbox/FeatureTest.java        | 44 +++++++++++++-
     .../josm/testutils/annotations/I18n.java      | 59 +++++++++++++++++++
     2 files changed, 102 insertions(+), 1 deletion(-)
     create mode 100644 test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java
    
    diff --git a/test/unit/org/openstreetmap/josm/data/imagery/vectortile/mapbox/FeatureTest.java b/test/unit/org/openstreetmap/josm/data/imagery/vectortile/mapbox/FeatureTest.java
    index 5468fe649..fcc71345b 100644
    a b import static org.openstreetmap.josm.data.imagery.vectortile.mapbox.LayerTest.ge  
    1212import static org.openstreetmap.josm.data.imagery.vectortile.mapbox.LayerTest.getLayer;
    1313
    1414import java.text.NumberFormat;
     15import java.util.ArrayList;
    1516import java.util.Arrays;
     17import java.util.List;
    1618
    1719import org.junit.jupiter.api.Test;
     20import org.openstreetmap.josm.testutils.annotations.I18n;
    1821
    1922/**
    2023 * Test class for {@link Feature}
    class FeatureTest {  
    5053        // This is the float we are adding
    5154        // 49 74 24 00 == 1_000_000f
    5255        // 3f 80 00 00 == 1f
    53         byte[] newBytes = new byte[] {0x22, 0x09, 0x15, 0x00, 0x24, 0x74, 0x49};
     56        byte[] newBytes = new byte[] {0x22, 0x05, 0x15, 0x00, 0x24, 0x74, 0x49};
    5457        byte[] copyBytes = Arrays.copyOf(getSimpleFeatureLayerBytes(), getSimpleFeatureLayerBytes().length + newBytes.length - 4);
    5558        // Change last few bytes
    5659        System.arraycopy(newBytes, 0, copyBytes, 25, newBytes.length);
    class FeatureTest {  
    7982        assertEquals("1000000", feature.getTags().get("a"));
    8083    }
    8184
     85    /**
     86     * Non-regression test for #20933 (Russian)
     87     * @see #testNumberGroupingDecimalEn()
     88     */
     89    @I18n("ru")
     90    @Test
     91    void testNumberGroupingDecimalRu() {
     92        testNumberGroupingDecimal();
     93    }
     94
     95    /**
     96     * Non-regression test for #20933 (English)
     97     * @see #testNumberGroupingDecimalRu()
     98     */
     99    @I18n("en")
     100    @Test
     101    void testNumberGroupingDecimalEn() {
     102        testNumberGroupingDecimal();
     103    }
     104
     105    /**
     106     * Common parts for non-regression tests for #20933
     107     * @see #testNumberGroupingDecimalEn()
     108     * @see #testNumberGroupingDecimalRu()
     109     */
     110    private void testNumberGroupingDecimal() {
     111        byte[] newBytes = new byte[] {0x22, 0x09, 0x19, -45, 0x4D, 0x62, 0x10, 0x58, -71, 0x67, 0x40};
     112        byte[] copyBytes = Arrays.copyOf(getSimpleFeatureLayerBytes(), getSimpleFeatureLayerBytes().length + newBytes.length - 4);
     113        // Change last few bytes
     114        System.arraycopy(newBytes, 0, copyBytes, 25, newBytes.length);
     115        // Update the length of the record
     116        copyBytes[1] = (byte) (copyBytes[1] + newBytes.length - 4);
     117        Layer layer = assertDoesNotThrow(() -> getLayer(copyBytes));
     118        layer.getKey(0);
     119        List<Feature> features = new ArrayList<>(layer.getFeatures());
     120        assertEquals(1, features.size());
     121        assertEquals("189.792", features.get(0).getTags().get("a"));
     122    }
     123
    82124    private void testCreation(byte[] bytes) {
    83125        Layer layer = assertDoesNotThrow(() -> getLayer(bytes));
    84126        // Sanity check the layer
  • new file test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java

    diff --git a/test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java b/test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java
    new file mode 100644
    index 000000000..0375427f2
    - +  
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import org.junit.jupiter.api.extension.AfterEachCallback;
     5import org.junit.jupiter.api.extension.BeforeEachCallback;
     6import org.junit.jupiter.api.extension.ExtendWith;
     7import org.junit.jupiter.api.extension.ExtensionContext;
     8import org.junit.platform.commons.support.AnnotationSupport;
     9
     10import java.lang.annotation.Documented;
     11import java.lang.annotation.ElementType;
     12import java.lang.annotation.Retention;
     13import java.lang.annotation.RetentionPolicy;
     14import java.lang.annotation.Target;
     15import java.util.Optional;
     16
     17import static java.util.Collections.emptyList;
     18
     19/**
     20 * Enables the i18n module for this test.
     21 * @author Taylor Smock
     22 * @see JOSMTestRules#i18n(String)
     23 *
     24 */
     25@Documented
     26@Retention(RetentionPolicy.RUNTIME)
     27@Target({ElementType.TYPE, ElementType.METHOD})
     28@ExtendWith(I18n.I18nExtension.class)
     29public @interface I18n {
     30    /**
     31     * Get the language to use for i18n
     32     * @return The language (default "en").
     33     */
     34    String value() default "en";
     35
     36    /**
     37     * Enables the i18n module for this test.
     38     * @author Taylor Smock
     39     * @see JOSMTestRules#i18n(String)
     40     *
     41     */
     42    class I18nExtension implements AfterEachCallback, BeforeEachCallback {
     43        @Override
     44        public void afterEach(ExtensionContext context) throws Exception {
     45            org.openstreetmap.josm.tools.I18n.set("en");
     46            org.openstreetmap.josm.tools.I18n.set(org.openstreetmap.josm.tools.I18n.getOriginalLocale().getLanguage());
     47        }
     48
     49        @Override
     50        public void beforeEach(ExtensionContext context) throws Exception {
     51            Optional<I18n> annotation = AnnotationSupport.findAnnotation(context.getElement(), I18n.class);
     52            String language = "en";
     53            if (annotation.isPresent()) {
     54                language = annotation.get().value();
     55            }
     56            org.openstreetmap.josm.tools.I18n.set(language);
     57        }
     58    }
     59}