Skip to content

Commit e42db36

Browse files
adds test about single discriminator serialization
1 parent c09e74c commit e42db36

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.codecs.pojo;
18+
19+
import org.bson.codecs.pojo.entities.DiscriminatorModel;
20+
import org.bson.codecs.pojo.entities.DiscriminatorWithGetterModel;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
24+
25+
public final class PojoCodecDiscriminatorTest extends PojoTestCase {
26+
27+
@Test
28+
public void testDiscriminatorEncodedOnceWhenItIsAlsoAGetter() {
29+
byte[] encodedDiscriminatorWithoutGetter = encode(
30+
getCodec(DiscriminatorModel.class),
31+
new DiscriminatorModel(),
32+
false
33+
).toByteArray();
34+
byte[] encodedDiscriminatorWithGetter = encode(
35+
getCodec(DiscriminatorWithGetterModel.class),
36+
new DiscriminatorWithGetterModel(),
37+
false
38+
).toByteArray();
39+
assertArrayEquals(encodedDiscriminatorWithoutGetter, encodedDiscriminatorWithGetter);
40+
}
41+
42+
@Test
43+
public void testDiscriminatorEncodingWhenItIsAlsoAGetter() {
44+
encodesTo(
45+
getCodec(DiscriminatorWithGetterModel.class),
46+
new DiscriminatorWithGetterModel(),
47+
"{discriminatorKey:'discriminatorValue'}"
48+
);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.codecs.pojo.entities;
18+
19+
import org.bson.codecs.pojo.annotations.BsonDiscriminator;
20+
21+
@BsonDiscriminator(key = "discriminatorKey", value = "discriminatorValue")
22+
public class DiscriminatorModel {
23+
24+
public DiscriminatorModel() {
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "DiscriminatorWithoutGetterModel{}";
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.codecs.pojo.entities;
18+
19+
import org.bson.codecs.pojo.annotations.BsonDiscriminator;
20+
21+
@BsonDiscriminator(key = "discriminatorKey", value = "discriminatorValue")
22+
public class DiscriminatorWithGetterModel {
23+
24+
public DiscriminatorWithGetterModel() {
25+
}
26+
27+
public String getDiscriminatorKey() {
28+
return "discriminatorValue";
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "DiscriminatorWithGetterModel{}";
34+
}
35+
}

0 commit comments

Comments
 (0)