|  | 
| 43 | 43 | import org.bson.codecs.pojo.entities.SimpleNestedPojoModel; | 
| 44 | 44 | import org.bson.codecs.pojo.entities.UpperBoundsModel; | 
| 45 | 45 | import org.bson.codecs.pojo.entities.conventions.AnnotationModel; | 
|  | 46 | +import org.bson.codecs.pojo.entities.conventions.CollectionsGetterNonEmptyModel; | 
|  | 47 | +import org.bson.codecs.pojo.entities.conventions.CollectionsGetterNullModel; | 
|  | 48 | +import org.bson.codecs.pojo.entities.conventions.CollectionsGetterImmutableModel; | 
|  | 49 | +import org.bson.codecs.pojo.entities.conventions.CollectionsGetterMutableModel; | 
| 46 | 50 | import org.bson.codecs.pojo.entities.conventions.CreatorConstructorPrimitivesModel; | 
| 47 | 51 | import org.bson.codecs.pojo.entities.conventions.CreatorConstructorThrowsExceptionModel; | 
| 48 | 52 | import org.bson.codecs.pojo.entities.conventions.CreatorMethodThrowsExceptionModel; | 
|  | 53 | +import org.bson.codecs.pojo.entities.conventions.MapGetterNonEmptyModel; | 
|  | 54 | +import org.bson.codecs.pojo.entities.conventions.MapGetterNullModel; | 
|  | 55 | +import org.bson.codecs.pojo.entities.conventions.MapGetterImmutableModel; | 
|  | 56 | +import org.bson.codecs.pojo.entities.conventions.MapGetterMutableModel; | 
| 49 | 57 | import org.bson.types.ObjectId; | 
| 50 | 58 | import org.junit.Test; | 
| 51 | 59 | 
 | 
|  | 
| 63 | 71 | import static org.bson.codecs.pojo.Conventions.DEFAULT_CONVENTIONS; | 
| 64 | 72 | import static org.bson.codecs.pojo.Conventions.NO_CONVENTIONS; | 
| 65 | 73 | import static org.bson.codecs.pojo.Conventions.SET_PRIVATE_FIELDS_CONVENTION; | 
|  | 74 | +import static org.bson.codecs.pojo.Conventions.USE_GETTERS_FOR_SETTERS; | 
| 66 | 75 | 
 | 
| 67 | 76 | public final class PojoCustomTest extends PojoTestCase { | 
| 68 | 77 | 
 | 
| @@ -168,6 +177,79 @@ public void testSetPrivateFieldConvention() { | 
| 168 | 177 |                 "{'integerField': 1, 'stringField': '2', listField: ['a', 'b']}"); | 
| 169 | 178 |     } | 
| 170 | 179 | 
 | 
|  | 180 | +    @Test | 
|  | 181 | +    public void testUseGettersForSettersConvention() { | 
|  | 182 | +        PojoCodecProvider.Builder builder = getPojoCodecProviderBuilder(CollectionsGetterMutableModel.class, MapGetterMutableModel.class) | 
|  | 183 | +                .conventions(getDefaultAndUseGettersConvention()); | 
|  | 184 | + | 
|  | 185 | +        roundTrip(builder, new CollectionsGetterMutableModel(asList(1, 2)), "{listField: [1, 2]}"); | 
|  | 186 | +        roundTrip(builder, new MapGetterMutableModel(Collections.singletonMap("a", 3)), "{mapField: {a: 3}}"); | 
|  | 187 | +    } | 
|  | 188 | + | 
|  | 189 | +    @Test(expected = CodecConfigurationException.class) | 
|  | 190 | +    public void testUseGettersForSettersConventionInvalidTypeForCollection() { | 
|  | 191 | +        PojoCodecProvider.Builder builder = getPojoCodecProviderBuilder(CollectionsGetterMutableModel.class) | 
|  | 192 | +                .conventions(getDefaultAndUseGettersConvention()); | 
|  | 193 | + | 
|  | 194 | +        decodingShouldFail(getCodec(builder, CollectionsGetterMutableModel.class), "{listField: ['1', '2']}"); | 
|  | 195 | +    } | 
|  | 196 | + | 
|  | 197 | +    @Test(expected = CodecConfigurationException.class) | 
|  | 198 | +    public void testUseGettersForSettersConventionInvalidTypeForMap() { | 
|  | 199 | +        PojoCodecProvider.Builder builder = getPojoCodecProviderBuilder(MapGetterMutableModel.class) | 
|  | 200 | +                .conventions(getDefaultAndUseGettersConvention()); | 
|  | 201 | + | 
|  | 202 | +        decodingShouldFail(getCodec(builder, MapGetterMutableModel.class), "{mapField: {a: '1'}}"); | 
|  | 203 | +    } | 
|  | 204 | + | 
|  | 205 | +    @Test(expected = CodecConfigurationException.class) | 
|  | 206 | +    public void testUseGettersForSettersConventionImmutableCollection() { | 
|  | 207 | +        PojoCodecProvider.Builder builder = getPojoCodecProviderBuilder(CollectionsGetterImmutableModel.class) | 
|  | 208 | +                .conventions(getDefaultAndUseGettersConvention()); | 
|  | 209 | + | 
|  | 210 | +        roundTrip(builder, new CollectionsGetterImmutableModel(asList(1, 2)), "{listField: [1, 2]}"); | 
|  | 211 | +    } | 
|  | 212 | + | 
|  | 213 | +    @Test(expected = CodecConfigurationException.class) | 
|  | 214 | +    public void testUseGettersForSettersConventionImmutableMap() { | 
|  | 215 | +        PojoCodecProvider.Builder builder = getPojoCodecProviderBuilder(MapGetterImmutableModel.class) | 
|  | 216 | +                .conventions(getDefaultAndUseGettersConvention()); | 
|  | 217 | + | 
|  | 218 | +        roundTrip(builder, new MapGetterImmutableModel(Collections.singletonMap("a", 3)), "{mapField: {a: 3}}"); | 
|  | 219 | +    } | 
|  | 220 | + | 
|  | 221 | +    @Test(expected = CodecConfigurationException.class) | 
|  | 222 | +    public void testUseGettersForSettersConventionNullCollection() { | 
|  | 223 | +        PojoCodecProvider.Builder builder = getPojoCodecProviderBuilder(CollectionsGetterNullModel.class) | 
|  | 224 | +                .conventions(getDefaultAndUseGettersConvention()); | 
|  | 225 | + | 
|  | 226 | +        roundTrip(builder, new CollectionsGetterNullModel(asList(1, 2)), "{listField: [1, 2]}"); | 
|  | 227 | +    } | 
|  | 228 | + | 
|  | 229 | +    @Test(expected = CodecConfigurationException.class) | 
|  | 230 | +    public void testUseGettersForSettersConventionNullMap() { | 
|  | 231 | +        PojoCodecProvider.Builder builder = getPojoCodecProviderBuilder(MapGetterNullModel.class) | 
|  | 232 | +                .conventions(getDefaultAndUseGettersConvention()); | 
|  | 233 | + | 
|  | 234 | +        roundTrip(builder, new MapGetterNullModel(Collections.singletonMap("a", 3)), "{mapField: {a: 3}}"); | 
|  | 235 | +    } | 
|  | 236 | + | 
|  | 237 | +    @Test(expected = CodecConfigurationException.class) | 
|  | 238 | +    public void testUseGettersForSettersConventionNotEmptyCollection() { | 
|  | 239 | +        PojoCodecProvider.Builder builder = getPojoCodecProviderBuilder(CollectionsGetterNonEmptyModel.class) | 
|  | 240 | +                .conventions(getDefaultAndUseGettersConvention()); | 
|  | 241 | + | 
|  | 242 | +        roundTrip(builder, new CollectionsGetterNonEmptyModel(asList(1, 2)), "{listField: [1, 2]}"); | 
|  | 243 | +    } | 
|  | 244 | + | 
|  | 245 | +    @Test(expected = CodecConfigurationException.class) | 
|  | 246 | +    public void testUseGettersForSettersConventionNotEmptyMap() { | 
|  | 247 | +        PojoCodecProvider.Builder builder = getPojoCodecProviderBuilder(MapGetterNonEmptyModel.class) | 
|  | 248 | +                .conventions(getDefaultAndUseGettersConvention()); | 
|  | 249 | + | 
|  | 250 | +        roundTrip(builder, new MapGetterNonEmptyModel(Collections.singletonMap("a", 3)), "{mapField: {a: 3}}"); | 
|  | 251 | +    } | 
|  | 252 | + | 
| 171 | 253 |     @Test | 
| 172 | 254 |     public void testEnumSupport() { | 
| 173 | 255 |         roundTrip(getPojoCodecProviderBuilder(SimpleEnumModel.class), new SimpleEnumModel(SimpleEnum.BRAVO), "{ 'myEnum': 'BRAVO' }"); | 
| @@ -402,4 +484,11 @@ public void testInvalidGetterAndSetterModelEncoding() { | 
| 402 | 484 |     public void testInvalidGetterAndSetterModelDecoding() { | 
| 403 | 485 |         decodingShouldFail(getCodec(InvalidGetterAndSetterModel.class), "{'integerField': 42, 'stringField': 'myString'}"); | 
| 404 | 486 |     } | 
|  | 487 | + | 
|  | 488 | +    private List<Convention> getDefaultAndUseGettersConvention() { | 
|  | 489 | +        List<Convention> conventions = new ArrayList<Convention>(DEFAULT_CONVENTIONS); | 
|  | 490 | +        conventions.add(USE_GETTERS_FOR_SETTERS); | 
|  | 491 | +        return conventions; | 
|  | 492 | +    } | 
|  | 493 | + | 
| 405 | 494 | } | 
0 commit comments