Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: HunspellSpellchecker: multiple initializations #1261

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;

import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
Expand All @@ -41,10 +42,14 @@
import org.junit.runners.MethodSorters;

import org.omegat.core.Core;
import org.omegat.core.CoreEvents;
import org.omegat.core.TestCoreInitializer;
import org.omegat.core.data.NotLoadedProject;
import org.omegat.core.data.ProjectProperties;
import org.omegat.core.events.IProjectEventListener;
import org.omegat.core.spellchecker.ISpellChecker;
import org.omegat.filters2.master.PluginUtils;
import org.omegat.gui.main.ConsoleWindow;
import org.omegat.util.Language;
import org.omegat.util.TestPreferencesInitializer;

Expand All @@ -56,12 +61,13 @@ public class HunspellSpellcheckerTest {
private static Path configDir;

@BeforeClass
public static void setUpClass() throws IOException {
public static void setUpClass() throws Exception {
PluginUtils.loadPlugins(Collections.emptyMap());
tmpDir = Files.createTempDirectory("omegat");
assertThat(tmpDir.toFile()).isDirectory();
configDir = Files.createDirectory(tmpDir.resolve(".omegat"));
TestPreferencesInitializer.init(configDir.toString());
TestCoreInitializer.initMainWindow(new ConsoleWindow());
Files.createDirectory(configDir.resolve("spelling"));
copyFile("es_MX.aff");
copyFile("es_MX.dic");
Expand Down Expand Up @@ -124,6 +130,29 @@ public ProjectProperties getProjectProperties() {
assertThat(checker.suggest("Erruer")).as("Get suggestion").contains("Erreur", "Errer");
}

@Test
public void testReinitializeWhenProjectChanged() throws Exception {
ProjectProperties props = new ProjectProperties(tmpDir.toFile());
props.setTargetLanguage(new Language("fr_FR"));
Core.setProject(new NotLoadedProject() {
@Override
public ProjectProperties getProjectProperties() {
return props;
}
});
HunSpellCheckerMock checker = new HunSpellCheckerMock();
assertThat(checker.initialize()).as("Success initialize").isTrue();
assertThat(checker.getInitializeCounter()).as("Hunspell Checker initialized once").isEqualTo(1);
// Fire project change events twice
final CountDownLatch latch = new CountDownLatch(2);
CoreEvents.registerProjectChangeListener(eventType -> latch.countDown());
CoreEvents.fireProjectChange(IProjectEventListener.PROJECT_CHANGE_TYPE.LOAD);
CoreEvents.fireProjectChange(IProjectEventListener.PROJECT_CHANGE_TYPE.LOAD);
latch.await();
//
assertThat(checker.getInitializeCounter()).as("Hunspell Checker loaded 3rd times").isEqualTo(3);
}

private static void copyFile(String target) throws IOException {
try (InputStream is = HunspellSpellcheckerTest.class.getResourceAsStream(DICTIONARY_PATH + target)) {
if (is == null) {
Expand All @@ -133,4 +162,21 @@ private static void copyFile(String target) throws IOException {
}

}

public static class HunSpellCheckerMock extends HunSpellChecker {
private int initializeCounter = 0;
public HunSpellCheckerMock() {
super();
}

@Override
public boolean initialize() {
initializeCounter++;
return super.initialize();
}

public int getInitializeCounter() {
return initializeCounter;
}
}
}
47 changes: 23 additions & 24 deletions src/org/omegat/core/spellchecker/AbstractSpellChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ public abstract class AbstractSpellChecker implements ISpellChecker {
private Path learnedFilePath;

public AbstractSpellChecker() {
CoreEvents.registerProjectChangeListener(eventType -> {
switch (eventType) {
case LOAD:
case CREATE:
initialize();
break;
case CLOSE:
destroy();
break;
default:
// Nothing
}
resetCache();
});
CoreEvents.registerEntryEventListener(new IEntryEventListener() {
public void onNewFile(String activeFileName) {
resetCache();
}

public void onEntryActivated(SourceTextEntry newEntry) {
}
});
}

/**
Expand All @@ -117,31 +139,8 @@ public boolean initialize() {
if (checker == null) {
LOGGER.atInfo().logRB("SPELLCHECKER_LANGUAGE_NOT_FOUND", targetLanguage);
return false;
} else {
loadWordLists();
CoreEvents.registerProjectChangeListener(eventType -> {
switch (eventType) {
case LOAD:
case CREATE:
initialize();
break;
case CLOSE:
destroy();
break;
default:
// Nothing
}
resetCache();
});
CoreEvents.registerEntryEventListener(new IEntryEventListener() {
public void onNewFile(String activeFileName) {
resetCache();
}

public void onEntryActivated(SourceTextEntry newEntry) {
}
});
}
loadWordLists();
return true;
}

Expand Down
9 changes: 6 additions & 3 deletions src/org/omegat/externalfinder/ExternalFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.omegat.util.StaticUtils;
import org.omegat.util.gui.MenuExtender;
import org.omegat.util.gui.MenuItemPager;
import org.omegat.util.gui.StaticUIUtils;

/**
* Entry point for ExternalFinder functionality.
Expand Down Expand Up @@ -89,9 +90,11 @@ private ExternalFinder() {
* OmegaT will call this method when loading.
*/
public static void loadPlugins() {
// register listeners
CoreEvents.registerApplicationEventListener(generateIApplicationEventListener());
CoreEvents.registerProjectChangeListener(generateIProjectEventListener());
// register listeners when GUI environment
if (StaticUIUtils.isGUI()) {
CoreEvents.registerApplicationEventListener(generateIApplicationEventListener());
CoreEvents.registerProjectChangeListener(generateIProjectEventListener());
}
}

private static IProjectEventListener generateIProjectEventListener() {
Expand Down
Loading