Skip to content

DATACMNS-1289 - Allow fragment creation only from implementations that implement their declared class. #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATACMNS-1289-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,27 @@ public String toString() {
}
}

@RequiredArgsConstructor
@EqualsAndHashCode(callSuper = false)
static class ImplementedRepositoryFragment<T> implements RepositoryFragment<T> {

private final @NonNull Optional<Class<T>> interfaceClass;
private final @NonNull T implementation;
private final Optional<Class<T>> interfaceClass;
private final T implementation;

public ImplementedRepositoryFragment(Optional<Class<T>> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {}
}