Remove reflection and add VersionedService to provide multiple versions support

This commit is contained in:
天クマ 2026-05-23 17:59:40 -03:00
commit eec7b2fcbb
14 changed files with 310 additions and 111 deletions

View file

@ -0,0 +1,126 @@
package org.adrianvictor.lib;
import org.adrianvictor.lib.reflection.VersionMatcher;
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.*;
// Define dummy interfaces for testing
interface TestService {}
class TestServiceB1_7_3 implements TestService {}
class TestServiceR1_21 implements TestService {}
// Dummy registrar for testing
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);
}
}
public class DefaultVersionedServiceFactoryTest {
private VersionMatcher mockVersionMatcher;
private DefaultVersionedServiceFactory factory;
@BeforeEach
void setUp() {
mockVersionMatcher = mock(VersionMatcher.class);
factory = new DefaultVersionedServiceFactory(mockVersionMatcher);
}
@Test
void testServiceRegistrationAndRetrievalForB1_7_3() {
// Mock VersionMatcher to return b1_7_3 suffix
when(mockVersionMatcher.getServerVersion()).thenReturn("1.7.3");
when(mockVersionMatcher.getClassSuffix(eq("1.7.3"))).thenReturn("b1_7_3");
// Register the dummy service for b1_7_3
new DummyRegistrarB1_7_3().register(factory);
// Retrieve the service
TestService service = factory.getService(TestService.class);
// Assert that the correct version is returned
assertNotNull(service);
assertTrue(service instanceof TestServiceB1_7_3);
}
@Test
void testServiceRegistrationAndRetrievalForR1_21() {
// Mock VersionMatcher to return r1_21 suffix
when(mockVersionMatcher.getServerVersion()).thenReturn("1.21.0");
when(mockVersionMatcher.getClassSuffix(eq("1.21.0"))).thenReturn("r1_21");
// Register the dummy service for r1_21
new DummyRegistrarR1_21().register(factory);
// Retrieve the service
TestService service = factory.getService(TestService.class);
// Assert that the correct version is returned
assertNotNull(service);
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
when(mockVersionMatcher.getServerVersion()).thenReturn("unknown");
when(mockVersionMatcher.getClassSuffix(anyString())).thenReturn("unknown_version");
// Attempt to retrieve a service without any registration
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->
factory.getService(TestService.class)
);
assertTrue(exception.getMessage().contains("No service registered for type"));
}
@Test
void testSpecificServiceNotRegisteredForVersionThrowsException() {
// Mock VersionMatcher to return b1_7_3 suffix
when(mockVersionMatcher.getServerVersion()).thenReturn("1.7.3");
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);
// Attempt to retrieve b1_7_3 service
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->
factory.getService(TestService.class)
);
assertTrue(exception.getMessage().contains("No service registered for type"));
}
}