001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.jellytools;
042:
043: import java.util.ResourceBundle;
044: import junit.framework.*;
045: import org.netbeans.jellytools.JellyTestCase;
046: import org.netbeans.jemmy.JemmyException;
047: import org.netbeans.junit.*;
048:
049: /**
050: * Test of org.netbeans.jellytools.Bundle
051: * @author Jiri.Skrivanek@sun.com
052: */
053: public class BundleTest extends JellyTestCase {
054:
055: /** Use for internal test execution inside IDE
056: * @param args command line arguments
057: */
058: public static void main(java.lang.String[] args) {
059: junit.textui.TestRunner.run(suite());
060: }
061:
062: /** Method used for explicit testsuite definition
063: * @return created suite
064: */
065: public static Test suite() {
066: TestSuite suite = new NbTestSuite(BundleTest.class);
067: return suite;
068: }
069:
070: /** Redirect output to log files, wait before each test case and
071: * show dialog to test. */
072: protected void setUp() {
073: System.out.println("### " + getName() + " ###");
074: }
075:
076: /** Clean up after each test case. */
077: protected void tearDown() {
078: }
079:
080: /** Constructor required by JUnit.
081: * @param testName method name to be used as testcase
082: */
083: public BundleTest(java.lang.String testName) {
084: super (testName);
085: }
086:
087: /** Test of getBundle method. */
088: public void testGetBundle() {
089: try {
090: ResourceBundle resBundle = Bundle
091: .getBundle("org.netbeans.core.Bundle");
092: assertNotNull("Should not return null.", resBundle);
093: } catch (JemmyException e) {
094: fail("Should always find org.netbeans.core.Bundle");
095: }
096: try {
097: Bundle.getBundle("nonsense.package.Bundle");
098: fail("Should not find nonsense.package.Bundle");
099: } catch (JemmyException e) {
100: // right, should fail
101: }
102: try {
103: Bundle.getBundle(null);
104: fail("Should not accept null parameter.");
105: } catch (JemmyException e) {
106: // right, should fail
107: }
108: }
109:
110: /** Test of getString method. Tests also negative cases */
111: public void testGetString() {
112: try {
113: String value = Bundle.getString(
114: "org.netbeans.core.windows.services.Bundle",
115: "OK_OPTION_CAPTION");
116: assertNotNull("Should not return null.", value);
117: assertTrue("Should not return empty string.", value
118: .length() != 0);
119: } catch (JemmyException e) {
120: fail("Should always find OK_OPTION_CAPTION at org.netbeans.core.windows.services.Bundle.");
121: }
122: try {
123: Bundle.getString("org.netbeans.core.Bundle", null);
124: fail("Should not accept null parameter.");
125: } catch (JemmyException e) {
126: // right, should fail
127: }
128: try {
129: Bundle.getString("org.netbeans.core.Bundle",
130: "nonsense key - @#$%^");
131: fail("Should not find nonsense key.");
132: } catch (JemmyException e) {
133: // right, should fail
134: }
135: try {
136: Bundle
137: .getString((ResourceBundle) null,
138: "OK_OPTION_CAPTION");
139: fail("Should not accept null ResourceBundle parameter.");
140: } catch (JemmyException e) {
141: // right, should fail
142: }
143: }
144:
145: /** Test of getString method with parameter to format. */
146: public void testGetStringParams() {
147: String pattern = Bundle.getString("org.netbeans.core.Bundle",
148: "CTL_FMT_LocalProperties");
149: Object[] params = new Object[] { new Integer(1), "AnObject" };
150: String value = Bundle.getString("org.netbeans.core.Bundle",
151: "CTL_FMT_LocalProperties", params);
152: String expected = java.text.MessageFormat.format(pattern,
153: params);
154: assertEquals("Parameters not properly formattted.", expected,
155: value);
156: }
157:
158: /** Test of getStringTrimmed method. */
159: public void testGetStringTrimmed() {
160: //Saving {0} ...
161: String valueRaw = Bundle.getString("org.netbeans.core.Bundle",
162: "CTL_FMT_SavingMessage");
163: String value = Bundle.getStringTrimmed(
164: "org.netbeans.core.Bundle", "CTL_FMT_SavingMessage");
165: assertTrue("Characters '{' should be cut off from \""
166: + valueRaw + "\".", value.indexOf('{') == -1);
167: // "&Help"
168: valueRaw = Bundle.getString("org.netbeans.core.Bundle",
169: "Menu/Help");
170: value = Bundle.getStringTrimmed("org.netbeans.core.Bundle",
171: "Menu/Help");
172: assertTrue("Characters '&' should be removed from \""
173: + valueRaw + "\".", value.indexOf('&') == -1);
174: }
175:
176: /** Test of getStringTrimmed method with parameter to format. */
177: public void testGetStringTrimmedParams() {
178: String pattern = Bundle.getString("org.netbeans.core.Bundle",
179: "CTL_FMT_LocalProperties");
180: Object[] params = new Object[] { new Integer(1), "AnOb&ject" };
181: String value = Bundle.getStringTrimmed(
182: "org.netbeans.core.Bundle", "CTL_FMT_LocalProperties",
183: params);
184: String expected = java.text.MessageFormat.format(pattern,
185: params);
186: expected = new StringBuffer(expected).deleteCharAt(
187: expected.indexOf('&')).toString();
188: assertEquals("Parameters not properly formattted.", expected,
189: value);
190: }
191: }
|