Skip to content

Commit 5da2dcf

Browse files
Implemented first tests for version
1 parent 0e4ecc7 commit 5da2dcf

4 files changed

Lines changed: 201 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 java.util.List;
19+
20+
import jakarta.persistence.OptimisticLockException;
21+
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Test;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.test.context.ContextConfiguration;
26+
27+
import software.xdev.spring.data.eclipse.store.helper.TestData;
28+
import software.xdev.spring.data.eclipse.store.helper.TestUtil;
29+
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
30+
31+
32+
@IsolatedTestAnnotations
33+
@ContextConfiguration(classes = {VersionTestConfiguration.class})
34+
class VersionTest
35+
{
36+
private final VersionTestConfiguration configuration;
37+
38+
@Autowired
39+
public VersionTest(final VersionTestConfiguration configuration)
40+
{
41+
this.configuration = configuration;
42+
}
43+
44+
@Test
45+
void simpleSave(@Autowired final VersionedEntityWithIntegerRepository repository)
46+
{
47+
final VersionedEntityWithInteger entity = new VersionedEntityWithInteger(TestData.FIRST_NAME);
48+
repository.save(entity);
49+
TestUtil.doBeforeAndAfterRestartOfDatastore(
50+
this.configuration,
51+
() -> {
52+
final List<VersionedEntityWithInteger> allEntities = repository.findAll();
53+
Assertions.assertEquals(1, allEntities.size());
54+
Assertions.assertEquals(1, allEntities.get(0).getVersion());
55+
}
56+
);
57+
}
58+
59+
@Test
60+
void doubleSave(@Autowired final VersionedEntityWithIntegerRepository repository)
61+
{
62+
final VersionedEntityWithInteger entity = new VersionedEntityWithInteger(TestData.FIRST_NAME);
63+
repository.save(entity);
64+
repository.save(repository.findAll().get(0));
65+
TestUtil.doBeforeAndAfterRestartOfDatastore(
66+
this.configuration,
67+
() -> {
68+
final List<VersionedEntityWithInteger> allEntities = repository.findAll();
69+
Assertions.assertEquals(1, allEntities.size());
70+
Assertions.assertEquals(2, allEntities.get(0).getVersion());
71+
}
72+
);
73+
}
74+
75+
@Test
76+
void saveButLocked(@Autowired final VersionedEntityWithIntegerRepository repository)
77+
{
78+
final VersionedEntityWithInteger entity = new VersionedEntityWithInteger(TestData.FIRST_NAME);
79+
repository.save(entity);
80+
81+
final VersionedEntityWithInteger firstLoadedEntry = repository.findAll().get(0);
82+
final VersionedEntityWithInteger secondLoadedEntry = repository.findAll().get(0);
83+
84+
firstLoadedEntry.setName(TestData.FIRST_NAME_ALTERNATIVE);
85+
repository.save(firstLoadedEntry);
86+
87+
Assertions.assertThrows(OptimisticLockException.class, () -> repository.save(secondLoadedEntry));
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 org.eclipse.store.integrations.spring.boot.types.configuration.EclipseStoreProperties;
19+
import org.eclipse.store.integrations.spring.boot.types.factories.EmbeddedStorageFoundationFactory;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.context.annotation.Configuration;
22+
23+
import software.xdev.spring.data.eclipse.store.integration.TestConfiguration;
24+
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
25+
26+
27+
@Configuration
28+
@EnableEclipseStoreRepositories
29+
public class VersionTestConfiguration extends TestConfiguration
30+
{
31+
@Autowired
32+
protected VersionTestConfiguration(
33+
final EclipseStoreProperties defaultEclipseStoreProperties,
34+
final EmbeddedStorageFoundationFactory defaultEclipseStoreProvider)
35+
{
36+
super(defaultEclipseStoreProperties, defaultEclipseStoreProvider);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 VersionedEntityWithInteger
22+
{
23+
@Version
24+
private int version;
25+
private String name;
26+
27+
public VersionedEntityWithInteger(final String name)
28+
{
29+
this.name = name;
30+
}
31+
32+
public int getVersion()
33+
{
34+
return this.version;
35+
}
36+
37+
public String getName()
38+
{
39+
return this.name;
40+
}
41+
42+
public void setVersion(final int version)
43+
{
44+
this.version = version;
45+
}
46+
47+
public void setName(final String name)
48+
{
49+
this.name = name;
50+
}
51+
}
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 VersionedEntityWithIntegerRepository extends EclipseStoreRepository<VersionedEntityWithInteger, Void>
22+
{
23+
}

0 commit comments

Comments
 (0)