forked from kitodo/kitodo-production
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test to check encoding of message properties
- Loading branch information
Showing
3 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
Kitodo/src/test/java/org/kitodo/PropertyEncodingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |