Skip to content

Commit

Permalink
refactor: update to get dialect from dialect registry
Browse files Browse the repository at this point in the history
Signed-off-by: Aman Prashant <aman.prashant@broadcom.com>
  • Loading branch information
ap891843 committed Jan 12, 2023
1 parent 5bba65b commit c28ad7b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.eclipse.lsp.cobol.common.DialectRegistryItem;
import org.eclipse.lsp.cobol.core.engine.dialects.WorkingFolderService;

import java.io.IOException;
Expand Down Expand Up @@ -53,20 +54,20 @@ public CobolLSPropertiesResourceBundle(
/**
* Updates resource bundle for a supplied dialect.
*
* @param dialectName dialect for which resource needs to be updated
* @param dialectRegistryItem dialect registry for which resource needs to be updated
* @throws IOException when resources for a dialect is not found
*/
public void updateMessageResourceBundle(String dialectName) throws IOException {
properties.putAll(load(dialectName, locale));
public void updateMessageResourceBundle(DialectRegistryItem dialectRegistryItem) throws IOException {
properties.putAll(load(dialectRegistryItem, locale));
}

private Properties load(String dialectName, Locale locale) throws IOException {
private Properties load(DialectRegistryItem dialectRegistryItem, Locale locale) throws IOException {
Properties properties = new Properties();
List<String> resourceName = toSuspectedBundleNames(locale);
Collections.reverse(resourceName);
URI workingFolder = this.workingFolderService.getWorkingFolder();
URI workingFolder = this.workingFolderService.getWorkingFolder(dialectRegistryItem.getPath());
InputStream validResources =
getDialectResources(resourceName, workingFolder, getJarName(dialectName));
getDialectResources(resourceName, workingFolder, getJarName(dialectRegistryItem.getName()));
properties.load(validResources);
return properties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.lsp.cobol.common.AnalysisConfig;
import org.eclipse.lsp.cobol.common.DialectRegistryItem;
import org.eclipse.lsp.cobol.common.copybook.CopybookProcessingMode;
import org.eclipse.lsp.cobol.common.message.LocaleStore;
import org.eclipse.lsp.cobol.common.message.MessageService;
import org.eclipse.lsp.cobol.common.message.MessageTemplate;
import org.eclipse.lsp.cobol.core.engine.dialects.WorkingFolderService;
import org.eclipse.lsp.cobol.service.settings.SettingsService;
import org.eclipse.lsp.cobol.service.settings.ConfigurationService;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -34,7 +37,6 @@

import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import static org.eclipse.lsp.cobol.service.settings.SettingsParametersEnum.DIALECTS;

/**
* This class is an properties file implementation of {@link MessageService} . It loads messages
Expand All @@ -47,18 +49,18 @@ public class PropertiesMessageService implements MessageService {
private final String baseName;
private final LocaleStore localeStore;
private CobolLSPropertiesResourceBundle resourceBundle;
private final SettingsService settingsService;
private final ConfigurationService configurationService;
private final WorkingFolderService workingFolderService;

@Inject
public PropertiesMessageService(
@Named("resourceFileLocation") String baseName,
LocaleStore localeStore,
SettingsService settingsService,
ConfigurationService configurationService,
WorkingFolderService workingFolderService) {
this.baseName = baseName;
this.localeStore = localeStore;
this.settingsService = settingsService;
this.configurationService = configurationService;
this.workingFolderService = workingFolderService;
resourceBundle =
new CobolLSPropertiesResourceBundle(
Expand All @@ -79,9 +81,7 @@ private void reloadResourceBundle(Locale locale) {
ResourceBundle.clearCache();
resourceBundle =
new CobolLSPropertiesResourceBundle(
baseName,
localeStore.getApplicationLocale(),
this.workingFolderService);
baseName, localeStore.getApplicationLocale(), this.workingFolderService);
updateResourceBundle();
}

Expand Down Expand Up @@ -109,18 +109,19 @@ public String localizeTemplate(MessageTemplate template) {

private void updateResourceBundle() {
reloadResourceBundle(this.localeStore.getApplicationLocale());
this.settingsService
.fetchTextConfiguration(DIALECTS.label)
.thenAccept(this::updateResourceBundle);
AnalysisConfig config = configurationService.getConfig(CopybookProcessingMode.ENABLED);
List<String> configuredDialects = config.getDialects();

config.getDialectRegistry().stream()
.filter(registeredDialects -> configuredDialects.contains(registeredDialects.getName()))
.forEach(this::updateResourceBundle);
}

private void updateResourceBundle(List<String> dialects) {
for (String dialectName : dialects) {
try {
this.resourceBundle.updateMessageResourceBundle(dialectName);
} catch (IOException e) {
LOG.error("Issue while loading resource bundle for " + dialectName);
}
private void updateResourceBundle(DialectRegistryItem dialectRegistryItem) {
try {
this.resourceBundle.updateMessageResourceBundle(dialectRegistryItem);
} catch (IOException e) {
LOG.error("Issue while loading resource bundle for " + dialectRegistryItem.getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.lsp.cobol.core.engine.dialects.WorkingFolderService;
import org.eclipse.lsp.cobol.core.messages.PropertiesMessageService;
import org.eclipse.lsp.cobol.core.strategy.CobolErrorStrategy;
import org.eclipse.lsp.cobol.service.settings.SettingsService;
import org.eclipse.lsp.cobol.service.settings.ConfigurationService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -44,11 +44,11 @@ void beforeEach() {
mockParser = mock(MessageServiceParser.class);
doCallRealMethod().when((MessageServiceParser) mockParser).notifyError(anyString());
LocaleStore localeMock = mock(LocaleStore.class);
SettingsService settingsService = mock(SettingsService.class);
ConfigurationService configurationService = mock(ConfigurationService.class);
when(localeMock.getApplicationLocale()).thenReturn(Locale.ENGLISH);
WorkingFolderService workingFolderService = mock(WorkingFolderService.class);
MessageService messageService =
new PropertiesMessageService("resourceBundles/test", localeMock, settingsService, workingFolderService);
new PropertiesMessageService("resourceBundles/test", localeMock, configurationService, workingFolderService);
CobolErrorStrategy errorStrategy = mock(CobolErrorStrategy.class);
when(mockParser.getErrorHandler()).thenReturn(errorStrategy);
when(errorStrategy.getMessageService()).thenReturn(messageService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.lsp.cobol.core.messages;

import com.google.common.collect.ImmutableList;
import org.eclipse.lsp.cobol.common.DialectRegistryItem;
import org.eclipse.lsp.cobol.core.engine.dialects.WorkingFolderService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -47,7 +48,8 @@ void updateMessageResourceBundle() throws IOException, URISyntaxException {
"test.test: flip flop", StandardCharsets.UTF_8))
.when(spyBundle)
.getDialectResources(any(), any(), any());
spyBundle.updateMessageResourceBundle("dummyDialect");
DialectRegistryItem dialectRegistryItem = new DialectRegistryItem("dummyDialect", "/path", "dummy dialect", "dummyDialect");
spyBundle.updateMessageResourceBundle(dialectRegistryItem);
Assertions.assertEquals(spyBundle.handleGetObject("test.test"), "flip flop");
Assertions.assertEquals(spyBundle.handleGetObject("1"), "French test selected.");
ImmutableList<String> expectedKeySet = ImmutableList.of("1", "2", "test.test");
Expand All @@ -63,13 +65,13 @@ void updateMessageResourceBundle() throws IOException, URISyntaxException {
@Test
void updateMessageResourceBundleWhenResourcesNotFound() throws IOException, URISyntaxException {
WorkingFolderService workingFolderService = mock(WorkingFolderService.class);

DialectRegistryItem dialectRegistryItem = new DialectRegistryItem("dummyDialect", "/path", "dummy dialect", "dummyDialect");
CobolLSPropertiesResourceBundle bundle =
new CobolLSPropertiesResourceBundle(
"resourceBundles/test", Locale.FRENCH, workingFolderService);
when(workingFolderService.getWorkingFolder()).thenReturn(new URI("file:test/"));
when(workingFolderService.getWorkingFolder(anyString())).thenReturn(new URI("file:test/"));

bundle.updateMessageResourceBundle("dummyDialect");
bundle.updateMessageResourceBundle(dialectRegistryItem);
Assertions.assertEquals(bundle.handleGetObject("test.test"), "test.test");
Assertions.assertEquals(bundle.handleGetObject("1"), "French test selected.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.eclipse.lsp.cobol.common.message.MessageService;
import org.eclipse.lsp.cobol.common.message.MessageTemplate;
import org.eclipse.lsp.cobol.core.engine.dialects.WorkingFolderService;
import org.eclipse.lsp.cobol.service.settings.SettingsService;
import org.eclipse.lsp.cobol.service.settings.ConfigurationService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -35,7 +35,7 @@ class PropertiesMessageServiceTest {

private static MessageService messageService;
private LocaleStore localeMock;
SettingsService settingsService = mock(SettingsService.class);
ConfigurationService configurationService = mock(ConfigurationService.class);
WorkingFolderService workingFolderService = mock(WorkingFolderService.class);

@BeforeEach
Expand All @@ -44,7 +44,7 @@ public void beforeAll() {
when(localeMock.getApplicationLocale()).thenReturn(Locale.ENGLISH);
messageService =
new PropertiesMessageService(
"resourceBundles/test", localeMock, settingsService, workingFolderService);
"resourceBundles/test", localeMock, configurationService, workingFolderService);
}

@Test
Expand All @@ -61,7 +61,7 @@ void whenValidMessageTemplateProvideFR_getFormattedMessage() {
when(localeMock.getApplicationLocale()).thenReturn(Locale.FRENCH);
MessageService messageServiceFR =
new PropertiesMessageService(
"resourceBundles/test", localeMock, settingsService, workingFolderService);
"resourceBundles/test", localeMock, configurationService, workingFolderService);
assertEquals("French test selected.", messageServiceFR.getMessage("1"));

assertEquals(
Expand All @@ -73,7 +73,7 @@ void whenValidMessageTemplateProvideFR_getFormattedMessage() {
void whenInValidMessageTemplatePathProvide_getException() {
Assertions.assertThrows(
MissingResourceException.class,
() -> new PropertiesMessageService("dummy", localeMock, settingsService, workingFolderService));
() -> new PropertiesMessageService("dummy", localeMock, configurationService, workingFolderService));
}

@Test
Expand All @@ -82,7 +82,7 @@ void whenEmptyMessageTemplateProvided_getNoException_getKeyInstead() {
new PropertiesMessageService(
"resourceBundles/Test_messageServiceEmptyFile",
localeMock,
settingsService,
configurationService,
workingFolderService);
assertEquals("1", messageServiceLocal.getMessage("1"));
}
Expand All @@ -91,7 +91,7 @@ void whenEmptyMessageTemplateProvided_getNoException_getKeyInstead() {
void whenMultipleMsgServiceExist_thenSupportDuplicateKeys() {
MessageService messageService1 =
new PropertiesMessageService(
"resourceBundles/test-2", localeMock, settingsService, workingFolderService);
"resourceBundles/test-2", localeMock, configurationService, workingFolderService);
final String formattedMessage = messageService1.getMessage("1", localeMock);
assertEquals("This is a duplicate key test for diff msg service.", formattedMessage);
}
Expand Down

0 comments on commit c28ad7b

Please sign in to comment.