Skip to content

Commit 576a086

Browse files
DATAMONGO-1288 - Update.inc(String, Number) method fails to work with AtomicInteger.
Prepare issue branch. - Alter version for issue branch build. - Include DATAMONGO-1302 (PR#330) for adding ConverterFactory to CustomConversions.
1 parent 38fc764 commit 576a086

File tree

7 files changed

+64
-13
lines changed

7 files changed

+64
-13
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>org.springframework.data</groupId>
77
<artifactId>spring-data-mongodb-parent</artifactId>
8-
<version>1.9.0.BUILD-SNAPSHOT</version>
8+
<version>1.9.0.DATAMONGO-1288-SNAPSHOT</version>
99
<packaging>pom</packaging>
1010

1111
<name>Spring Data MongoDB</name>

spring-data-mongodb-cross-store/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.data</groupId>
88
<artifactId>spring-data-mongodb-parent</artifactId>
9-
<version>1.9.0.BUILD-SNAPSHOT</version>
9+
<version>1.9.0.DATAMONGO-1288-SNAPSHOT</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

@@ -48,7 +48,7 @@
4848
<dependency>
4949
<groupId>org.springframework.data</groupId>
5050
<artifactId>spring-data-mongodb</artifactId>
51-
<version>1.9.0.BUILD-SNAPSHOT</version>
51+
<version>1.9.0.DATAMONGO-1288-SNAPSHOT</version>
5252
</dependency>
5353

5454
<dependency>

spring-data-mongodb-distribution/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<parent>
1414
<groupId>org.springframework.data</groupId>
1515
<artifactId>spring-data-mongodb-parent</artifactId>
16-
<version>1.9.0.BUILD-SNAPSHOT</version>
16+
<version>1.9.0.DATAMONGO-1288-SNAPSHOT</version>
1717
<relativePath>../pom.xml</relativePath>
1818
</parent>
1919

spring-data-mongodb-log4j/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.data</groupId>
77
<artifactId>spring-data-mongodb-parent</artifactId>
8-
<version>1.9.0.BUILD-SNAPSHOT</version>
8+
<version>1.9.0.DATAMONGO-1288-SNAPSHOT</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

spring-data-mongodb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>org.springframework.data</groupId>
1313
<artifactId>spring-data-mongodb-parent</artifactId>
14-
<version>1.9.0.BUILD-SNAPSHOT</version>
14+
<version>1.9.0.DATAMONGO-1288-SNAPSHOT</version>
1515
<relativePath>../pom.xml</relativePath>
1616
</parent>
1717

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
import org.springframework.data.mapping.model.SimpleTypeHolder;
4545
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
4646
import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToStringConverter;
47-
import org.springframework.data.mongodb.core.convert.MongoConverters.NamedMongoScriptToDBObjectConverter;
4847
import org.springframework.data.mongodb.core.convert.MongoConverters.DBObjectToNamedMongoScriptCoverter;
4948
import org.springframework.data.mongodb.core.convert.MongoConverters.DBObjectToStringConverter;
49+
import org.springframework.data.mongodb.core.convert.MongoConverters.NamedMongoScriptToDBObjectConverter;
5050
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
5151
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter;
5252
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToURLConverter;
@@ -192,8 +192,8 @@ public void registerConvertersIn(GenericConversionService conversionService) {
192192
}
193193

194194
/**
195-
* Registers a conversion for the given converter. Inspects either generics or the {@link ConvertiblePair}s returned
196-
* by a {@link GenericConverter}.
195+
* Registers a conversion for the given converter. Inspects either generics of {@link Converter} and
196+
* {@link ConverterFactory} or the {@link ConvertiblePair}s returned by a {@link GenericConverter}.
197197
*
198198
* @param converter
199199
*/
@@ -208,6 +208,10 @@ private void registerConversion(Object converter) {
208208
for (ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
209209
register(new ConverterRegistration(pair, isReading, isWriting));
210210
}
211+
} else if (converter instanceof ConverterFactory) {
212+
213+
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), ConverterFactory.class);
214+
register(new ConverterRegistration(arguments[0], arguments[1], isReading, isWriting));
211215
} else if (converter instanceof Converter) {
212216
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
213217
register(new ConverterRegistration(arguments[0], arguments[1], isReading, isWriting));

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/CustomConversionsUnitTests.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,9 @@
2121
import java.net.URL;
2222
import java.text.DateFormat;
2323
import java.text.Format;
24+
import java.text.SimpleDateFormat;
2425
import java.util.Arrays;
26+
import java.util.Collections;
2527
import java.util.Date;
2628
import java.util.Locale;
2729
import java.util.UUID;
@@ -32,8 +34,10 @@
3234
import org.junit.Test;
3335
import org.springframework.aop.framework.ProxyFactory;
3436
import org.springframework.core.convert.converter.Converter;
37+
import org.springframework.core.convert.converter.ConverterFactory;
3538
import org.springframework.core.convert.support.DefaultConversionService;
3639
import org.springframework.core.convert.support.GenericConversionService;
40+
import org.springframework.data.convert.WritingConverter;
3741
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter;
3842
import org.threeten.bp.LocalDateTime;
3943

@@ -43,12 +47,11 @@
4347
* Unit tests for {@link CustomConversions}.
4448
*
4549
* @author Oliver Gierke
46-
* @auhtor Christoph Strobl
50+
* @author Christoph Strobl
4751
*/
4852
public class CustomConversionsUnitTests {
4953

5054
@Test
51-
@SuppressWarnings("unchecked")
5255
public void findsBasicReadAndWriteConversions() {
5356

5457
CustomConversions conversions = new CustomConversions(Arrays.asList(FormatToStringConverter.INSTANCE,
@@ -62,7 +65,6 @@ public void findsBasicReadAndWriteConversions() {
6265
}
6366

6467
@Test
65-
@SuppressWarnings("unchecked")
6668
public void considersSubtypesCorrectly() {
6769

6870
CustomConversions conversions = new CustomConversions(Arrays.asList(NumberToStringConverter.INSTANCE,
@@ -132,6 +134,7 @@ public void populatesConversionServiceCorrectly() {
132134
*/
133135
@Test
134136
public void doesNotConsiderTypeSimpleIfOnlyReadConverterIsRegistered() {
137+
135138
CustomConversions conversions = new CustomConversions(Arrays.asList(StringToFormatConverter.INSTANCE));
136139
assertThat(conversions.isSimpleType(Format.class), is(false));
137140
}
@@ -257,6 +260,17 @@ public void registersConvertersForThreeTenBackPort() {
257260
assertThat(customConversions.hasCustomWriteTarget(LocalDateTime.class), is(true));
258261
}
259262

263+
/**
264+
* @see DATAMONGO-1302
265+
*/
266+
@Test
267+
public void registersConverterFactoryCorrectly() {
268+
269+
CustomConversions customConversions = new CustomConversions(Collections.singletonList(new FormatConverterFactory()));
270+
271+
assertThat(customConversions.getCustomWriteTarget(String.class, SimpleDateFormat.class), notNullValue());
272+
}
273+
260274
private static Class<?> createProxyTypeFor(Class<?> type) {
261275

262276
ProxyFactory factory = new ProxyFactory();
@@ -331,4 +345,37 @@ public String convert(Object source) {
331345
}
332346

333347
}
348+
349+
@WritingConverter
350+
static class FormatConverterFactory implements ConverterFactory<String, Format> {
351+
352+
@Override
353+
public <T extends Format> Converter<String, T> getConverter(Class<T> targetType) {
354+
return new StringToFormat<T>(targetType);
355+
}
356+
357+
private static final class StringToFormat<T extends Format> implements Converter<String, T> {
358+
359+
private final Class<T> targetType;
360+
361+
public StringToFormat(Class<T> targetType) {
362+
this.targetType = targetType;
363+
}
364+
365+
@Override
366+
public T convert(String source) {
367+
368+
if (source.length() == 0) {
369+
return null;
370+
}
371+
372+
try {
373+
return targetType.newInstance();
374+
} catch (Exception e) {
375+
throw new IllegalArgumentException(e.getMessage(), e);
376+
}
377+
}
378+
}
379+
380+
}
334381
}

0 commit comments

Comments
 (0)