Files
Stirling-PDF/stirling-pdf/src/test/java/stirling/software/SPDF/service/LanguageServiceBasicTest.java
T
Dario Ghunney WareandGitHub c7d6a063d7 Multi module refactor (#3640)
# Description of Changes

Migrated Stirling PDF to a multi-module structure:

* Introduced new `:stirling-pdf` module
* Moved all the core logic and features of Stirling PDF into
`:stirling-pdf`
* Updated paths of jobs and scripts

---

## Checklist

### General

- [ ] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [ ] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md)
(if applicable)
- [ ] I have performed a self-review of my own code
- [ ] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [ ] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing)
for more details.
2025-06-09 12:51:41 +01:00

132 lines
5.1 KiB
Java

package stirling.software.SPDF.service;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.Resource;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.model.ApplicationProperties.Ui;
class LanguageServiceBasicTest {
private LanguageService languageService;
private ApplicationProperties applicationProperties;
@BeforeEach
void setUp() {
// Mock application properties
applicationProperties = mock(ApplicationProperties.class);
Ui ui = mock(Ui.class);
when(applicationProperties.getUi()).thenReturn(ui);
// Create language service with test implementation
languageService = new LanguageServiceForTest(applicationProperties);
}
@Test
void testGetSupportedLanguages_BasicFunctionality() throws IOException {
// Set up mocked resources
Resource enResource = createMockResource("messages_en_US.properties");
Resource frResource = createMockResource("messages_fr_FR.properties");
Resource[] mockResources = new Resource[] {enResource, frResource};
// Configure the test service
((LanguageServiceForTest) languageService).setMockResources(mockResources);
when(applicationProperties.getUi().getLanguages()).thenReturn(Collections.emptyList());
// Execute the method
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Basic assertions
assertTrue(supportedLanguages.contains("en_US"), "en_US should be included");
assertTrue(supportedLanguages.contains("fr_FR"), "fr_FR should be included");
}
@Test
void testGetSupportedLanguages_FilteringInvalidFiles() throws IOException {
// Set up mocked resources with invalid files
Resource[] mockResources =
new Resource[] {
createMockResource("messages_en_US.properties"), // Valid
createMockResource("invalid_file.properties"), // Invalid
createMockResource(null) // Null filename
};
// Configure the test service
((LanguageServiceForTest) languageService).setMockResources(mockResources);
when(applicationProperties.getUi().getLanguages()).thenReturn(Collections.emptyList());
// Execute the method
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Verify filtering
assertTrue(supportedLanguages.contains("en_US"), "Valid language should be included");
assertFalse(
supportedLanguages.contains("invalid_file"),
"Invalid filename should be filtered out");
}
@Test
void testGetSupportedLanguages_WithRestrictions() throws IOException {
// Set up test resources
Resource[] mockResources =
new Resource[] {
createMockResource("messages_en_US.properties"),
createMockResource("messages_fr_FR.properties"),
createMockResource("messages_de_DE.properties"),
createMockResource("messages_en_GB.properties")
};
// Configure the test service
((LanguageServiceForTest) languageService).setMockResources(mockResources);
// Allow only specific languages (en_GB is always included)
when(applicationProperties.getUi().getLanguages())
.thenReturn(Arrays.asList("en_US", "fr_FR"));
// Execute the method
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Verify filtering by restrictions
assertTrue(supportedLanguages.contains("en_US"), "Allowed language should be included");
assertTrue(supportedLanguages.contains("fr_FR"), "Allowed language should be included");
assertTrue(supportedLanguages.contains("en_GB"), "en_GB should always be included");
assertFalse(supportedLanguages.contains("de_DE"), "Restricted language should be excluded");
}
// Helper methods
private Resource createMockResource(String filename) {
Resource mockResource = mock(Resource.class);
when(mockResource.getFilename()).thenReturn(filename);
return mockResource;
}
// Test subclass
private static class LanguageServiceForTest extends LanguageService {
private Resource[] mockResources;
public LanguageServiceForTest(ApplicationProperties applicationProperties) {
super(applicationProperties);
}
public void setMockResources(Resource[] mockResources) {
this.mockResources = mockResources;
}
@Override
protected Resource[] getResourcesFromPattern(String pattern) throws IOException {
return mockResources;
}
}
}