InMemoryJpaRepository.java

1
package com.reallifedeveloper.tools.test.database.inmemory;
2
3
import java.lang.reflect.Field;
4
import java.lang.reflect.Method;
5
import java.util.List;
6
import java.util.Optional;
7
import java.util.function.Function;
8
9
import org.springframework.data.domain.Example;
10
import org.springframework.data.jpa.repository.JpaRepository;
11
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;
12
13
import jakarta.persistence.EmbeddedId;
14
import jakarta.persistence.EntityNotFoundException;
15
import jakarta.persistence.Id;
16
import jakarta.persistence.IdClass;
17
18
/**
19
 * An implementation of the Spring Data JPA {@code JpaRepository} interface that holds entities in a map. Useful for testing.
20
 *
21
 * @param <T>  the type of the entities handled by this repository
22
 * @param <ID> the type of the entities' primary keys
23
 *
24
 * @author RealLifeDeveloper
25
 */
26
public class InMemoryJpaRepository<T, ID extends Comparable<ID>> extends AbstractInMemoryCrudRepository<T, ID>
27
        implements JpaRepository<T, ID> {
28
29
    /**
30
     * Creates a new {@code InMemoryJpaRepository} with no primary key generator. If an entity with a {@code null} primary key is saved, an
31
     * exception is thrown.
32
     */
33
    public InMemoryJpaRepository() {
34
        super();
35
    }
36
37
    /**
38
     * Creates a new {@code InMemoryJpaRepository} with the provided primary key generator. If an entity with a {@code null} primary key is
39
     * saved, the generator is used to create a new primary key that is stored in the entity before saving.
40
     *
41
     * @param primaryKeyGenerator the primary key generator to use
42
     */
43
    public InMemoryJpaRepository(PrimaryKeyGenerator<ID> primaryKeyGenerator) {
44
        super(primaryKeyGenerator);
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    @Override
51
    public void deleteAllInBatch() {
52 1 1. deleteAllInBatch : removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::deleteAll → KILLED
        deleteAll();
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    @Override
59
    public void deleteAllInBatch(Iterable<T> entities) {
60 1 1. deleteAllInBatch : removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::deleteAll → KILLED
        deleteAll(entities);
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    @Override
67
    public void deleteAllByIdInBatch(Iterable<ID> ids) {
68 1 1. deleteAllByIdInBatch : removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::deleteAllById → KILLED
        deleteAllById(ids);
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    @Override
75
    public void flush() {
76
        // Do nothing.
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    @Override
83
    public <S extends T> S saveAndFlush(S entity) {
84
        S savedEntity = save(entity);
85 1 1. saveAndFlush : removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::flush → SURVIVED
        flush();
86 1 1. saveAndFlush : replaced return value with null for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::saveAndFlush → SURVIVED
        return savedEntity;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    @Override
93
    public <S extends T> List<S> saveAllAndFlush(Iterable<S> entities) {
94
        List<S> savedEntities = saveAll(entities);
95 1 1. saveAllAndFlush : removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::flush → SURVIVED
        flush();
96 1 1. saveAllAndFlush : replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::saveAllAndFlush → SURVIVED
        return savedEntities;
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    @Override
103
    @SuppressWarnings("deprecation")
104
    public T getById(ID id) {
105
        Optional<T> optionalElement = findById(id);
106 2 1. getById : replaced return value with null for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::getById → RUN_ERROR
2. lambda$getById$0 : replaced return value with null for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::lambda$getById$0 → KILLED
        return optionalElement.orElseThrow(() -> new EntityNotFoundException("Entity with ID " + id + " not found"));
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    @Override
113
    @SuppressWarnings("deprecation")
114
    public T getOne(ID id) {
115 1 1. getOne : replaced return value with null for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::getOne → KILLED
        return getById(id);
116
    }
117
118
    @Override
119
    protected boolean isIdField(Field field) {
120 3 1. isIdField : negated conditional → KILLED
2. isIdField : replaced boolean return with true for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::isIdField → KILLED
3. isIdField : negated conditional → KILLED
        return field.getAnnotation(Id.class) != null || field.getAnnotation(EmbeddedId.class) != null;
121
    }
122
123
    @Override
124
    protected boolean isIdMethod(Method method) {
125 3 1. isIdMethod : replaced boolean return with true for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::isIdMethod → KILLED
2. isIdMethod : negated conditional → KILLED
3. isIdMethod : negated conditional → KILLED
        return method.getAnnotation(Id.class) != null || method.getAnnotation(EmbeddedId.class) != null;
126
    }
127
128
    @Override
129
    @SuppressWarnings("unchecked")
130
    protected Optional<Class<ID>> getCompositeIdClass(Object entity) {
131
        IdClass idClass = entity.getClass().getAnnotation(IdClass.class);
132 1 1. getCompositeIdClass : negated conditional → KILLED
        if (idClass == null) {
133
            return Optional.empty();
134
        } else {
135 1 1. getCompositeIdClass : replaced return value with Optional.empty for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::getCompositeIdClass → KILLED
            return Optional.ofNullable((Class<ID>) idClass.value());
136
        }
137
    }
138
139
    @Override
140
    public <S extends T, R> R findBy(Example<S> example, Function<FetchableFluentQuery<S>, R> queryFunction) {
141
        throw new UnsupportedOperationException("Unimplemented method 'findBy'");
142
    }
143
144
    @Override
145
    public T getReferenceById(ID id) {
146
        throw new UnsupportedOperationException("Unimplemented method 'getReferenceById'");
147
    }
148
149
}

Mutations

52

1.1
Location : deleteAllInBatch
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:deleteAllInBatch()]
removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::deleteAll → KILLED

60

1.1
Location : deleteAllInBatch
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:deleteNullEntitiesInBatch()]
removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::deleteAll → KILLED

68

1.1
Location : deleteAllByIdInBatch
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:deleteAllByIdInBatch()]
removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::deleteAllById → KILLED

85

1.1
Location : saveAndFlush
Killed by : none
removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::flush → SURVIVED
Covering tests

86

1.1
Location : saveAndFlush
Killed by : none
replaced return value with null for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::saveAndFlush → SURVIVED
Covering tests

95

1.1
Location : saveAllAndFlush
Killed by : none
removed call to com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::flush → SURVIVED
Covering tests

96

1.1
Location : saveAllAndFlush
Killed by : none
replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::saveAllAndFlush → SURVIVED
Covering tests

106

1.1
Location : getById
Killed by : none
replaced return value with null for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::getById → RUN_ERROR

2.2
Location : lambda$getById$0
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:getByIdEmpty()]
replaced return value with null for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::lambda$getById$0 → KILLED

115

1.1
Location : getOne
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:getOne()]
replaced return value with null for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::getOne → KILLED

120

1.1
Location : isIdField
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:deleteEntityWithNullId()]
negated conditional → KILLED

2.2
Location : isIdField
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:deleteEntityWithNullId()]
replaced boolean return with true for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::isIdField → KILLED

3.3
Location : isIdField
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:saveNullPrimaryKeyWithNoGenerator()]
negated conditional → KILLED

125

1.1
Location : isIdMethod
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:entityWithNoIdAnnotation()]
replaced boolean return with true for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::isIdMethod → KILLED

2.2
Location : isIdMethod
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:entityWithNoIdAnnotation()]
negated conditional → KILLED

3.3
Location : isIdMethod
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:entityWithNoIdAnnotation()]
negated conditional → KILLED

132

1.1
Location : getCompositeIdClass
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:deleteEntityWithNullId()]
negated conditional → KILLED

135

1.1
Location : getCompositeIdClass
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:idClassSaveWithPrimaryKeys()]
replaced return value with Optional.empty for com/reallifedeveloper/tools/test/database/inmemory/InMemoryJpaRepository::getCompositeIdClass → KILLED

Active mutators

Tests examined


Report generated by PIT 1.20.2