forked from clbanning/mxj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_test.go
107 lines (87 loc) · 2.41 KB
/
bulk_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// bulk_test.go - uses Handler and Writer functions to process some streams as a demo.
package mxj
import (
"bytes"
"fmt"
"testing"
)
func TestBulkHeader(t *testing.T) {
fmt.Println("\n---------------- bulk_test.go ...")
}
var jsonWriter = new(bytes.Buffer)
var xmlWriter = new(bytes.Buffer)
var jsonErrLog = new(bytes.Buffer)
var xmlErrLog = new(bytes.Buffer)
func TestXmlReader(t *testing.T) {
// create Reader for xmldata
xmlReader := bytes.NewReader(xmldata)
// read XML from Readerand pass Map value with the raw XML to handler
err := HandleXmlReader(xmlReader, bxmaphandler, bxerrhandler)
if err != nil {
t.Fatal("err:", err.Error())
}
// get the JSON
j := make([]byte, jsonWriter.Len())
_, _ = jsonWriter.Read(j)
// get the errors
e := make([]byte, xmlErrLog.Len())
_, _ = xmlErrLog.Read(e)
// print the input
fmt.Println("XmlReader, xmldata:\n", string(xmldata))
// print the result
fmt.Println("XmlReader, result :\n", string(j))
// print the errors
fmt.Println("XmlReader, errors :\n", string(e))
}
func bxmaphandler(m Map) bool {
j, err := m.JsonIndent("", " ", true)
if err != nil {
return false
}
_, _ = jsonWriter.Write(j)
// put in a NL to pretty up printing the Writer
_, _ = jsonWriter.Write([]byte("\n"))
return true
}
func bxerrhandler(err error) bool {
// write errors to file
_, _ = xmlErrLog.Write([]byte(err.Error()))
_, _ = xmlErrLog.Write([]byte("\n")) // pretty up
return true
}
func TestJsonReader(t *testing.T) {
jsonReader := bytes.NewReader(jsondata)
// read all the JSON
err := HandleJsonReader(jsonReader, bjmaphandler, bjerrhandler)
if err != nil {
t.Fatal("err:", err.Error())
}
// get the XML
x := make([]byte, xmlWriter.Len())
_, _ = xmlWriter.Read(x)
// get the errors
e := make([]byte, jsonErrLog.Len())
_, _ = jsonErrLog.Read(e)
// print the input
fmt.Println("JsonReader, jsondata:\n", string(jsondata))
// print the result
fmt.Println("JsonReader, result :\n", string(x))
// print the errors
fmt.Println("JsonReader, errors :\n", string(e))
}
func bjmaphandler(m Map) bool {
x, err := m.XmlIndent(" ", " ")
if err != nil {
return false
}
_, _ = xmlWriter.Write(x)
// put in a NL to pretty up printing the Writer
_, _ = xmlWriter.Write([]byte("\n"))
return true
}
func bjerrhandler(err error) bool {
// write errors to file
_, _ = jsonErrLog.Write([]byte(err.Error()))
_, _ = jsonErrLog.Write([]byte("\n")) // pretty up
return true
}