BaseJpaRepository.java

1
package com.reallifedeveloper.common.infrastructure.persistence;
2
3
import java.lang.reflect.Method;
4
import java.util.Optional;
5
6
import org.springframework.data.jpa.repository.Query;
7
8
/**
9
 * Base class for implementations of repositories that use JPA but that cannot use Spring Data, e.g., because they work with more than one
10
 * entity type.
11
 *
12
 * @author RealLifeDeveloper
13
 */
14
public class BaseJpaRepository {
15
16
    /**
17
     * Creates a new {@code BaseJpaRepository}, intended to be used by sub-classes.
18
     */
19
    protected BaseJpaRepository() {
20
        // The only constructor is protected, to disallow direct instantiation.
21
    }
22
23
    /**
24
     * Gives the value of the {@code @Query} annotation on the given method. The annotation can be on the method in the class, or on the
25
     * method in an implemented interface.
26
     *
27
     * @param methodName     the name of the method with the {@code @Query} annotation
28
     * @param parameterTypes the classes of the method parameters
29
     *
30
     * @return the query string, i.e., the value of the {@code @Query} annotation, or an empty Optional if no such annotation can be found
31
     */
32
    protected Optional<String> getQueryString(String methodName, Class<?>... parameterTypes) {
33
        try {
34 1 1. getQueryString : replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/BaseJpaRepository::getQueryString → KILLED
            return findQueryAnnotation(methodName, parameterTypes).map(Query::value);
35
        } catch (NoSuchMethodException e) {
36
            throw new IllegalStateException("Unexpected error, this should never occur", e);
37
        }
38
    }
39
40
    @SuppressWarnings("PMD.EmptyCatchBlock")
41
    private Optional<Query> findQueryAnnotation(String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
42
        Method method = getClass().getMethod(methodName, parameterTypes);
43
        Query query = method.getAnnotation(Query.class);
44 1 1. findQueryAnnotation : negated conditional → KILLED
        if (query == null) {
45
            for (Class<?> c : getClass().getInterfaces()) {
46
                try {
47
                    method = c.getMethod(methodName, parameterTypes);
48
                    query = method.getAnnotation(Query.class);
49 1 1. findQueryAnnotation : negated conditional → SURVIVED
                    if (query != null) {
50
                        break;
51
                    }
52
                } catch (NoSuchMethodException e) {
53
                    // Ignore, try next interface (if there is one)
54
                }
55
            }
56
        }
57 1 1. findQueryAnnotation : replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/BaseJpaRepository::findQueryAnnotation → KILLED
        return Optional.ofNullable(query);
58
    }
59
}

Mutations

34

1.1
Location : getQueryString
Killed by : com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest]/[method:baz()]
replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/BaseJpaRepository::getQueryString → KILLED

44

1.1
Location : findQueryAnnotation
Killed by : com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest]/[method:baz()]
negated conditional → KILLED

49

1.1
Location : findQueryAnnotation
Killed by : none
negated conditional → SURVIVED
Covering tests

57

1.1
Location : findQueryAnnotation
Killed by : com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest]/[method:baz()]
replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/BaseJpaRepository::findQueryAnnotation → KILLED

Active mutators

Tests examined


Report generated by PIT 1.20.0