This is an automated email from the ASF dual-hosted git repository.

zrlw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-hessian-lite.git


The following commit(s) were added to refs/heads/master by this push:
     new 5b1f4fcc Enhance LocaleHandle to support script in Locale creation 
(#98)
5b1f4fcc is described below

commit 5b1f4fcc15c35e07bd4b6c68a5a3caa6b7002863
Author: wuwen <[email protected]>
AuthorDate: Sat Sep 20 08:40:11 2025 +0800

    Enhance LocaleHandle to support script in Locale creation (#98)
---
 .../com/caucho/hessian/io/LocaleHandle.java        |  24 ++++-
 .../hessian/io/LocaleSerializerScriptTest.java     | 107 +++++++++++++++++++++
 2 files changed, 129 insertions(+), 2 deletions(-)

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 880f0a5a..86e69844 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
@@ -66,12 +66,16 @@ public class LocaleHandle implements java.io.Serializable, 
HessianHandle {
             return null;
         }
 
-        if (value.length() == 0) {
+        if (value.isEmpty()) {
             return new Locale("");
         }
 
         int extStart = value.indexOf("_#");
-        if (extStart != -1) value = value.substring(0, extStart);
+        String script = null;
+        if (extStart != -1) {
+            script = value.substring(extStart + 2);
+            value = value.substring(0, extStart);
+        } 
 
         String language = value, country = "", variant = "";
         int pos1 = value.indexOf('_');
@@ -86,6 +90,22 @@ public class LocaleHandle implements java.io.Serializable, 
HessianHandle {
                 variant = value.substring(pos2 + 1);
             }
         }
+
+        if (script != null && !script.isEmpty()) {
+            Locale.Builder builder = new Locale.Builder();
+            if (!language.isEmpty()) {
+                builder.setLanguage(language);
+            }
+            if (!country.isEmpty()) {
+                builder.setRegion(country);
+            }
+            if (!variant.isEmpty()) {
+                builder.setVariant(variant);
+            }
+            builder.setScript(script);
+            return builder.build();
+        }
+        
         return new Locale(language, country, variant);
     }
 }
diff --git 
a/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/LocaleSerializerScriptTest.java
 
b/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/LocaleSerializerScriptTest.java
new file mode 100644
index 00000000..e39d2adc
--- /dev/null
+++ 
b/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/LocaleSerializerScriptTest.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 com.alibaba.com.caucho.hessian.io;
+
+import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Test for Locale script preservation during serialization/deserialization
+ */
+public class LocaleSerializerScriptTest extends SerializeTestBase {
+    
+    @Test
+    void testLocaleWithScript() throws IOException {
+        // Create a Locale with script
+        Locale zh_CN_Hans = new Locale.Builder()
+                .setLanguage("zh")
+                .setRegion("CN")
+                .setScript("Hans")
+                .build();
+
+        // Serialize and deserialize
+        Locale result = baseHessian2Serialize(zh_CN_Hans);
+
+        // The script should be preserved
+        assertEquals(zh_CN_Hans.getLanguage(), result.getLanguage(), "Language 
should be preserved");
+        assertEquals(zh_CN_Hans.getCountry(), result.getCountry(), "Country 
should be preserved");
+        assertEquals(zh_CN_Hans.getScript(), result.getScript(), "Script 
should be preserved");
+        assertEquals(zh_CN_Hans.toString(), result.toString(), "String 
representation should match");
+        assertEquals(zh_CN_Hans, result, "Locales should be equal");
+    }
+
+    @Test
+    void testLocaleWithScriptAndVariant() throws IOException {
+        // Create a Locale with script and variant
+        Locale complex = new Locale.Builder()
+                .setLanguage("en")
+                .setRegion("US")
+                .setScript("Latn")
+                .setVariant("POSIX")
+                .build();
+
+        // Serialize and deserialize
+        Locale result = baseHessian2Serialize(complex);
+
+        // All components should be preserved
+        assertEquals(complex.getLanguage(), result.getLanguage(), "Language 
should be preserved");
+        assertEquals(complex.getCountry(), result.getCountry(), "Country 
should be preserved");
+        assertEquals(complex.getScript(), result.getScript(), "Script should 
be preserved");
+        assertEquals(complex.getVariant(), result.getVariant(), "Variant 
should be preserved");
+        assertEquals(complex.toString(), result.toString(), "String 
representation should match");
+        assertEquals(complex, result, "Locales should be equal");
+    }
+
+    @Test
+    void testVariousScriptLocales() throws IOException {
+        // Test different script codes
+        Locale[] testLocales = {
+            new Locale.Builder()
+                    .setLanguage("ar")
+                    .setRegion("EG")
+                    .setScript("Arab")
+                    .build(),
+            new Locale.Builder()
+                    .setLanguage("ja")
+                    .setRegion("JP")
+                    .setScript("Jpan")
+                    .build(),
+            new Locale.Builder()
+                    .setLanguage("ru")
+                    .setRegion("RU")
+                    .setScript("Cyrl")
+                    .build(),
+            new Locale.Builder()
+                    .setLanguage("hi")
+                    .setRegion("IN")
+                    .setScript("Deva")
+                    .build()
+        };
+
+        for (Locale original : testLocales) {
+            Locale result = baseHessian2Serialize(original);
+            assertEquals(original.getScript(), result.getScript(), "Script 
should be preserved for " + original);
+            assertEquals(original, result, "Locales should be equal for " + 
original);
+        }
+    }
+}

Reply via email to