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 c28ad7b commit 306cbef
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
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.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand All @@ -39,12 +39,10 @@ public class CobolLSPropertiesResourceBundle extends ResourceBundle {
private final String baseName;
public static final char BUNDLE_SEPARATOR = '_';
private final Properties properties;
private final WorkingFolderService workingFolderService;
private final Locale locale;

public CobolLSPropertiesResourceBundle(
String basename, Locale locale, WorkingFolderService workingFolderService) {
this.workingFolderService = workingFolderService;
String basename, Locale locale) {
this.baseName = basename;
this.properties = new Properties();
this.locale = locale;
Expand All @@ -65,7 +63,7 @@ private Properties load(DialectRegistryItem dialectRegistryItem, Locale locale)
Properties properties = new Properties();
List<String> resourceName = toSuspectedBundleNames(locale);
Collections.reverse(resourceName);
URI workingFolder = this.workingFolderService.getWorkingFolder(dialectRegistryItem.getPath());
URI workingFolder = new File(dialectRegistryItem.getPath()).getAbsoluteFile().toURI();
InputStream validResources =
getDialectResources(resourceName, workingFolder, getJarName(dialectRegistryItem.getName()));
properties.load(validResources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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.ConfigurationService;

import java.io.IOException;
Expand All @@ -50,21 +49,18 @@ public class PropertiesMessageService implements MessageService {
private final LocaleStore localeStore;
private CobolLSPropertiesResourceBundle resourceBundle;
private final ConfigurationService configurationService;
private final WorkingFolderService workingFolderService;

@Inject
public PropertiesMessageService(
@Named("resourceFileLocation") String baseName,
LocaleStore localeStore,
ConfigurationService configurationService,
WorkingFolderService workingFolderService) {
@Named("resourceFileLocation") String baseName,
LocaleStore localeStore,
ConfigurationService configurationService) {
this.baseName = baseName;
this.localeStore = localeStore;
this.configurationService = configurationService;
this.workingFolderService = workingFolderService;
resourceBundle =
new CobolLSPropertiesResourceBundle(
baseName, localeStore.getApplicationLocale(), workingFolderService);
baseName, localeStore.getApplicationLocale());
subscribeToLocaleStore();
}

Expand All @@ -81,7 +77,7 @@ private void reloadResourceBundle(Locale locale) {
ResourceBundle.clearCache();
resourceBundle =
new CobolLSPropertiesResourceBundle(
baseName, localeStore.getApplicationLocale(), this.workingFolderService);
baseName, localeStore.getApplicationLocale());
updateResourceBundle();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void beforeEach() {
when(localeMock.getApplicationLocale()).thenReturn(Locale.ENGLISH);
WorkingFolderService workingFolderService = mock(WorkingFolderService.class);
MessageService messageService =
new PropertiesMessageService("resourceBundles/test", localeMock, configurationService, workingFolderService);
new PropertiesMessageService("resourceBundles/test", localeMock, configurationService);
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 @@ -39,7 +39,7 @@ void updateMessageResourceBundle() throws IOException, URISyntaxException {

CobolLSPropertiesResourceBundle bundle =
new CobolLSPropertiesResourceBundle(
"resourceBundles/test", Locale.FRENCH, workingFolderService);
"resourceBundles/test", Locale.FRENCH);
when(workingFolderService.getWorkingFolder()).thenReturn(new URI("file:test/"));
CobolLSPropertiesResourceBundle spyBundle = spy(bundle);

Expand Down Expand Up @@ -68,7 +68,7 @@ void updateMessageResourceBundleWhenResourcesNotFound() throws IOException, URIS
DialectRegistryItem dialectRegistryItem = new DialectRegistryItem("dummyDialect", "/path", "dummy dialect", "dummyDialect");
CobolLSPropertiesResourceBundle bundle =
new CobolLSPropertiesResourceBundle(
"resourceBundles/test", Locale.FRENCH, workingFolderService);
"resourceBundles/test", Locale.FRENCH);
when(workingFolderService.getWorkingFolder(anyString())).thenReturn(new URI("file:test/"));

bundle.updateMessageResourceBundle(dialectRegistryItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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.ConfigurationService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -36,15 +35,14 @@ class PropertiesMessageServiceTest {
private static MessageService messageService;
private LocaleStore localeMock;
ConfigurationService configurationService = mock(ConfigurationService.class);
WorkingFolderService workingFolderService = mock(WorkingFolderService.class);

@BeforeEach
public void beforeAll() {
localeMock = mock(LocaleStore.class);
when(localeMock.getApplicationLocale()).thenReturn(Locale.ENGLISH);
messageService =
new PropertiesMessageService(
"resourceBundles/test", localeMock, configurationService, workingFolderService);
"resourceBundles/test", localeMock, configurationService);
}

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

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

@Test
Expand All @@ -82,16 +80,16 @@ void whenEmptyMessageTemplateProvided_getNoException_getKeyInstead() {
new PropertiesMessageService(
"resourceBundles/Test_messageServiceEmptyFile",
localeMock,
configurationService,
workingFolderService);
configurationService
);
assertEquals("1", messageServiceLocal.getMessage("1"));
}

@Test
void whenMultipleMsgServiceExist_thenSupportDuplicateKeys() {
MessageService messageService1 =
new PropertiesMessageService(
"resourceBundles/test-2", localeMock, configurationService, workingFolderService);
"resourceBundles/test-2", localeMock, configurationService);
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 306cbef

Please sign in to comment.