1
|
public class PrivateSetConventionTest {
|
2
|
|
3
|
public static class Pojo {
|
4
|
protected String protectedFoo;
|
5
|
private String privateFoo;
|
6
|
String ppFoo;
|
7
|
public String getProtectedFoo() {
|
8
|
return protectedFoo;
|
9
|
}
|
10
|
public String getPrivateFoo() {
|
11
|
return privateFoo;
|
12
|
} @Override
|
13
|
public boolean equals( Object o ) {
|
14
|
if ( this == o ) return true;
|
15
|
if ( !( o instanceof Pojo ) ) return false;
|
16
|
Pojo pojo = ( Pojo ) o;
|
17
|
return Objects.equals( protectedFoo, pojo.protectedFoo ) &&
|
18
|
Objects.equals( privateFoo, pojo.privateFoo ) &&
|
19
|
Objects.equals( ppFoo, pojo.ppFoo );
|
20
|
} @Override
|
21
|
public int hashCode() {
|
22
|
return Objects.hash( protectedFoo, privateFoo, ppFoo );
|
23
|
} @Override
|
24
|
public String toString() {
|
25
|
return "Pojo{" + "protectedFoo='" + protectedFoo + '\'' + ", privateFoo='" + privateFoo + '\'' +
|
26
|
", ppFoo='" + ppFoo + '\'' + '}';
|
27
|
}
|
28
|
} private CodecRegistry codecRegistry; @BeforeClass
|
29
|
public void setup() {
|
30
|
List<Convention> conventions = Arrays.asList( Conventions.SET_PRIVATE_FIELDS_CONVENTION );
|
31
|
CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).conventions( conventions ).build();
|
32
|
codecRegistry = CodecRegistries.fromRegistries(
|
33
|
MongoClientSettings.getDefaultCodecRegistry(),
|
34
|
CodecRegistries.fromProviders( pojoCodecProvider )
|
35
|
);
|
36
|
} @Test
|
37
|
public void testDecode() {
|
38
|
Pojo expected = new Pojo();
|
39
|
expected.protectedFoo = "Test 1";
|
40
|
expected.privateFoo = "Test 2";
|
41
|
expected.ppFoo = "Test 3";
|
42
|
BsonDocument rawBson = new BsonDocument().append( "protectedFoo", new BsonString( expected.protectedFoo ) )
|
43
|
.append( "privateFoo", new BsonString( expected.privateFoo ) )
|
44
|
.append( "ppFoo", new BsonString( expected.ppFoo ) );
|
45
|
Codec<Pojo> pojoCodec = codecRegistry.get( Pojo.class );
|
46
|
BsonDocumentReader reader = new BsonDocumentReader( rawBson );
|
47
|
Pojo actual = pojoCodec.decode( reader, DecoderContext.builder().build() );
|
48
|
Assert.assertEquals( actual, expected );
|
49
|
}
|
50
|
}
|