DATACMNS-1508 - Address review comments.
Added ClassUtils.ifPresent(…) to conditionally call back a Consumer<Class> if a class is available from the given ClassLoader. Extract isSuspend(…) method into KotlinReflectionUtils. Deprecate Kotlin-related methods in our ReflectionUtils as parts are available from Spring Framework directly. Rename CoCrudRepository to CoroutineCrudRepository and CoroutineSortingRepository. Add tests for KotlinReflectionUtils to test calls without Kotlin dependencies. Original pull request: #415.
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.data.repository.core.support;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -28,10 +29,9 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.util.Streamable;
|
||||
|
||||
/**
|
||||
* Unit test for {@link QueryExecuterMethodInterceptor}.
|
||||
* Unit test for {@link QueryExecutorMethodInterceptor}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
@@ -40,27 +40,24 @@ import org.springframework.data.util.Streamable;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class QueryExecutorMethodInterceptorUnitTests {
|
||||
|
||||
@Mock RepositoryFactorySupport factory;
|
||||
@Mock RepositoryInformation information;
|
||||
@Mock QueryLookupStrategy strategy;
|
||||
|
||||
@Test
|
||||
@Test // DATACMNS-1508
|
||||
public void rejectsRepositoryInterfaceWithQueryMethodsIfNoQueryLookupStrategyIsDefined() {
|
||||
|
||||
when(information.hasQueryMethods()).thenReturn(true);
|
||||
when(factory.getQueryLookupStrategy(any(), any())).thenReturn(Optional.empty());
|
||||
|
||||
assertThatIllegalStateException().isThrownBy(
|
||||
() -> factory.new QueryExecutorMethodInterceptor(information, new SpelAwareProxyProjectionFactory()));
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> new QueryExecutorMethodInterceptor(information, new SpelAwareProxyProjectionFactory(),
|
||||
Optional.empty(), PropertiesBasedNamedQueries.EMPTY, Collections.emptyList()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // DATACMNS-1508
|
||||
public void skipsQueryLookupsIfQueryLookupStrategyIsNotPresent() {
|
||||
|
||||
when(information.getQueryMethods()).thenReturn(Streamable.empty());
|
||||
when(factory.getQueryLookupStrategy(any(), any())).thenReturn(Optional.of(strategy));
|
||||
|
||||
factory.new QueryExecutorMethodInterceptor(information, new SpelAwareProxyProjectionFactory());
|
||||
new QueryExecutorMethodInterceptor(information, new SpelAwareProxyProjectionFactory(), Optional.empty(),
|
||||
PropertiesBasedNamedQueries.EMPTY, Collections.emptyList());
|
||||
|
||||
verify(strategy, times(0)).resolveQuery(any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2020 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
|
||||
*
|
||||
* https://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.util;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link KotlinReflectionUtilsUnitTests}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class KotlinReflectionUtilsUnitTests {
|
||||
|
||||
@Test // DATACMNS-1508
|
||||
public void classShouldLoadWithKotlin() {
|
||||
assertThat(KotlinDetector.isKotlinPresent()).isTrue();
|
||||
assertThat(KotlinReflectionUtils.isSupportedKotlinClass(TypeCreatingSyntheticClass.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1508
|
||||
public void classShouldLoadWithoutKotlin() throws Exception {
|
||||
runTest("loadClassWithoutKotlin");
|
||||
}
|
||||
|
||||
// executed via reflection in the context of a ClassLoader without Kotlin dependencies.
|
||||
public void loadClassWithoutKotlin() {
|
||||
|
||||
assertThat(KotlinDetector.isKotlinPresent()).isFalse();
|
||||
assertThat(KotlinReflectionUtils.isSupportedKotlinClass(TypeCreatingSyntheticClass.class)).isFalse();
|
||||
}
|
||||
|
||||
protected void runTest(String testName)
|
||||
throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
|
||||
KotlinExcludingURLClassLoader classLoader = new KotlinExcludingURLClassLoader(
|
||||
((URLClassLoader) getClass().getClassLoader()).getURLs());
|
||||
Class<?> testClass = ClassUtils.forName(getClass().getName(), classLoader);
|
||||
|
||||
ReflectionUtils.invokeMethod(testClass.getMethod(testName), testClass.newInstance());
|
||||
}
|
||||
|
||||
static class KotlinExcludingURLClassLoader extends URLClassLoader {
|
||||
public KotlinExcludingURLClassLoader(URL[] urls) {
|
||||
super(urls, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
|
||||
if (name.startsWith("kotlin")) {
|
||||
throw new ClassNotFoundException("Denied: " + name);
|
||||
}
|
||||
|
||||
return super.findClass(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user