feat(renderer): Add getOrAcquire method and enhance get method in RendererWorkingSet implementation

This commit is contained in:
extio-de
2025-12-25 12:49:53 +01:00
parent 478dbc3f3c
commit 5d80ba3f9e
4 changed files with 70 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.List;
@@ -315,7 +316,50 @@ public class RendererWorkingSetImplTest {
assertTrue(filteredLive.containsAll(List.of(boA1, boA2)));
assertFalse(filteredLive.contains(boB1));
}
@Test
void getWithTypeReturnsTypedInstanceAndNullForUnknown() {
final var boA = new TestBoA();
boA.setId("bo1");
this.workingSet.put(this.moduleA.getId(), boA);
final var retrieved = this.workingSet.get(this.moduleA.getId(), "bo1", TestBoA.class);
assertSame(boA, retrieved);
final var notFound = this.workingSet.get(this.moduleA.getId(), "nonexistent", TestBoA.class);
assertTrue(notFound == null);
}
@Test
void getWithTypeThrowsClassCastForWrongType() {
final var boA = new TestBoA();
boA.setId("bo1");
this.workingSet.put(this.moduleA.getId(), boA);
assertThrows(ClassCastException.class, () -> this.workingSet.get(this.moduleA.getId(), "bo1", TestBoB.class));
}
@Test
void getOrAcquireReturnsUncommittedWhenPresent() {
final var boA = new TestBoA();
boA.setId("bo1");
this.workingSet.put(this.moduleA.getId(), boA);
final var retrieved = this.workingSet.getOrAcquire(this.moduleA.getId(), "bo1", TestBoA.class);
assertSame(boA, retrieved);
}
@Test
void getOrAcquireAcquiresFromPoolWhenMissing() {
final var pooledBo = new TestBoA();
pooledBo.setId("pooled");
when(this.renderingBoPool.acquire("pooled", TestBoA.class)).thenReturn(pooledBo);
final var retrieved = this.workingSet.getOrAcquire(this.moduleA.getId(), "pooled", TestBoA.class);
assertSame(pooledBo, retrieved);
verify(this.renderingBoPool, times(1)).acquire("pooled", TestBoA.class);
}
private static abstract class TestBoBase implements RenderingBo {
private final AtomicInteger closeStaticCalls = new AtomicInteger();