dk.clanie.test.util.MessageEnumTest Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2008, Claus Nielsen, [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package dk.clanie.test.util;
import static dk.clanie.collections.CollectionFactory.newArrayList;
import static org.junit.Assert.fail;
import java.util.EnumSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import dk.clanie.util.MessageDefinition;
/**
* Test Enumeration of MessageDefinitions.
*
* Tests an Enumeration of MessageDefinitions against it's ResourceBundle,
* checking that each Message is defined in the ResourceBundle, and that
* there are no unused entries in the ResourceBundle.
*
* Assumes that all MessageDefinitions in the Enumeration are defined in
* the same ResourceBundle.
*
* @author Claus Nielsen
*/
public class MessageEnumTest {
public static & MessageDefinition> void testMessageEnumeration(
Class messageEnum) {
EnumSet.allOf(messageEnum);
T[] enumConstants = messageEnum.getEnumConstants();
ResourceBundle bundle = enumConstants[0].getBundle();
Set unusedKeys = bundle.keySet();
List missingKeys = newArrayList();
for (T t : enumConstants) {
if (unusedKeys.remove(t.key()) || missingKeys.add(t.key()));
}
// If there are unused or missing keys fail with a message identifying them.
if (unusedKeys.size() > 0 || missingKeys.size() > 0) {
StringBuilder builder = new StringBuilder();
if (missingKeys.size() > 0) {
builder.append("Missing keys: ");
for (String key : missingKeys) {
builder.append(key).append(", ");
}
builder.setLength(builder.length() - ", ".length());
}
if (unusedKeys.size() > 0) {
if (builder.length() > 0) builder.append("; ");
builder.append("Unused keys: ");
for (String key : unusedKeys) {
builder.append(key).append(", ");
}
builder.setLength(builder.length() - ", ".length());
}
fail(builder.toString());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy