Skip to content

Commit

Permalink
Just print the differences between messages_{LOCALE} properties and
Browse files Browse the repository at this point in the history
print a warning message instead of build failure.
  • Loading branch information
katysaintin committed Nov 20, 2024
1 parent da382b8 commit 4f6f6b1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 57 deletions.
63 changes: 62 additions & 1 deletion core/framework/src/main/java/org/phoebus/framework/nls/NLS.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,53 @@ public static List<String> 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<String> checkAllMessageFilesDifferences(){
List<String> 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<File> fileList = listMessagesFiles(parentFile);

for (File file : fileList) {
List<String> 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
*
* @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<String> checkMessageFilesDifferences(String resourceFile) {
private static List<String> checkMessageFilesDifferences(String resourceFile) {
List<String> differences = new ArrayList<>();
if (resourceFile != null) {
try {
Expand Down Expand Up @@ -239,6 +278,28 @@ public boolean accept(File dir, String name) {
return differences;
}

private static List<File> listMessagesFiles(File folder) {
String filename = MESSAGE + ".properties";
List<File> 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<File> list = listMessagesFiles(file);
fileList.addAll(list);
}
else if (file.getName().equals(filename)){
fileList.add(file);
}
}
}
return fileList;
}


/**
* To get Locale from a countryCode
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,15 +30,14 @@ 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,
// typically using 'static' code like this:
//
// static
// {
// NLS.initializeMessages(NLSMessagesTest.class);
// NLS.initializeMessages(NLSMessagesTest.class);
// }
//
// For the test, we call NLS.initializeMessages with various locates
Expand Down Expand Up @@ -102,70 +98,28 @@ public void testUpdateMessages() {
List<String> 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<File> fileList = listMessagesFiles(parentFile);
List<String> differences = new ArrayList<>();
for (File file : fileList) {
List<String> 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<File> listMessagesFiles(File folder) {
List<File> 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<File> list = listMessagesFiles(file);
fileList.addAll(list);
}
else if (file.getName().equals(FILENAME)){
fileList.add(file);
}
}
List<String> 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);
}
}

0 comments on commit 4f6f6b1

Please sign in to comment.