1
0

json_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 config
  15. import (
  16. "fmt"
  17. "os"
  18. "testing"
  19. )
  20. func TestJsonStartsWithArray(t *testing.T) {
  21. const jsoncontextwitharray = `[
  22. {
  23. "url": "user",
  24. "serviceAPI": "http://www.test.com/user"
  25. },
  26. {
  27. "url": "employee",
  28. "serviceAPI": "http://www.test.com/employee"
  29. }
  30. ]`
  31. f, err := os.Create("testjsonWithArray.conf")
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. _, err = f.WriteString(jsoncontextwitharray)
  36. if err != nil {
  37. f.Close()
  38. t.Fatal(err)
  39. }
  40. f.Close()
  41. defer os.Remove("testjsonWithArray.conf")
  42. jsonconf, err := NewConfig("json", "testjsonWithArray.conf")
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. rootArray, err := jsonconf.DIY("rootArray")
  47. if err != nil {
  48. t.Error("array does not exist as element")
  49. }
  50. rootArrayCasted := rootArray.([]interface{})
  51. if rootArrayCasted == nil {
  52. t.Error("array from root is nil")
  53. } else {
  54. elem := rootArrayCasted[0].(map[string]interface{})
  55. if elem["url"] != "user" || elem["serviceAPI"] != "http://www.test.com/user" {
  56. t.Error("array[0] values are not valid")
  57. }
  58. elem2 := rootArrayCasted[1].(map[string]interface{})
  59. if elem2["url"] != "employee" || elem2["serviceAPI"] != "http://www.test.com/employee" {
  60. t.Error("array[1] values are not valid")
  61. }
  62. }
  63. }
  64. func TestJson(t *testing.T) {
  65. var (
  66. jsoncontext = `{
  67. "appname": "beeapi",
  68. "testnames": "foo;bar",
  69. "httpport": 8080,
  70. "mysqlport": 3600,
  71. "PI": 3.1415976,
  72. "runmode": "dev",
  73. "autorender": false,
  74. "copyrequestbody": true,
  75. "session": "on",
  76. "cookieon": "off",
  77. "newreg": "OFF",
  78. "needlogin": "ON",
  79. "enableSession": "Y",
  80. "enableCookie": "N",
  81. "flag": 1,
  82. "path1": "${GOPATH}",
  83. "path2": "${GOPATH||/home/go}",
  84. "database": {
  85. "host": "host",
  86. "port": "port",
  87. "database": "database",
  88. "username": "username",
  89. "password": "${GOPATH}",
  90. "conns":{
  91. "maxconnection":12,
  92. "autoconnect":true,
  93. "connectioninfo":"info",
  94. "root": "${GOPATH}"
  95. }
  96. }
  97. }`
  98. keyValue = map[string]interface{}{
  99. "appname": "beeapi",
  100. "testnames": []string{"foo", "bar"},
  101. "httpport": 8080,
  102. "mysqlport": int64(3600),
  103. "PI": 3.1415976,
  104. "runmode": "dev",
  105. "autorender": false,
  106. "copyrequestbody": true,
  107. "session": true,
  108. "cookieon": false,
  109. "newreg": false,
  110. "needlogin": true,
  111. "enableSession": true,
  112. "enableCookie": false,
  113. "flag": true,
  114. "path1": os.Getenv("GOPATH"),
  115. "path2": os.Getenv("GOPATH"),
  116. "database::host": "host",
  117. "database::port": "port",
  118. "database::database": "database",
  119. "database::password": os.Getenv("GOPATH"),
  120. "database::conns::maxconnection": 12,
  121. "database::conns::autoconnect": true,
  122. "database::conns::connectioninfo": "info",
  123. "database::conns::root": os.Getenv("GOPATH"),
  124. "unknown": "",
  125. }
  126. )
  127. f, err := os.Create("testjson.conf")
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. _, err = f.WriteString(jsoncontext)
  132. if err != nil {
  133. f.Close()
  134. t.Fatal(err)
  135. }
  136. f.Close()
  137. defer os.Remove("testjson.conf")
  138. jsonconf, err := NewConfig("json", "testjson.conf")
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. for k, v := range keyValue {
  143. var err error
  144. var value interface{}
  145. switch v.(type) {
  146. case int:
  147. value, err = jsonconf.Int(k)
  148. case int64:
  149. value, err = jsonconf.Int64(k)
  150. case float64:
  151. value, err = jsonconf.Float(k)
  152. case bool:
  153. value, err = jsonconf.Bool(k)
  154. case []string:
  155. value = jsonconf.Strings(k)
  156. case string:
  157. value = jsonconf.String(k)
  158. default:
  159. value, err = jsonconf.DIY(k)
  160. }
  161. if err != nil {
  162. t.Fatalf("get key %q value fatal,%v err %s", k, v, err)
  163. } else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
  164. t.Fatalf("get key %q value, want %v got %v .", k, v, value)
  165. }
  166. }
  167. if err = jsonconf.Set("name", "astaxie"); err != nil {
  168. t.Fatal(err)
  169. }
  170. if jsonconf.String("name") != "astaxie" {
  171. t.Fatal("get name error")
  172. }
  173. if db, err := jsonconf.DIY("database"); err != nil {
  174. t.Fatal(err)
  175. } else if m, ok := db.(map[string]interface{}); !ok {
  176. t.Log(db)
  177. t.Fatal("db not map[string]interface{}")
  178. } else {
  179. if m["host"].(string) != "host" {
  180. t.Fatal("get host err")
  181. }
  182. }
  183. if _, err := jsonconf.Int("unknown"); err == nil {
  184. t.Error("unknown keys should return an error when expecting an Int")
  185. }
  186. if _, err := jsonconf.Int64("unknown"); err == nil {
  187. t.Error("unknown keys should return an error when expecting an Int64")
  188. }
  189. if _, err := jsonconf.Float("unknown"); err == nil {
  190. t.Error("unknown keys should return an error when expecting a Float")
  191. }
  192. if _, err := jsonconf.DIY("unknown"); err == nil {
  193. t.Error("unknown keys should return an error when expecting an interface{}")
  194. }
  195. if val := jsonconf.String("unknown"); val != "" {
  196. t.Error("unknown keys should return an empty string when expecting a String")
  197. }
  198. if _, err := jsonconf.Bool("unknown"); err == nil {
  199. t.Error("unknown keys should return an error when expecting a Bool")
  200. }
  201. if !jsonconf.DefaultBool("unknow", true) {
  202. t.Error("unknown keys with default value wrong")
  203. }
  204. }