-
Notifications
You must be signed in to change notification settings - Fork 114
/
date_test.go
179 lines (158 loc) · 4.32 KB
/
date_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* 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 hessian
import (
"testing"
"time"
)
import (
"github.com/stretchr/testify/assert"
)
func init() {
RegisterPOJO(&DateDemo{})
}
type DateDemo struct {
Name string
Date time.Time
Dates []**time.Time
NilDate *time.Time
Date1 *time.Time
Date2 **time.Time
Date3 ***time.Time
}
// JavaClassName java fully qualified path
func (DateDemo) JavaClassName() string {
return "test.model.DateDemo"
}
func TestEncDate(t *testing.T) {
var (
v string
tz time.Time
err error
e *Encoder
d *Decoder
res interface{}
)
e = NewEncoder()
v = "2014-02-09 06:15:23"
tz, _ = time.Parse("2006-01-02 15:04:05", v)
e.Encode(tz)
if len(e.Buffer()) == 0 {
t.Fail()
}
d = NewDecoder(e.Buffer())
res, err = d.Decode()
t.Logf("decode(%s, %s) = %v, %v\n", v, tz.Local(), res, err)
assert.Equal(t, tz.Local(), res)
}
func TestEncDateIssue348(t *testing.T) {
e := NewEncoder()
v := "2914-02-09 06:15:23"
tz, _ := time.Parse("2006-01-02 15:04:05", v)
e.Encode(tz)
if len(e.Buffer()) == 0 {
t.Fail()
}
d := NewDecoder(e.Buffer())
res, err := d.Decode()
t.Logf("decode(%s, %s) = %v, %v\n", v, tz.Local(), res, err)
assert.Equal(t, tz.Local(), res)
}
func testDateFramework(t *testing.T, method string, expected time.Time) {
r, e := decodeJavaResponse(method, "", false)
if e != nil {
t.Errorf("%s: decode fail with error %+v", method, e)
return
}
v, ok := r.(time.Time)
if !ok {
t.Errorf("%s: %v is not date", method, r)
return
}
if !v.Equal(expected) {
t.Errorf("%s: got %v, wanted %v", method, v, expected)
}
}
func TestDate(t *testing.T) {
testDateFramework(t, "replyDate_0", time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC))
testDateFramework(t, "replyDate_1", time.Date(1998, 5, 8, 9, 51, 31, 0, time.UTC))
testDateFramework(t, "replyDate_2", time.Date(1998, 5, 8, 9, 51, 0, 0, time.UTC))
}
func TestDateEncode(t *testing.T) {
testJavaDecode(t, "argDate_0", time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC))
testJavaDecode(t, "argDate_1", time.Date(1998, 5, 8, 9, 51, 31, 0, time.UTC))
testJavaDecode(t, "argDate_2", time.Date(1998, 5, 8, 9, 51, 0, 0, time.UTC))
}
func TestEncDateNull(t *testing.T) {
var (
v string
tz time.Time
e *Encoder
d *Decoder
res interface{}
err error
)
v = "2014-02-09 06:15:23 +0800 CST"
tz, _ = time.Parse("2006-01-02 15:04:05 +0800 CST", v)
d1 := &tz
d2 := &d1
d3 := &d2
date := DateDemo{
Name: "zs",
Date: ZeroDate,
Dates: []**time.Time{d2, d2},
NilDate: nil,
Date1: nil,
Date2: d2,
Date3: d3,
}
e = NewEncoder()
err = e.Encode(date)
if err != nil {
t.Fatal(err)
}
if len(e.Buffer()) == 0 {
t.Fail()
}
d = NewDecoder(e.Buffer())
res, err = d.Decode()
assert.Nil(t, err)
assert.Equal(t, ZeroDate, res.(*DateDemo).Date)
assert.Equal(t, 2, len(res.(*DateDemo).Dates))
assert.Equal(t, tz.Local().String(), (*res.(*DateDemo).Dates[0]).String())
assert.Nil(t, res.(*DateDemo).NilDate)
assert.Nil(t, res.(*DateDemo).Date1)
assert.Equal(t, tz.Local().String(), (*res.(*DateDemo).Date2).String())
assert.Equal(t, tz.Local().String(), (*(*res.(*DateDemo).Date3)).String())
}
func TestDateNulJavaDecode(t *testing.T) {
date := DateDemo{
Name: "zs",
Date: ZeroDate,
}
testJavaDecode(t, "customArgTypedFixed_DateNull", date)
}
func TestDateNilDecode(t *testing.T) {
doTestDateNull(t, "customReplyTypedFixedDateNull")
}
func doTestDateNull(t *testing.T, method string) {
testDecodeFrameworkFunc(t, method, func(r interface{}) {
t.Logf("%#v", r)
assert.Equal(t, ZeroDate, r.(*DateDemo).Date)
assert.Nil(t, r.(*DateDemo).Date1)
})
}