KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > ApplicationResourcesTest


1 /*
2  * Filename: ApplicationResourcesTest.java
3  *
4  * Created on 24-May-04
5  */

6 package org.roller.presentation;
7
8 import java.io.FileInputStream JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import junit.framework.Test;
14 import junit.framework.TestCase;
15 import junit.framework.TestSuite;
16
17 /**
18  * The purpose of this class is to verify that all messages in
19  * the base ApplicationResources.properties file also appear
20  * in the localized properties files.
21  *
22  * If messages do not appear, the test fails and the 'evil-doers' are
23  * printed to System.out.
24  *
25  * Note: we need to make sure that new property files are added to this
26  * test.
27  *
28  * @author <a HREF="mailto:molen@mail.com">Jaap van der Molen</a>
29  * @version $Revision: 1.7 $
30  */

31 public class ApplicationResourcesTest extends TestCase
32 {
33     private String JavaDoc userDir = null;
34     private Properties JavaDoc baseProps = null;
35
36     /**
37      * @param arg0
38      */

39     public ApplicationResourcesTest(String JavaDoc name)
40     {
41         super(name);
42     }
43     
44     public static Test suite() {
45         TestSuite suite = new TestSuite();
46         //suite.addTest(new ApplicationResourcesTest("testSystemProperties"));
47
suite.addTest(
48             new ApplicationResourcesTest("testApplicationResources_nl"));
49         suite.addTest(
50             new ApplicationResourcesTest("testApplicationResources_zh_cn"));
51         suite.addTest(
52             new ApplicationResourcesTest("testApplicationResources_zh_tw"));
53         suite.addTest(
54             new ApplicationResourcesTest("testApplicationResources_vi"));
55         return suite;
56     }
57     
58     /**
59      * @see junit.framework.TestCase#setUp()
60      */

61     protected void setUp() throws Exception JavaDoc
62     {
63         super.setUp();
64         userDir = System.getProperty("user.dir");
65         
66         // load base ApplicationResources.properties file
67
baseProps = new Properties JavaDoc();
68         baseProps.load(new FileInputStream JavaDoc(
69                 userDir + "/WEB-INF/classes/ApplicationResources.properties"));
70     }
71
72     /**
73      * Test Dutch stuff.
74      *
75      * @throws Exception
76      */

77     public void testApplicationResources_nl() throws Exception JavaDoc
78     {
79         verifyResourceBundle("ApplicationResources_nl");
80     }
81
82     /**
83      * Test Simple Chinese stuff.
84      *
85      * @throws Exception
86      */

87     public void testApplicationResources_zh_cn() throws Exception JavaDoc
88     {
89         verifyResourceBundle("ApplicationResources_zh_cn");
90     }
91
92     /**
93      * Test Traditional Chinese stuff.
94      *
95      * @throws Exception
96      */

97     public void testApplicationResources_zh_tw() throws Exception JavaDoc
98     {
99         verifyResourceBundle("ApplicationResources_zh_tw");
100     }
101
102     /**
103      * Test Vietnamese stuff.
104      *
105      * @throws Exception
106      */

107     public void testApplicationResources_vi() throws Exception JavaDoc
108     {
109         verifyResourceBundle("ApplicationResources_vi");
110     }
111
112     public void testSystemProperties()
113     {
114         Properties JavaDoc sysProps = System.getProperties();
115         Set JavaDoc keys = sysProps.keySet();
116         for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();)
117         {
118             String JavaDoc key = (String JavaDoc) iter.next();
119             System.out.println(key + " = " + sysProps.getProperty(key));
120         }
121     }
122
123     /**
124      * Helper method to do the actual testing.
125      *
126      * @param bundle name of bundle to test
127      * @throws Exception if file not found, or if io ecxeption occurs.
128      */

129     private void verifyResourceBundle(String JavaDoc bundle) throws Exception JavaDoc
130     {
131         // verify user-dir; should end with roller
132
assertNotNull(userDir);
133         assertTrue(userDir.endsWith("roller"));
134         
135         // load Chinese resource file
136
Properties JavaDoc props = new Properties JavaDoc();
137         props.load(
138             new FileInputStream JavaDoc(
139                 userDir
140                     + "/web/WEB-INF/classes/"
141                     + bundle
142                     + ".properties"));
143
144         Set JavaDoc keys = baseProps.keySet();
145         boolean missingMessage = false;
146
147         // check Chinese
148
System.out.println("Veriyfing " + bundle + "...");
149         for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();)
150         {
151             String JavaDoc key = (String JavaDoc) iter.next();
152             if (props.getProperty(key) == null)
153             {
154                 System.err.println(key + " = " + baseProps.getProperty(key));
155                 missingMessage = true;
156             }
157         }
158
159         assertFalse(missingMessage);
160     }
161
162 }
163
Popular Tags