Skip to content

Commit 312e80a

Browse files
Expanded @Version tests
1 parent 1156469 commit 312e80a

18 files changed

Lines changed: 527 additions & 24 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.10
2+
3+
* Optimistic locking with @Version now possible
4+
15
# 1.0.9
26

37
* Inherited entities with repositories are now realized by reading (finding coherent repositories) and not by writing

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/copier/version/incrementer/IntegerVersionIncrementer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public Integer increment(final Integer original)
2323
{
2424
if(original == null || original == Integer.MAX_VALUE)
2525
{
26-
return 0;
26+
return 1;
2727
}
2828
return original + 1;
2929
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/copier/version/incrementer/LongVersionIncrementer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public Long increment(final Long original)
2323
{
2424
if(original == null || original == Long.MAX_VALUE)
2525
{
26-
return 0L;
26+
return 1L;
2727
}
2828
return original + 1;
2929
}

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/version/VersionTest.java

Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,86 @@
1616
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.version;
1717

1818
import java.util.List;
19+
import java.util.function.Function;
20+
import java.util.stream.Stream;
1921

2022
import jakarta.persistence.OptimisticLockException;
2123

2224
import org.junit.jupiter.api.Assertions;
2325
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.Arguments;
28+
import org.junit.jupiter.params.provider.MethodSource;
2429
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.context.ApplicationContext;
2531
import org.springframework.test.context.ContextConfiguration;
2632

2733
import software.xdev.spring.data.eclipse.store.helper.TestData;
2834
import software.xdev.spring.data.eclipse.store.helper.TestUtil;
2935
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
36+
import software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreRepository;
3037

3138

3239
@IsolatedTestAnnotations
3340
@ContextConfiguration(classes = {VersionTestConfiguration.class})
3441
class VersionTest
3542
{
43+
public static Stream<Arguments> generateData()
44+
{
45+
return List.of(
46+
new SingleTestDataset<>(
47+
name -> new VersionedEntityWithInteger(name),
48+
context -> context.getBean(VersionedEntityWithIntegerRepository.class),
49+
1,
50+
2
51+
).toArguments(),
52+
new SingleTestDataset<>(
53+
name -> new VersionedEntityWithLong(name),
54+
context -> context.getBean(VersionedEntityWithLongRepository.class),
55+
1L,
56+
2L
57+
).toArguments(),
58+
new SingleTestDataset<>(
59+
name -> new VersionedEntityWithPrimitiveInteger(name),
60+
context -> context.getBean(VersionedEntityWithPrimitiveIntegerRepository.class),
61+
1,
62+
2
63+
).toArguments(),
64+
new SingleTestDataset<>(
65+
name -> new VersionedEntityWithPrimitiveLong(name),
66+
context -> context.getBean(VersionedEntityWithPrimitiveLongRepository.class),
67+
1L,
68+
2L
69+
).toArguments(),
70+
new SingleTestDataset<>(
71+
name -> new VersionedEntityWithString(name),
72+
context -> context.getBean(VersionedEntityWithStringRepository.class),
73+
null,
74+
null
75+
).toArguments(),
76+
new SingleTestDataset<>(
77+
name -> new VersionedEntityWithUuid(name),
78+
context -> context.getBean(VersionedEntityWithUuidRepository.class),
79+
null,
80+
null
81+
).toArguments()
82+
).stream();
83+
}
84+
85+
private record SingleTestDataset<T extends VersionedEntity>(
86+
Function<String, T> enitityGenerator,
87+
Function<ApplicationContext, EclipseStoreRepository<T, Void>> repositoryGenerator,
88+
Object firstVersion,
89+
Object secondVersion
90+
)
91+
{
92+
public Arguments toArguments()
93+
{
94+
return Arguments.of(this);
95+
}
96+
}
97+
98+
3699
private final VersionTestConfiguration configuration;
37100

38101
@Autowired
@@ -41,45 +104,68 @@ public VersionTest(final VersionTestConfiguration configuration)
41104
this.configuration = configuration;
42105
}
43106

44-
@Test
45-
void simpleSave(@Autowired final VersionedEntityWithIntegerRepository repository)
107+
@ParameterizedTest
108+
@MethodSource("generateData")
109+
<T extends VersionedEntity> void simpleSave(
110+
final SingleTestDataset<T> data, @Autowired final ApplicationContext context)
46111
{
47-
final VersionedEntityWithInteger entity = new VersionedEntityWithInteger(TestData.FIRST_NAME);
112+
final EclipseStoreRepository<T, Void> repository = data.repositoryGenerator.apply(context);
113+
final T entity = data.enitityGenerator.apply(TestData.FIRST_NAME);
48114
repository.save(entity);
49115
TestUtil.doBeforeAndAfterRestartOfDatastore(
50116
this.configuration,
51117
() -> {
52-
final List<VersionedEntityWithInteger> allEntities = repository.findAll();
118+
final List<T> allEntities = repository.findAll();
53119
Assertions.assertEquals(1, allEntities.size());
54-
Assertions.assertEquals(1, allEntities.get(0).getVersion());
120+
if(data.firstVersion != null)
121+
{
122+
Assertions.assertEquals(data.firstVersion, allEntities.get(0).getVersion());
123+
}
124+
else
125+
{
126+
Assertions.assertNotNull(allEntities.get(0).getVersion());
127+
}
55128
}
56129
);
57130
}
58131

59-
@Test
60-
void doubleSave(@Autowired final VersionedEntityWithIntegerRepository repository)
132+
@ParameterizedTest
133+
@MethodSource("generateData")
134+
<T extends VersionedEntity> void doubleSave(
135+
final SingleTestDataset<T> data, @Autowired final ApplicationContext context)
61136
{
62-
final VersionedEntityWithInteger entity = new VersionedEntityWithInteger(TestData.FIRST_NAME);
137+
final EclipseStoreRepository<T, Void> repository = data.repositoryGenerator.apply(context);
138+
final T entity = data.enitityGenerator.apply(TestData.FIRST_NAME);
63139
repository.save(entity);
64140
repository.save(repository.findAll().get(0));
65141
TestUtil.doBeforeAndAfterRestartOfDatastore(
66142
this.configuration,
67143
() -> {
68-
final List<VersionedEntityWithInteger> allEntities = repository.findAll();
144+
final List<T> allEntities = repository.findAll();
69145
Assertions.assertEquals(1, allEntities.size());
70-
Assertions.assertEquals(2, allEntities.get(0).getVersion());
146+
if(data.secondVersion != null)
147+
{
148+
Assertions.assertEquals(data.secondVersion, allEntities.get(0).getVersion());
149+
}
150+
else
151+
{
152+
Assertions.assertNotNull(allEntities.get(0).getVersion());
153+
}
71154
}
72155
);
73156
}
74157

75-
@Test
76-
void saveButLocked(@Autowired final VersionedEntityWithIntegerRepository repository)
158+
@ParameterizedTest
159+
@MethodSource("generateData")
160+
<T extends VersionedEntity> void saveButLocked(
161+
final SingleTestDataset<T> data, @Autowired final ApplicationContext context)
77162
{
78-
final VersionedEntityWithInteger entity = new VersionedEntityWithInteger(TestData.FIRST_NAME);
163+
final EclipseStoreRepository<T, Void> repository = data.repositoryGenerator.apply(context);
164+
final T entity = data.enitityGenerator.apply(TestData.FIRST_NAME);
79165
repository.save(entity);
80166

81-
final VersionedEntityWithInteger firstLoadedEntry = repository.findAll().get(0);
82-
final VersionedEntityWithInteger secondLoadedEntry = repository.findAll().get(0);
167+
final T firstLoadedEntry = repository.findAll().get(0);
168+
final T secondLoadedEntry = repository.findAll().get(0);
83169

84170
firstLoadedEntry.setName(TestData.FIRST_NAME_ALTERNATIVE);
85171
repository.save(firstLoadedEntry);

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/version/VersionedChildEntityWithLong.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import jakarta.persistence.Version;
1919

2020

21-
public class VersionedChildEntityWithLong
21+
public class VersionedChildEntityWithLong implements VersionedEntity<Long>
2222
{
2323
@Version
2424
private long version;
@@ -29,7 +29,8 @@ public VersionedChildEntityWithLong(final String name)
2929
this.name = name;
3030
}
3131

32-
public long getVersion()
32+
@Override
33+
public Long getVersion()
3334
{
3435
return this.version;
3536
}
@@ -44,6 +45,7 @@ public void setVersion(final int version)
4445
this.version = version;
4546
}
4647

48+
@Override
4749
public void setName(final String name)
4850
{
4951
this.name = name;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.version;
17+
18+
public interface VersionedEntity<T>
19+
{
20+
T getVersion();
21+
22+
void setName(String name);
23+
}

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/version/VersionedEntityWithInteger.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
import jakarta.persistence.Version;
1919

2020

21-
public class VersionedEntityWithInteger
21+
public class VersionedEntityWithInteger implements VersionedEntity<Integer>
2222
{
2323
@Version
24-
private int version;
24+
private Integer version;
2525
private String name;
2626

2727
public VersionedEntityWithInteger(final String name)
2828
{
2929
this.name = name;
3030
}
3131

32-
public int getVersion()
32+
@Override
33+
public Integer getVersion()
3334
{
3435
return this.version;
3536
}
@@ -44,6 +45,7 @@ public void setVersion(final int version)
4445
this.version = version;
4546
}
4647

48+
@Override
4749
public void setName(final String name)
4850
{
4951
this.name = name;

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/version/VersionedEntityWithIntegerAndVersionedChild.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import jakarta.persistence.Version;
1919

2020

21-
public class VersionedEntityWithIntegerAndVersionedChild
21+
public class VersionedEntityWithIntegerAndVersionedChild implements VersionedEntity<Integer>
2222
{
2323
@Version
2424
private int version;
@@ -31,7 +31,8 @@ public VersionedEntityWithIntegerAndVersionedChild(final String name, final Vers
3131
this.child = child;
3232
}
3333

34-
public int getVersion()
34+
@Override
35+
public Integer getVersion()
3536
{
3637
return this.version;
3738
}
@@ -46,6 +47,7 @@ public void setVersion(final int version)
4647
this.version = version;
4748
}
4849

50+
@Override
4951
public void setName(final String name)
5052
{
5153
this.name = name;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.version;
17+
18+
import jakarta.persistence.Version;
19+
20+
21+
public class VersionedEntityWithLong implements VersionedEntity<Long>
22+
{
23+
@Version
24+
private Long version;
25+
private String name;
26+
27+
public VersionedEntityWithLong(final String name)
28+
{
29+
this.name = name;
30+
}
31+
32+
@Override
33+
public Long getVersion()
34+
{
35+
return this.version;
36+
}
37+
38+
public String getName()
39+
{
40+
return this.name;
41+
}
42+
43+
public void setVersion(final Long version)
44+
{
45+
this.version = version;
46+
}
47+
48+
@Override
49+
public void setName(final String name)
50+
{
51+
this.name = name;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.version;
17+
18+
import software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreRepository;
19+
20+
21+
public interface VersionedEntityWithLongRepository extends EclipseStoreRepository<VersionedEntityWithLong, Void>
22+
{
23+
}

0 commit comments

Comments
 (0)