1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-15 06:40:54 +00:00
Beego/core/config/xml/xml_test.go

158 lines
3.4 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed 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 xml
import (
"fmt"
"os"
"testing"
2020-08-31 13:57:26 +00:00
"github.com/stretchr/testify/assert"
"github.com/astaxie/beego/core/config"
2020-07-22 14:50:08 +00:00
)
func TestXML(t *testing.T) {
var (
2020-08-26 03:46:22 +00:00
// xml parse should incluce in <config></config> tags
2020-07-22 14:50:08 +00:00
xmlcontext = `<?xml version="1.0" encoding="UTF-8"?>
<config>
<appname>beeapi</appname>
<httpport>8080</httpport>
<mysqlport>3600</mysqlport>
<PI>3.1415976</PI>
<runmode>dev</runmode>
<autorender>false</autorender>
<copyrequestbody>true</copyrequestbody>
<path1>${GOPATH}</path1>
<path2>${GOPATH||/home/go}</path2>
<mysection>
<id>1</id>
<name>MySection</name>
</mysection>
</config>
`
keyValue = map[string]interface{}{
"appname": "beeapi",
"httpport": 8080,
"mysqlport": int64(3600),
"PI": 3.1415976,
"runmode": "dev",
"autorender": false,
"copyrequestbody": true,
"path1": os.Getenv("GOPATH"),
"path2": os.Getenv("GOPATH"),
"error": "",
"emptystrings": []string{},
}
)
f, err := os.Create("testxml.conf")
if err != nil {
t.Fatal(err)
}
_, err = f.WriteString(xmlcontext)
if err != nil {
f.Close()
t.Fatal(err)
}
f.Close()
defer os.Remove("testxml.conf")
xmlconf, err := config.NewConfig("xml", "testxml.conf")
if err != nil {
t.Fatal(err)
}
var xmlsection map[string]string
2020-10-13 14:32:41 +00:00
xmlsection, err = xmlconf.GetSection("mysection")
2020-07-22 14:50:08 +00:00
if err != nil {
t.Fatal(err)
}
if len(xmlsection) == 0 {
t.Error("section should not be empty")
}
for k, v := range keyValue {
var (
value interface{}
err error
)
switch v.(type) {
case int:
2020-10-13 14:32:41 +00:00
value, err = xmlconf.Int(k)
2020-07-22 14:50:08 +00:00
case int64:
2020-10-13 14:32:41 +00:00
value, err = xmlconf.Int64(k)
2020-07-22 14:50:08 +00:00
case float64:
2020-10-13 14:32:41 +00:00
value, err = xmlconf.Float(k)
2020-07-22 14:50:08 +00:00
case bool:
2020-10-13 14:32:41 +00:00
value, err = xmlconf.Bool(k)
2020-07-22 14:50:08 +00:00
case []string:
2020-10-13 14:32:41 +00:00
value, err = xmlconf.Strings(k)
2020-07-22 14:50:08 +00:00
case string:
2020-10-13 14:32:41 +00:00
value, err = xmlconf.String(k)
2020-07-22 14:50:08 +00:00
default:
2020-10-13 14:32:41 +00:00
value, err = xmlconf.DIY(k)
2020-07-22 14:50:08 +00:00
}
if err != nil {
t.Errorf("get key %q value fatal,%v err %s", k, v, err)
} else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
t.Errorf("get key %q value, want %v got %v .", k, v, value)
}
}
2020-10-13 14:32:41 +00:00
if err = xmlconf.Set("name", "astaxie"); err != nil {
2020-07-22 14:50:08 +00:00
t.Fatal(err)
}
2020-08-26 03:46:22 +00:00
2020-10-13 14:32:41 +00:00
res, _ := xmlconf.String("name")
2020-08-26 03:46:22 +00:00
if res != "astaxie" {
2020-07-22 14:50:08 +00:00
t.Fatal("get name error")
}
2020-08-31 13:57:26 +00:00
2020-10-13 14:32:41 +00:00
sub, err := xmlconf.Sub("mysection")
2020-08-31 13:57:26 +00:00
assert.Nil(t, err)
assert.NotNil(t, sub)
2020-10-13 14:32:41 +00:00
name, err := sub.String("name")
2020-08-31 13:57:26 +00:00
assert.Nil(t, err)
assert.Equal(t, "MySection", name)
2020-10-13 14:32:41 +00:00
id, err := sub.Int("id")
2020-08-31 13:57:26 +00:00
assert.Nil(t, err)
assert.Equal(t, 1, id)
sec := &Section{}
2020-10-13 14:32:41 +00:00
err = sub.Unmarshaler("", sec)
2020-08-31 13:57:26 +00:00
assert.Nil(t, err)
assert.Equal(t, "MySection", sec.Name)
sec = &Section{}
2020-10-13 14:32:41 +00:00
err = xmlconf.Unmarshaler("mysection", sec)
2020-08-31 13:57:26 +00:00
assert.Nil(t, err)
assert.Equal(t, "MySection", sec.Name)
}
type Section struct {
Name string `xml:"name"`
2020-07-22 14:50:08 +00:00
}