mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-12 17:52:13 +02:00
add UploadLimitServiceTest
This commit is contained in:
parent
8bfdb2abb5
commit
c9a6ab2856
@ -1,5 +1,6 @@
|
||||
package stirling.software.SPDF.controller.web;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -52,6 +53,6 @@ public class UploadLimitService {
|
||||
if (bytes < 1024) return bytes + " B";
|
||||
int exp = (int) (Math.log(bytes) / Math.log(1024));
|
||||
String pre = "KMGTPE".charAt(exp - 1) + "B";
|
||||
return String.format("%.1f %s", bytes / Math.pow(1024, exp), pre);
|
||||
return String.format(Locale.US, "%.1f %s", bytes / Math.pow(1024, exp), pre);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,79 @@
|
||||
package stirling.software.SPDF.controller.web;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import stirling.software.SPDF.model.ApplicationProperties;
|
||||
|
||||
class UploadLimitServiceTest {
|
||||
|
||||
private UploadLimitService uploadLimitService;
|
||||
private ApplicationProperties applicationProperties;
|
||||
private ApplicationProperties.System systemProps;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
applicationProperties = mock(ApplicationProperties.class);
|
||||
systemProps = mock(ApplicationProperties.System.class);
|
||||
when(applicationProperties.getSystem()).thenReturn(systemProps);
|
||||
|
||||
uploadLimitService = new UploadLimitService();
|
||||
// inject mock
|
||||
try {
|
||||
var field = UploadLimitService.class.getDeclaredField("applicationProperties");
|
||||
field.setAccessible(true);
|
||||
field.set(uploadLimitService, applicationProperties);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "getUploadLimit case #{index}: input={0}, expected={2}")
|
||||
@MethodSource("uploadLimitParams")
|
||||
void shouldComputeUploadLimitCorrectly(String input, boolean validFormat, long expected) {
|
||||
when(systemProps.getFileUploadLimit()).thenReturn(validFormat ? input : input);
|
||||
|
||||
long result = uploadLimitService.getUploadLimit();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
static Stream<Arguments> uploadLimitParams() {
|
||||
return Stream.of(
|
||||
// empty or null input yields 0
|
||||
Arguments.of(null, false, 0L),
|
||||
Arguments.of("", false, 0L),
|
||||
// invalid formats
|
||||
Arguments.of("1234MB", false, 0L),
|
||||
Arguments.of("5TB", false, 0L),
|
||||
// valid formats
|
||||
Arguments.of("10KB", true, 10 * 1024L),
|
||||
Arguments.of("2MB", true, 2 * 1024 * 1024L),
|
||||
Arguments.of("1GB", true, 1L * 1024 * 1024 * 1024),
|
||||
Arguments.of("5mb", true, 5 * 1024 * 1024L),
|
||||
Arguments.of("0MB", true, 0L));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "getReadableUploadLimit case #{index}: rawValue={0}, expected={1}")
|
||||
@MethodSource("readableLimitParams")
|
||||
void shouldReturnReadableFormat(String rawValue, String expected) {
|
||||
when(systemProps.getFileUploadLimit()).thenReturn(rawValue);
|
||||
String result = uploadLimitService.getReadableUploadLimit();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
static Stream<Arguments> readableLimitParams() {
|
||||
return Stream.of(
|
||||
Arguments.of(null, "0 B"),
|
||||
Arguments.of("", "0 B"),
|
||||
Arguments.of("1KB", "1.0 KB"),
|
||||
Arguments.of("2MB", "2.0 MB"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user