-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind_test.go
117 lines (94 loc) · 4.26 KB
/
bind_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
package context
import (
core "github.com/procyon-projects/procyon-core"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
type testProperties struct {
DriverName string `yaml:"driver-name" json:"driver-name"`
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
DatabaseName string `yaml:"database-name" json:"database-name"`
Port uint16 `yaml:"port" json:"port"`
Timeout uint32 `yaml:"timeout" json:"timeout"`
DefaultValue string `yaml:"default" json:"default" default:"test"`
}
func (testProperties) GetConfigurationPrefix() string {
return "test"
}
func TestConfigurationPropertiesBinder(t *testing.T) {
standardEnvironment := core.NewStandardEnvironment()
tempArgs := os.Args
os.Args = append(os.Args, "--test.driver-name=test-driver")
os.Args = append(os.Args, "--test.username=test-user")
os.Args = append(os.Args, "--test.password=test-pass")
os.Args = append(os.Args, "--test.database-name=test-db")
os.Args = append(os.Args, "--test.port=3000")
os.Args = append(os.Args, "--test.timeout=1000")
propertySources := core.NewSimpleCommandLinePropertySource(os.Args)
standardEnvironment.GetPropertySources().Add(propertySources)
propertiesBindingProcessor := newConfigurationPropertiesBinder(standardEnvironment, core.NewDefaultTypeConverterService())
properties := &testProperties{}
err := propertiesBindingProcessor.Bind(properties)
assert.Nil(t, err)
assert.Equal(t, "test-driver", properties.DriverName)
assert.Equal(t, "test-user", properties.Username)
assert.Equal(t, "test-pass", properties.Password)
assert.Equal(t, "test-db", properties.DatabaseName)
assert.Equal(t, uint16(3000), properties.Port)
assert.Equal(t, uint32(1000), properties.Timeout)
assert.Equal(t, "test", properties.DefaultValue)
os.Args = tempArgs
}
type testYamlProperties struct {
DriverName string `yaml:"driver-name"`
Username string `yaml:"username"`
Password string `yaml:"password"`
DatabaseName string `yaml:"database-name"`
Port uint16 `yaml:"port"`
Timeout uint32 `yaml:"timeout"`
}
func (testYamlProperties) GetConfigurationPrefix() string {
return "test"
}
func TestConfigurationPropertiesBinderForYaml(t *testing.T) {
standardEnvironment := core.NewStandardEnvironment()
tempArgs := os.Args
os.Args = append(os.Args, "--test.driver-name=test-driver")
os.Args = append(os.Args, "--test.username=test-user")
os.Args = append(os.Args, "--test.password=test-pass")
os.Args = append(os.Args, "--test.database-name=test-db")
os.Args = append(os.Args, "--test.port=3000")
os.Args = append(os.Args, "--test.timeout=1000")
propertySources := core.NewSimpleCommandLinePropertySource(os.Args)
standardEnvironment.GetPropertySources().Add(propertySources)
propertiesBindingProcessor := newConfigurationPropertiesBinder(standardEnvironment, core.NewDefaultTypeConverterService())
properties := &testProperties{}
err := propertiesBindingProcessor.Bind(properties)
assert.Nil(t, err)
assert.Equal(t, "test-driver", properties.DriverName)
assert.Equal(t, "test-user", properties.Username)
assert.Equal(t, "test-pass", properties.Password)
assert.Equal(t, "test-db", properties.DatabaseName)
assert.Equal(t, uint16(3000), properties.Port)
assert.Equal(t, uint32(1000), properties.Timeout)
assert.Equal(t, "test", properties.DefaultValue)
os.Args = tempArgs
}
func TestConfigurationPropertiesBinder_BindWithEmptyInstance(t *testing.T) {
standardEnvironment := core.NewStandardEnvironment()
propertySources := core.NewSimpleCommandLinePropertySource(os.Args)
standardEnvironment.GetPropertySources().Add(propertySources)
propertiesBindingProcessor := newConfigurationPropertiesBinder(standardEnvironment, core.NewDefaultTypeConverterService())
err := propertiesBindingProcessor.Bind(nil)
assert.Nil(t, err)
}
func TestConfigurationPropertiesBinder_BindWithNonConfigurationPropertiesInstance(t *testing.T) {
standardEnvironment := core.NewStandardEnvironment()
propertySources := core.NewSimpleCommandLinePropertySource(os.Args)
standardEnvironment.GetPropertySources().Add(propertySources)
propertiesBindingProcessor := newConfigurationPropertiesBinder(standardEnvironment, core.NewDefaultTypeConverterService())
err := propertiesBindingProcessor.Bind(testContext{})
assert.Nil(t, err)
}