diff --git a/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java b/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java index ec9bbde0..ece54107 100644 --- a/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java +++ b/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java @@ -29,13 +29,22 @@ import org.springframework.data.couchbase.core.convert.CustomConversions; import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter; import org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService; import org.springframework.data.couchbase.core.convert.translation.TranslationService; -import org.springframework.data.couchbase.core.mapping.*; +import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext; +import org.springframework.data.couchbase.core.mapping.Document; +import org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy; +import org.springframework.data.mapping.model.FieldNamingStrategy; +import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; import java.net.URI; import java.net.URISyntaxException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; /** * Base class for Spring Data Couchbase configuration using JavaConfig. @@ -200,7 +209,7 @@ public abstract class AbstractCouchbaseConfiguration { } /** - * Set to true if field names should be abbreviated with the {@link org.springframework.data.couchbase.core.mapping.CamelCaseAbbreviatingFieldNamingStrategy}. + * Set to true if field names should be abbreviated with the {@link CamelCaseAbbreviatingFieldNamingStrategy}. * * @return true if field names should be abbreviated, default is false. */ @@ -214,7 +223,7 @@ public abstract class AbstractCouchbaseConfiguration { * @return the naming strategy. */ protected FieldNamingStrategy fieldNamingStrategy() { - return abbreviateFieldNames() ? new CamelCaseAbbreviatingFieldNamingStrategy() : FallbackFieldNamingStrategy.INSTANCE; + return abbreviateFieldNames() ? new CamelCaseAbbreviatingFieldNamingStrategy() : PropertyNameFieldNamingStrategy.INSTANCE; } /** diff --git a/src/main/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentProperty.java b/src/main/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentProperty.java index ea944aae..8804c08c 100644 --- a/src/main/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentProperty.java +++ b/src/main/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentProperty.java @@ -18,7 +18,9 @@ package org.springframework.data.couchbase.core.mapping; 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; @@ -51,7 +53,7 @@ public class BasicCouchbasePersistentProperty final CouchbasePersistentEntity owner, final SimpleTypeHolder simpleTypeHolder, final FieldNamingStrategy fieldNamingStrategy) { super(field, propertyDescriptor, owner, simpleTypeHolder); - this.fieldNamingStrategy = fieldNamingStrategy == null ? FallbackFieldNamingStrategy.INSTANCE + this.fieldNamingStrategy = fieldNamingStrategy == null ? PropertyNameFieldNamingStrategy.INSTANCE : fieldNamingStrategy; } diff --git a/src/main/java/org/springframework/data/couchbase/core/mapping/CamelCaseAbbreviatingFieldNamingStrategy.java b/src/main/java/org/springframework/data/couchbase/core/mapping/CamelCaseAbbreviatingFieldNamingStrategy.java deleted file mode 100644 index 861472cf..00000000 --- a/src/main/java/org/springframework/data/couchbase/core/mapping/CamelCaseAbbreviatingFieldNamingStrategy.java +++ /dev/null @@ -1,38 +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.couchbase.core.mapping; - -/** - * {@link FieldNamingStrategy} that abbreviates field names by using the very first letter of the camel case parts of - * the {@link CouchbasePersistentProperty}'s name. - * - * @since 1.1 - * @author Michael Nitschinger - * @author Oliver Gierke - */ -public class CamelCaseAbbreviatingFieldNamingStrategy extends CamelCaseSplittingFieldNamingStrategy { - - public CamelCaseAbbreviatingFieldNamingStrategy() { - super(""); - } - - @Override - protected String preparePart(String part) { - return part.substring(0, 1); - } - - -} diff --git a/src/main/java/org/springframework/data/couchbase/core/mapping/CamelCaseSplittingFieldNamingStrategy.java b/src/main/java/org/springframework/data/couchbase/core/mapping/CamelCaseSplittingFieldNamingStrategy.java deleted file mode 100644 index 1a96f569..00000000 --- a/src/main/java/org/springframework/data/couchbase/core/mapping/CamelCaseSplittingFieldNamingStrategy.java +++ /dev/null @@ -1,74 +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.couchbase.core.mapping; - -import org.springframework.data.util.ParsingUtils; -import org.springframework.util.Assert; -import org.springframework.util.StringUtils; - -import java.util.ArrayList; -import java.util.List; - -/** - * 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 - * @author Michael Nitschinger - * @since 1.1 - */ -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; - } - - @Override - public String getFieldName(final CouchbasePersistentProperty 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 the part. - * @return the prepared part. - */ - protected String preparePart(String part) { - return part; - } - -} diff --git a/src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseMappingContext.java b/src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseMappingContext.java index d82414b7..89730dda 100644 --- a/src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseMappingContext.java +++ b/src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseMappingContext.java @@ -20,6 +20,8 @@ import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.data.mapping.context.AbstractMappingContext; +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; @@ -44,7 +46,7 @@ public class CouchbaseMappingContext /** * The default field naming strategy. */ - private static final FieldNamingStrategy DEFAULT_NAMING_STRATEGY = FallbackFieldNamingStrategy.INSTANCE; + private static final FieldNamingStrategy DEFAULT_NAMING_STRATEGY = PropertyNameFieldNamingStrategy.INSTANCE; /** * The field naming strategy to use. diff --git a/src/main/java/org/springframework/data/couchbase/core/mapping/FallbackFieldNamingStrategy.java b/src/main/java/org/springframework/data/couchbase/core/mapping/FallbackFieldNamingStrategy.java deleted file mode 100644 index 94b087c8..00000000 --- a/src/main/java/org/springframework/data/couchbase/core/mapping/FallbackFieldNamingStrategy.java +++ /dev/null @@ -1,32 +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.couchbase.core.mapping; - -/** - * {@link FieldNamingStrategy} simply using the {@link CouchbasePersistentProperty}'s name. - * - * @author Michael Nitschinger - * @since 1.1 - */ -public enum FallbackFieldNamingStrategy implements FieldNamingStrategy { - INSTANCE; - - @Override - public String getFieldName(final CouchbasePersistentProperty property) { - return property.getName(); - } - -} diff --git a/src/main/java/org/springframework/data/couchbase/core/mapping/FieldNamingStrategy.java b/src/main/java/org/springframework/data/couchbase/core/mapping/FieldNamingStrategy.java deleted file mode 100644 index 8282e19a..00000000 --- a/src/main/java/org/springframework/data/couchbase/core/mapping/FieldNamingStrategy.java +++ /dev/null @@ -1,39 +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.couchbase.core.mapping; - -/** - * SPI interface to determine how to name document fields in cases the field name is not manually defined. - * - * @see FallbackFieldNamingStrategy - * @see CamelCaseAbbreviatingFieldNamingStrategy - * @see SnakeCaseFieldNamingStrategy - * - * @author Michael Nitschinger - * @author Oliver Gierke - * @since 1.1 - */ -public interface FieldNamingStrategy { - - /** - * Returns the field name to be used for the given {@link CouchbasePersistentProperty}. - * - * @param property must not be {@literal null} or empty. - * @return the field name to be used. - */ - String getFieldName(CouchbasePersistentProperty property); - -} diff --git a/src/main/java/org/springframework/data/couchbase/core/mapping/SnakeCaseFieldNamingStrategy.java b/src/main/java/org/springframework/data/couchbase/core/mapping/SnakeCaseFieldNamingStrategy.java deleted file mode 100644 index 4040de95..00000000 --- a/src/main/java/org/springframework/data/couchbase/core/mapping/SnakeCaseFieldNamingStrategy.java +++ /dev/null @@ -1,35 +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.couchbase.core.mapping; - -/** - * {@link FieldNamingStrategy} that translates typical camel case Java property names to lower case JSON element names, - * separated by underscores. - * - * @since 1.1 - * @author Ryan Tenney - * @author Michael Nitschinger - */ -public class SnakeCaseFieldNamingStrategy extends CamelCaseSplittingFieldNamingStrategy { - - /** - * Creates a new {@link SnakeCaseFieldNamingStrategy}. - */ - public SnakeCaseFieldNamingStrategy() { - super("_"); - } - -} diff --git a/src/test/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentPropertyTests.java b/src/test/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentPropertyTests.java index 9ede60d3..9d9f9e6a 100644 --- a/src/test/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentPropertyTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentPropertyTests.java @@ -19,6 +19,7 @@ package org.springframework.data.couchbase.core.mapping; import org.junit.Before; import org.junit.Test; import org.springframework.data.annotation.Id; +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; @@ -74,7 +75,7 @@ public class BasicCouchbasePersistentPropertyTests { */ private CouchbasePersistentProperty getPropertyFor(Field field) { return new BasicCouchbasePersistentProperty(field, null, entity, - new SimpleTypeHolder(), FallbackFieldNamingStrategy.INSTANCE); + new SimpleTypeHolder(), PropertyNameFieldNamingStrategy.INSTANCE); } /** diff --git a/src/test/java/org/springframework/data/couchbase/core/mapping/CamelCaseAbbreviatingFieldNamingStrategyUnitTests.java b/src/test/java/org/springframework/data/couchbase/core/mapping/CamelCaseAbbreviatingFieldNamingStrategyUnitTests.java deleted file mode 100644 index d53fc139..00000000 --- a/src/test/java/org/springframework/data/couchbase/core/mapping/CamelCaseAbbreviatingFieldNamingStrategyUnitTests.java +++ /dev/null @@ -1,53 +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.couchbase.core.mapping; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.when; - -/** - * Unit tests for {@link CamelCaseAbbreviatingFieldNamingStrategy}. - * - * @author Oliver Gierke - * @author Michael Nitschinger - * @since 1.1 - */ -@RunWith(MockitoJUnitRunner.class) -public class CamelCaseAbbreviatingFieldNamingStrategyUnitTests { - - FieldNamingStrategy strategy = new CamelCaseAbbreviatingFieldNamingStrategy(); - - @Mock - CouchbasePersistentProperty property; - - @Test - public void shouldConvertPropertyNames() { - assertFieldNameForPropertyName("fooBar", "fb"); - assertFieldNameForPropertyName("fooBARFooBar", "fbfb"); - } - - private void assertFieldNameForPropertyName(final String propertyName, final String fieldName) { - when(property.getName()).thenReturn(propertyName); - assertThat(strategy.getFieldName(property), is(fieldName)); - } - -} diff --git a/src/test/java/org/springframework/data/couchbase/core/mapping/SnakeCaseFieldNamingStrategyUnitTests.java b/src/test/java/org/springframework/data/couchbase/core/mapping/SnakeCaseFieldNamingStrategyUnitTests.java deleted file mode 100644 index f8b28692..00000000 --- a/src/test/java/org/springframework/data/couchbase/core/mapping/SnakeCaseFieldNamingStrategyUnitTests.java +++ /dev/null @@ -1,55 +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.couchbase.core.mapping; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.when; - -/** - * Unit tests for {@link SnakeCaseFieldNamingStrategy}. - * - * @author Ryan Tenney - * @author Michael Nitschinger - * @since 1.1 - */ -@RunWith(MockitoJUnitRunner.class) -public class SnakeCaseFieldNamingStrategyUnitTests { - - FieldNamingStrategy strategy = new SnakeCaseFieldNamingStrategy(); - - @Mock - CouchbasePersistentProperty property; - - @Test - public void shouldConvertPropertyNames() { - assertFieldNameForPropertyName("fooBar", "foo_bar"); - assertFieldNameForPropertyName("FooBar", "foo_bar"); - assertFieldNameForPropertyName("foo_bar", "foo_bar"); - assertFieldNameForPropertyName("FOO_BAR", "foo_bar"); - } - - private void assertFieldNameForPropertyName(final String propertyName, final String fieldName) { - when(property.getName()).thenReturn(propertyName); - assertThat(strategy.getFieldName(property), is(fieldName)); - } - -}