Skip to content

Commit 1963aa7

Browse files
Test and fix for cold initialisation with ids
1 parent 76ec03f commit 1963aa7

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

  • spring-data-eclipse-store/src

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/EclipseStoreStorage.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,15 @@ public <T> IdSetter<T> ensureIdSetter(final Class<T> domainClass)
280280
final String entityName = this.getEntityName(domainClass);
281281
final Consumer<Object> idSetter = id ->
282282
{
283+
this.ensureEntitiesInRoot();
283284
this.root.getLastIds().put(entityName, id);
284285
this.storageManager.store(this.root.getLastIds());
285286
};
286-
final Supplier<Object> idGetter = () -> this.root.getLastIds().get(entityName);
287+
final Supplier<Object> idGetter = () ->
288+
{
289+
this.ensureEntitiesInRoot();
290+
return this.root.getLastIds().get(entityName);
291+
};
287292
return IdSetter.createIdSetter(domainClass, idSetter, idGetter);
288293
}
289294
);

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/tests/IdTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*/
1616
package software.xdev.spring.data.eclipse.store.integration.tests;
1717

18+
import static software.xdev.spring.data.eclipse.store.helper.TestUtil.restartDatastore;
19+
1820
import java.util.List;
1921
import java.util.Optional;
2022

2123
import org.junit.jupiter.api.Assertions;
2224
import org.junit.jupiter.api.Test;
2325
import org.springframework.beans.factory.annotation.Autowired;
2426

25-
import org.springframework.beans.factory.annotation.Autowired;
2627
import software.xdev.spring.data.eclipse.store.helper.TestData;
2728
import software.xdev.spring.data.eclipse.store.helper.TestUtil;
2829
import software.xdev.spring.data.eclipse.store.integration.DefaultTestAnnotations;
@@ -65,6 +66,27 @@ void testCreateSingleWithAutoIdInteger(@Autowired final CustomerWithIdIntegerRep
6566
);
6667
}
6768

69+
/**
70+
* In other tests {@link EclipseStoreStorage#clearData} is called. Here the datastore is restarted again to ensure
71+
* no previous method is called before the test.
72+
*/
73+
@Test
74+
void testSaveSingleWithoutAnyPreviousCall(@Autowired final CustomerWithIdIntegerRepository customerRepository)
75+
{
76+
restartDatastore(this.storage);
77+
final CustomerWithIdInteger customer1 = new CustomerWithIdInteger(TestData.FIRST_NAME, TestData.LAST_NAME);
78+
customerRepository.save(customer1);
79+
80+
TestUtil.doBeforeAndAfterRestartOfDatastore(
81+
this.storage,
82+
() -> {
83+
final Optional<CustomerWithIdInteger> loadedCustomer = customerRepository.findById(0);
84+
Assertions.assertTrue(loadedCustomer.isPresent());
85+
Assertions.assertEquals(customer1, loadedCustomer.get());
86+
}
87+
);
88+
}
89+
6890
@Test
6991
void testCreateSingleWithAutoIdIntegerWorkingCopyIdSet(
7092
@Autowired final CustomerWithIdIntegerRepository customerRepository)

0 commit comments

Comments
 (0)