KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > i18n > MessageBundleTest


1 /*
2  
3    Derby - Class org.apache.derbyTesting.functionTests.tests.i18n.MessageBundleTest
4  
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11  
12       http://www.apache.org/licenses/LICENSE-2.0
13  
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19  
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.i18n;
23
24 import org.apache.derbyTesting.junit.BaseTestCase;
25 import org.apache.derby.shared.common.reference.SQLState;
26 import org.apache.derby.shared.common.reference.MessageId;
27
28 import java.util.HashSet JavaDoc;
29 import java.lang.reflect.Field JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * This class does everything we can to validate that the messages_en.properties
36  * file is in synch with SQLState.java and MessageId.java. We want to make sure
37  * that message ids defined in SQLState and MessageId have matching messages
38  * in the messages properties file, and also find out if there are any messages
39  * that don't have matching ids in the SQLState and MessageId files. The
40  * first is a bug, the second is something to be aware of.
41  */

42 public class MessageBundleTest extends BaseTestCase {
43     public MessageBundleTest(String JavaDoc name) {
44         super(name);
45     }
46
47     // The list of ids. We use a HashSet so we can detect duplicates easily
48
static HashSet JavaDoc sqlStateIds = new HashSet JavaDoc();
49     static HashSet JavaDoc messageIdIds = new HashSet JavaDoc();
50     static HashSet JavaDoc messageBundleIds = new HashSet JavaDoc();
51     
52     static {
53         try {
54             // Load all the ids for the SQLState class
55
loadClassIds(SQLState.class, sqlStateIds);
56
57             // Load all the ids for the MessageId class
58
loadClassIds(MessageId.class, messageIdIds);
59
60             // Load all the ids for the messages_en properties file
61
loadMessageBundleIds();
62         } catch ( Exception JavaDoc e ) {
63             e.printStackTrace();
64             throw new RuntimeException JavaDoc(e.getMessage());
65         }
66     }
67     
68     static void loadClassIds(Class JavaDoc idclass, HashSet JavaDoc set) throws Exception JavaDoc {
69         Field JavaDoc[] fields = idclass.getFields();
70         
71         int length = fields.length;
72         for ( int i = 0; i < length ; i++ )
73         {
74             String JavaDoc id = (String JavaDoc)fields[i].get(null);
75             
76             if ( id.length() == 2 ) {
77                 // Skip past identifiers that are just categories
78
continue;
79             }
80             
81             // Skip past "special" SQL States that are not expected
82
// to have messages
83
if ( id.equals("close.C.1") ) continue;
84             if ( id.equals("rwupd" ) ) continue;
85             if ( id.equals("02502" ) ) continue;
86             if ( id.equals("XSAX0") ) continue;
87             
88             if ( ! set.add(id) )
89             {
90                 System.err.println("ERROR: The id " + id +
91                     " was found twice in " + idclass.getName());
92             }
93         }
94     }
95             
96     /**
97      * Load all the message ids from messages_en.properties into a HashSet.
98      * This assumes its available on the classpath
99      */

100     static void loadMessageBundleIds() throws Exception JavaDoc {
101         ResourceBundle JavaDoc bundle;
102         
103         // The messages_*.properties files are split into fifty separate
104
// message bundle files. We need to load each one in turn
105
int numBundles = 50;
106         
107         for ( int i=0 ; i < numBundles ; i++ ) {
108             loadMessageBundle(i);
109         }
110     }
111     
112     static void loadMessageBundle(int index) {
113         String JavaDoc bundleName = "org.apache.derby.loc.m" + index;
114         
115         ResourceBundle JavaDoc bundle =
116             ResourceBundle.getBundle(bundleName, Locale.ENGLISH);
117
118         java.util.Enumeration JavaDoc keys = bundle.getKeys();
119
120         while ( keys.hasMoreElements() ) {
121             String JavaDoc key = (String JavaDoc)keys.nextElement();
122
123             if ( ! messageBundleIds.add(key) ) {
124                 System.err.println("ERROR: the key " + key +
125                     " exists twice in messages_en.properties");
126             }
127         }
128     }
129
130     /**
131      * See if there are any message ids in SQLState.java that are
132      * not in the message bundle
133      */

134     public void testSQLStateOrphanedIds() throws Exception JavaDoc {
135         Iterator JavaDoc it = sqlStateIds.iterator();
136         
137         while ( it.hasNext() ) {
138             String JavaDoc sqlStateId = (String JavaDoc)it.next();
139             
140             if ( ! messageBundleIds.contains(sqlStateId) ) {
141                 // Don't fail out on the first one, we want to catch
142
// all of them. Just note there was a failure and continue
143
System.err.println("ERROR: Message id " + sqlStateId +
144                     " in SQLState.java was not found in" +
145                     " messages_en.properties");
146              }
147         }
148     }
149
150     /**
151      * See if there are any message ids in MessageId.java not in
152      * the message bundle
153      */

154     public void testMessageIdOrphanedIds() throws Exception JavaDoc {
155         Iterator JavaDoc it = messageIdIds.iterator();
156         
157         while ( it.hasNext() ) {
158             String JavaDoc sqlStateId = (String JavaDoc)it.next();
159             
160             if ( ! messageBundleIds.contains(sqlStateId) ) {
161                 // Don't fail out on the first one, we want to catch
162
// all of them. Just note there was a failure and continue
163
System.err.println("ERROR: Message id " + sqlStateId +
164                     " in MessageId.java was not found in" +
165                     " messages_en.properties");
166              }
167         }
168     }
169      
170     /**
171      * See if there are any message ids in the message bundle that
172      * are <b>not</b> in SQLState.java or MessageId.java
173      */

174     public void testMessageBundleOrphanedMessages() throws Exception JavaDoc {
175         Iterator JavaDoc it = messageBundleIds.iterator();
176         
177         while (it.hasNext() ) {
178             String JavaDoc msgid = (String JavaDoc)it.next();
179             
180             if ( sqlStateIds.contains(msgid)) {
181                 continue;
182             }
183             
184             if ( messageIdIds.contains(msgid)) {
185                 continue;
186             }
187             
188             // Don't fail out on the first one, we want to catch
189
// all of them. Just note there was a failure and continue
190
System.err.println("WARNING: Message id " + msgid +
191                 " in messages_en.properties is not " +
192                 "referenced in either SQLState.java or MessageId.java");
193         }
194     }
195 }
196
Popular Tags