github.com/astaxie/beego@v1.12.3/config/xml/xml_test.go (about) 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 15 package xml 16 17 import ( 18 "fmt" 19 "os" 20 "testing" 21 22 "github.com/astaxie/beego/config" 23 ) 24 25 func TestXML(t *testing.T) { 26 27 var ( 28 //xml parse should incluce in <config></config> tags 29 xmlcontext = `<?xml version="1.0" encoding="UTF-8"?> 30 <config> 31 <appname>beeapi</appname> 32 <httpport>8080</httpport> 33 <mysqlport>3600</mysqlport> 34 <PI>3.1415976</PI> 35 <runmode>dev</runmode> 36 <autorender>false</autorender> 37 <copyrequestbody>true</copyrequestbody> 38 <path1>${GOPATH}</path1> 39 <path2>${GOPATH||/home/go}</path2> 40 <mysection> 41 <id>1</id> 42 <name>MySection</name> 43 </mysection> 44 </config> 45 ` 46 keyValue = map[string]interface{}{ 47 "appname": "beeapi", 48 "httpport": 8080, 49 "mysqlport": int64(3600), 50 "PI": 3.1415976, 51 "runmode": "dev", 52 "autorender": false, 53 "copyrequestbody": true, 54 "path1": os.Getenv("GOPATH"), 55 "path2": os.Getenv("GOPATH"), 56 "error": "", 57 "emptystrings": []string{}, 58 } 59 ) 60 61 f, err := os.Create("testxml.conf") 62 if err != nil { 63 t.Fatal(err) 64 } 65 _, err = f.WriteString(xmlcontext) 66 if err != nil { 67 f.Close() 68 t.Fatal(err) 69 } 70 f.Close() 71 defer os.Remove("testxml.conf") 72 73 xmlconf, err := config.NewConfig("xml", "testxml.conf") 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 var xmlsection map[string]string 79 xmlsection, err = xmlconf.GetSection("mysection") 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 if len(xmlsection) == 0 { 85 t.Error("section should not be empty") 86 } 87 88 for k, v := range keyValue { 89 90 var ( 91 value interface{} 92 err error 93 ) 94 95 switch v.(type) { 96 case int: 97 value, err = xmlconf.Int(k) 98 case int64: 99 value, err = xmlconf.Int64(k) 100 case float64: 101 value, err = xmlconf.Float(k) 102 case bool: 103 value, err = xmlconf.Bool(k) 104 case []string: 105 value = xmlconf.Strings(k) 106 case string: 107 value = xmlconf.String(k) 108 default: 109 value, err = xmlconf.DIY(k) 110 } 111 if err != nil { 112 t.Errorf("get key %q value fatal,%v err %s", k, v, err) 113 } else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) { 114 t.Errorf("get key %q value, want %v got %v .", k, v, value) 115 } 116 117 } 118 119 if err = xmlconf.Set("name", "astaxie"); err != nil { 120 t.Fatal(err) 121 } 122 if xmlconf.String("name") != "astaxie" { 123 t.Fatal("get name error") 124 } 125 }