From 4f6f6b1c4d82ef9fc503b2b21c53433ea3c32d15 Mon Sep 17 00:00:00 2001 From: ksaintin Date: Wed, 20 Nov 2024 14:52:25 +0100 Subject: [PATCH] Just print the differences between messages_{LOCALE} properties and print a warning message instead of build failure. --- .../java/org/phoebus/framework/nls/NLS.java | 63 +++++++++++++++++- .../framework/nls/CheckMessagesFiles.txt | 1 + .../framework/nls/NLSMessagesTest.java | 66 +++---------------- 3 files changed, 73 insertions(+), 57 deletions(-) create mode 100644 core/framework/src/main/resources/org/phoebus/framework/nls/CheckMessagesFiles.txt diff --git a/core/framework/src/main/java/org/phoebus/framework/nls/NLS.java b/core/framework/src/main/java/org/phoebus/framework/nls/NLS.java index d4d0ca2fa0..5633186ed9 100644 --- a/core/framework/src/main/java/org/phoebus/framework/nls/NLS.java +++ b/core/framework/src/main/java/org/phoebus/framework/nls/NLS.java @@ -156,6 +156,45 @@ public static List checkMessageFilesDifferences(Class clazz) { return checkMessageFilesDifferences(resource.getFile()); } + /** + * Use for unit test only + * Check if all the existing messages_{LOCALE}.properties are synchronized on default messages.propertiesresource in the project + * + * @return the list of difference between the default resources , null or empty if it is synchronized + */ + public static List checkAllMessageFilesDifferences(){ + List differences = new ArrayList<>(); + URL resource = NLS.class.getResource("CheckMessagesFiles.txt"); + if (resource != null) { + String filePath = resource.getFile(); + System.out.println(filePath); + String[] split = filePath.split("/core/framework/"); + //First part is the parent folder + String parentFolder = split != null && split.length > 0 ? split[0] : null; + System.out.println("parentFolder=" + parentFolder); + File parentFile = new File(parentFolder); + List fileList = listMessagesFiles(parentFile); + + for (File file : fileList) { + List diff = NLS.checkMessageFilesDifferences(file.getAbsolutePath()); + if (diff != null && !diff.isEmpty()) { + differences.addAll(diff); + } + } + + if(differences.isEmpty()) { + System.out.println("All the "+ MESSAGE+ "_{LOCALE}.properties files are syncronized "); + } + else { + System.out.println("**There is " + differences.size() + " difference(s) found**"); + for (String dif : differences) { + System.out.println(dif); + } + } + } + return differences; + } + /** * Use for unit test only * Check if the existing messages_{LOCALE}.properties are synchronized on default messages.propertiesresource @@ -163,7 +202,7 @@ public static List checkMessageFilesDifferences(Class clazz) { * @param clazz Class relative to which message resources are located * @return the list of difference between the default ressources , null or empty if it is synchronized */ - public static List checkMessageFilesDifferences(String resourceFile) { + private static List checkMessageFilesDifferences(String resourceFile) { List differences = new ArrayList<>(); if (resourceFile != null) { try { @@ -239,6 +278,28 @@ public boolean accept(File dir, String name) { return differences; } + private static List listMessagesFiles(File folder) { + String filename = MESSAGE + ".properties"; + List fileList = new ArrayList<>(); + //Ignore target folder from build + if(folder != null && folder.isDirectory() + && !folder.getAbsolutePath().contains("\\target\\") + && !folder.getAbsolutePath().contains("\\test\\")) { + File[] listFiles = folder.listFiles(); + for(File file : listFiles) { + if(file.isDirectory()) { + List list = listMessagesFiles(file); + fileList.addAll(list); + } + else if (file.getName().equals(filename)){ + fileList.add(file); + } + } + } + return fileList; + } + + /** * To get Locale from a countryCode * diff --git a/core/framework/src/main/resources/org/phoebus/framework/nls/CheckMessagesFiles.txt b/core/framework/src/main/resources/org/phoebus/framework/nls/CheckMessagesFiles.txt new file mode 100644 index 0000000000..b3cee89460 --- /dev/null +++ b/core/framework/src/main/resources/org/phoebus/framework/nls/CheckMessagesFiles.txt @@ -0,0 +1 @@ +This file is used by org.phoebus.framework.nls.NLS.java class to check the synchronization between messages_{LOCALE}.properties and messages.properties in the Phoebus project \ No newline at end of file diff --git a/core/framework/src/test/java/org/phoebus/framework/nls/NLSMessagesTest.java b/core/framework/src/test/java/org/phoebus/framework/nls/NLSMessagesTest.java index 140f23f094..d4b31729c9 100644 --- a/core/framework/src/test/java/org/phoebus/framework/nls/NLSMessagesTest.java +++ b/core/framework/src/test/java/org/phoebus/framework/nls/NLSMessagesTest.java @@ -11,9 +11,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import java.io.File; -import java.net.URL; -import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -33,7 +30,6 @@ public class NLSMessagesTest public static String HowAreYou; public static String MissingMessage; public static String AppVersion; - private static final String FILENAME = NLS.MESSAGE + ".properties"; private static Locale original; // They are initialized from a "messages*.properties" file in the same package, @@ -41,7 +37,7 @@ public class NLSMessagesTest // // static // { - // NLS.initializeMessages(NLSMessagesTest.class); + // NLS.initializeMessages(NLSMessagesTest.class); // } // // For the test, we call NLS.initializeMessages with various locates @@ -102,70 +98,28 @@ public void testUpdateMessages() { List difference = NLS.checkMessageFilesDifferences(NLSMessagesTest.class); System.out.println("**There is " + (difference != null ? difference.size() : 0) + " difference(s) found**"); boolean appversionFound = false; - if(difference != null && !difference.isEmpty()) { - for(String dif : difference) { + if (difference != null && !difference.isEmpty()) { + for (String dif : difference) { System.out.println(dif); appversionFound = !appversionFound && dif.contains("AppVersion"); } } - + assertThat("Env variable value is ignored", !appversionFound); assertThat("Differences between properties", difference.size() == 3); } - + @Test public void checkAllMessagesResources() { - URL resource = NLSMessagesTest.class.getResource(FILENAME); - if (resource != null) { - String filePath = resource.getFile(); - System.out.println(filePath); - String[] split = filePath.split("/core/framework/"); - //First part is the parent folder - String parentFolder = split != null && split.length > 0 ? split[0] : null; - System.out.println("parentFolder=" + parentFolder); - File parentFile = new File(parentFolder); - List fileList = listMessagesFiles(parentFile); - List differences = new ArrayList<>(); - for (File file : fileList) { - List diff = NLS.checkMessageFilesDifferences(file.getAbsolutePath()); - if (diff != null && !diff.isEmpty()) { - differences.addAll(diff); - } - } - - System.out.println("**There is " + differences.size() + " difference(s) found**"); - for (String dif : differences) { - System.out.println(dif); - } - assertThat("All resources are synchronize ", differences.isEmpty()); - - } - } - - private static List listMessagesFiles(File folder) { - List fileList = new ArrayList<>(); - //Ignore target folder from build - if(folder != null && folder.isDirectory() - && !folder.getAbsolutePath().contains("\\target\\") - && !folder.getAbsolutePath().contains("\\test\\")) { - File[] listFiles = folder.listFiles(); - for(File file : listFiles) { - if(file.isDirectory()) { - List list = listMessagesFiles(file); - fileList.addAll(list); - } - else if (file.getName().equals(FILENAME)){ - fileList.add(file); - } - } + List differences = NLS.checkAllMessageFilesDifferences(); + if (differences != null && !differences.isEmpty()) { + System.out.println("WARNING ! there is " + differences.size() + " difference(s) detected in Messages_{LOCAL}.properties files"); } - return fileList; + //assertThat("All resources are synchronize ", differences.isEmpty()); } - @AfterAll - public static void restoreLocale() - { + public static void restoreLocale() { Locale.setDefault(original); } }