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.core;
17  
18  import java.lang.reflect.Field;
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.ResourceBundle;
23  
24  import junit.framework.TestCase;
25  
26  import org.seasar.framework.beans.BeanDesc;
27  import org.seasar.framework.beans.factory.BeanDescFactory;
28  import org.seasar.framework.message.MessageFormatter;
29  import org.seasar.framework.util.FieldUtil;
30  import org.seasar.framework.util.StringUtil;
31  
32  /**
33   * {@link UrumaMessageCodes} のためのテストクラスです。<br />
34   * 
35   * @author y-komori
36   */
37  public class UrumaMessageCodesTest extends TestCase {
38      /**
39       * {@link UrumaMessageCodes} に定義されている定数に対応するメッセージが URMMessages
40       * に登録されているかどうかをテストします。<br />
41       */
42      public void testConstants() {
43          BeanDesc desc = BeanDescFactory.getBeanDesc(UrumaMessageCodes.class);
44          int size = desc.getFieldSize();
45  
46          for (int i = 0; i < size; i++) {
47              Field field = desc.getField(i);
48              String code = FieldUtil.getString(field);
49              String message = MessageFormatter.getSimpleMessage(code, null);
50              assertFalse(code + " is not found in URMMessages.", StringUtil
51                      .isEmpty(message));
52          }
53      }
54  
55      /**
56       * <code>URMMessages</code> に登録されているキーがすべて {@link UrumaMessageCodes}
57       * に登録されているかどうかをテストします。<br />
58       */
59      public void testMessages() {
60          BeanDesc desc = BeanDescFactory.getBeanDesc(UrumaMessageCodes.class);
61          Map<String, String> constantMap = new HashMap<String, String>();
62  
63          int size = desc.getFieldSize();
64  
65          for (int i = 0; i < size; i++) {
66              Field field = desc.getField(i);
67              constantMap.put(FieldUtil.getString(field), field.getName());
68          }
69  
70          ResourceBundle bundle = ResourceBundle.getBundle("URMMessages");
71          assertNotNull("1", bundle);
72  
73          Enumeration<String> keys = bundle.getKeys();
74          while (keys.hasMoreElements()) {
75              String key = keys.nextElement();
76              assertTrue(key + " is not found in UrumaMessageCodes", constantMap
77                      .containsKey(key));
78          }
79      }
80  }