001: /*
002: * Copyright (c) 2003, Rafael Steil
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 30/09/2004 - 12:50:46
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.util;
044:
045: import junit.framework.TestCase;
046: import net.jforum.SessionFacade;
047: import net.jforum.TestCaseUtils;
048: import net.jforum.entities.UserSession;
049: import net.jforum.util.preferences.ConfigKeys;
050: import net.jforum.util.preferences.SystemGlobals;
051:
052: /**
053: * @author Rafael Steil
054: * @version $Id: I18nTest.java,v 1.4 2005/07/26 03:04:41 rafaelsteil Exp $
055: */
056: public class I18nTest extends TestCase {
057: private static boolean loaded = false;
058: private static final String SESSION_ID = "1";
059: private UserSession us;
060:
061: /**
062: * @see TestCase#setUp()
063: */
064: protected void setUp() throws Exception {
065: if (!loaded) {
066: TestCaseUtils.loadEnvironment();
067: SystemGlobals.setValue(ConfigKeys.RESOURCE_DIR,
068: SystemGlobals.getApplicationResourceDir()
069: + "/tests");
070: loaded = true;
071: }
072:
073: SystemGlobals
074: .setValue(ConfigKeys.I18N_DEFAULT_ADMIN, "default");
075: SystemGlobals.setValue(ConfigKeys.I18N_DEFAULT, "default");
076:
077: this .us = new UserSession();
078: this .us.setSessionId(SESSION_ID);
079: SessionFacade.add(this .us, SESSION_ID);
080:
081: I18n.reset();
082: I18n.load();
083: }
084:
085: public void testLoad() throws Exception {
086: assertTrue(I18n.contains("default"));
087: }
088:
089: public void testDefaultKeys() {
090: assertEquals("default value 1", I18n.getMessage("defaultKey1",
091: this .us));
092: assertEquals("default value 2", I18n.getMessage("defaultKey2",
093: this .us));
094: assertEquals("default value 3", I18n.getMessage("defaultKey3",
095: this .us));
096: assertEquals("default value 4", I18n.getMessage("defaultKey4",
097: this .us));
098: assertEquals("default value 5", I18n.getMessage("defaultKey5",
099: this .us));
100: }
101:
102: public void testLoadCheese() throws Exception {
103: I18n.load("cheese");
104: assertTrue(I18n.contains("cheese"));
105: }
106:
107: public void testCheeseKeys() {
108: this .us.setLang("cheese");
109: assertEquals("default cheese 1", I18n.getMessage("defaultKey1",
110: this .us));
111: assertEquals("default cheese 2", I18n.getMessage("defaultKey2",
112: this .us));
113: assertEquals("default cheese 3", I18n.getMessage("defaultKey3",
114: this .us));
115: assertEquals("default cheese 4", I18n.getMessage("defaultKey4",
116: this .us));
117: assertEquals("default value 5", I18n.getMessage("defaultKey5",
118: this .us));
119: }
120:
121: public void testLoadOrange() throws Exception {
122: I18n.load("orange");
123: assertTrue(I18n.contains("orange"));
124: }
125:
126: public void testOrangeKeys() {
127: this .us.setLang("orange");
128: assertEquals("default orange 1", I18n.getMessage("defaultKey1",
129: this .us));
130: assertEquals("default orange 2", I18n.getMessage("defaultKey2",
131: this .us));
132: assertEquals("default orange 3", I18n.getMessage("defaultKey3",
133: this .us));
134: assertEquals("default value 4", I18n.getMessage("defaultKey4",
135: this .us));
136: assertEquals("default value 5", I18n.getMessage("defaultKey5",
137: this .us));
138: assertEquals("orange is not cheese", I18n.getMessage("orange",
139: this .us));
140: }
141:
142: public void testGetMessageWithLocale() {
143: assertEquals("default value 1", I18n.getMessage("default",
144: "defaultKey1"));
145: assertEquals("default cheese 1", I18n.getMessage("cheese",
146: "defaultKey1"));
147: assertEquals("default orange 1", I18n.getMessage("orange",
148: "defaultKey1"));
149: assertNull(I18n.getMessage("default", "orange"));
150: }
151:
152: public void testRest() {
153: I18n.reset();
154: assertFalse(I18n.contains("default"));
155: assertFalse(I18n.contains("orange"));
156: assertFalse(I18n.contains("cheese"));
157: }
158:
159: public void testMergeCheeseOrange() throws Exception {
160: this .testRest();
161: I18n.load("cheese", "orange");
162: assertTrue(I18n.contains("cheese"));
163: assertTrue(I18n.contains("orange"));
164: assertEquals("default cheese 1", I18n.getMessage("cheese",
165: "defaultKey1"));
166: assertEquals("orange is not cheese", I18n.getMessage("cheese",
167: "orange"));
168: }
169:
170: public void testOrangeIsDefault() throws Exception {
171: this .testRest();
172: SystemGlobals.setValue(ConfigKeys.I18N_DEFAULT, "orange");
173: I18n.load();
174: assertTrue(I18n.contains("default"));
175: assertTrue(I18n.contains("orange"));
176: this.testOrangeKeys();
177: }
178: }
|