Skip to content

Commit

Permalink
Test put fallbacks into cache
Browse files Browse the repository at this point in the history
  • Loading branch information
André Laugks committed Apr 26, 2024
1 parent 99f193e commit 38865ce
Showing 1 changed file with 59 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.junit.jupiter.api.BeforeEach;
import java.util.Map;
import org.junit.jupiter.api.Test;

class CatalogHandlerTest {

private Locale locale;
private String domain;

@BeforeEach
void beforeEach() {
this.domain = "messages";
this.locale = Locale.forLanguageTag("en");
}
private final Locale locale = Locale.forLanguageTag("en");

@Test
void test_getAll() {
Expand All @@ -43,11 +36,12 @@ void test_get() {

@Test
void test_handling_catalog_and_caching() {
String key = this.domain + ".key";
String domain = "messages";
String key = domain + ".key";
String localeKey = "en|" + key;
List<Translation> translations = new ArrayList<>();
translations.add(new Translation(this.locale, "key", "value_from_file", this.domain));
var catalog = new BaseCatalog(translations, this.locale, this.domain).build();
translations.add(new Translation(this.locale, "key", "value_from_file", domain));
var catalog = new BaseCatalog(translations, this.locale, domain).build();

var cache = TestUtilities.getCache();

Expand All @@ -73,4 +67,57 @@ void test_handling_catalog_and_caching() {
assertEquals("value_from_cache", catalogHandler.get(locale, key));
}

@Test
void test_putFallbackToCache() {

var xliffCatalogBuildernew = new XliffCatalogBuilder(
TestUtilities.getResourcesLoader(
Locale.forLanguageTag("en"),
"translations_example/*"
).getTranslationFiles(),
"messages",
Locale.forLanguageTag("en"),
null
);

var cache = TestUtilities.getCache();

var catalogHandler = new CatalogHandler(
xliffCatalogBuildernew.getBaseCatalog(),
cache
);

// Check cache before the messages are fetched
Map<Object, Object> cacheItems = TestUtilities.cacheToArray(cache);
assertNull(cacheItems.get("en|headline"));
assertEquals("Headline (en)", cacheItems.get("en|messages.headline"));
assertNull(cacheItems.get("en-us|headline"));
assertNull(cacheItems.get("en-us|messages.headline"));
assertNull(cacheItems.get("jp|headline"));
assertNull(cacheItems.get("jp|messages.headline"));
assertEquals("Payment (es)", cacheItems.get("es|payment.headline"));
assertNull(cacheItems.get("es-cr|payment.text"));

// Fetch messages
catalogHandler.get(Locale.forLanguageTag("en"), "headline");
catalogHandler.get(Locale.forLanguageTag("en"), "messages.headline");
catalogHandler.get(Locale.forLanguageTag("en-US"), "headline");
catalogHandler.get(Locale.forLanguageTag("en-US"), "messages.headline");
catalogHandler.get(Locale.forLanguageTag("jp"), "headline");
catalogHandler.get(Locale.forLanguageTag("jp"), "messages.headline");
catalogHandler.get(Locale.forLanguageTag("es"), "payment.headline");
catalogHandler.get(Locale.forLanguageTag("es-cr"), "payment.text");

// Check cache after the messages are fetched
cacheItems = TestUtilities.cacheToArray(cache);
assertEquals("Headline (en)", cacheItems.get("en|headline"));
assertEquals("Headline (en)", cacheItems.get("en|messages.headline"));
assertEquals("Headline (en)", cacheItems.get("en-us|headline"));
assertEquals("Headline (en)", cacheItems.get("en-us|messages.headline"));
assertEquals("Headline (en)", cacheItems.get("jp|headline"));
assertEquals("Headline (en)", cacheItems.get("jp|messages.headline"));
assertEquals("Payment (es)", cacheItems.get("es|payment.headline"));
assertEquals("Payment Text (es)", cacheItems.get("es-cr|payment.text"));
}

}

0 comments on commit 38865ce

Please sign in to comment.