CQRSHandlerRegistrar.java

1
package org.sanmibuh.ddd.infrastructure;
2
3
import java.util.Arrays;
4
import java.util.List;
5
import java.util.Optional;
6
import lombok.RequiredArgsConstructor;
7
import org.sanmibuh.ddd.port.CommandHandler;
8
import org.sanmibuh.ddd.port.QueryHandler;
9
import org.springframework.beans.BeansException;
10
import org.springframework.beans.factory.BeanFactory;
11
import org.springframework.beans.factory.config.BeanDefinition;
12
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
13
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
14
import org.springframework.beans.factory.support.RootBeanDefinition;
15
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
16
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
17
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
18
import org.springframework.core.type.filter.AssignableTypeFilter;
19
20
@RequiredArgsConstructor(access = lombok.AccessLevel.PACKAGE)
21
final class CQRSHandlerRegistrar implements BeanDefinitionRegistryPostProcessor {
22
23
  private static final AnnotationBeanNameGenerator NAME_GENERATOR =
24
      new AnnotationBeanNameGenerator();
25
26
  private final BeanFactory beanFactory;
27
28
  @Override
29
  public void postProcessBeanDefinitionRegistry(final BeanDefinitionRegistry registry)
30
      throws BeansException {
31 1 1. postProcessBeanDefinitionRegistry : negated conditional → KILLED
    if (!AutoConfigurationPackages.has(beanFactory)) {
32
      return;
33
    }
34
35
    final var packages = AutoConfigurationPackages.get(beanFactory);
36 1 1. postProcessBeanDefinitionRegistry : removed call to org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::registerHandlers → KILLED
    registerHandlers(registry, packages, CommandHandler.class);
37 1 1. postProcessBeanDefinitionRegistry : removed call to org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::registerHandlers → KILLED
    registerHandlers(registry, packages, QueryHandler.class);
38
  }
39
40
  private void registerHandlers(
41
      final BeanDefinitionRegistry registry,
42
      final List<String> packages,
43
      final Class<?> handlerType) {
44
    final var scanner = new ClassPathScanningCandidateComponentProvider(false);
45 1 1. registerHandlers : removed call to org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider::addIncludeFilter → KILLED
    scanner.addIncludeFilter(new AssignableTypeFilter(handlerType));
46
    packages.stream()
47 1 1. lambda$registerHandlers$0 : replaced return value with Stream.empty for org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::lambda$registerHandlers$0 → KILLED
        .flatMap(basePackage -> scanner.findCandidateComponents(basePackage).stream())
48 2 1. lambda$registerHandlers$1 : removed call to org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::registerIfAbsent → KILLED
2. registerHandlers : removed call to java/util/stream/Stream::forEach → KILLED
        .forEach(candidate -> registerIfAbsent(registry, candidate));
49
  }
50
51
  private void registerIfAbsent(
52
      final BeanDefinitionRegistry registry, final BeanDefinition candidate) {
53
    findBeanClassNamePITEquivalent(candidate)
54 1 1. registerIfAbsent : removed call to java/util/Optional::ifPresent → KILLED
        .ifPresent(
55
            beanClassName -> {
56 1 1. lambda$registerIfAbsent$0 : negated conditional → KILLED
              if (!isClassRegistered(registry, beanClassName)) {
57
                final var beanName = NAME_GENERATOR.generateBeanName(candidate, registry);
58 1 1. lambda$registerIfAbsent$0 : removed call to org/springframework/beans/factory/support/BeanDefinitionRegistry::registerBeanDefinition → KILLED
                registry.registerBeanDefinition(beanName, new RootBeanDefinition(beanClassName));
59
              }
60
            });
61
  }
62
63
  private boolean isClassRegistered(
64
      final BeanDefinitionRegistry registry, final String beanClassName) {
65 2 1. isClassRegistered : replaced boolean return with true for org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::isClassRegistered → KILLED
2. isClassRegistered : replaced boolean return with false for org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::isClassRegistered → KILLED
    return Arrays.stream(registry.getBeanDefinitionNames())
66 1 1. lambda$isClassRegistered$0 : replaced return value with "" for org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::lambda$isClassRegistered$0 → KILLED
        .map(name -> registry.getBeanDefinition(name).getBeanClassName())
67
        .anyMatch(beanClassName::equals);
68
  }
69
70
  private Optional<String> findBeanClassNamePITEquivalent(final BeanDefinition candidate) {
71
    return Optional.ofNullable(candidate.getBeanClassName());
72
  }
73
}

Mutations

31

1.1
Location : postProcessBeanDefinitionRegistry
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_notRegisterAnyBean_whenAutoConfigurationPackagesNotPresent()]
negated conditional → KILLED

36

1.1
Location : postProcessBeanDefinitionRegistry
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerCommandHandlerBean_whenFoundInAutoConfigurationPackage()]
removed call to org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::registerHandlers → KILLED

37

1.1
Location : postProcessBeanDefinitionRegistry
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerQueryHandlerBean_whenFoundInAutoConfigurationPackage()]
removed call to org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::registerHandlers → KILLED

45

1.1
Location : registerHandlers
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerCommandHandlerBean_whenFoundInAutoConfigurationPackage()]
removed call to org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider::addIncludeFilter → KILLED

47

1.1
Location : lambda$registerHandlers$0
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerCommandHandlerBean_whenFoundInAutoConfigurationPackage()]
replaced return value with Stream.empty for org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::lambda$registerHandlers$0 → KILLED

48

1.1
Location : lambda$registerHandlers$1
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerCommandHandlerBean_whenFoundInAutoConfigurationPackage()]
removed call to org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::registerIfAbsent → KILLED

2.2
Location : registerHandlers
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerCommandHandlerBean_whenFoundInAutoConfigurationPackage()]
removed call to java/util/stream/Stream::forEach → KILLED

54

1.1
Location : registerIfAbsent
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerCommandHandlerBean_whenFoundInAutoConfigurationPackage()]
removed call to java/util/Optional::ifPresent → KILLED

56

1.1
Location : lambda$registerIfAbsent$0
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerCommandHandlerBean_whenFoundInAutoConfigurationPackage()]
negated conditional → KILLED

58

1.1
Location : lambda$registerIfAbsent$0
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerCommandHandlerBean_whenFoundInAutoConfigurationPackage()]
removed call to org/springframework/beans/factory/support/BeanDefinitionRegistry::registerBeanDefinition → KILLED

65

1.1
Location : isClassRegistered
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_registerCommandHandlerBean_whenFoundInAutoConfigurationPackage()]
replaced boolean return with true for org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::isClassRegistered → KILLED

2.2
Location : isClassRegistered
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_notRegisterHandlerBean_whenAlreadyRegisteredUnderCustomName()]
replaced boolean return with false for org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::isClassRegistered → KILLED

66

1.1
Location : lambda$isClassRegistered$0
Killed by : org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest.[engine:junit-jupiter]/[class:org.sanmibuh.ddd.infrastructure.CQRSHandlerRegistrarTest]/[method:should_notRegisterHandlerBean_whenAlreadyRegisteredUnderCustomName()]
replaced return value with "" for org/sanmibuh/ddd/infrastructure/CQRSHandlerRegistrar::lambda$isClassRegistered$0 → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support