Skip to content

Commit

Permalink
add catch user defined exceptions. #208
Browse files Browse the repository at this point in the history
add support java8 time object. #212, #221
  • Loading branch information
zhangymPerson committed Sep 12, 2020
1 parent db1ea9a commit 7c8f746
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
3 changes: 3 additions & 0 deletions output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
39 changes: 39 additions & 0 deletions output/testfuncs/java8_time.go
Original file line number Diff line number Diff line change
@@ -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()
}
30 changes: 30 additions & 0 deletions output/testfuncs/java_exception.go
Original file line number Diff line number Diff line change
@@ -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()
}
48 changes: 48 additions & 0 deletions test_hessian/src/test/java/unit/GoJava8TimeTest.java
Original file line number Diff line number Diff line change
@@ -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 <br/>
* description class <br/>
* test java8
*
* @author [email protected]
* @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"));
}

}
49 changes: 49 additions & 0 deletions test_hessian/src/test/java/unit/GoJavaExceptionTest.java
Original file line number Diff line number Diff line change
@@ -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 <br/>
* description class <br/>
* test java8
*
* @author [email protected]
* @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"));
}

}

0 comments on commit 7c8f746

Please sign in to comment.