Fix VersionMatcher not parsing version string correctly
This commit is contained in:
parent
0b753d6a4e
commit
425599ff62
5 changed files with 78 additions and 54 deletions
|
|
@ -7,6 +7,8 @@ import org.adrianvictor.lib.versioning.VersionedServiceRegistrar;
|
|||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
|
|
@ -48,7 +50,7 @@ public class DefaultVersionedServiceFactoryTest {
|
|||
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");
|
||||
when(mockVersionMatcher.getClassSuffixes(eq("1.7.3"))).thenReturn(List.of("b1_7_3"));
|
||||
|
||||
// Register the dummy service for b1_7_3
|
||||
new DummyRegistrarB1_7_3().register(factory);
|
||||
|
|
@ -65,7 +67,7 @@ public class DefaultVersionedServiceFactoryTest {
|
|||
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");
|
||||
when(mockVersionMatcher.getClassSuffixes(eq("1.10.2"))).thenReturn(List.of("r1_1"));
|
||||
|
||||
// Register the dummy service for R1_21 (which covers r1_1 compat)
|
||||
new DummyRegistrarR1_21().register(factory);
|
||||
|
|
@ -79,18 +81,17 @@ public class DefaultVersionedServiceFactoryTest {
|
|||
}
|
||||
|
||||
@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");
|
||||
void testServiceFallbackCompatibility() {
|
||||
// Mock a 1.21 server which supports r1_21, r1_16_5, and r1_1
|
||||
when(mockVersionMatcher.getServerVersion()).thenReturn("1.21.0");
|
||||
when(mockVersionMatcher.getClassSuffixes(eq("1.21.0"))).thenReturn(List.of("r1_21", "r1_16_5", "r1_1"));
|
||||
|
||||
// Register the dummy service for R1_21 (which covers r1_16_5 compat)
|
||||
new DummyRegistrarR1_21().register(factory);
|
||||
// Register ONLY for r1_1
|
||||
factory.register(TestService.class, "r1_1", TestServiceR1_21::new);
|
||||
|
||||
// Retrieve the service
|
||||
// Retrieve the service - should fall back to r1_1
|
||||
TestService service = factory.getService(TestService.class);
|
||||
|
||||
// Assert that the correct compatible version is returned
|
||||
assertNotNull(service);
|
||||
assertTrue(service instanceof TestServiceR1_21);
|
||||
}
|
||||
|
|
@ -99,7 +100,7 @@ public class DefaultVersionedServiceFactoryTest {
|
|||
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");
|
||||
when(mockVersionMatcher.getClassSuffixes(eq("1.21.0"))).thenReturn(List.of("r1_21", "r1_16_5", "r1_1"));
|
||||
|
||||
// Register the dummy service for r1_21
|
||||
new DummyRegistrarR1_21().register(factory);
|
||||
|
|
@ -116,7 +117,7 @@ public class DefaultVersionedServiceFactoryTest {
|
|||
void testNoServiceRegisteredThrowsException() {
|
||||
// Mock VersionMatcher to return an unknown suffix
|
||||
when(mockVersionMatcher.getServerVersion()).thenReturn("unknown");
|
||||
when(mockVersionMatcher.getClassSuffix(anyString())).thenReturn("unknown_version");
|
||||
when(mockVersionMatcher.getClassSuffixes(anyString())).thenReturn(List.of("unknown_version"));
|
||||
|
||||
// Attempt to retrieve a service without any registration
|
||||
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->
|
||||
|
|
@ -125,21 +126,4 @@ public class DefaultVersionedServiceFactoryTest {
|
|||
|
||||
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); // R1_21 registrar doesn't register b1_7_3
|
||||
|
||||
// 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"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue