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 MessageBundle key/file name resolver algorithm #39990

Merged
merged 1 commit into from
Apr 12, 2024
Merged
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 @@ -205,7 +205,7 @@ List<MessageBundleBuildItem> processBundles(BeanArchiveIndexBuildItem beanArchiv
Map<String, Path> localeToMergeCandidate = new HashMap<>();
for (Path messageFile : messageFiles) {
String fileName = messageFile.getFileName().toString();
if (fileName.startsWith(name)) {
if (bundleNameMatchesFileName(fileName, name)) {
final String locale;
int postfixIdx = fileName.indexOf('.');
if (postfixIdx == name.length()) {
Expand Down Expand Up @@ -315,6 +315,30 @@ List<MessageBundleBuildItem> processBundles(BeanArchiveIndexBuildItem beanArchiv
return bundles;
}

static boolean bundleNameMatchesFileName(String fileName, String name) {
int fileSeparatorIdx = fileName.indexOf('.');
// Remove file extension if exists
if (fileSeparatorIdx > -1) {
fileName = fileName.substring(0, fileSeparatorIdx);
}
// Split the filename and the bundle name by underscores
String[] fileNameParts = fileName.split("_");
String[] nameParts = name.split("_");

if (fileNameParts.length < nameParts.length) {
return false;
}

// Compare each part of the filename with the corresponding part of the bundle name
for (int i = 0; i < nameParts.length; i++) {
if (!fileNameParts[i].equals(nameParts[i])) {
return false;
}
}

return true;
}

@Record(value = STATIC_INIT)
@BuildStep
void initBundleContext(MessageBundleRecorder recorder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.qute.deployment;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class MessageBundleProcessorTest {

@Test
void bundleNameMatchesFileName() {
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("messages.properties", "messages"));
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("started.properties", "started"));
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("startedValidation.properties", "startedValidation"));
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("EmailBundles_startedValidation.properties",
"EmailBundles_startedValidation"));
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("EmailBundles_startedValidation_pt_BR.properties",
"EmailBundles_startedValidation"));

assertFalse(MessageBundleProcessor.bundleNameMatchesFileName("startedValidation.properties", "started"));
assertFalse(MessageBundleProcessor.bundleNameMatchesFileName("EmailBundles_startedValidation.properties",
"EmailBundles_started"));
assertFalse(MessageBundleProcessor.bundleNameMatchesFileName("EmailBundles_startedValidation_pt_BR.properties",
"EmailBundles_started"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.quarkus.qute.deployment.i18n;

import io.quarkus.qute.i18n.Message;
import io.quarkus.qute.i18n.MessageBundle;

public class EmailBundles {
@MessageBundle
interface started {
@Message
String started(String id, String filename);

@Message
String documentAccessUrl(String url);

@Message
String nextNotification();

@Message
String signingProcessStart(String id, String filename);

@Message
String subject(String customer, String filename);

@Message
String signForValidation();
}

@MessageBundle
interface startedValidator {
@Message
String started(String id, String filename);

@Message
String turnEmailWillBeSent();

@Message
String youMayAlreadyAccessDocument();

@Message
String subject(String customer, String filename);

@Message
String signForValidation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkus.qute.deployment.i18n;

import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.Engine;
import io.quarkus.qute.i18n.MessageBundles;
import io.quarkus.test.QuarkusUnitTest;

public class MessageBundleNameCollisionTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.overrideConfigKey("quarkus.default-locale", "en_US")
.withApplicationRoot((jar) -> jar
.addClasses(EmailBundles.class)
.addAsResource("messages/EmailBundles_started.properties")
.addAsResource("messages/EmailBundles_started_en.properties")
.addAsResource("messages/EmailBundles_startedValidator.properties")
.addAsResource("messages/EmailBundles_startedValidator_en.properties"));

@Inject
Engine engine;

@Test
public void testBundleMethodIsFound() {
EmailBundles.startedValidator startedValidator = MessageBundles.get(EmailBundles.startedValidator.class);
assertEquals("You will be notified with another email when it is your turn to sign.",
startedValidator.turnEmailWillBeSent());
assertEquals("You will be notified with another email when it is your turn to sign.",
engine.parse("{EmailBundles_startedValidator:turnEmailWillBeSent()}").render());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
started=In this process you will sign the document to validate it.
signingProcessStart=you have started a signing process {id} for document "{filename}".
nextNotification=You will be notified with another email when it is your signing turn.
documentAccessUrl=You may access the document in the following link:
subject=Signing process initiated by {customer} for file {filename}.
signForValidation=In this process you will sign the document to validate it.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
signForValidation=In this process you will sign the document to validate it.
started=has started a signing process {id} for the document "{filename}".
subject=Signing process initiated by {customer} for file {filename}.
turnEmailWillBeSent=You will be notified with another email when it is your turn to sign.
youMayAlreadyAccessDocument=You can access the document at the following link:
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
signForValidation=In this process you will sign the document to validate it.
started=has started a signing process {id} for the document "{filename}".
subject=Signing process initiated by {customer} for file {filename}.
turnEmailWillBeSent=You will be notified with another email when it is your turn to sign.
youMayAlreadyAccessDocument=You can access the document at the following link:
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
started=In this process you will sign the document to validate it.
signingProcessStart=you have started a signing process {id} for document "{filename}".
nextNotification=You will be notified with another email when it is your signing turn.
documentAccessUrl=You may access the document in the following link:
subject=Signing process initiated by {customer} for file {filename}.
signForValidation=In this process you will sign the document to validate it.