Skip to content

Commit a351a66

Browse files
committed
Polishing.
Using records in tests. Removing "public" modifier in tests. See #1924
1 parent 305f675 commit a351a66

File tree

1 file changed

+28
-110
lines changed

1 file changed

+28
-110
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java

Lines changed: 28 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import java.util.List;
2021
import java.util.Objects;
2122
import java.util.concurrent.atomic.AtomicLong;
2223

@@ -35,6 +36,7 @@
3536
import org.springframework.data.relational.core.mapping.NamingStrategy;
3637
import org.springframework.data.relational.core.mapping.event.BeforeConvertCallback;
3738
import org.springframework.data.repository.CrudRepository;
39+
import org.springframework.data.repository.ListCrudRepository;
3840
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
3941

4042
/**
@@ -44,29 +46,29 @@
4446
* @author Greg Turnquist
4547
*/
4648
@IntegrationTest
47-
public class JdbcRepositoryIdGenerationIntegrationTests {
49+
class JdbcRepositoryIdGenerationIntegrationTests {
4850

4951
@Autowired NamedParameterJdbcTemplate template;
50-
@Autowired ReadOnlyIdEntityRepository readOnlyIdrepository;
52+
@Autowired ReadOnlyIdEntityRepository readOnlyIdRepository;
5153
@Autowired PrimitiveIdEntityRepository primitiveIdRepository;
5254
@Autowired ImmutableWithManualIdEntityRepository immutableWithManualIdEntityRepository;
5355

5456
@Test // DATAJDBC-98
55-
public void idWithoutSetterGetsSet() {
57+
void idWithoutSetterGetsSet() {
5658

57-
ReadOnlyIdEntity entity = readOnlyIdrepository.save(new ReadOnlyIdEntity(null, "Entity Name"));
59+
ReadOnlyIdEntity entity = readOnlyIdRepository.save(new ReadOnlyIdEntity(null, "Entity Name"));
5860

59-
assertThat(entity.getId()).isNotNull();
61+
assertThat(entity.id()).isNotNull();
6062

61-
assertThat(readOnlyIdrepository.findById(entity.getId())).hasValueSatisfying(it -> {
63+
assertThat(readOnlyIdRepository.findById(entity.id())).hasValueSatisfying(it -> {
6264

63-
assertThat(it.getId()).isEqualTo(entity.getId());
64-
assertThat(it.getName()).isEqualTo(entity.getName());
65+
assertThat(it.id()).isEqualTo(entity.id());
66+
assertThat(it.name()).isEqualTo(entity.name());
6567
});
6668
}
6769

6870
@Test // DATAJDBC-98
69-
public void primitiveIdGetsSet() {
71+
void primitiveIdGetsSet() {
7072

7173
PrimitiveIdEntity entity = new PrimitiveIdEntity();
7274
entity.setName("Entity Name");
@@ -83,68 +85,23 @@ public void primitiveIdGetsSet() {
8385
}
8486

8587
@Test // DATAJDBC-393
86-
public void manuallyGeneratedId() {
88+
void manuallyGeneratedId() {
8789

8890
ImmutableWithManualIdEntity entity = new ImmutableWithManualIdEntity(null, "immutable");
8991
ImmutableWithManualIdEntity saved = immutableWithManualIdEntityRepository.save(entity);
9092

91-
assertThat(saved.getId()).isNotNull();
93+
assertThat(saved.id()).isNotNull();
9294

9395
assertThat(immutableWithManualIdEntityRepository.findAll()).hasSize(1);
9496
}
9597

96-
private interface PrimitiveIdEntityRepository extends CrudRepository<PrimitiveIdEntity, Long> {}
98+
private interface PrimitiveIdEntityRepository extends ListCrudRepository<PrimitiveIdEntity, Long> {}
9799

98-
public interface ReadOnlyIdEntityRepository extends CrudRepository<ReadOnlyIdEntity, Long> {}
100+
private interface ReadOnlyIdEntityRepository extends ListCrudRepository<ReadOnlyIdEntity, Long> {}
99101

100-
private interface ImmutableWithManualIdEntityRepository extends CrudRepository<ImmutableWithManualIdEntity, Long> {}
102+
private interface ImmutableWithManualIdEntityRepository extends ListCrudRepository<ImmutableWithManualIdEntity, Long> {}
101103

102-
static final class ReadOnlyIdEntity {
103-
104-
@Id private final Long id;
105-
private final String name;
106-
107-
public ReadOnlyIdEntity(Long id, String name) {
108-
this.id = id;
109-
this.name = name;
110-
}
111-
112-
public Long getId() {
113-
return this.id;
114-
}
115-
116-
public String getName() {
117-
return this.name;
118-
}
119-
120-
public boolean equals(final Object o) {
121-
if (o == this)
122-
return true;
123-
if (!(o instanceof final ReadOnlyIdEntity other))
124-
return false;
125-
final Object this$id = this.getId();
126-
final Object other$id = other.getId();
127-
if (!Objects.equals(this$id, other$id))
128-
return false;
129-
final Object this$name = this.getName();
130-
final Object other$name = other.getName();
131-
return Objects.equals(this$name, other$name);
132-
}
133-
134-
public int hashCode() {
135-
final int PRIME = 59;
136-
int result = 1;
137-
final Object $id = this.getId();
138-
result = result * PRIME + ($id == null ? 43 : $id.hashCode());
139-
final Object $name = this.getName();
140-
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
141-
return result;
142-
}
143-
144-
public String toString() {
145-
return "JdbcRepositoryIdGenerationIntegrationTests.ReadOnlyIdEntity(id=" + this.getId() + ", name="
146-
+ this.getName() + ")";
147-
}
104+
record ReadOnlyIdEntity(@Id Long id, String name) {
148105
}
149106

150107
static class PrimitiveIdEntity {
@@ -169,61 +126,22 @@ public void setName(String name) {
169126
}
170127
}
171128

172-
static final class ImmutableWithManualIdEntity {
173-
@Id private final Long id;
174-
private final String name;
129+
record ImmutableWithManualIdEntity(@Id Long id, String name) {
175130

176-
public ImmutableWithManualIdEntity(Long id, String name) {
177-
this.id = id;
178-
this.name = name;
179-
}
180-
181-
public Long getId() {
182-
return this.id;
183-
}
184-
185-
public String getName() {
186-
return this.name;
187-
}
131+
@Override
132+
public Long id() {
133+
return this.id;
134+
}
188135

189-
public boolean equals(final Object o) {
190-
if (o == this)
191-
return true;
192-
if (!(o instanceof final ImmutableWithManualIdEntity other))
193-
return false;
194-
final Object this$id = this.getId();
195-
final Object other$id = other.getId();
196-
if (!Objects.equals(this$id, other$id))
197-
return false;
198-
final Object this$name = this.getName();
199-
final Object other$name = other.getName();
200-
return Objects.equals(this$name, other$name);
201-
}
136+
public ImmutableWithManualIdEntity withId(Long id) {
137+
return this.id == id ? this : new ImmutableWithManualIdEntity(id, this.name);
138+
}
202139

203-
public int hashCode() {
204-
final int PRIME = 59;
205-
int result = 1;
206-
final Object $id = this.getId();
207-
result = result * PRIME + ($id == null ? 43 : $id.hashCode());
208-
final Object $name = this.getName();
209-
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
210-
return result;
140+
public ImmutableWithManualIdEntity withName(String name) {
141+
return this.name == name ? this : new ImmutableWithManualIdEntity(this.id, name);
142+
}
211143
}
212144

213-
public String toString() {
214-
return "JdbcRepositoryIdGenerationIntegrationTests.ImmutableWithManualIdEntity(id=" + this.getId() + ", name="
215-
+ this.getName() + ")";
216-
}
217-
218-
public ImmutableWithManualIdEntity withId(Long id) {
219-
return this.id == id ? this : new ImmutableWithManualIdEntity(id, this.name);
220-
}
221-
222-
public ImmutableWithManualIdEntity withName(String name) {
223-
return this.name == name ? this : new ImmutableWithManualIdEntity(this.id, name);
224-
}
225-
}
226-
227145
@Configuration
228146
@EnableJdbcRepositories(considerNestedRepositories = true,
229147
includeFilters = @ComponentScan.Filter(value = CrudRepository.class, type = FilterType.ASSIGNABLE_TYPE))

0 commit comments

Comments
 (0)