xml_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package xml
  15. import (
  16. "fmt"
  17. "os"
  18. "testing"
  19. "github.com/astaxie/beego/config"
  20. )
  21. func TestXML(t *testing.T) {
  22. var (
  23. //xml parse should incluce in <config></config> tags
  24. xmlcontext = `<?xml version="1.0" encoding="UTF-8"?>
  25. <config>
  26. <appname>beeapi</appname>
  27. <httpport>8080</httpport>
  28. <mysqlport>3600</mysqlport>
  29. <PI>3.1415976</PI>
  30. <runmode>dev</runmode>
  31. <autorender>false</autorender>
  32. <copyrequestbody>true</copyrequestbody>
  33. <path1>${GOPATH}</path1>
  34. <path2>${GOPATH||/home/go}</path2>
  35. <mysection>
  36. <id>1</id>
  37. <name>MySection</name>
  38. </mysection>
  39. </config>
  40. `
  41. keyValue = map[string]interface{}{
  42. "appname": "beeapi",
  43. "httpport": 8080,
  44. "mysqlport": int64(3600),
  45. "PI": 3.1415976,
  46. "runmode": "dev",
  47. "autorender": false,
  48. "copyrequestbody": true,
  49. "path1": os.Getenv("GOPATH"),
  50. "path2": os.Getenv("GOPATH"),
  51. "error": "",
  52. "emptystrings": []string{},
  53. }
  54. )
  55. f, err := os.Create("testxml.conf")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. _, err = f.WriteString(xmlcontext)
  60. if err != nil {
  61. f.Close()
  62. t.Fatal(err)
  63. }
  64. f.Close()
  65. defer os.Remove("testxml.conf")
  66. xmlconf, err := config.NewConfig("xml", "testxml.conf")
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. var xmlsection map[string]string
  71. xmlsection, err = xmlconf.GetSection("mysection")
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. if len(xmlsection) == 0 {
  76. t.Error("section should not be empty")
  77. }
  78. for k, v := range keyValue {
  79. var (
  80. value interface{}
  81. err error
  82. )
  83. switch v.(type) {
  84. case int:
  85. value, err = xmlconf.Int(k)
  86. case int64:
  87. value, err = xmlconf.Int64(k)
  88. case float64:
  89. value, err = xmlconf.Float(k)
  90. case bool:
  91. value, err = xmlconf.Bool(k)
  92. case []string:
  93. value = xmlconf.Strings(k)
  94. case string:
  95. value = xmlconf.String(k)
  96. default:
  97. value, err = xmlconf.DIY(k)
  98. }
  99. if err != nil {
  100. t.Errorf("get key %q value fatal,%v err %s", k, v, err)
  101. } else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
  102. t.Errorf("get key %q value, want %v got %v .", k, v, value)
  103. }
  104. }
  105. if err = xmlconf.Set("name", "astaxie"); err != nil {
  106. t.Fatal(err)
  107. }
  108. if xmlconf.String("name") != "astaxie" {
  109. t.Fatal("get name error")
  110. }
  111. }