Finish factory implementation

This commit is contained in:
天クマ 2026-05-23 21:41:17 -03:00
commit 0b753d6a4e
15 changed files with 75 additions and 52 deletions

View file

@ -1,11 +1,12 @@
package org.adrianvictor.lib;
import org.adrianvictor.lib.reflection.VersionMatcher;
import org.adrianvictor.lib.versioning.DefaultVersionedServiceFactory;
import org.adrianvictor.lib.versioning.VersionMatcher;
import org.adrianvictor.lib.versioning.VersionedServiceFactory;
import org.adrianvictor.lib.versioning.VersionedServiceRegistrar;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@ -19,14 +20,15 @@ class DummyRegistrarB1_7_3 implements VersionedServiceRegistrar {
@Override
public void register(VersionedServiceFactory factory) {
factory.register(TestService.class, "b1_7_3", TestServiceB1_7_3::new);
factory.register(TestService.class, "b1_8_compat", TestServiceB1_7_3::new); // Register for b1_8_compat
}
}
class DummyRegistrarR1_21 implements VersionedServiceRegistrar {
@Override
public void register(VersionedServiceFactory factory) {
factory.register(TestService.class, "r1_21", TestServiceR1_21::new);
factory.register(TestService.class, "r1_1", TestServiceR1_21::new); // Registers for r1_1 compat
factory.register(TestService.class, "r1_16_5", TestServiceR1_21::new); // Registers for r1_16_5 compat
factory.register(TestService.class, "r1_21", TestServiceR1_21::new); // Registers for r1_21
}
}
@ -59,6 +61,40 @@ public class DefaultVersionedServiceFactoryTest {
assertTrue(service instanceof TestServiceB1_7_3);
}
@Test
void testServiceRegistrationAndRetrievalForR1_1() {
// Mock VersionMatcher to return r1_1 suffix for a 1.10.2 server
when(mockVersionMatcher.getServerVersion()).thenReturn("1.10.2");
when(mockVersionMatcher.getClassSuffix(eq("1.10.2"))).thenReturn("r1_1");
// Register the dummy service for R1_21 (which covers r1_1 compat)
new DummyRegistrarR1_21().register(factory);
// Retrieve the service
TestService service = factory.getService(TestService.class);
// Assert that the correct compatible version is returned
assertNotNull(service);
assertTrue(service instanceof TestServiceR1_21);
}
@Test
void testServiceRegistrationAndRetrievalForR1_16_5() {
// Mock VersionMatcher to return r1_16_5 suffix for a 1.18.1 server
when(mockVersionMatcher.getServerVersion()).thenReturn("1.18.1");
when(mockVersionMatcher.getClassSuffix(eq("1.18.1"))).thenReturn("r1_16_5");
// Register the dummy service for R1_21 (which covers r1_16_5 compat)
new DummyRegistrarR1_21().register(factory);
// Retrieve the service
TestService service = factory.getService(TestService.class);
// Assert that the correct compatible version is returned
assertNotNull(service);
assertTrue(service instanceof TestServiceR1_21);
}
@Test
void testServiceRegistrationAndRetrievalForR1_21() {
// Mock VersionMatcher to return r1_21 suffix
@ -76,23 +112,6 @@ public class DefaultVersionedServiceFactoryTest {
assertTrue(service instanceof TestServiceR1_21);
}
@Test
void testServiceRegistrationAndRetrievalForB1_8_Compat() {
// Mock VersionMatcher to return b1_8_compat suffix for a 1.16.5 server
when(mockVersionMatcher.getServerVersion()).thenReturn("1.16.5");
when(mockVersionMatcher.getClassSuffix(eq("1.16.5"))).thenReturn("b1_8_compat");
// Register the dummy service for b1_7_3 (which also covers b1_8_compat)
new DummyRegistrarB1_7_3().register(factory);
// Retrieve the service
TestService service = factory.getService(TestService.class);
// Assert that the correct compatible version is returned
assertNotNull(service);
assertTrue(service instanceof TestServiceB1_7_3);
}
@Test
void testNoServiceRegisteredThrowsException() {
// Mock VersionMatcher to return an unknown suffix
@ -114,7 +133,7 @@ public class DefaultVersionedServiceFactoryTest {
when(mockVersionMatcher.getClassSuffix(eq("1.7.3"))).thenReturn("b1_7_3");
// Register R1_21 service, but not B1_7_3
new DummyRegistrarR1_21().register(factory);
new DummyRegistrarR1_21().register(factory); // R1_21 registrar doesn't register b1_7_3
// Attempt to retrieve b1_7_3 service
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->