mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

kenneth_suter
17.03.2007 45ce51c3a5db743e9ffaf5e8e4fa0db7ad338b61
enhanced tests for Message
2 files modified
121 ■■■■ changed files
opends/src/messages/src/org/opends/messages/Message.java 29 ●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessageTest.java 92 ●●●● patch | view | raw | blame | history
opends/src/messages/src/org/opends/messages/Message.java
@@ -30,7 +30,7 @@
import java.util.Locale;
import java.util.Formatter;
import java.util.Formattable;
import java.util.UnknownFormatConversionException;
import java.util.IllegalFormatException;
/**
 * Renders sensitive textural strings.  In most cases message are intended
@@ -57,6 +57,11 @@
   * category of <code>Categore.USER_DEFINED</code> and a severity
   * of <code>Severity.INFORMATION</code>
   *
   * Note that the types for <code>args</code> must be consistent with any
   * argument specifiers appearing in <code>formatString</code> according
   * to the rules of java.util.Formatter.  A mismatch in type information
   * will cause this message to render without argument substitution.
   *
   * Before using this method you should be sure that the message you
   * are creating is locale sensitive.  If so you should instead create
   * a formal message.
@@ -80,6 +85,11 @@
   * the same way regardless of the locale requested in
   * <code>toString(Locale)</code>.
   *
   * Note that the types for <code>args</code> must be consistent with any
   * argument specifiers appearing in <code>formatString</code> according
   * to the rules of java.util.Formatter.  A mismatch in type information
   * will cause this message to render without argument substitution.
   *
   * Before using this method you should be sure that the message you
   * are creating is locale sensitive.  If so you should instead create
   * a formal message.
@@ -108,6 +118,12 @@
  /**
   * Creates an uninternationalized message from the string representation
   * of an object.
   *
   * Note that the types for <code>args</code> must be consistent with any
   * argument specifiers appearing in <code>formatString</code> according
   * to the rules of java.util.Formatter.  A mismatch in type information
   * will cause this message to render without argument substitution.
   *
   * @param object from which the message will be created
   * @param arguments for message
   * @return a message object that will render the same in all locales;
@@ -147,8 +163,8 @@
  }
  /**
   * Gets the string representation of this message
   * appropriate for <code>locale</code>.
   * Gets the string representation of this message appropriate for
   * <code>locale</code>.
   * @param locale for which the string representation
   *        will be returned
   * @return String representation of this message
@@ -159,8 +175,11 @@
    if (needsFormatting(fmt)) {
      try {
        s = new Formatter(locale).format(locale, fmt, args).toString();
      } catch (UnknownFormatConversionException e) {
        s = fmt; // This shouldn't happen but just in case...
      } catch (IllegalFormatException e) {
        // This should not happend with any of our internal messages.
        // However, this may happen for raw messages that have a
        // mismatch between argument specifier type and argument type.
        s = fmt;
      }
    } else {
      s = fmt;
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessageTest.java
@@ -29,17 +29,30 @@
import org.testng.annotations.*;
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.TestCaseUtils;
import static org.testng.Assert.*;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
/**
 * Message Tester.
 */
public class MessageTest extends DirectoryServerTestCase {
  //@BeforeClass
  public void setUp() {
  /** Locale for accessing a pseudo localized test messages file. */
  private static final Locale TEST_LOCALE = Locale.CHINA;
  /** Message to appear in pseudo localized test messages file. */
  private static final String TEST_MSG = "XXX";
  @BeforeClass
  public void setUp() throws IOException {
    createDummyLocalizedCoreMessagesFile();
  }
  @DataProvider(name = "rawData")
@@ -52,6 +65,9 @@
            {"Hel%nlo", "Hel\nlo", new Object[]{}},
            {"Hel%Dlo", "Hel%Dlo", new Object[]{}},
            {"Hel%Dlo", "Hel%Dlo", new Object[]{ "abc"}},
            // %d and String arg mismatch
            {"Hel%dlo", "Hel%dlo", new Object[]{ "abc"}},
    };
  }
@@ -66,28 +82,57 @@
  @Test(dataProvider = "rawData")
  public void testRaw(String fmt, String result, Object... args) {
    Message message = Message.raw(fmt, args);
    assert (message.toString().equals(result));
    assert (message.toString(Locale.CHINESE).equals(result));
    assertTrue(message.toString().equals(result));
    assertTrue(message.toString(TEST_LOCALE).equals(result));
  }
  @Test(dataProvider = "rawData1")
  public void testRaw1(String fmt, Category c, Severity s,
                       String result, Object... args) {
    Message message = Message.raw(c, s, fmt, args);
    assert (message.toString().equals(result));
    assert (message.toString(Locale.CHINESE).equals(result));
    assertTrue(message.toString().equals(result));
    assertTrue(message.toString(TEST_LOCALE).equals(result));
  }
  //@Test
  public void testToString() {
    //TODO: Test goes here...
    assert false : "testToString not implemented.";
  @DataProvider(name = "messages")
  public Object[][] getMessages() {
    return new Object[][]
    {
      { Message.raw("Hi Ho") },
      { Message.raw("Hi Ho %n") },
      { Message.raw("Hi Ho %%") },
      { Message.raw("Hi Ho %s", new Object[]{null}) },
      { Message.raw("Hi Ho", "Hum") },
      { Message.raw("Hi Ho %s", "Hum") },
      { Message.raw("Hi Ho %d", "Hum") },
      { Message.raw("Hi Ho %c", "Hum") },
      { Message.raw("Hi Ho %d", "Hum") },
      { CoreMessages.ERR_ADD_CANNOT_ADD_ROOT_DSE.get() },
      { CoreMessages.ERR_ABANDON_OP_NO_SUCH_OPERATION.get(1) },
      { CoreMessages.ERR_ABANDON_OP_NO_SUCH_OPERATION.get(null) },
      { CoreMessages.ERR_ADD_CANNOT_ADD_ROOT_DSE.get() }
    };
  }
  //@Test
  public void testToString1() {
    //TODO: Test goes here...
    assert false : "testToString1 not implemented.";
  @DataProvider(name = "localizableMessages")
  public Object[][] getLocalizableMessages() {
    return new Object[][]
    {
      { CoreMessages.ERR_ADD_CANNOT_ADD_ROOT_DSE.get() },
      { CoreMessages.ERR_ABANDON_OP_NO_SUCH_OPERATION.get(1) },
      { CoreMessages.ERR_ABANDON_OP_NO_SUCH_OPERATION.get(null) },
      { CoreMessages.ERR_ADD_CANNOT_ADD_ROOT_DSE.get() }
    };
  }
  @Test(dataProvider = "messages")
  public void testToString(Message msg) {
    assertNotNull(msg.toString());
  }
  @Test(dataProvider = "localizableMessages")
  public void testToString1(Message msg) throws IOException {
    assertEquals(msg.toString(TEST_LOCALE), TEST_MSG);
  }
  @Test(dataProvider = "rawData1")
@@ -104,4 +149,23 @@
    assert(desc2.getSeverity().equals(Severity.INFORMATION));
  }
  private void createDummyLocalizedCoreMessagesFile() throws IOException {
    Properties corePseudoI18nMsgs = new Properties();
    ResourceBundle coreDefaultMsgs = ResourceBundle.getBundle("messages/core");
    for (Object key : coreDefaultMsgs.keySet()) {
      corePseudoI18nMsgs.put(key, TEST_MSG);
    }
    File buildRoot = new File(System.getProperty(TestCaseUtils.PROPERTY_BUILD_ROOT));
    File corePseudoI18nMsgsFile = new File(buildRoot,
            "build" + File.separator + "unit-tests" +
                    File.separator + "classes" +
                    File.separator + "messages" +
                    File.separator + "core_" + TEST_LOCALE.getLanguage() +
                    ".properties");
    if (!corePseudoI18nMsgsFile.getParentFile().exists()) {
      corePseudoI18nMsgsFile.getParentFile().mkdirs();
    }
    corePseudoI18nMsgs.store(new FileOutputStream(corePseudoI18nMsgsFile), "");
  }
}