Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1411 Java Locale use '_' split language, country, variant. #1413

Merged
merged 1 commit into from Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert commit 0423219

线上环境需要LocaleHandler版本兼容,所以还原代码。


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);
}
}
Expand Up @@ -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()));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert commit 0423219

线上环境需要LocaleHandler版本兼容,所以还原代码。

}
}
}
@@ -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));
}
}
}