cache;
-
/**
* Constructor of the connector.
*/
public MicrosoftTranslatorAzure() {
+ super();
if (Core.getMainWindow() != null) {
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem();
menuItem.setText(getName());
@@ -105,40 +89,6 @@ public MicrosoftTranslatorAzure() {
enabled = newValue;
});
}
-
- cache = getCacheLayer(getName());
- setCacheClearPolicy();
- }
-
- /**
- * Creat cache object.
- *
- * MT connectors can override cache size and invalidate the policy.
- * @param name name of cache which should be unique among MT connectors.
- * @return Cache object
- */
- protected Cache getCacheLayer(String name) {
- CachingProvider provider = Caching.getCachingProvider();
- CacheManager manager = provider.getCacheManager();
- Cache cache1 = manager.getCache(name);
- if (cache1 != null) {
- return cache1;
- }
- CaffeineConfiguration config = new CaffeineConfiguration<>();
- config.setExpiryPolicyFactory(() -> new CreatedExpiryPolicy(Duration.ONE_DAY));
- config.setMaximumSize(OptionalLong.of(1_000));
- return manager.createCache(name, config);
- }
-
- /**
- * Register cache clear policy.
- */
- protected void setCacheClearPolicy() {
- CoreEvents.registerProjectChangeListener(eventType -> {
- if (eventType.equals(IProjectEventListener.PROJECT_CHANGE_TYPE.CLOSE)) {
- cache.clear();
- }
- });
}
/**
@@ -146,7 +96,7 @@ protected void setCacheClearPolicy() {
* @param key bundle key.
* @return a localized string.
*/
- public static String getString(String key) {
+ static String getString(String key) {
return BUNDLE.getString(key);
}
@@ -194,47 +144,8 @@ public String getName() {
}
@Override
- public boolean isEnabled() {
- return enabled;
- }
-
- @Override
- public void setEnabled(boolean b) {
- enabled = b;
- Preferences.setPreference(ALLOW_MICROSOFT_TRANSLATOR_AZURE, b);
- }
-
- @Override
- public String getTranslation(Language sLang, Language tLang, String text) throws Exception {
- if (enabled) {
- return translate(sLang, tLang, text);
- } else {
- return null;
- }
- }
-
- @Override
- public String getCachedTranslation(Language sLang, Language tLang, String text) {
- if (enabled) {
- String prev;
- if (text.length() > 10000) {
- prev = getFromCache(sLang, tLang, text.substring(0, 9997) + "...");
- } else {
- prev = getFromCache(sLang, tLang, text);
- }
- if (prev != null) {
- return prev;
- }
- try {
- String translation = translate(sLang, tLang, text);
- if (translation != null) {
- putToCache(sLang, tLang, text, translation);
- }
- return translation;
- } catch (Exception ignored) {
- }
- }
- return null;
+ protected String getPreferenceName() {
+ return ALLOW_MICROSOFT_TRANSLATOR_AZURE;
}
/**
@@ -287,7 +198,8 @@ protected String getKey() throws Exception {
return key;
}
- protected synchronized String translate(Language sLang, Language tLang, String text) throws Exception {
+ @Override
+ protected String translate(Language sLang, Language tLang, String text) throws Exception {
if (isV2() && (translator == null || translator instanceof AzureTranslatorV3)) {
translator = new MicrosoftTranslatorV2(this);
} else if (translator == null || translator instanceof MicrosoftTranslatorV2) {
@@ -296,14 +208,6 @@ protected synchronized String translate(Language sLang, Language tLang, String t
return translator.translate(sLang, tLang, text);
}
- protected String getFromCache(Language sLang, Language tLang, String text) {
- return cache.get(sLang + "/" + tLang + "/" + text);
- }
-
- protected void putToCache(Language sLang, Language tLang, String text, String result) {
- cache.put(sLang.toString() + "/" + tLang.toString() + "/" + text, result);
- }
-
@Override
public boolean isConfigurable() {
return true;
diff --git a/src/test/java/org/omegat/connectors/machinetranslators/azure/TestAzureTranslatorV3.java b/src/test/java/org/omegat/connectors/machinetranslators/azure/TestAzureTranslatorV3.java
index 08ab5dc..61ccac3 100644
--- a/src/test/java/org/omegat/connectors/machinetranslators/azure/TestAzureTranslatorV3.java
+++ b/src/test/java/org/omegat/connectors/machinetranslators/azure/TestAzureTranslatorV3.java
@@ -25,12 +25,58 @@
package org.omegat.connectors.machinetranslators.azure;
+import org.omegat.util.Preferences;
+import org.omegat.util.PreferencesImpl;
+import org.omegat.util.PreferencesXML;
+import org.omegat.util.RuntimePreferences;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+
import com.fasterxml.jackson.core.JsonProcessingException;
+import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import wiremock.org.apache.commons.io.FileUtils;
public class TestAzureTranslatorV3 {
+ private File tmpDir;
+
+ /**
+ * Prepare a temporary directory.
+ * @throws IOException when I/O error.
+ */
+ @BeforeEach
+ public final void setUp() throws IOException {
+ tmpDir = Files.createTempDirectory("omegat").toFile();
+ Assertions.assertTrue(tmpDir.isDirectory());
+ File prefsFile = new File(tmpDir, Preferences.FILE_PREFERENCES);
+ Preferences.IPreferences prefs = new PreferencesImpl(new PreferencesXML(null, prefsFile));
+ prefs.setPreference(MicrosoftTranslatorAzure.ALLOW_MICROSOFT_TRANSLATOR_AZURE, true);
+ init(prefsFile.getAbsolutePath());
+ }
+
+ /**
+ * Clean up a temporary directory.
+ * @throws IOException when I/O error.
+ */
+ @AfterEach
+ public final void tearDown() throws IOException {
+ FileUtils.deleteDirectory(tmpDir);
+ }
+
+ /**
+ * Initialize preferences for test.
+ * @param configDir to create omegat.prefs.
+ */
+ public static synchronized void init(String configDir) {
+ RuntimePreferences.setConfigDir(configDir);
+ Preferences.init();
+ }
+
@Test
public void testCreateJsonRequest() throws JsonProcessingException {
MicrosoftTranslatorAzure azure = new TestMicrosoftTranslatorAzure.MicrosoftTranslatorAzureMock();