Skip to content

Commit

Permalink
Add test to check encoding of message properties
Browse files Browse the repository at this point in the history
  • Loading branch information
solth committed Nov 29, 2024
1 parent 256951c commit 41ffa29
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Kitodo/src/main/resources/messages/errors_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ error=Fehler...
errors=Fehler

# A
errorAdding=Fehler beim Hinzuf\u00FCgen von ''{0}''
errorAdding=Fehler beim Hinzufügen von ''{0}''
authorityAssignedError=Berechtigung kann nicht gel\u00F6scht werden, da dieser noch Rollen zugewiesen sind.

# B
batchPropertyEmpty=Die Eigenschaft {0} im Vorgang {1} ist ein Pflichtfeld, enth\u00E4lt aber keinen Wert.
batchPropertyEmpty=Die Eigenschaft {0} im Vorgang {1} ist ein Pflichtfeld, enthält aber keinen Wert.

# C
calendar.upload.error=Fehler beim Laden der Datei\:
calendar.upload.isEmpty=Es wurde keine Datei \u00FCbertragen.
calendar.upload.isEmpty=Es wurde keine Datei übertragen.
calendar.upload.missingMandatoryElement=Ein erforderliches XML-Element konnte nicht gefunden werden.
calendar.upload.missingMandatoryValue=Ein erforderlicher Wert konnte nicht gefunden werden.
calendar.upload.overlappingDateRanges=Der Erscheinungsverlauf konnte nicht importiert werden, da sich Datumsbereiche von Bl\u00F6cken \u00FCberlappen\:
Expand Down Expand Up @@ -98,7 +98,7 @@ kitodoScript.importProcesses.indir.isNull=Parameter ''indir'' fehlt!
kitodoScript.importProcesses.indir.isNoDirectory=''indir'' ist kein existierendes Verzeichnis!
kitodoScript.importProcesses.indir.cannotExecute=Der Inhalt des Importverzeichnisses kann nicht abgerufen werden (fehlende Berechtigung \u201Cexecute\u201D)
kitodoScript.importProcesses.missingChildren=Vorgang {0} enth\u004E Verweise auf fehlendene Kindvorg\u004Enge {1}
kitodoScript.importProcesses.noProcessTitleRule=Regelsatz gibt keine Bildungsvorschrift für den ''processTitle'' für Division {0} an!
kitodoScript.importProcesses.noProcessTitleRule=Regelsatz gibt keine Bildungsvorschrift für den ''processTitle'' für Division {0} an!
kitodoScript.importProcesses.project.isNull=Parameter ''project'' fehlt!
kitodoScript.importProcesses.project.isNoProjectID=''project'' ist keine g\u00FCltige Projekt-ID
kitodoScript.importProcesses.project.noProjectWithID=Ein solches Projekt existiert nicht!
Expand Down
2 changes: 1 addition & 1 deletion Kitodo/src/main/resources/messages/errors_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ kitodoScript.importProcesses.project.noProjectWithID=¡El proyecto con ese ID no
kitodoScript.importProcesses.template.isNull=¡Falta la variable ''template''!
kitodoScript.importProcesses.template.isNoTemplateID=¡''template'' no es un entero!
kitodoScript.importProcesses.template.noTemplateWithID=No existe la plantilla con este ID
kitodoScript.noStructureOfTypeFound=No se encontró ningún elemento estructurado de tipo "{0}"
kitodoScript.noStructureOfTypeFound=No se encontró ningún elemento estructurado de tipo "{0}"

# L
errorLoadingDocTypes=Error de configuración del conjunto de reglas: No se ha encontrado ningún DocType ('division')
Expand Down
143 changes: 143 additions & 0 deletions Kitodo/src/test/java/org/kitodo/PropertyEncodingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo;

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

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertyEncodingTest {

private Properties messages;
private static final Charset CORRECT_ENCODING = StandardCharsets.UTF_8;
private static final Charset WRONG_ENCODING = StandardCharsets.ISO_8859_1;
private static final String MESSAGES_DIR = "src/main/resources/messages/";
private static final HashMap<String, String> GERMAN_TEST_MESSAGES = new HashMap<>();
private static final HashMap<String, String> GERMAN_ERROR_MESSAGES = new HashMap<>();
private static final HashMap<String, String> SPANISH_TEST_MESSAGES = new HashMap<>();
private static final HashMap<String, String> SPANISH_ERROR_MESSAGES = new HashMap<>();
private static final String GERMAN_MESSAGES = MESSAGES_DIR + "messages_de.properties";
private static final String SPANISH_MESSAGES = MESSAGES_DIR + "messages_es.properties";
private static final String GERMAN_ERRORS = MESSAGES_DIR + "errors_de.properties";
private static final String SPANISH_ERRORS = MESSAGES_DIR + "errors_es.properties";

@BeforeAll
public static void prepare() {
// prepare German test messages
GERMAN_TEST_MESSAGES.put("viewPageInNewWindow", "Seite in neuem Browser-Fenster öffnen");
GERMAN_TEST_MESSAGES.put("importConfig.searchFields.idPrefix", "ID-Präfix");
GERMAN_TEST_MESSAGES.put("addMappingFile", "Abbildungsdatei hinzufügen");

// prepare German error test message
GERMAN_ERROR_MESSAGES.put("errorAdding", "Fehler beim Hinzufügen von ''{0}''");
GERMAN_ERROR_MESSAGES.put("batchPropertyEmpty", "Die Eigenschaft {0} im Vorgang {1} ist ein Pflichtfeld, enthält aber keinen Wert.");
GERMAN_ERROR_MESSAGES.put("calendar.upload.isEmpty", "Es wurde keine Datei übertragen.");

// prepare Spanish test messages
SPANISH_TEST_MESSAGES.put("confirm", "Esta acción puede llevar algún tiempo. ¿Estás seguro de que deseas continuar?");
SPANISH_TEST_MESSAGES.put("passwordChanged", "La contraseña se ha cambiado con éxito.");
SPANISH_TEST_MESSAGES.put("projectNotAssignedToCurrentUser", "¡El proyecto \"{0}\" no está asignado al usuario actual!");

// prepare Spanish error test messages
SPANISH_ERROR_MESSAGES.put("errorAdding", "Error al añadir ''{0}''");
SPANISH_ERROR_MESSAGES.put("projectTitleAlreadyInUse", "El título del proyecto ya está en uso.");
SPANISH_ERROR_MESSAGES.put("workflowExceptionParallelGatewayNoTask", "¡Ninguna tarea sigue la ramificación paralela!");
}

@Test
public void checkCharacterEncodingOfGermanMessagesFile() throws Exception {
// ensure messages are decoded as expected using UTF-8 encoding
messages = readPropertiesWithEncoding(GERMAN_MESSAGES, CORRECT_ENCODING);
for (Map.Entry<String, String> testMessage : GERMAN_TEST_MESSAGES.entrySet()) {
assertEquals(messages.getProperty(testMessage.getKey()), testMessage.getValue());
}

// ensure messages are _not_ decoded as expected using ISO_8859_1 encoding
messages = readPropertiesWithEncoding(GERMAN_MESSAGES, WRONG_ENCODING);
for (Map.Entry<String, String> testMessage : GERMAN_TEST_MESSAGES.entrySet()) {
assertNotEquals(messages.getProperty(testMessage.getKey()), testMessage.getValue());
}
}

@Test
public void checkCharacterEncodingsOfGermanErrorMessageFile() throws Exception {
// ensure messages are decoded as expected using UTF-8 encoding
messages = readPropertiesWithEncoding(GERMAN_ERRORS, CORRECT_ENCODING);
for (Map.Entry<String, String> testMessage : GERMAN_ERROR_MESSAGES.entrySet()) {
assertEquals(messages.getProperty(testMessage.getKey()), testMessage.getValue());
}

// ensure messages are _not_ decoded as expected using ISO_8859_1 encoding
messages = readPropertiesWithEncoding(GERMAN_ERRORS, WRONG_ENCODING);
for (Map.Entry<String, String> testMessage : GERMAN_ERROR_MESSAGES.entrySet()) {
assertNotEquals(messages.getProperty(testMessage.getKey()), testMessage.getValue());
}
}

@Test
public void checkCharacterEncodingsOfSpanishMessagesFile() throws Exception {
// ensure messages are decoded as expected using UTF-8 encoding
messages = readPropertiesWithEncoding(SPANISH_MESSAGES, CORRECT_ENCODING);
for (Map.Entry<String, String> testMessage : SPANISH_TEST_MESSAGES.entrySet()) {
assertEquals(messages.getProperty(testMessage.getKey()), testMessage.getValue());
}

// ensure messages are _not_ decoded as expected using ISO_8859_1 encoding
messages = readPropertiesWithEncoding(SPANISH_MESSAGES, WRONG_ENCODING);
for (Map.Entry<String, String> testMessage : SPANISH_TEST_MESSAGES.entrySet()) {
assertNotEquals(messages.getProperty(testMessage.getKey()), testMessage.getValue());
}
}

@Test
public void checkCharacterEncodingsOfSpanishErrorMessagesFile() throws Exception {
// ensure messages are decoded as expected using UTF-8 encoding
messages = readPropertiesWithEncoding(SPANISH_ERRORS, CORRECT_ENCODING);
for (Map.Entry<String, String> testMessage : SPANISH_ERROR_MESSAGES.entrySet()) {
assertEquals(messages.getProperty(testMessage.getKey()), testMessage.getValue());
}

// ensure messages are _not_ decoded as expected using ISO_8859_1 encoding
messages = readPropertiesWithEncoding(SPANISH_ERRORS, WRONG_ENCODING);
for (Map.Entry<String, String> testMessage : SPANISH_ERROR_MESSAGES.entrySet()) {
assertNotEquals(messages.getProperty(testMessage.getKey()), testMessage.getValue());
}
}

private Properties readPropertiesWithEncoding(String pathString, Charset encoding)
throws IOException {
Path path = Paths.get(pathString);
assertTrue(Files.exists(path));
Properties properties = new Properties();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path.toFile()),
encoding))) {
properties.load(reader);
return properties;
}
}
}

0 comments on commit 41ffa29

Please sign in to comment.