Skip to content

Commit e4b9be6

Browse files
committed
DATAMONGO-408 - Added StringToWriteConverter for XML setup convenience.
When using a PropertyPlaceHolderConfigurer to set WriteConcerns on a MongoFactoryBean just like this: <bean class="….mongodb.core.MongoFactoryBean"> <property name="writeConcern" value="${mongodb.writeConcern}"/> </bean> we might create invalid WriteConcerns as the BeanFactory will use the WriteConcern's constructor taking a String to create the instance by default. To make Spring use the valueOf(…) method in advance one needs to register either our already existing WriteConcernPropertyEditor or the newly introduced StringToWriteConcernConverter in Springs ConversionService.
1 parent 09a8b9c commit e4b9be6

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
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+
package org.springframework.data.mongodb.config;
17+
18+
import org.springframework.core.convert.converter.Converter;
19+
20+
import com.mongodb.WriteConcern;
21+
22+
/**
23+
* Converter to create {@link WriteConcern} instances from String representations.
24+
*
25+
* @author Oliver Gierke
26+
*/
27+
public class StringToWriteConcernConverter implements Converter<String, WriteConcern> {
28+
29+
/*
30+
* (non-Javadoc)
31+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
32+
*/
33+
public WriteConcern convert(String source) {
34+
35+
WriteConcern writeConcern = WriteConcern.valueOf(source);
36+
return writeConcern != null ? writeConcern : new WriteConcern(source);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
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+
package org.springframework.data.mongodb.config;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
23+
import com.mongodb.WriteConcern;
24+
25+
/**
26+
* Unit tests for {@link StringToWriteConcernConverter}.
27+
*
28+
* @author Oliver Gierke
29+
*/
30+
public class StringToWriteConcernConverterUnitTest {
31+
32+
StringToWriteConcernConverter converter = new StringToWriteConcernConverter();
33+
34+
@Test
35+
public void createsWellKnownConstantsCorrectly() {
36+
assertThat(converter.convert("SAFE"), is(WriteConcern.SAFE));
37+
}
38+
39+
@Test
40+
public void createsWriteConcernForUnknownValue() {
41+
assertThat(converter.convert("-1"), is(new WriteConcern("-1")));
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
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+
package org.springframework.data.mongodb.config;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import com.mongodb.WriteConcern;
25+
26+
/**
27+
* Unit tests for {@link WriteConcernPropertyEditor}.
28+
*
29+
* @author Oliver Gierke
30+
*/
31+
public class WriteConcernPropertyEditorUnitTests {
32+
33+
WriteConcernPropertyEditor editor;
34+
35+
@Before
36+
public void setUp() {
37+
editor = new WriteConcernPropertyEditor();
38+
}
39+
40+
@Test
41+
public void createsWriteConcernForWellKnownConstants() {
42+
43+
editor.setAsText("SAFE");
44+
assertThat(editor.getValue(), is((Object) WriteConcern.SAFE));
45+
}
46+
47+
@Test
48+
public void createsWriteConcernForUnknownConstants() {
49+
50+
editor.setAsText("-1");
51+
assertThat(editor.getValue(), is((Object) new WriteConcern("-1")));
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
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+
package org.springframework.data.mongodb.core;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
23+
import org.springframework.beans.factory.support.RootBeanDefinition;
24+
import org.springframework.data.mongodb.config.WriteConcernPropertyEditor;
25+
import org.springframework.test.util.ReflectionTestUtils;
26+
27+
import com.mongodb.WriteConcern;
28+
29+
/**
30+
* Integration tests for {@link MongoFactoryBean}.
31+
*
32+
* @author Oliver Gierke
33+
*/
34+
public class MongoFactoryBeanIntegrationTest {
35+
36+
/**
37+
* @see DATAMONGO-408
38+
*/
39+
@Test
40+
public void convertsWriteConcernCorrectly() {
41+
42+
RootBeanDefinition definition = new RootBeanDefinition(MongoFactoryBean.class);
43+
definition.getPropertyValues().addPropertyValue("writeConcern", "SAFE");
44+
45+
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
46+
factory.registerCustomEditor(WriteConcern.class, WriteConcernPropertyEditor.class);
47+
factory.registerBeanDefinition("factory", definition);
48+
49+
MongoFactoryBean bean = factory.getBean("&factory", MongoFactoryBean.class);
50+
assertThat(ReflectionTestUtils.getField(bean, "writeConcern"), is((Object) WriteConcern.SAFE));
51+
}
52+
}

0 commit comments

Comments
 (0)