From 2264331065da92543e7db915f8619350dc4da0cd Mon Sep 17 00:00:00 2001 From: zhangym Date: Sat, 12 Sep 2020 11:51:57 +0800 Subject: [PATCH] add catch user defined exceptions. #208 add support java8 time object. #212, #221 --- output/output.go | 3 ++ output/testfuncs/java8_time.go | 39 +++++++++++++++ output/testfuncs/java_exception.go | 30 ++++++++++++ .../src/test/java/unit/GoJava8TimeTest.java | 48 ++++++++++++++++++ .../test/java/unit/GoJavaExceptionTest.java | 49 +++++++++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 output/testfuncs/java8_time.go create mode 100644 output/testfuncs/java_exception.go create mode 100644 test_hessian/src/test/java/unit/GoJava8TimeTest.java create mode 100644 test_hessian/src/test/java/unit/GoJavaExceptionTest.java diff --git a/output/output.go b/output/output.go index 2e13a5b4..65082ca7 100644 --- a/output/output.go +++ b/output/output.go @@ -37,6 +37,9 @@ var ( // add all output func here func init() { funcMap["HelloWorldString"] = testfuncs.HelloWorldString + funcMap["Java8TimeYear"] = testfuncs.Java8TimeYear + funcMap["Java8LocalDate"] = testfuncs.Java8LocalDate + funcMap["JavaException"] = testfuncs.JavaException } func main() { diff --git a/output/testfuncs/java8_time.go b/output/testfuncs/java8_time.go new file mode 100644 index 00000000..3685aa81 --- /dev/null +++ b/output/testfuncs/java8_time.go @@ -0,0 +1,39 @@ +/* + * 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 testfuncs + +import ( + hessian "github.com/apache/dubbo-go-hessian2" + "github.com/apache/dubbo-go-hessian2/java8_time" +) + +// test java8 java.time.Year +func Java8TimeYear() []byte { + e := hessian.NewEncoder() + year := java8_time.Year{Year: 2020} + e.Encode(year) + return e.Buffer() +} + +// test java8 java.time.LocalDate +func Java8LocalDate() []byte { + e := hessian.NewEncoder() + date := java8_time.LocalDate{Year: 2020, Month: 9, Day: 12} + e.Encode(date) + return e.Buffer() +} diff --git a/output/testfuncs/java_exception.go b/output/testfuncs/java_exception.go new file mode 100644 index 00000000..82c36214 --- /dev/null +++ b/output/testfuncs/java_exception.go @@ -0,0 +1,30 @@ +/* + * 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 testfuncs + +import ( + hessian "github.com/apache/dubbo-go-hessian2" + "github.com/apache/dubbo-go-hessian2/java_exception" +) + +// test java Exception +func JavaException() []byte { + e := hessian.NewEncoder() + exception := java_exception.NewException("java_exception") + e.Encode(exception) + return e.Buffer() +} diff --git a/test_hessian/src/test/java/unit/GoJava8TimeTest.java b/test_hessian/src/test/java/unit/GoJava8TimeTest.java new file mode 100644 index 00000000..85c81fc4 --- /dev/null +++ b/test_hessian/src/test/java/unit/GoJava8TimeTest.java @@ -0,0 +1,48 @@ +/* + * 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 unit; + +import junit.framework.Assert; +import org.junit.Test; + +import java.time.LocalDate; +import java.time.Year; + +/** + * date 2020/9/12 11:09
+ * description class
+ * test java8 + * + * @author zhangyanmingjiayou@163.com + * @version 1.0 + * @since 1.0 + */ +public class GoJava8TimeTest { + + /** + * test java8 java.time.* object and go java8_time/* struct + */ + @Test + public void testJava8Year() { + Year year = Year.of(2020); + Assert.assertEquals(year + , GoTestUtil.readGoObject("Java8TimeYear")); + LocalDate localDate = LocalDate.of(2020, 9, 12); + Assert.assertEquals(localDate, GoTestUtil.readGoObject("Java8LocalDate")); + } + +} diff --git a/test_hessian/src/test/java/unit/GoJavaExceptionTest.java b/test_hessian/src/test/java/unit/GoJavaExceptionTest.java new file mode 100644 index 00000000..65d189dd --- /dev/null +++ b/test_hessian/src/test/java/unit/GoJavaExceptionTest.java @@ -0,0 +1,49 @@ +/* + * 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 unit; + +import junit.framework.Assert; +import org.junit.Test; + +/** + * date 2020/9/12 11:09
+ * description class
+ * test java8 + * + * @author zhangyanmingjiayou@163.com + * @version 1.0 + * @since 1.0 + */ +public class GoJavaExceptionTest { + + /** + * test java java.lang.Exception object and go java_exception Exception struct + */ + @Test + public void testException() { + Exception exception = new Exception("java_exception"); + Object javaException = GoTestUtil.readGoObject("JavaException"); + if (javaException instanceof Exception) { + Assert.assertEquals(exception.getMessage(), ((Exception) javaException).getMessage()); + } + // assertEquals don't compare Exception object + // Exception exception2 = new Exception("java_exception"); + // Assert.assertEquals(exception, exception2); + // Assert.assertEquals(exception, GoTestUtil.readGoObject("JavaException")); + } + +}