Browse Source
* pr/9224: Polish "Add Slice test annotation for Redis" Add Slice test annotation for Redispull/9443/merge
12 changed files with 548 additions and 1 deletions
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
/* |
||||
* Copyright 2012-2017 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.boot.test.autoconfigure.data.redis; |
||||
|
||||
import java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Inherited; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
||||
|
||||
/** |
||||
* {@link ImportAutoConfiguration Auto-configuration imports} for typical Data redis |
||||
* tests. Most tests should consider using {@link DataRedisTest @DataRedisTest} rather |
||||
* than using this annotation directly. |
||||
* |
||||
* @author Jayaram Pradhan |
||||
* @since 2.0.0 |
||||
*/ |
||||
@Target(ElementType.TYPE) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Documented |
||||
@Inherited |
||||
@ImportAutoConfiguration |
||||
public @interface AutoConfigureDataRedis { |
||||
|
||||
} |
||||
@ -0,0 +1,91 @@
@@ -0,0 +1,91 @@
|
||||
/* |
||||
* Copyright 2012-2017 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.boot.test.autoconfigure.data.redis; |
||||
|
||||
import java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Inherited; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; |
||||
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; |
||||
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; |
||||
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; |
||||
import org.springframework.context.annotation.ComponentScan.Filter; |
||||
import org.springframework.core.annotation.AliasFor; |
||||
import org.springframework.test.context.BootstrapWith; |
||||
|
||||
/** |
||||
* Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)} |
||||
* for a typical Data Redis test. Can be used when a test focuses <strong>only</strong> on |
||||
* Redis components. |
||||
* <p> |
||||
* Using this annotation will disable full auto-configuration and instead apply only |
||||
* configuration relevant to Redis tests. |
||||
* <p> |
||||
* |
||||
* @author Jayaram Pradhan |
||||
* @since 2.0.0 |
||||
*/ |
||||
@Target(ElementType.TYPE) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Documented |
||||
@Inherited |
||||
@BootstrapWith(SpringBootTestContextBootstrapper.class) |
||||
@OverrideAutoConfiguration(enabled = false) |
||||
@TypeExcludeFilters(DataRedisTypeExcludeFilter.class) |
||||
@AutoConfigureCache |
||||
@AutoConfigureDataRedis |
||||
@ImportAutoConfiguration |
||||
public @interface DataRedisTest { |
||||
|
||||
/** |
||||
* Determines if default filtering should be used with |
||||
* {@link SpringBootApplication @SpringBootApplication}. By default no beans are |
||||
* included. |
||||
* @see #includeFilters() |
||||
* @see #excludeFilters() |
||||
* @return if default filters should be used |
||||
*/ |
||||
boolean useDefaultFilters() default true; |
||||
|
||||
/** |
||||
* A set of include filters which can be used to add otherwise filtered beans to the |
||||
* application context. |
||||
* @return include filters to apply |
||||
*/ |
||||
Filter[] includeFilters() default {}; |
||||
|
||||
/** |
||||
* A set of exclude filters which can be used to filter beans that would otherwise be |
||||
* added to the application context. |
||||
* @return exclude filters to apply |
||||
*/ |
||||
Filter[] excludeFilters() default {}; |
||||
|
||||
/** |
||||
* Auto-configuration exclusions that should be applied for this test. |
||||
* @return auto-configuration exclusions to apply |
||||
*/ |
||||
@AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude") |
||||
Class<?>[] excludeAutoConfiguration() default {}; |
||||
|
||||
} |
||||
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
/* |
||||
* Copyright 2012-2017 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.boot.test.autoconfigure.data.redis; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.Set; |
||||
|
||||
import org.springframework.boot.context.TypeExcludeFilter; |
||||
import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter; |
||||
import org.springframework.context.annotation.ComponentScan.Filter; |
||||
import org.springframework.core.annotation.AnnotatedElementUtils; |
||||
|
||||
/** |
||||
* {@link TypeExcludeFilter} for {@link DataRedisTest @DataRedisTest}. |
||||
* |
||||
* @author Jayaram Pradhan |
||||
*/ |
||||
class DataRedisTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter { |
||||
|
||||
private final DataRedisTest annotation; |
||||
|
||||
DataRedisTypeExcludeFilter(Class<?> testClass) { |
||||
this.annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, |
||||
DataRedisTest.class); |
||||
} |
||||
|
||||
@Override |
||||
protected boolean hasAnnotation() { |
||||
return this.annotation != null; |
||||
} |
||||
|
||||
@Override |
||||
protected Filter[] getFilters(FilterType type) { |
||||
switch (type) { |
||||
case INCLUDE: |
||||
return this.annotation.includeFilters(); |
||||
case EXCLUDE: |
||||
return this.annotation.excludeFilters(); |
||||
default: |
||||
throw new IllegalStateException("Unsupported type " + type); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected boolean isUseDefaultFilters() { |
||||
return this.annotation.useDefaultFilters(); |
||||
} |
||||
|
||||
@Override |
||||
protected Set<Class<?>> getDefaultIncludes() { |
||||
return Collections.emptySet(); |
||||
} |
||||
|
||||
@Override |
||||
protected Set<Class<?>> getComponentIncludes() { |
||||
return Collections.emptySet(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,79 @@
@@ -0,0 +1,79 @@
|
||||
/* |
||||
* Copyright 2012-2017 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.boot.test.autoconfigure.data.redis; |
||||
|
||||
import java.nio.charset.Charset; |
||||
|
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.ExpectedException; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.testsupport.rule.RedisTestServer; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.data.redis.connection.RedisConnection; |
||||
import org.springframework.data.redis.core.RedisOperations; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Integration test for {@link DataRedisTest}. |
||||
* |
||||
* @author Jayaram Pradhan |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@DataRedisTest |
||||
public class DataRedisTestIntegrationTests { |
||||
|
||||
@Rule |
||||
public RedisTestServer redis = new RedisTestServer(); |
||||
|
||||
@Rule |
||||
public ExpectedException thrown = ExpectedException.none(); |
||||
|
||||
@Autowired |
||||
private RedisOperations<Object, Object> operations; |
||||
|
||||
@Autowired |
||||
private ExampleRepository exampleRepository; |
||||
|
||||
@Autowired |
||||
private ApplicationContext applicationContext; |
||||
|
||||
private static final Charset CHARSET = Charset.forName("UTF-8"); |
||||
|
||||
@Test |
||||
public void testRepository() { |
||||
PersonHash personHash = new PersonHash(); |
||||
personHash.setDescription("Look, new @DataRedisTest!"); |
||||
assertThat(personHash.getId()).isNull(); |
||||
PersonHash savedEntity = this.exampleRepository.save(personHash); |
||||
assertThat(savedEntity.getId()).isNotNull(); |
||||
assertThat(this.operations.execute((RedisConnection connection) -> connection.exists(("persons:" + savedEntity.getId()).getBytes(CHARSET)))).isTrue(); |
||||
this.exampleRepository.deleteAll(); |
||||
} |
||||
|
||||
@Test |
||||
public void didNotInjectExampleService() { |
||||
this.thrown.expect(NoSuchBeanDefinitionException.class); |
||||
this.applicationContext.getBean(ExampleService.class); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
/* |
||||
* Copyright 2012-2017 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.boot.test.autoconfigure.data.redis; |
||||
|
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.testsupport.rule.RedisTestServer; |
||||
import org.springframework.context.annotation.ComponentScan.Filter; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Integration test with custom include filter for {@link DataRedisTest}. |
||||
* |
||||
* @author Jayaram Pradhan |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@DataRedisTest(includeFilters = @Filter(Service.class)) |
||||
public class DataRedisTestWithIncludeFilterIntegrationTests { |
||||
|
||||
@Rule |
||||
public RedisTestServer redis = new RedisTestServer(); |
||||
|
||||
@Autowired |
||||
private ExampleRepository exampleRepository; |
||||
|
||||
@Autowired |
||||
private ExampleService service; |
||||
|
||||
@Test |
||||
public void testService() { |
||||
PersonHash personHash = new PersonHash(); |
||||
personHash.setDescription("Look, new @DataRedisTest!"); |
||||
assertThat(personHash.getId()).isNull(); |
||||
PersonHash savedEntity = this.exampleRepository.save(personHash); |
||||
assertThat(this.service.hasRecord(savedEntity)).isTrue(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
/* |
||||
* Copyright 2012-2017 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.boot.test.autoconfigure.data.redis; |
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
||||
/** |
||||
* Example {@link SpringBootApplication} used with {@link DataRedisTest} tests. |
||||
* |
||||
* @author Jayaram Pradhan |
||||
*/ |
||||
@SpringBootApplication |
||||
public class ExampleRedisApplication { |
||||
|
||||
} |
||||
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
/* |
||||
* Copyright 2012-2017 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.boot.test.autoconfigure.data.redis; |
||||
|
||||
import org.springframework.data.repository.CrudRepository; |
||||
|
||||
/** |
||||
* Example repository used with {@link DataRedisTest} tests. |
||||
* |
||||
* @author Jayaram Pradhan |
||||
*/ |
||||
public interface ExampleRepository extends CrudRepository<PersonHash, String> { |
||||
} |
||||
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* Copyright 2012-2017 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.boot.test.autoconfigure.data.redis; |
||||
|
||||
import java.nio.charset.Charset; |
||||
|
||||
import org.springframework.data.redis.connection.RedisConnection; |
||||
import org.springframework.data.redis.core.RedisOperations; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
/** |
||||
* Example service used with {@link DataRedisTest} tests. |
||||
* |
||||
* @author Jayaram Pradhan |
||||
*/ |
||||
@Service |
||||
public class ExampleService { |
||||
|
||||
private static final Charset CHARSET = Charset.forName("UTF-8"); |
||||
|
||||
private RedisOperations<Object, Object> operations; |
||||
|
||||
public ExampleService(RedisOperations<Object, Object> operations) { |
||||
this.operations = operations; |
||||
} |
||||
|
||||
public boolean hasRecord(PersonHash personHash) { |
||||
return this.operations.execute((RedisConnection connection) -> |
||||
connection.exists(("persons:" + personHash.getId()).getBytes(CHARSET))); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
/* |
||||
* Copyright 2012-2017 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.boot.test.autoconfigure.data.redis; |
||||
|
||||
import org.springframework.data.annotation.Id; |
||||
import org.springframework.data.redis.core.RedisHash; |
||||
|
||||
/** |
||||
* Example graph used with {@link DataRedisTest} tests. |
||||
* |
||||
* @author Jayaram Pradhan |
||||
*/ |
||||
@RedisHash("persons") |
||||
public class PersonHash { |
||||
|
||||
@Id |
||||
private String id; |
||||
|
||||
private String description; |
||||
|
||||
public String getId() { |
||||
return this.id; |
||||
} |
||||
|
||||
public void setId(String id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public String getDescription() { |
||||
return this.description; |
||||
} |
||||
|
||||
public void setDescription(String description) { |
||||
this.description = description; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue