diff --git a/pom.xml b/pom.xml index 877ee6a008..f0711f783c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-commons - 2.1.0.BUILD-SNAPSHOT + 2.1.0.DATACMNS-1289-SNAPSHOT Spring Data Core diff --git a/src/main/java/org/springframework/data/repository/core/support/RepositoryFragment.java b/src/main/java/org/springframework/data/repository/core/support/RepositoryFragment.java index 66fd51e98a..51a664e61b 100644 --- a/src/main/java/org/springframework/data/repository/core/support/RepositoryFragment.java +++ b/src/main/java/org/springframework/data/repository/core/support/RepositoryFragment.java @@ -155,12 +155,27 @@ public String toString() { } } - @RequiredArgsConstructor @EqualsAndHashCode(callSuper = false) static class ImplementedRepositoryFragment implements RepositoryFragment { - private final @NonNull Optional> interfaceClass; - private final @NonNull T implementation; + private final Optional> interfaceClass; + private final T implementation; + + public ImplementedRepositoryFragment(Optional> interfaceClass, T implementation) { + + Assert.notNull(interfaceClass, "Interface class must not be null!"); + Assert.notNull(implementation, "Implementation object must not be null!"); + + interfaceClass.ifPresent(it -> { + + Assert.isTrue(ClassUtils.isAssignableValue(it, implementation), + () -> String.format("Fragment implementation %s does not implement %s!", ClassUtils.getQualifiedName(it), + ClassUtils.getQualifiedName(implementation.getClass()))); + }); + + this.interfaceClass = interfaceClass; + this.implementation = implementation; + } /* * (non-Javadoc) diff --git a/src/test/java/org/springframework/data/repository/core/support/RepositoryFragmentUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/RepositoryFragmentUnitTests.java new file mode 100644 index 0000000000..976877e227 --- /dev/null +++ b/src/test/java/org/springframework/data/repository/core/support/RepositoryFragmentUnitTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2018 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.repository.core.support; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; + +/** + * Unit tests for {@link RepositoryFragment}. + * + * @author Mark Paluch + */ +public class RepositoryFragmentUnitTests { + + @SuppressWarnings("unchecked") + @Test // DATACMNS-1289 + public void fragmentCreationFromUnrelatedTypesShouldFail() { + + assertThatThrownBy(() -> RepositoryFragment.implemented((Class) CustomFragment.class, new UnrelatedImpl())) + .hasMessageMatching("Fragment implementation .* does not implement .*UnrelatedImpl!") + .isInstanceOf(IllegalArgumentException.class); + } + + @Test // DATACMNS-1289 + public void fragmentCreationFromRelatedTypesShouldCreateNewFragment() { + assertThat(RepositoryFragment.implemented(CustomFragment.class, new CustomFragmentImpl())).isNotNull(); + } + + interface CustomFragment {} + + private static class CustomFragmentImpl implements CustomFragment {} + + private static class UnrelatedImpl {} +}