You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/04/04 03:01:27 UTC

[GitHub] beiwei30 closed pull request #1413: Fix #1411 Java Locale use '_' split language, country, variant.

beiwei30 closed pull request #1413: Fix #1411 Java Locale use '_' split language, country, variant.
URL: https://github.com/apache/incubator-dubbo/pull/1413
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/LocaleHandle.java b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/LocaleHandle.java
index 6018184644..751eee651d 100644
--- a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/LocaleHandle.java
+++ b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/LocaleHandle.java
@@ -54,17 +54,37 @@
  * Handle for a locale object.
  */
 public class LocaleHandle implements java.io.Serializable, HessianHandle {
-    private String language;
-    private String country;
-    private String variant;
+    private String value;
 
-    public LocaleHandle(String language, String country, String variant) {
-        this.language = language;
-        this.country = country;
-        this.variant = variant;
+    public LocaleHandle(String locale) {
+        this.value = locale;
     }
 
     private Object readResolve() {
+        if (value == null) {
+            return null;
+        }
+
+        if (value.length() == 0) {
+            return new Locale("");
+        }
+
+        int extStart = value.indexOf("_#");
+        if (extStart != -1) value = value.substring(0, extStart);
+
+        String language = value, country = "", variant = "";
+        int pos1 = value.indexOf('_');
+        if (pos1 != -1) {
+            language = value.substring(0, pos1++);
+
+            int pos2 = value.indexOf('_', pos1);
+            if (pos2 == -1) {
+                country = value.substring(pos1);
+            } else {
+                country = value.substring(pos1, pos2);
+                variant = value.substring(pos2 + 1);
+            }
+        }
         return new Locale(language, country, variant);
     }
 }
diff --git a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/LocaleSerializer.java b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/LocaleSerializer.java
index 6555e4a6b5..86f6cb0f3c 100644
--- a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/LocaleSerializer.java
+++ b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/LocaleSerializer.java
@@ -68,7 +68,7 @@ public void writeObject(Object obj, AbstractHessianOutput out)
         else {
             Locale locale = (Locale) obj;
 
-            out.writeObject(new LocaleHandle(locale.getLanguage(), locale.getCountry(), locale.getVariant()));
+            out.writeObject(new LocaleHandle(locale.toString()));
         }
     }
 }
diff --git a/hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/LocaleSerializerTest.java b/hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/LocaleSerializerTest.java
index 0732e5171e..73cdd3f174 100644
--- a/hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/LocaleSerializerTest.java
+++ b/hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/LocaleSerializerTest.java
@@ -1,41 +1,29 @@
 package com.alibaba.com.caucho.hessian.io;
 
-import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
-import junit.framework.TestCase;
+import java.io.IOException;
+import java.util.Locale;
+
 import org.junit.Test;
 
-import java.util.Locale;
+import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
+
+import junit.framework.TestCase;
 
 public class LocaleSerializerTest extends SerializeTestBase {
+
+    /** {@linkplain LocaleSerializer#writeObject(Object, AbstractHessianOutput)} */
     @Test
-    public void hessian2() throws Exception {
-        Locale locale = new Locale("zh");
-        Locale result = baseHession2Serialize(locale);
-        TestCase.assertEquals(locale, result);
-        locale = new Locale("zh", "CN");
-        result = baseHession2Serialize(locale);
-        TestCase.assertEquals(locale, result);
-        locale = new Locale("zh", "CN", "GBK");
-        result = baseHession2Serialize(locale);
-        TestCase.assertEquals(locale, result);
-        locale = new Locale("zh-hant", "CN");
-        result = baseHession2Serialize(locale);
-        TestCase.assertEquals(locale, result);
+    public void locale() throws IOException {
+        assertLocale(null);
+        assertLocale(new Locale(""));
+        assertLocale(new Locale("zh"));
+        assertLocale(new Locale("zh", "CN"));
+        assertLocale(new Locale("zh-hant", "CN"));
+        assertLocale(new Locale("zh-hant", "CN", "GBK"));
     }
 
-    @Test
-    public void hessian1() throws Exception {
-        Locale locale = new Locale("zh");
-        Locale result = baseHessionSerialize(locale);
-        TestCase.assertEquals(locale, result);
-        locale = new Locale("zh", "CN");
-        result = baseHessionSerialize(locale);
-        TestCase.assertEquals(locale, result);
-        locale = new Locale("zh", "CN", "GBK");
-        result = baseHessionSerialize(locale);
-        TestCase.assertEquals(locale, result);
-        locale = new Locale("zh-hant", "CN");
-        result = baseHessionSerialize(locale);
-        TestCase.assertEquals(locale, result);
+    private void assertLocale(Locale locale) throws IOException {
+        TestCase.assertEquals(locale, baseHession2Serialize(locale));
+        TestCase.assertEquals(locale, baseHessionSerialize(locale));
     }
-}
+}
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services