1 // Copyright 2017 Frank Schroeder. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 "github.com/magiconair/properties/assert"
19 func TestLoadFailsWithNotExistingFile(t *testing.T) {
20 _, err := LoadFile("doesnotexist.properties", ISO_8859_1)
21 assert.Equal(t, err != nil, true, "")
22 assert.Matches(t, err.Error(), "open.*no such file or directory")
25 func TestLoadFilesFailsOnNotExistingFile(t *testing.T) {
26 _, err := LoadFile("doesnotexist.properties", ISO_8859_1)
27 assert.Equal(t, err != nil, true, "")
28 assert.Matches(t, err.Error(), "open.*no such file or directory")
31 func TestLoadFilesDoesNotFailOnNotExistingFileAndIgnoreMissing(t *testing.T) {
32 p, err := LoadFiles([]string{"doesnotexist.properties"}, ISO_8859_1, true)
33 assert.Equal(t, err, nil)
34 assert.Equal(t, p.Len(), 0)
37 func TestLoadString(t *testing.T) {
39 p1 := MustLoadString(x)
40 p2 := must(Load([]byte(x), UTF8))
41 assert.Equal(t, p1, p2)
44 func TestLoadMap(t *testing.T) {
45 // LoadMap does not guarantee the same import order
46 // of keys every time since map access is randomized.
47 // Therefore, we need to compare the generated maps.
48 m := map[string]string{"key": "value", "abc": "def"}
49 assert.Equal(t, LoadMap(m).Map(), m)
52 func TestLoadFile(t *testing.T) {
53 tf := make(tempFiles, 0)
56 filename := tf.makeFile("key=value")
57 p := MustLoadFile(filename, ISO_8859_1)
59 assert.Equal(t, p.Len(), 1)
60 assertKeyValues(t, "", p, "key", "value")
63 func TestLoadFiles(t *testing.T) {
64 tf := make(tempFiles, 0)
67 filename := tf.makeFile("key=value")
68 filename2 := tf.makeFile("key2=value2")
69 p := MustLoadFiles([]string{filename, filename2}, ISO_8859_1, false)
70 assertKeyValues(t, "", p, "key", "value", "key2", "value2")
73 func TestLoadExpandedFile(t *testing.T) {
74 tf := make(tempFiles, 0)
77 if err := os.Setenv("_VARX", "some-value"); err != nil {
80 filename := tf.makeFilePrefix(os.Getenv("_VARX"), "key=value")
81 filename = strings.Replace(filename, os.Getenv("_VARX"), "${_VARX}", -1)
82 p := MustLoadFile(filename, ISO_8859_1)
83 assertKeyValues(t, "", p, "key", "value")
86 func TestLoadFilesAndIgnoreMissing(t *testing.T) {
87 tf := make(tempFiles, 0)
90 filename := tf.makeFile("key=value")
91 filename2 := tf.makeFile("key2=value2")
92 p := MustLoadFiles([]string{filename, filename + "foo", filename2, filename2 + "foo"}, ISO_8859_1, true)
93 assertKeyValues(t, "", p, "key", "value", "key2", "value2")
96 func TestLoadURL(t *testing.T) {
99 p := MustLoadURL(srv.URL + "/a")
100 assertKeyValues(t, "", p, "key", "value")
103 func TestLoadURLs(t *testing.T) {
106 p := MustLoadURLs([]string{srv.URL + "/a", srv.URL + "/b"}, false)
107 assertKeyValues(t, "", p, "key", "value", "key2", "value2")
110 func TestLoadURLsAndFailMissing(t *testing.T) {
113 p, err := LoadURLs([]string{srv.URL + "/a", srv.URL + "/c"}, false)
114 assert.Equal(t, p, (*Properties)(nil))
115 assert.Matches(t, err.Error(), ".*returned 404.*")
118 func TestLoadURLsAndIgnoreMissing(t *testing.T) {
121 p := MustLoadURLs([]string{srv.URL + "/a", srv.URL + "/b", srv.URL + "/c"}, true)
122 assertKeyValues(t, "", p, "key", "value", "key2", "value2")
125 func TestLoadURLEncoding(t *testing.T) {
129 uris := []string{"/none", "/utf8", "/plain", "/latin1", "/iso88591"}
130 for i, uri := range uris {
131 p := MustLoadURL(srv.URL + uri)
132 assert.Equal(t, p.GetString("key", ""), "äöü", fmt.Sprintf("%d", i))
136 func TestLoadURLFailInvalidEncoding(t *testing.T) {
140 p, err := LoadURL(srv.URL + "/json")
141 assert.Equal(t, p, (*Properties)(nil))
142 assert.Matches(t, err.Error(), ".*invalid content type.*")
145 func TestLoadAll(t *testing.T) {
146 tf := make(tempFiles, 0)
149 filename := tf.makeFile("key=value")
150 filename2 := tf.makeFile("key2=value3")
151 filename3 := tf.makeFile("key=value4")
154 p := MustLoadAll([]string{filename, filename2, srv.URL + "/a", srv.URL + "/b", filename3}, UTF8, false)
155 assertKeyValues(t, "", p, "key", "value4", "key2", "value2")
158 type tempFiles []string
160 func (tf *tempFiles) removeAll() {
161 for _, path := range *tf {
162 err := os.Remove(path)
164 fmt.Printf("os.Remove: %v", err)
169 func (tf *tempFiles) makeFile(data string) string {
170 return tf.makeFilePrefix("properties", data)
173 func (tf *tempFiles) makeFilePrefix(prefix, data string) string {
174 f, err := ioutil.TempFile("", prefix)
176 panic("ioutil.TempFile: " + err.Error())
179 // remember the temp file so that we can remove it later
180 *tf = append(*tf, f.Name())
182 n, err := fmt.Fprint(f, data)
184 panic("fmt.Fprintln: " + err.Error())
187 panic(fmt.Sprintf("Data size mismatch. expected=%d wrote=%d\n", len(data), n))
192 panic("f.Close: " + err.Error())
198 func testServer() *httptest.Server {
199 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
200 send := func(data []byte, contentType string) {
201 w.Header().Set("Content-Type", contentType)
202 if _, err := w.Write(data); err != nil {
207 utf8 := []byte("key=äöü")
208 iso88591 := []byte{0x6b, 0x65, 0x79, 0x3d, 0xe4, 0xf6, 0xfc} // key=äöü
210 switch r.RequestURI {
212 send([]byte("key=value"), "")
214 send([]byte("key2=value2"), "")
218 send(utf8, "text/plain; charset=utf-8")
220 send(utf8, "application/json; charset=utf-8")
222 send(iso88591, "text/plain")
224 send(iso88591, "text/plain; charset=latin1")
226 send(iso88591, "text/plain; charset=iso-8859-1")