diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/StringToWriteConcernConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/StringToWriteConcernConverter.java new file mode 100644 index 000000000..b0fba90e2 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/StringToWriteConcernConverter.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.config; + +import org.springframework.core.convert.converter.Converter; + +import com.mongodb.WriteConcern; + +/** + * Converter to create {@link WriteConcern} instances from String representations. + * + * @author Oliver Gierke + */ +public class StringToWriteConcernConverter implements Converter { + + /* + * (non-Javadoc) + * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object) + */ + public WriteConcern convert(String source) { + + WriteConcern writeConcern = WriteConcern.valueOf(source); + return writeConcern != null ? writeConcern : new WriteConcern(source); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/StringToWriteConcernConverterUnitTest.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/StringToWriteConcernConverterUnitTest.java new file mode 100644 index 000000000..5e8d0fbcd --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/StringToWriteConcernConverterUnitTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.config; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.mongodb.WriteConcern; + +/** + * Unit tests for {@link StringToWriteConcernConverter}. + * + * @author Oliver Gierke + */ +public class StringToWriteConcernConverterUnitTest { + + StringToWriteConcernConverter converter = new StringToWriteConcernConverter(); + + @Test + public void createsWellKnownConstantsCorrectly() { + assertThat(converter.convert("SAFE"), is(WriteConcern.SAFE)); + } + + @Test + public void createsWriteConcernForUnknownValue() { + assertThat(converter.convert("-1"), is(new WriteConcern("-1"))); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/WriteConcernPropertyEditorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/WriteConcernPropertyEditorUnitTests.java new file mode 100644 index 000000000..e315bbf1f --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/WriteConcernPropertyEditorUnitTests.java @@ -0,0 +1,53 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.config; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.mongodb.WriteConcern; + +/** + * Unit tests for {@link WriteConcernPropertyEditor}. + * + * @author Oliver Gierke + */ +public class WriteConcernPropertyEditorUnitTests { + + WriteConcernPropertyEditor editor; + + @Before + public void setUp() { + editor = new WriteConcernPropertyEditor(); + } + + @Test + public void createsWriteConcernForWellKnownConstants() { + + editor.setAsText("SAFE"); + assertThat(editor.getValue(), is((Object) WriteConcern.SAFE)); + } + + @Test + public void createsWriteConcernForUnknownConstants() { + + editor.setAsText("-1"); + assertThat(editor.getValue(), is((Object) new WriteConcern("-1"))); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoFactoryBeanIntegrationTest.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoFactoryBeanIntegrationTest.java new file mode 100644 index 000000000..48c6e0cb1 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoFactoryBeanIntegrationTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.data.mongodb.config.WriteConcernPropertyEditor; +import org.springframework.test.util.ReflectionTestUtils; + +import com.mongodb.WriteConcern; + +/** + * Integration tests for {@link MongoFactoryBean}. + * + * @author Oliver Gierke + */ +public class MongoFactoryBeanIntegrationTest { + + /** + * @see DATAMONGO-408 + */ + @Test + public void convertsWriteConcernCorrectly() { + + RootBeanDefinition definition = new RootBeanDefinition(MongoFactoryBean.class); + definition.getPropertyValues().addPropertyValue("writeConcern", "SAFE"); + + DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); + factory.registerCustomEditor(WriteConcern.class, WriteConcernPropertyEditor.class); + factory.registerBeanDefinition("factory", definition); + + MongoFactoryBean bean = factory.getBean("&factory", MongoFactoryBean.class); + assertThat(ReflectionTestUtils.getField(bean, "writeConcern"), is((Object) WriteConcern.SAFE)); + } +}