This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
123 lines (97 loc) · 2.62 KB
/
context_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
package bean
import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
"time"
)
type foo struct {
Bar string
Client *http.Client
}
func (c *foo) Init() {
c.Bar = "test"
}
var definitions = []BeanDefinition{
{
Name: "httpclient",
Reflect: NewReflect(http.Client{}),
Fields: Fields{
"Timeout": time.Duration(time.Second * 3),
},
},
{
Name: "httpclient2",
Reflect: NewReflect(NewHttpClient),
ConstructorArgs: ConstructorArgs{
time.Duration(time.Second * 3),
},
},
{
Name: "httpclient3",
Reflect: NewReflect(NewHttpClient),
ConstructorArgs: ConstructorArgs{
time.Duration(time.Second * 3),
},
Fields: Fields{
"Timeout": time.Duration(time.Second * 2),
},
},
{
Name: "foo",
InitMethod: "Init",
Reflect: NewReflect(foo{}),
Fields: Fields{
"Bar": "bar",
"Client": NewReference("httpclient2"),
},
},
}
// 只能返回指针类型方能注入成功
func NewHttpClient(timeout time.Duration) *http.Client {
return &http.Client{
Timeout: timeout,
}
}
func TestGetReflectStruct(t *testing.T) {
a := assert.New(t)
context := NewApplicationContext(definitions)
cli := context.Get("httpclient").(*http.Client)
_, err := cli.Get("http://www.baidu.com/")
a.Equal(err, nil)
}
func TestGetReflectFunc(t *testing.T) {
a := assert.New(t)
context := NewApplicationContext(definitions)
cli := context.Get("httpclient3").(*http.Client)
_, err := cli.Get("http://www.baidu.com/")
a.Equal(err, nil)
}
func TestGetReference(t *testing.T) {
a := assert.New(t)
context := NewApplicationContext(definitions)
foo := context.Get("foo").(*foo)
a.Equal(foo.Bar, "test")
cli := foo.Client
_, err := cli.Get("http://www.baidu.com/")
a.Equal(err, nil)
}
func TestCoverConstructorArgs(t *testing.T) {
a := assert.New(t)
context := NewApplicationContext(definitions)
cli := context.Get("httpclient2").(*http.Client)
cli1 := context.GetBean("httpclient2", nil, nil).(*http.Client)
a.Equal(fmt.Sprintf("%v", cli), fmt.Sprintf("%v", cli1))
cli2 := context.GetBean("httpclient2", nil, ConstructorArgs{time.Duration(time.Second * 4)}).(*http.Client)
a.NotEqual(fmt.Sprintf("%v", cli), fmt.Sprintf("%v", cli2))
}
func TestCoverFields(t *testing.T) {
a := assert.New(t)
context := NewApplicationContext(definitions)
cli := context.Get("httpclient").(*http.Client)
cli1 := context.GetBean("httpclient", nil, nil).(*http.Client)
a.Equal(fmt.Sprintf("%v", cli), fmt.Sprintf("%v", cli1))
cli2 := context.GetBean("httpclient", Fields{"Timeout": time.Duration(time.Second * 4)}, nil).(*http.Client)
a.NotEqual(fmt.Sprintf("%v", cli), fmt.Sprintf("%v", cli2))
}