1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.util;
17  
18  import junit.framework.TestCase;
19  
20  import org.seasar.framework.message.MessageFormatter;
21  import org.seasar.uruma.core.UrumaMessageCodes;
22  import org.seasar.uruma.exception.NotFoundException;
23  
24  /**
25   * {@link MessageUtil} のためのテストクラスです。<br />
26   * 
27   * @author y-komori
28   */
29  public class MessageUtilTest extends TestCase {
30      private static final String TEST_BUNDLE = "testMessages";
31  
32      private static final String NG_BUNDLE = "ng";
33  
34      private static final String NG_KEY = "ngkey";
35  
36      private static final String TEST_MESSAGE1 = "TEST_MESSAGE1";
37  
38      private static final String TEST_MESSAGE2 = "TEST_MESSAGE2";
39  
40      /**
41       * {@link MessageUtil#getMessage(String)} メソッドのテストです。<br />
42       */
43      public void testGetMessage1() {
44          assertEquals("テストメッセージ1", MessageUtil.getMessage(TEST_MESSAGE1));
45      }
46  
47      /**
48       * {@link MessageUtil#getMessage(String, Object...)} メソッドのテストです。<br />
49       */
50      public void testGetMessage2() {
51          assertEquals("テストメッセージ2 引数 arg1 arg2 arg3", MessageUtil.getMessage(
52                  TEST_MESSAGE2, "arg1", "arg2", "arg3"));
53      }
54  
55      /**
56       * {@link MessageUtil#getMessageWithBundleName(String, String)} メソッドのテストです。<br />
57       */
58      public void testGetMessageWithBundleName1() {
59          assertEquals("1", "テストメッセージ1", MessageUtil.getMessageWithBundleName(
60                  TEST_BUNDLE, TEST_MESSAGE1));
61  
62          String actual = MessageFormatter.getSimpleMessage(
63                  UrumaMessageCodes.MESSAGE_KEY_NOT_FOUND, new Object[] {
64                          TEST_BUNDLE, NG_KEY });
65          assertEquals("2", actual, MessageUtil.getMessageWithBundleName(
66                  TEST_BUNDLE, NG_KEY));
67      }
68  
69      /**
70       * {@link MessageUtil#getMessageWithBundleName(String, String, Object...)}
71       * メソッドのテストです。<br />
72       */
73      public void testGetMessageWithBundleName2() {
74          assertEquals("1", "テストメッセージ2 引数 arg1 arg2 arg3", MessageUtil
75                  .getMessageWithBundleName(TEST_BUNDLE, TEST_MESSAGE2, "arg1",
76                          "arg2", "arg3"));
77  
78          try {
79              MessageUtil.getMessageWithBundleName(NG_BUNDLE, TEST_MESSAGE2,
80                      "arg1", "arg2", "arg3");
81              fail("2");
82          } catch (NotFoundException ex) {
83              assertTrue(true);
84          }
85      }
86  }