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

kenneth_suter
13.17.2007 2200390db35c05d494c33417d9914a10f67a74e0
issue 2142: fixes MessageBuilder.toMessage to that the resulting message maintains the ability to be rendered in locales different that the local locale.
1 files added
3 files modified
233 ■■■■ changed files
opends/src/messages/src/org/opends/messages/MessageBuilder.java 34 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessageBuilderTest.java 125 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessageTest.java 40 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessagesTestCase.java 34 ●●●●● patch | view | raw | blame | history
opends/src/messages/src/org/opends/messages/MessageBuilder.java
@@ -158,13 +158,17 @@
  /**
   * Append a string to this builder.
   *
   * @param rawString to append
   * @param cs to append
   * @return reference to this builder
   */
  public MessageBuilder append(CharSequence rawString) {
    if (rawString != null) {
      sb.append(rawString);
      messages.add(Message.raw(rawString));
  public MessageBuilder append(CharSequence cs) {
    if (cs != null) {
      sb.append(cs);
      if (cs instanceof Message) {
        messages.add((Message)cs);
      } else {
        messages.add(Message.raw(cs));
      }
    }
    return this;
  }
@@ -254,21 +258,11 @@
   * @return Message raw message representing builder content
   */
  public Message toMessage() {
    return Message.raw(sb.toString());
  }
  /**
   * Returns a raw message representation of the appended
   * content in a specific locale.  Only <code>Message</code>s
   * appended to this builder are rendered in the requested
   * locale.  Raw strings appended to this buffer are not
   * translated to different locale.
   *
   * @param locale requested
   * @return Message raw message representing builder content
   */
  public Message toMessage(Locale locale) {
    return Message.raw(toString(locale));
    StringBuffer fmtString = new StringBuffer();
    for (int i = 0; i < messages.size(); i++) {
      fmtString.append("%s");
    }
    return Message.raw(fmtString, messages.toArray());
  }
  /**
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessageBuilderTest.java
New file
@@ -0,0 +1,125 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2007 Sun Microsystems, Inc.
 */
package org.opends.messages;
import static org.testng.Assert.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.Locale;
/**
 * Message Tester.
 */
public class MessageBuilderTest extends MessagesTestCase {
  @BeforeClass
  public void setUp() throws IOException {
    createDummyLocalizedCoreMessagesFile();
  }
  @DataProvider(name = "toMessageData")
  public Object[][] toMessageData() {
    return new Object[][]{
            // All strings
            { new CharSequence[] {
                    "Once", " upon", " a", " time." },
                    "Once upon a time." },
            // All messages
            { new CharSequence[] {
                    Message.raw("Once"),
                    Message.raw(" upon"),
                    Message.raw(" a"),
                    Message.raw(" time.") },
                    "Once upon a time." },
            // Mix of strings and messages
            { new CharSequence[] {
                    Message.raw("Once"),
                    " upon",
                    Message.raw(" a"),
                    " time." },
                    "Once upon a time." },
    };
  }
  @Test (dataProvider = "toMessageData")
  public void testToMessage(CharSequence[] content, String result)
  {
    MessageBuilder mb = new MessageBuilder();
    for (CharSequence c : content) {
      mb.append(c);
    }
    Message m = mb.toMessage();
    assertTrue(result.equals(m.toString()));
  }
  @DataProvider(name = "toMessageData1")
  public Object[][] toMessageData1() {
    return new Object[][]{
            // default locale
            { new CharSequence[] {
                    CoreMessages.ERR_ADD_CANNOT_ADD_ROOT_DSE.get(),
                    CoreMessages.ERR_ABANDON_OP_NO_SUCH_OPERATION.get(1) },
                    Locale.getDefault(),
                    CoreMessages.ERR_ADD_CANNOT_ADD_ROOT_DSE.get().toString() +
                    CoreMessages.ERR_ABANDON_OP_NO_SUCH_OPERATION.get(1).toString() },
            { new CharSequence[] {
                    CoreMessages.ERR_ADD_CANNOT_ADD_ROOT_DSE.get(),
                    CoreMessages.ERR_ABANDON_OP_NO_SUCH_OPERATION.get(1) },
                    TEST_LOCALE,
                    CoreMessages.ERR_ADD_CANNOT_ADD_ROOT_DSE.get().toString(TEST_LOCALE) +
                    CoreMessages.ERR_ABANDON_OP_NO_SUCH_OPERATION.get(1).toString(TEST_LOCALE) }
    };
  }
  @Test (dataProvider = "toMessageData1")
  public void testToMessage1(CharSequence[] content, Locale locale, String result)
  {
    MessageBuilder mb = new MessageBuilder();
    for (CharSequence c : content) {
      mb.append(c);
    }
    Message m = mb.toMessage();
    System.out.println("result:" + result);
    System.out.println("message:" + m.toString(locale));
    assertTrue(result.equals(m.toString(locale)));
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessageTest.java
@@ -27,32 +27,18 @@
package org.opends.messages;
import org.testng.annotations.*;
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.TestCaseUtils;
import static org.testng.Assert.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Enumeration;
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 MessagesTestCase {
  /** 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";
  private static final String EOL = System.getProperty("line.separator");
  @BeforeClass
  public void setUp() throws IOException {
    createDummyLocalizedCoreMessagesFile();
@@ -156,24 +142,4 @@
    assert(desc2.getSeverity().equals(Severity.INFORMATION));
  }
  private void createDummyLocalizedCoreMessagesFile() throws IOException {
    Properties corePseudoI18nMsgs = new Properties();
    ResourceBundle coreDefaultMsgs = ResourceBundle.getBundle("messages/core");
    Enumeration<String> keyEnum = coreDefaultMsgs.getKeys();
    while (keyEnum.hasMoreElements()) {
      corePseudoI18nMsgs.put(keyEnum.nextElement(), 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), "");
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessagesTestCase.java
@@ -31,7 +31,15 @@
import org.testng.annotations.Test;
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.TestCaseUtils;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Enumeration;
import java.util.Locale;
/**
@@ -41,6 +49,32 @@
public abstract class MessagesTestCase
       extends DirectoryServerTestCase
{
  /** Locale for accessing a pseudo localized test messages file. */
  protected static final Locale TEST_LOCALE = Locale.CHINA;
  /** Message to appear in pseudo localized test messages file. */
  protected static final String TEST_MSG = "XXX";
  protected static final String EOL = System.getProperty("line.separator");
  // No implementation required.
  protected void createDummyLocalizedCoreMessagesFile() throws IOException {
    Properties corePseudoI18nMsgs = new Properties();
    ResourceBundle coreDefaultMsgs = ResourceBundle.getBundle("messages/core");
    Enumeration<String> keyEnum = coreDefaultMsgs.getKeys();
    while (keyEnum.hasMoreElements()) {
      corePseudoI18nMsgs.put(keyEnum.nextElement(), 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), "");
  }
}