2086N/A/*
2086N/A * CDDL HEADER START
2086N/A *
2086N/A * The contents of this file are subject to the terms of the
2086N/A * Common Development and Distribution License, Version 1.0 only
2086N/A * (the "License"). You may not use this file except in compliance
2086N/A * with the License.
2086N/A *
2086N/A * You can obtain a copy of the license at
2086N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
2086N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
2086N/A * See the License for the specific language governing permissions
2086N/A * and limitations under the License.
2086N/A *
2086N/A * When distributing Covered Code, include this CDDL HEADER in each
2086N/A * file and include the License file at
2086N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
2086N/A * add the following below this CDDL HEADER, with the fields enclosed
2086N/A * by brackets "[]" replaced with your own identifying information:
2086N/A * Portions Copyright [yyyy] [name of copyright owner]
2086N/A *
2086N/A * CDDL HEADER END
2086N/A *
2086N/A *
3232N/A * Copyright 2006-2008 Sun Microsystems, Inc.
2086N/A */
2086N/A
2086N/Apackage org.opends.messages;
2086N/A
2086N/Aimport static org.testng.Assert.*;
2086N/Aimport org.testng.annotations.*;
2086N/A
2122N/Aimport java.util.Set;
2122N/Aimport java.util.List;
2122N/Aimport java.util.ArrayList;
2122N/Aimport java.util.Locale;
2122N/Aimport java.util.EnumSet;
2122N/Aimport java.util.Map;
2122N/Aimport java.util.HashMap;
2122N/Aimport java.lang.reflect.Field;
2122N/A
2086N/A/**
2086N/A * MessageDescriptorRegistry Tester.
2086N/A *
2086N/A */
2123N/Apublic class MessageDescriptorRegistryTest extends MessagesTestCase
2086N/A{
2086N/A
2086N/A @DataProvider(name = "message descriptors")
2086N/A public Object[][] getMessageDescriptors() {
2086N/A return new Object[][] {
2086N/A { CoreMessages.ERR_ABANDON_OP_NO_SUCH_OPERATION }
2086N/A };
2086N/A }
2086N/A
2086N/A @Test(dataProvider = "message descriptors")
2086N/A public void testGetMessageDescriptor(MessageDescriptor md) {
2122N/A MessageDescriptor md2 = MessageDescriptorRegistry.getMessageDescriptor(md.getId());
2122N/A assertEquals(md, md2);
2122N/A }
2122N/A
2122N/A @DataProvider(name = "message classes")
2122N/A public Object[][] getMessageClasses() {
2940N/A Set<Class<?>> mdClasses = MessageDescriptorRegistry.getRegisteredClasses();
2940N/A List<Class<?>> classesToTest = new ArrayList<Class<?>>(mdClasses);
2122N/A
2122N/A // These newer message files don't comply
2122N/A classesToTest.remove(AdminToolMessages.class);
2122N/A classesToTest.remove(QuickSetupMessages.class);
2122N/A
2122N/A Object[][] ret = new Object[classesToTest.size()][1];
2122N/A for (int i = 0; i < ret.length; i++) {
2122N/A ret[i] = new Object[] { classesToTest.get(i) };
2122N/A }
2122N/A return ret;
2086N/A }
2086N/A
2122N/A /**
2122N/A * Tests that messages don't end with a period (.) excluding those that end
2122N/A * with an ellipsis (...)
2122N/A *
2122N/A * @param messagesClass containing definitions of MessageDescriptor objects
2122N/A * @throws IllegalAccessException if there is a problem accessing the
2122N/A * class through reflection
2122N/A */
2122N/A @Test(dataProvider = "message classes")
2940N/A public void testFormatStringsDontEndWithPeriod(Class<?> messagesClass)
2122N/A throws IllegalAccessException
2122N/A {
2122N/A Field[] fa = messagesClass.getFields();
2122N/A if (fa != null) {
2122N/A for (Field f : fa) {
2122N/A Class<?> c = f.getType();
2122N/A if (MessageDescriptor.class.isAssignableFrom(c)) {
2122N/A MessageDescriptor md = (MessageDescriptor)f.get(null);
2122N/A String fmtString = md.getFormatString(Locale.getDefault());
2122N/A boolean bad = fmtString.endsWith(".") && !fmtString.endsWith("...");
2122N/A assertFalse(bad,
2122N/A "Format string for message descriptor " + f.getName() +
2122N/A " obtained through key " + md.getKey() +
2122N/A " defined in class " + messagesClass.getName() +
2122N/A " \'" + md.getFormatString(Locale.getDefault()) +
2122N/A "\' ends with a '.'");
2122N/A }
2122N/A }
2122N/A }
2122N/A }
2122N/A
2122N/A /**
2122N/A * Tests that messages for each category are restricted to a single
2122N/A * messages file and that each file only contains messages from a
2122N/A * single category.
2122N/A */
2122N/A @Test
2122N/A public void testCategoriesDontSpanFiles() {
2940N/A Map<Category,Class<?>> categoriesToClass = new HashMap<Category,Class<?>>();
2940N/A Set<?> categories = EnumSet.allOf(Category.class);
2940N/A Set<Class<?>> msgClasses = MessageDescriptorRegistry.getRegisteredClasses();
2940N/A for (Class<?> msgClass : msgClasses) {
2122N/A List<MessageDescriptor> mds =
2122N/A MessageDescriptorRegistry.getMessageDescriptorsForClass(msgClass);
2122N/A Category currentCategory = null;
2122N/A for (MessageDescriptor md : mds) {
2122N/A if (currentCategory == null) {
2122N/A currentCategory = md.getCategory();
2122N/A if (categories.contains(currentCategory)) {
2122N/A categories.remove(currentCategory);
2122N/A categoriesToClass.put(currentCategory, msgClass);
2122N/A } else {
2122N/A assertTrue(false,
2122N/A "Message file " + msgClass + " defines descriptors " +
2122N/A "for category " + currentCategory + " but message file " +
2122N/A categoriesToClass.get(currentCategory) + " defines descriptors " +
2122N/A "of " + currentCategory + ". Descriptors for a particular " +
2122N/A "category can only be defined in a single messages file.");
2122N/A }
2122N/A } else {
2122N/A boolean categoriesMatch = currentCategory.equals(md.getCategory());
2122N/A assertTrue(categoriesMatch,
2122N/A "Message file " + msgClass + " contains descriptors from at least " +
2122N/A "two different categories: descriptor of key " +
2122N/A md.getFormatString() + " is of category " + md.getCategory() +
2122N/A " but expected category was " + currentCategory);
2122N/A
2122N/A }
2122N/A }
2122N/A }
2122N/A }
2086N/A}