| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package amqp |
| 21 |
|
| 22 |
import ( |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/apache/qpid-proton/go/pkg/internal/test" |
| 27 |
) |
| 28 |
|
| 29 |
func TestSymbolKey(t *testing.T) { |
| 30 |
bytes, err := Marshal(AnnotationKeySymbol("foo"), nil) |
| 31 |
test.FatalIf(t, err) |
| 32 |
var k AnnotationKey |
| 33 |
_, err = Unmarshal(bytes, &k) |
| 34 |
test.ErrorIf(t, err) |
| 35 |
test.ErrorIf(t, test.Differ("foo", string(k.Get().(Symbol)))) |
| 36 |
var sym Symbol |
| 37 |
_, err = Unmarshal(bytes, &sym) |
| 38 |
test.ErrorIf(t, err) |
| 39 |
test.ErrorIf(t, test.Differ("foo", sym.String())) |
| 40 |
} |
| 41 |
|
| 42 |
func TestStringKey(t *testing.T) { |
| 43 |
bytes, err := Marshal(AnnotationKeyString("foo"), nil) |
| 44 |
test.FatalIf(t, err) |
| 45 |
var k AnnotationKey |
| 46 |
|
| 47 |
_, err = Unmarshal(bytes, &k) |
| 48 |
test.ErrorIf(t, err) |
| 49 |
test.ErrorIf(t, test.Differ("foo", string(k.Get().(Symbol)))) |
| 50 |
var s string |
| 51 |
_, err = Unmarshal(bytes, &s) |
| 52 |
test.ErrorIf(t, err) |
| 53 |
test.ErrorIf(t, test.Differ("foo", s)) |
| 54 |
} |
| 55 |
|
| 56 |
func TestIntKey(t *testing.T) { |
| 57 |
bytes, err := Marshal(AnnotationKeyUint64(12345), nil) |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
var k AnnotationKey |
| 62 |
if _, err := Unmarshal(bytes, &k); err != nil { |
| 63 |
t.Error(err) |
| 64 |
} |
| 65 |
if 12345 != k.Get().(uint64) { |
| 66 |
t.Errorf("%v != %v", 12345, k.Get().(uint64)) |
| 67 |
} |
| 68 |
var n uint64 |
| 69 |
if _, err := Unmarshal(bytes, &n); err != nil { |
| 70 |
t.Error(err) |
| 71 |
} |
| 72 |
if 12345 != n { |
| 73 |
t.Errorf("%v != %v", 12345, k.Get().(uint64)) |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
func TestMapToMap(t *testing.T) { |
| 79 |
in := Map{"k": "v", "x": "y", true: false, int8(3): uint64(24)} |
| 80 |
if bytes, err := Marshal(in, nil); err == nil { |
| 81 |
var out Map |
| 82 |
if _, err := Unmarshal(bytes, &out); err == nil { |
| 83 |
if err = test.Differ(in, out); err != nil { |
| 84 |
t.Error(err) |
| 85 |
} |
| 86 |
} else { |
| 87 |
t.Error(err) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestMapToInterface(t *testing.T) { |
| 93 |
in := Map{"k": "v", "x": "y", true: false, int8(3): uint64(24)} |
| 94 |
if bytes, err := Marshal(in, nil); err == nil { |
| 95 |
var out interface{} |
| 96 |
if _, err := Unmarshal(bytes, &out); err == nil { |
| 97 |
if err = test.Differ(in, out); err != nil { |
| 98 |
t.Error(err) |
| 99 |
} |
| 100 |
} else { |
| 101 |
t.Error(err) |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func TestAnyMap(t *testing.T) { |
| 107 |
// nil |
| 108 |
bytes, err := Marshal(AnyMap(nil), nil) |
| 109 |
if err != nil { |
| 110 |
t.Error(err) |
| 111 |
} |
| 112 |
var out AnyMap |
| 113 |
if _, err = Unmarshal(bytes, &out); err != nil { |
| 114 |
t.Error(err) |
| 115 |
} |
| 116 |
if err = test.Differ(AnyMap(nil), out); err != nil { |
| 117 |
t.Error(err) |
| 118 |
} |
| 119 |
|
| 120 |
// empty |
| 121 |
bytes, err = Marshal(AnyMap{}, nil) |
| 122 |
if err != nil { |
| 123 |
t.Error(err) |
| 124 |
} |
| 125 |
if _, err = Unmarshal(bytes, &out); err != nil { |
| 126 |
t.Error(err) |
| 127 |
} |
| 128 |
if err = test.Differ(AnyMap(nil), out); err != nil { |
| 129 |
t.Error(err) |
| 130 |
} |
| 131 |
|
| 132 |
// with data |
| 133 |
in := AnyMap{{"k", "v"}, {true, false}} |
| 134 |
bytes, err = Marshal(in, nil) |
| 135 |
if err != nil { |
| 136 |
t.Error(err) |
| 137 |
} |
| 138 |
if _, err = Unmarshal(bytes, &out); err != nil { |
| 139 |
t.Error(err) |
| 140 |
} |
| 141 |
if err = test.Differ(in, out); err != nil { |
| 142 |
t.Error(err) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func TestBadMap(t *testing.T) { |
| 147 |
// unmarshal map with invalid keys |
| 148 |
in := AnyMap{{"k", "v"}, {[]string{"x", "y"}, "invalid-key"}} |
| 149 |
bytes, err := Marshal(in, nil) |
| 150 |
if err != nil { |
| 151 |
t.Error(err) |
| 152 |
} |
| 153 |
m := Map{} |
| 154 |
// Should fail to unmarshal to a map |
| 155 |
if _, err = Unmarshal(bytes, &m); err != nil { |
| 156 |
if !strings.Contains(err.Error(), "key []string{\"x\", \"y\"} is not comparable") { |
| 157 |
t.Error(err) |
| 158 |
} |
| 159 |
} else { |
| 160 |
t.Error("expected error") |
| 161 |
} |
| 162 |
// Should unmarshal to an AnyMap |
| 163 |
var out AnyMap |
| 164 |
if _, err = Unmarshal(bytes, &out); err != nil { |
| 165 |
t.Error(err) |
| 166 |
} else if err = test.Differ(in, out); err != nil { |
| 167 |
t.Error(err) |
| 168 |
} |
| 169 |
// Should unmarshal to interface{} as AnyMap |
| 170 |
var v interface{} |
| 171 |
if _, err = Unmarshal(bytes, &v); err != nil { |
| 172 |
t.Error(err) |
| 173 |
} else if err = test.Differ(in, v); err != nil { |
| 174 |
t.Error(err) |
| 175 |
} |
| 176 |
// Round trip from interface to interface |
| 177 |
in = AnyMap{{[]int8{1, 2, 3}, "bad-key"}, {int16(1), "duplicate-1"}, {int16(1), "duplicate-2"}} |
| 178 |
bytes, err = Marshal(interface{}(in), nil) |
| 179 |
if err != nil { |
| 180 |
t.Error(err) |
| 181 |
} |
| 182 |
v = nil |
| 183 |
if _, err = Unmarshal(bytes, &v); err != nil { |
| 184 |
t.Error(err) |
| 185 |
} else if err = test.Differ(in, v); err != nil { |
| 186 |
t.Error(err) |
| 187 |
} |
| 188 |
} |