001: package org.uispec4j.utils;
002:
003: import org.uispec4j.UIComponent;
004: import org.uispec4j.assertion.Assertion;
005:
006: import javax.swing.*;
007: import java.awt.*;
008: import java.util.ArrayList;
009: import java.util.HashMap;
010: import java.util.Map;
011:
012: public class UIComponentFactoryTest extends UnitTestCase {
013:
014: public void testInitWithDummyComponent() throws Exception {
015: Map result = new HashMap();
016: UIComponentFactory.fillSwingToUISpecMap(result,
017: new Class[] { ComponentWithSwingClassesField.class });
018: assertEquals(ComponentWithSwingClassesField.class, result
019: .get(JButton.class));
020: }
021:
022: public void testInitErrorForClassWithoutSwingClassesField()
023: throws Exception {
024: checkInitError(
025: ComponentWithoutSwingClassesField.class,
026: "Class "
027: + ComponentWithoutSwingClassesField.class
028: + " should have a field 'static Class[] SWING_CLASSES'");
029: }
030:
031: public void testInitErrorForClassWithUninitialisedSwingClassesField()
032: throws Exception {
033: checkInitError(
034: ComponentWithUninitializedSwingClassesField.class,
035: "Field 'static Class[] SWING_CLASSES' in class "
036: + ComponentWithUninitializedSwingClassesField.class
037: + " should be initialized");
038: }
039:
040: public void testNonSwingClassInSwingClassesField() throws Exception {
041: checkInitError(ComponentWithInvalidSwingClass.class, "Class '"
042: + String.class
043: + "' in field 'SWING_CLASSES' of class '"
044: + ComponentWithInvalidSwingClass.class
045: + "' should extend '" + Container.class + "'");
046: }
047:
048: public void testInitErrorForClassWithoutTypeName() throws Exception {
049: checkInitError(
050: ComponentWithoutTypeName.class,
051: "Class "
052: + ComponentWithoutTypeName.class
053: + " should have a field 'public static String TYPE_NAME'");
054: }
055:
056: public void testInitErrorForClassWithUninitialisedTypeName()
057: throws Exception {
058: checkInitError(ComponentWithUninitializedTypeNameField.class,
059: "Field 'static String TYPE_NAME' in class "
060: + ComponentWithUninitializedTypeNameField.class
061: + " should be initialized");
062: }
063:
064: public void testInitErrorForClassWithPrivateTypeName()
065: throws Exception {
066: checkInitError(ComponentWithPrivateTypeNameField.class,
067: "Field 'static String TYPE_NAME' in class "
068: + ComponentWithPrivateTypeNameField.class
069: + " should be public");
070: }
071:
072: public void testInitErrorForClassWithUnexpectedTypeNameClass()
073: throws Exception {
074: checkInitError(ComponentWithoutUnexpectedTypeNameClass.class,
075: "Static field 'TYPE_NAME' in class "
076: + ComponentWithoutUnexpectedTypeNameClass.class
077: + " should be of type String");
078: }
079:
080: public void testComponentClassesMustImplementUIComponent()
081: throws Exception {
082: checkInitError(String.class, "Class '" + String.class
083: + "' should implement " + UIComponent.class);
084: }
085:
086: private void checkInitError(Class swingClass, String message) {
087: try {
088: UIComponentFactory.initUIComponentClasses(
089: new Class[] { swingClass }, new ArrayList());
090: fail();
091: } catch (RuntimeException e) {
092: assertEquals(message, e.getMessage());
093: }
094: }
095:
096: private static class ComponentWithSwingClassesField extends
097: DummyUIComponent {
098: public static Class[] SWING_CLASSES = { JButton.class };
099: public static String TYPE_NAME = "Type";
100: }
101:
102: private static class ComponentWithoutSwingClassesField extends
103: DummyUIComponent {
104: public static String TYPE_NAME = "Type";
105: }
106:
107: private static class ComponentWithUninitializedSwingClassesField
108: extends DummyUIComponent {
109: static Class[] SWING_CLASSES;
110: public static String TYPE_NAME = "Type";
111: }
112:
113: private static class ComponentWithInvalidSwingClass extends
114: DummyUIComponent {
115: static Class[] SWING_CLASSES = { String.class };
116: public static String TYPE_NAME = "Type";
117: }
118:
119: private static class ComponentWithoutTypeName extends
120: DummyUIComponent {
121: public static Class[] SWING_CLASSES = { org.uispec4j.AbstractButton.class };
122: }
123:
124: private static class ComponentWithUninitializedTypeNameField extends
125: DummyUIComponent {
126: public static Class[] SWING_CLASSES = { org.uispec4j.AbstractButton.class };
127: public static String TYPE_NAME;
128: }
129:
130: private static class ComponentWithPrivateTypeNameField extends
131: DummyUIComponent {
132: public static Class[] SWING_CLASSES = { org.uispec4j.AbstractButton.class };
133: private static String TYPE_NAME = "Type";
134: }
135:
136: private static class ComponentWithoutUnexpectedTypeNameClass extends
137: DummyUIComponent {
138: public static Class[] SWING_CLASSES = { org.uispec4j.AbstractButton.class };
139: public static Object TYPE_NAME = new Integer(3);
140: }
141:
142: private static class DummyUIComponent implements UIComponent {
143:
144: public Component getAwtComponent() {
145: return null;
146: }
147:
148: public String getDescription() {
149: return null;
150: }
151:
152: public String getDescriptionTypeName() {
153: return null;
154: }
155:
156: public Assertion isEnabled() {
157: return new Assertion() {
158: public void check() {
159: }
160: };
161: }
162:
163: public String getName() {
164: return null;
165: }
166:
167: public String getLabel() {
168: return null;
169: }
170: }
171: }
|