KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > i18n > TestCmsMessageBundles


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/i18n/TestCmsMessageBundles.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.13 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.i18n;
33
34 import java.lang.reflect.Field JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Locale JavaDoc;
39
40 import junit.framework.TestCase;
41
42 /**
43  * Tests for the CmsMessageBundles.<p>
44  *
45  * @author Alexander Kandzior
46  *
47  * @version $Revision: 1.13 $
48  *
49  * @since 6.0.0
50  */

51 public abstract class TestCmsMessageBundles extends TestCase {
52
53     /**
54      * Tests if message will be returned in the correct locale.<p>
55      *
56      * @throws Exception if the test fails
57      */

58     public final void testLocale() throws Exception JavaDoc {
59
60         CmsMessages messages = new CmsMessages("org.opencms.i18n.messages", Locale.GERMANY);
61         String JavaDoc value = messages.key("LOG_LOCALE_MANAGER_FLUSH_CACHE_1", new Object JavaDoc[] {"TestEvent"});
62         assertEquals("Locale manager leerte die Caches nachdem Event TestEvent empfangen wurde.", value);
63     }
64
65     /**
66      * Checks all OpenCms internal message bundles if the are correctly build.<p>
67      *
68      * @throws Exception if the test fails
69      */

70     public final void testMessagesBundleConstants() throws Exception JavaDoc {
71
72         I_CmsMessageBundle[] bundles = getTestMessageBundles();
73         for (int i = 0; i < bundles.length; i++) {
74             doTestBundle(bundles[i]);
75         }
76     }
77
78     /**
79      * Template method that has to be overwritten to return the <code>I_CmsMessageBundle</code>
80      * instances that will be tested.<p>
81      *
82      * @return the <code>I_CmsMessageBundle</code> instances to test: these will be the
83      * singleton instances of the <code>Messages</code> classes residing in every localized package.
84      */

85     protected abstract I_CmsMessageBundle[] getTestMessageBundles();
86
87     /**
88      * Tests an individual message bundle.<p>
89      *
90      * @param bundle the bundle to test
91      * @throws Exception if the test fails
92      */

93     private void doTestBundle(I_CmsMessageBundle bundle) throws Exception JavaDoc {
94
95         List JavaDoc keys = new ArrayList JavaDoc();
96
97         System.out.println("\nValidating all keys in bundle " + bundle.getBundleName() + ":");
98
99         // use reflection on all member constants
100
Field JavaDoc[] fields = bundle.getClass().getDeclaredFields();
101         for (int i = 0; i < fields.length; i++) {
102             Field JavaDoc field = fields[i];
103             if (field.getType().equals(String JavaDoc.class)) {
104                 // check all String fields
105
String JavaDoc key = field.getName();
106
107                 String JavaDoc value;
108                 try {
109                     value = (String JavaDoc)field.get(bundle);
110                 } catch (IllegalAccessException JavaDoc e) {
111                     continue;
112                 }
113
114                 System.out.println("Validating key '" + key + "'");
115
116                 // ensure the name id identical to the value
117
if (!key.equals(value)) {
118                     fail("Key '" + key + "' in bundle " + bundle.getBundleName() + " has bad value '" + value + "'");
119                 }
120
121                 // check if key exists in bundle for constant
122
String JavaDoc message = bundle.getBundle().key(key);
123                 if (CmsMessages.isUnknownKey(message)) {
124                     String JavaDoc bundleName = bundle.getBundleName();
125                     if (!bundleName.endsWith(".messages")) {
126                         fail("The Message bundle name \""
127                             + bundleName
128                             + "\" does not end with: \".messages\". \n "
129                             + "Change the constant literal (\"private static final String BUNDLE_NAME\")");
130                     } else {
131                         fail("No message for '" + key + "' in bundle " + bundleName);
132                     }
133                 }
134
135                 // ensure key has the form "{ERR|LOG|INIT|GUI|RPT}_KEYNAME_{0-9}";
136
if (key.length() < 7) {
137                     fail("Key '"
138                         + key
139                         + "' in bundle "
140                         + bundle.getBundleName()
141                         + " is to short (length must be at last 7)");
142                 }
143                 if (!key.equals(key.toUpperCase())) {
144                     fail("Key '" + key + "' in bundle " + bundle.getBundleName() + " must be all upper case");
145                 }
146                 if ((key.charAt(key.length() - 2) != '_')
147                     || (!key.startsWith("ERR_")
148                         && !key.startsWith("LOG_")
149                         && !key.startsWith("INIT_")
150                         && !key.startsWith("GUI_") && !key.startsWith("RPT_"))) {
151                     fail("Key '"
152                         + key
153                         + "' in bundle "
154                         + bundle.getBundleName()
155                         + " must have the form {ERR|LOG|INIT|GUI|RPT}_KEYNAME_{0-9}");
156                 }
157                 int argCount = Integer.valueOf(key.substring(key.length() - 1)).intValue();
158
159                 for (int j = 0; j < argCount; j++) {
160                     String JavaDoc arg = "{" + j;
161                     int pos = message.indexOf(arg);
162                     if (pos < 0) {
163                         fail("Message '"
164                             + message
165                             + "' for key '"
166                             + key
167                             + "' in bundle "
168                             + bundle.getBundleName()
169                             + " misses argument {"
170                             + j
171                             + "}");
172                     }
173                 }
174                 for (int j = argCount; j < 10; j++) {
175                     String JavaDoc arg = "{" + j;
176                     int pos = message.indexOf(arg);
177                     if (pos >= 0) {
178                         fail("Message '"
179                             + message
180                             + "' for key '"
181                             + key
182                             + "' in bundle "
183                             + bundle.getBundleName()
184                             + " containes unused argument {"
185                             + j
186                             + "}");
187                     }
188                 }
189
190                 // store this key for later check against all properties in the bundle
191
keys.add(key);
192             }
193         }
194
195         CmsMessages messages = bundle.getBundle();
196
197         Enumeration JavaDoc bundleKeys = messages.getResourceBundle().getKeys();
198         while (bundleKeys.hasMoreElements()) {
199             String JavaDoc bundleKey = (String JavaDoc)bundleKeys.nextElement();
200             if (bundleKey.toUpperCase().equals(bundleKey)) {
201                 // only check keys which are all upper case
202
if (!keys.contains(bundleKey)) {
203                     fail("Bundle " + bundle.getBundleName() + " contains unreferenced message " + bundleKey);
204                 }
205             } else {
206                 System.out.println("Additional key in bundle '" + bundleKey + "'");
207             }
208         }
209     }
210 }
211
Popular Tags