From bb62c8b2f10138cafbdf2b3ad8838b0d8a109356 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 20 Jun 2014 21:27:24 +0200 Subject: [PATCH] DATAMONGO-958 - Switch to FieldNamingStrategy SPI in Spring Data Commons. --- .../config/AbstractMongoConfiguration.java | 6 +- .../config/MappingMongoConverterParser.java | 2 +- .../mapping/BasicMongoPersistentProperty.java | 4 +- .../CachingMongoPersistentProperty.java | 3 +- ...elCaseAbbreviatingFieldNamingStrategy.java | 39 --------- ...CamelCaseSplittingFieldNamingStrategy.java | 79 ------------------- .../core/mapping/FieldNamingStrategy.java | 37 --------- .../core/mapping/MongoMappingContext.java | 4 +- .../PropertyNameFieldNamingStrategy.java | 35 -------- .../mapping/SnakeCaseFieldNamingStrategy.java | 34 -------- ...gMongoConverterParserIntegrationTests.java | 2 +- ...BasicMongoPersistentPropertyUnitTests.java | 19 +++-- ...reviatingFieldNamingStrategyUnitTests.java | 51 ------------ .../mapping/MongoMappingContextUnitTests.java | 4 +- ...SnakeCaseFieldNamingStrategyUnitTests.java | 57 ------------- 15 files changed, 25 insertions(+), 351 deletions(-) delete mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CamelCaseAbbreviatingFieldNamingStrategy.java delete mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CamelCaseSplittingFieldNamingStrategy.java delete mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/FieldNamingStrategy.java delete mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/PropertyNameFieldNamingStrategy.java delete mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/SnakeCaseFieldNamingStrategy.java delete mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/CamelCaseAbbreviatingFieldNamingStrategyUnitTests.java delete mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/SnakeCaseFieldNamingStrategyUnitTests.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java index 8e18966a4..dc2776d72 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java @@ -28,6 +28,9 @@ import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.data.annotation.Persistent; import org.springframework.data.authentication.UserCredentials; import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory; +import org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy; +import org.springframework.data.mapping.model.FieldNamingStrategy; +import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; @@ -35,11 +38,8 @@ import org.springframework.data.mongodb.core.convert.CustomConversions; import org.springframework.data.mongodb.core.convert.DbRefResolver; import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; -import org.springframework.data.mongodb.core.mapping.CamelCaseAbbreviatingFieldNamingStrategy; import org.springframework.data.mongodb.core.mapping.Document; -import org.springframework.data.mongodb.core.mapping.FieldNamingStrategy; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; -import org.springframework.data.mongodb.core.mapping.PropertyNameFieldNamingStrategy; import org.springframework.data.support.CachingIsNewStrategyFactory; import org.springframework.data.support.IsNewStrategyFactory; import org.springframework.util.ClassUtils; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java index 32d1670ec..3aae75689 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java @@ -52,10 +52,10 @@ import org.springframework.core.type.filter.TypeFilter; import org.springframework.data.annotation.Persistent; import org.springframework.data.config.BeanComponentDefinitionBuilder; import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory; +import org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy; import org.springframework.data.mongodb.core.convert.CustomConversions; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator; -import org.springframework.data.mongodb.core.mapping.CamelCaseAbbreviatingFieldNamingStrategy; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java index ad15ca46b..96c150f55 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 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. @@ -27,7 +27,9 @@ import org.slf4j.LoggerFactory; import org.springframework.data.annotation.Id; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; +import org.springframework.data.mapping.model.FieldNamingStrategy; import org.springframework.data.mapping.model.MappingException; +import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.util.StringUtils; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CachingMongoPersistentProperty.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CachingMongoPersistentProperty.java index c91a5de5e..bafaa0a32 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CachingMongoPersistentProperty.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CachingMongoPersistentProperty.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 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. @@ -18,6 +18,7 @@ package org.springframework.data.mongodb.core.mapping; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; +import org.springframework.data.mapping.model.FieldNamingStrategy; import org.springframework.data.mapping.model.SimpleTypeHolder; /** diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CamelCaseAbbreviatingFieldNamingStrategy.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CamelCaseAbbreviatingFieldNamingStrategy.java deleted file mode 100644 index 2019cc629..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CamelCaseAbbreviatingFieldNamingStrategy.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2013-2014 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.mapping; - -/** - * {@link FieldNamingStrategy} that abbreviates field names by using the very first letter of the camel case parts of - * the {@link MongoPersistentProperty}'s name. - * - * @since 1.3 - * @author Oliver Gierke - */ -public class CamelCaseAbbreviatingFieldNamingStrategy extends CamelCaseSplittingFieldNamingStrategy { - - public CamelCaseAbbreviatingFieldNamingStrategy() { - super(""); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.core.mapping.CamelCaseSplittingFieldNamingStrategy#preparePart(java.lang.String) - */ - @Override - protected String preparePart(String part) { - return part.substring(0, 1); - } -} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CamelCaseSplittingFieldNamingStrategy.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CamelCaseSplittingFieldNamingStrategy.java deleted file mode 100644 index ced0504ea..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CamelCaseSplittingFieldNamingStrategy.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2014 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.mapping; - -import java.util.ArrayList; -import java.util.List; - -import org.springframework.data.util.ParsingUtils; -import org.springframework.util.Assert; -import org.springframework.util.StringUtils; - -/** - * Configurable {@link FieldNamingStrategy} that splits up camel-case property names and reconcatenates them using a - * configured delimiter. Individual parts of the name can be manipulated using {@link #preparePart(String)}. - * - * @author Oliver Gierke - * @since 1.5 - */ -public class CamelCaseSplittingFieldNamingStrategy implements FieldNamingStrategy { - - private final String delimiter; - - /** - * Creates a new {@link CamelCaseSplittingFieldNamingStrategy}. - * - * @param delimiter must not be {@literal null}. - */ - public CamelCaseSplittingFieldNamingStrategy(String delimiter) { - - Assert.notNull(delimiter, "Delimiter must not be null!"); - this.delimiter = delimiter; - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.core.mapping.FieldNamingStrategy#getFieldName(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) - */ - @Override - public String getFieldName(MongoPersistentProperty property) { - - List parts = ParsingUtils.splitCamelCaseToLower(property.getName()); - List result = new ArrayList(); - - for (String part : parts) { - - String candidate = preparePart(part); - - if (StringUtils.hasText(candidate)) { - result.add(candidate); - } - } - - return StringUtils.collectionToDelimitedString(result, delimiter); - } - - /** - * Callback to prepare the uncapitalized part obtained from the split up of the camel case source. Default - * implementation returns the part as is. - * - * @param part - * @return - */ - protected String preparePart(String part) { - return part; - } -} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/FieldNamingStrategy.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/FieldNamingStrategy.java deleted file mode 100644 index c2c2941a8..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/FieldNamingStrategy.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2013 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.mapping; - -/** - * SPI interface to determine how to name document fields in cases the field name is not manually defined. - * - * @see DocumentField - * @see PropertyNameFieldNamingStrategy - * @see CamelCaseAbbreviatingFieldNamingStrategy - * @see SnakeCaseFieldNamingStrategy - * @since 1.3 - * @author Oliver Gierke - */ -public interface FieldNamingStrategy { - - /** - * Returns the field name to be used for the given {@link MongoPersistentProperty}. - * - * @param property must not be {@literal null} or empty; - * @return - */ - String getFieldName(MongoPersistentProperty property); -} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoMappingContext.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoMappingContext.java index c95ddea63..c87806285 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoMappingContext.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoMappingContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 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. @@ -24,6 +24,8 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.data.mapping.context.AbstractMappingContext; import org.springframework.data.mapping.context.MappingContext; +import org.springframework.data.mapping.model.FieldNamingStrategy; +import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.util.TypeInformation; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/PropertyNameFieldNamingStrategy.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/PropertyNameFieldNamingStrategy.java deleted file mode 100644 index cee9c648d..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/PropertyNameFieldNamingStrategy.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2013 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.mapping; - -/** - * {@link FieldNamingStrategy} simply using the {@link MongoPersistentProperty}'s name. - * - * @since 1.3 - * @author Oliver Gierke - */ -public enum PropertyNameFieldNamingStrategy implements FieldNamingStrategy { - - INSTANCE; - - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.core.mapping.FieldNamingStrategy#getFieldName(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) - */ - public String getFieldName(MongoPersistentProperty property) { - return property.getName(); - } -} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/SnakeCaseFieldNamingStrategy.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/SnakeCaseFieldNamingStrategy.java deleted file mode 100644 index cf5328b31..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/SnakeCaseFieldNamingStrategy.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2013-2014 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.mapping; - -/** - * {@link FieldNamingStrategy} that translates typical camel case Java property names to lower case JSON element names, - * separated by underscores. - * - * @since 1.5 - * @author Ryan Tenney - * @author Oliver Gierke - */ -public class SnakeCaseFieldNamingStrategy extends CamelCaseSplittingFieldNamingStrategy { - - /** - * Creates a new {@link SnakeCaseFieldNamingStrategy}. - */ - public SnakeCaseFieldNamingStrategy() { - super("_"); - } -} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MappingMongoConverterParserIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MappingMongoConverterParserIntegrationTests.java index 7f28694eb..5082cc3ae 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MappingMongoConverterParserIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MappingMongoConverterParserIntegrationTests.java @@ -34,11 +34,11 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.io.ClassPathResource; +import org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy; import org.springframework.data.mongodb.core.convert.CustomConversions; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MongoTypeMapper; import org.springframework.data.mongodb.core.mapping.Account; -import org.springframework.data.mongodb.core.mapping.CamelCaseAbbreviatingFieldNamingStrategy; import org.springframework.data.mongodb.repository.Person; import org.springframework.stereotype.Component; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java index c8cc202ed..eaade63a4 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java @@ -26,7 +26,10 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.data.annotation.Id; +import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.mapping.model.FieldNamingStrategy; import org.springframework.data.mapping.model.MappingException; +import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.util.ClassTypeInformation; import org.springframework.util.ReflectionUtils; @@ -40,8 +43,7 @@ public class BasicMongoPersistentPropertyUnitTests { MongoPersistentEntity entity; - @Rule - public ExpectedException exception = ExpectedException.none(); + @Rule public ExpectedException exception = ExpectedException.none(); @Before public void setup() { @@ -128,22 +130,19 @@ public class BasicMongoPersistentPropertyUnitTests { class Person { - @Id - String id; + @Id String id; - @org.springframework.data.mongodb.core.mapping.Field("foo") - String firstname; + @org.springframework.data.mongodb.core.mapping.Field("foo") String firstname; String lastname; - @org.springframework.data.mongodb.core.mapping.Field(order = -20) - String ssn; + @org.springframework.data.mongodb.core.mapping.Field(order = -20) String ssn; } enum UppercaseFieldNamingStrategy implements FieldNamingStrategy { INSTANCE; - public String getFieldName(MongoPersistentProperty property) { + public String getFieldName(PersistentProperty property) { return property.getName().toUpperCase(Locale.US); } } @@ -152,7 +151,7 @@ public class BasicMongoPersistentPropertyUnitTests { INSTANCE; - public String getFieldName(MongoPersistentProperty property) { + public String getFieldName(PersistentProperty property) { return null; } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/CamelCaseAbbreviatingFieldNamingStrategyUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/CamelCaseAbbreviatingFieldNamingStrategyUnitTests.java deleted file mode 100644 index 2340e7205..000000000 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/CamelCaseAbbreviatingFieldNamingStrategyUnitTests.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2013 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.mapping; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - -/** - * Unit tests for {@link CamelCaseAbbreviatingFieldNamingStrategy}. - * - * @author Oliver Gierke - */ -@RunWith(MockitoJUnitRunner.class) -public class CamelCaseAbbreviatingFieldNamingStrategyUnitTests { - - FieldNamingStrategy strategy = new CamelCaseAbbreviatingFieldNamingStrategy(); - - @Mock - MongoPersistentProperty property; - - @Test - public void foo() { - assertFieldNameForPropertyName("fooBar", "fb"); - assertFieldNameForPropertyName("fooBARFooBar", "fbfb"); - } - - private void assertFieldNameForPropertyName(String propertyName, String fieldName) { - - when(property.getName()).thenReturn(propertyName); - assertThat(strategy.getFieldName(property), is(fieldName)); - } -} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/MongoMappingContextUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/MongoMappingContextUnitTests.java index a931c3379..abaa66c4a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/MongoMappingContextUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/MongoMappingContextUnitTests.java @@ -31,6 +31,8 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.context.ApplicationContext; import org.springframework.data.annotation.Id; +import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.mapping.model.FieldNamingStrategy; import org.springframework.data.mapping.model.MappingException; import com.mongodb.DBRef; @@ -84,7 +86,7 @@ public class MongoMappingContextUnitTests { context.setApplicationContext(applicationContext); context.setFieldNamingStrategy(new FieldNamingStrategy() { - public String getFieldName(MongoPersistentProperty property) { + public String getFieldName(PersistentProperty property) { return property.getName().toUpperCase(Locale.US); } }); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/SnakeCaseFieldNamingStrategyUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/SnakeCaseFieldNamingStrategyUnitTests.java deleted file mode 100644 index f791f1ffa..000000000 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/SnakeCaseFieldNamingStrategyUnitTests.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2013-2014 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.mapping; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - -/** - * Unit tests for {@link SnakeCaseFieldNamingStrategy}. - * - * @author Ryan Tenney - * @author Oliver Gierke - */ -@RunWith(MockitoJUnitRunner.class) -public class SnakeCaseFieldNamingStrategyUnitTests { - - FieldNamingStrategy strategy = new SnakeCaseFieldNamingStrategy(); - - @Mock MongoPersistentProperty property; - - /** - * @see DATAMONGO-866 - */ - @Test - public void rendersSnakeCaseFieldNames() { - - assertFieldNameForPropertyName("fooBar", "foo_bar"); - assertFieldNameForPropertyName("FooBar", "foo_bar"); - assertFieldNameForPropertyName("foo_bar", "foo_bar"); - assertFieldNameForPropertyName("FOO_BAR", "foo_bar"); - } - - private void assertFieldNameForPropertyName(String propertyName, String fieldName) { - - when(property.getName()).thenReturn(propertyName); - assertThat(strategy.getFieldName(property), is(fieldName)); - } -}