001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005: package org.jasig.portal.lang;
006:
007: import java.text.MessageFormat;
008: import java.util.MissingResourceException;
009: import java.util.Properties;
010: import junit.framework.ComparisonFailure;
011: import junit.framework.TestCase;
012:
013: /**
014: * The <code>TypeConverter_Test</code> class tests <code>TypeConverter</code> class.
015: *
016: *
017: * @version $Revision: 35984 $
018: *
019: * @author <a href="mailto:jnielsen@sct.com">Jan Nielsen</a>
020: **/
021: public class Resources_Test extends TestCase {
022: /**
023: * Run all the test cases defined in the class.
024: *
025: * @param args not used
026: **/
027: public static void main(String[] args) {
028: junit.textui.TestRunner.run(suite());
029: }
030:
031: /**
032: * Build a test suite using reflection.
033: *
034: * @return test suite for the class
035: **/
036: public static junit.framework.TestSuite suite() {
037: return new junit.framework.TestSuite(Resources_Test.class);
038: }
039:
040: /**
041: * Setup for each test method.
042: **/
043: public void setUp() {
044: }
045:
046: /**
047: * Tear down for each test method.
048: **/
049: public void tearDown() {
050: }
051:
052: public Resources_Test(String name) {
053: super (name);
054: }
055:
056: public void test() throws Exception {
057: Object[][] tests = new Object[][] {
058: { null, NullPointerException.class },
059: { "", MissingResourceException.class },
060: { "someUnknownProperty", MissingResourceException.class },
061: { "testGetString", null } };
062: tryEach(tests);
063: }
064:
065: private void tryEach(Object[][] objects) throws Exception {
066: for (int i = 0; i < objects.length; i++) {
067: String name = (String) objects[i][0];
068: Class throwable = (Class) objects[i][1];
069: tryValue(name, throwable);
070: tryValue(name, null, throwable);
071: tryValue(name, new String[] { "some value" }, throwable);
072: }
073: }
074:
075: private void tryValue(String name, String[] objects, Class throwable) {
076: try {
077: stringsEqual(name, objects);
078: } catch (Throwable t) {
079: TestCase.assertEquals((null != throwable ? throwable
080: .getName() : "No throwable object"), t.getClass()
081: .getName());
082: }
083: }
084:
085: private void tryValue(String name, Class throwable) {
086: try {
087: stringsEqual(name);
088: } catch (ComparisonFailure x) {
089: throw x;
090: } catch (Throwable t) {
091: String expected = "Should not have thrown an exception.";
092: if (null != throwable)
093: expected = throwable.getName();
094: String got = t.getClass().getName();
095: TestCase.assertEquals(expected, got);
096: }
097: }
098:
099: private String getString(String name, String[] objects) {
100: return Resources.getString(Resources_Test.class, name, objects);
101: }
102:
103: private String getString(String name) {
104: return Resources.getString(Resources_Test.class, name);
105: }
106:
107: private String loadString(String name) throws Exception {
108: Properties properties = new Properties();
109:
110: properties.load(getClass().getResourceAsStream(
111: "Resources_Test.properties"));
112: String value = properties.getProperty(name);
113: if (null == value)
114: throw new MissingResourceException("Undefined property.",
115: Resources_Test.class.getName(), name);
116: return value;
117: }
118:
119: private String loadString(String name, String[] objects)
120: throws Exception {
121: return MessageFormat.format(loadString(name),
122: (Object[]) objects);
123: }
124:
125: private void stringsEqual(String name) throws Exception {
126: TestCase.assertEquals(loadString(name), getString(name));
127: }
128:
129: private void stringsEqual(String name, String[] objects)
130: throws Exception {
131: TestCase.assertEquals(loadString(name, objects), getString(
132: name, objects));
133: }
134: }
|