-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetattributes_test.go
126 lines (104 loc) · 3.71 KB
/
getattributes_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package gowrapmx4j
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestAttributesFromioReadCloser(t *testing.T) {
input := `<?xml version="1.0" encoding="UTF-8"?>
<MBean classname="com.yammer.metrics.reporting.JmxReporter$Timer" description="Information on the management interface of the MBean" objectname="org.apache.cassandra.metrics:type=ColumnFamily,keyspace=yourkeyspace,scope=node,name=ReadLatency">
<Attribute classname="double" isnull="false" name="Max" value="100.0"/>
</MBean>
`
f, err := ioutil.TempFile("/tmp", "gowrapmx4jtest-")
defer f.Close()
if err != nil {
t.Errorf("Failed to open temp file in /tmp/: %#v\n", err)
}
f.WriteString(input)
_, err = f.Seek(0, 0)
if err != nil {
t.Errorf("Error setting seek on tmp file: %#v", err)
}
defer os.Remove(f.Name())
mbean, err := getBeans(f, beanUnmarshal)
if err != nil {
t.Errorf("Error reading tmp file in getAttributes: %#v\n", err)
}
attr0 := mbean.Attributes[0]
if attr0.Name != "Max" {
t.Errorf("Attribute 'Name' was not unmarshalled correctly")
}
}
func TestBasicUnmarshal(t *testing.T) {
//<?xml version="1.0" encoding="UTF-8"?>
input := `
<?xml version="1.0" encoding="UTF-8"?>
<MBean classname="com.yammer.metrics.reporting.JmxReporter$Timer" description="Information on the management interface of the MBean" objectname="org.apache.cassandra.metrics:type=ColumnFamily,keyspace=yourkeyspace,scope=node,name=ReadLatency">
<Attribute classname="double" isnull="false" name="Max" value="100.0"/>
</MBean>
`
x := Bean{ObjectName: "neh"}
err := xml.Unmarshal([]byte(input), &x)
if err != nil {
t.Errorf("Error unmarshalling xml: %#v", err)
}
if x.ObjectName == "neh" {
fmt.Printf("%#v\n", x)
t.Errorf("Incorrect default value not overwritten.")
}
if x.Attributes[0].Name != "Max" {
t.Errorf("Attribute 'Name' was not unmarshalled correctly")
}
}
func TestUnmarshalFunction(t *testing.T) {
input := `
<?xml version="1.0" encoding="UTF-8"?>
<MBean classname="com.yammer.metrics.reporting.JmxReporter$Timer" description="Information on the management interface of the MBean" objectname="org.apache.cassandra.metrics:type=ColumnFamily,keyspace=yourkeyspace,scope=node,name=ReadLatency">
<Attribute classname="double" isnull="false" name="Max" value="100.0"/>
</MBean>
`
x, err := beanUnmarshal([]byte(input))
if err != nil {
t.Errorf("Error running GetAttrUnmarshal: %v\n", err)
}
if x.ObjectName == "neh" {
fmt.Printf("%#v\n", x)
t.Errorf("Incorrect default value not overwritten.")
}
if x.Attributes[0].Name != "Max" {
t.Errorf("Attribute 'Name' was not unmarshalled correctly")
}
}
func TestUnmarshalFunctionMap(t *testing.T) {
input := `<MBean classname="org.apache.cassandra.gms.FailureDetector" description="Information on the management interface of the MBean" objectname="org.apache.cassandra.net:type=FailureDetector">
<Attribute classname="java.util.Map" isnull="false" name="SimpleStates">
<Map length="1">
<Element element="UP" elementclass="java.lang.String" index="0" key="/127.0.0.1" keyclass="java.lang.String"/>
</Map>
</Attribute>
</MBean>`
x, err := beanUnmarshal([]byte(input))
if err != nil {
t.Errorf("Error running GetAttrUnmarshal: %v\n", err)
}
if x.ObjectName != "org.apache.cassandra.net:type=FailureDetector" {
fmt.Printf("%#v\n", x)
t.Errorf("Parsing failure of objectname")
}
if x.Attributes[0].Name != "SimpleStates" {
t.Errorf("Attribute 'Name' was not unmarshalled correctly")
}
if x.Attributes[0].Map.Length != "1" {
t.Errorf("Map Lenght incorrect: %s\n", x.Attributes[0].Map.Length)
}
e0 := x.Attributes[0].Map.Elements[0]
if e0.Element != "UP" {
t.Errorf("Value 'element' not 'UP': %#v", e0)
}
if e0.Key != "/127.0.0.1" {
t.Errorf("Key of map incorrect")
}
}