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

Use PropertyResourceBundle in test suite and validate .properties files are not both valid UTF-8 and valid ISO-8859-1 #433

Merged
merged 3 commits into from
May 10, 2022
Merged
Changes from 1 commit
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
46 changes: 45 additions & 1 deletion src/main/java/org/jvnet/hudson/test/PropertiesTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,22 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.io.IOUtils;

import static org.jvnet.hudson.test.JellyTestSuiteBuilder.scan;

/**
Expand Down Expand Up @@ -66,11 +78,43 @@ public synchronized Object put(Object key, Object value) {
return null;
}
};

byte[] contents = IOUtils.toByteArray(resource.openStream());
timja marked this conversation as resolved.
Show resolved Hide resolved
if (!isEncoded(contents, StandardCharsets.US_ASCII)) {
boolean isUtf8 = isEncoded(contents, StandardCharsets.UTF_8);
boolean isIso88591 = isEncoded(contents, StandardCharsets.ISO_8859_1);
if (isUtf8 && isIso88591) {
throw new AssertionError(resource + " is encoded with both valid utf-8 and ISO-8859-1 characters, the utf-8 characters need to be encoded with something like `native2ascii`");
timja marked this conversation as resolved.
Show resolved Hide resolved
}
}

try (InputStream is = resource.openStream()) {
props.load(is);
PropertyResourceBundle propertyResourceBundle = new PropertyResourceBundle(is);
Enumeration<String> keys = propertyResourceBundle.getKeys();
// TODO Java 9+ can use 'asIterator' and get rid of below collections conversion
List<String> keysAsSaneType = Collections.list(keys);

for (String localKey : keysAsSaneType) {
String value = propertyResourceBundle.getString(localKey);
props.setProperty(localKey, value);
}
}
}

}

private static boolean isEncoded(byte[] bytes, Charset charset) {
CharsetDecoder decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
ByteBuffer buffer = ByteBuffer.wrap(bytes);

try {
decoder.decode(buffer);
return true;
} catch (CharacterCodingException e) {
return false;
}
}

}