View Javadoc

1   /*
2    * $Id: SettingsTest.java 454856 2006-10-10 18:06:01Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.config;
19  
20  import java.util.Iterator;
21  import java.util.Locale;
22  
23  import org.apache.struts2.StrutsConstants;
24  import org.apache.struts2.StrutsTestCase;
25  
26  import com.opensymphony.xwork2.util.LocalizedTextUtil;
27  
28  
29  /***
30   * Unit test for {@link SettingsTest}.
31   *
32   */
33  public class SettingsTest extends StrutsTestCase {
34  
35      public void testSettings() {
36          assertEquals("get", Settings.get(StrutsConstants.STRUTS_URL_INCLUDEPARAMS));
37          assertEquals("12345", Settings.get(StrutsConstants.STRUTS_MULTIPART_MAXSIZE));
38          assertEquals("\temp", Settings.get(StrutsConstants.STRUTS_MULTIPART_SAVEDIR));
39  
40          assertEquals("test,org/apache/struts2/othertest", Settings.get( StrutsConstants.STRUTS_CUSTOM_PROPERTIES));
41          assertEquals("testvalue", Settings.get("testkey"));
42          assertEquals("othertestvalue", Settings.get("othertestkey"));
43  
44          Locale locale = Settings.getLocale();
45          assertEquals("de", locale.getLanguage());
46  
47          int count = getKeyCount();
48          assertEquals(31, count);
49      }
50  
51      public void testDefaultResourceBundlesLoaded() {
52          assertEquals("testmessages,testmessages2", Settings.get(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES));
53          assertEquals("This is a test message", LocalizedTextUtil.findDefaultText("default.testmessage", Locale.getDefault()));
54          assertEquals("This is another test message", LocalizedTextUtil.findDefaultText("default.testmessage2", Locale.getDefault()));
55      }
56  
57      public void testReplaceDefaultMessages() {
58          Locale.setDefault(Locale.US); // force to US locale as we also have _de and _da properties
59          
60          LocalizedTextUtil.clearDefaultResourceBundles();
61          LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
62          assertEquals("The form has already been processed or no token was supplied, please try again.", LocalizedTextUtil.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
63          Settings.reset();
64  
65          assertEquals("testmessages,testmessages2", Settings.get(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES));
66          assertEquals("Replaced message for token tag", LocalizedTextUtil.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
67      }
68  
69      public void testSetSettings() {
70          Settings.setInstance(new TestSettings());
71          
72          String keyName = "a.long.property.key.name";
73          assertEquals(keyName, Settings.get(keyName));
74          assertEquals(2, getKeyCount());
75      }
76  
77      private int getKeyCount() {
78          int count = 0;
79          Iterator keyNames = Settings.list();
80  
81          while (keyNames.hasNext()) {
82          	keyNames.next();
83              count++;
84          }
85  
86          return count;
87      }
88  }