001: package jaxx.junit;
002:
003: import java.awt.*;
004: import java.io.*;
005: import javax.swing.*;
006: import org.w3c.dom.*;
007: import junit.framework.TestCase;
008:
009: import jaxx.compiler.*;
010: import jaxx.reflect.*;
011: import jaxx.tags.*;
012:
013: public class TagManagerTest extends TestCase {
014: private JAXXCompiler compiler = JAXXCompiler.createDummyCompiler();
015:
016: public static class TestHandler extends DefaultObjectHandler {
017: public TestHandler(ClassDescriptor beanClass) {
018: super (beanClass);
019: }
020: }
021:
022: public void setUp() {
023: compiler.addImport("javax.swing.*");
024: }
025:
026: public void testRegisterBean() {
027: TagManager.registerBean(ClassDescriptorLoader
028: .getClassDescriptor(InputStream.class),
029: TestHandler.class);
030:
031: assertTrue(TagManager.getTagHandler(ClassDescriptorLoader
032: .getClassDescriptor(InputStream.class)) instanceof TestHandler);
033: assertTrue(TagManager.getTagHandler(ClassDescriptorLoader
034: .getClassDescriptor(FileInputStream.class)) instanceof TestHandler);
035: }
036:
037: public void testRegisterDefaultNamespace() {
038: TagManager.registerBean(ClassDescriptorLoader
039: .getClassDescriptor(OutputStream.class),
040: TestHandler.class);
041:
042: TagManager
043: .registerDefaultNamespace("OutputStream", "java.io.*");
044: assertTrue(
045: "Could not find handler for OutputStream despite default namespace",
046: TagManager
047: .getTagHandler(null, "OutputStream", compiler) instanceof TestHandler);
048:
049: PrintStream oldErr = System.err;
050: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
051: System.setErr(new PrintStream(buffer));
052: TagManager.registerDefaultNamespace("OutputStream",
053: "java.dummy.*");
054: assertNull(
055: "Found handler for OutputStream despite ambiguous default namespace",
056: TagManager
057: .getTagHandler(null, "OutputStream", compiler));
058: System.setErr(oldErr);
059: assertTrue(
060: "No errors were produced with an ambiguous default namespace",
061: buffer.size() > 0);
062: assertTrue(buffer.size() > 0);
063: }
064:
065: public void testResolveClassName() {
066: assertEquals("Could not resolve class name 'Object'",
067: TagManager.resolveClassName("Object", compiler),
068: "java.lang.Object");
069: assertEquals("Could not resolve class name 'java.lang.Object'",
070: TagManager.resolveClassName("java.lang.Object",
071: compiler), "java.lang.Object");
072: assertNull(
073: "Unexpectedly resolved class name 'java.awt.Object'",
074: TagManager
075: .resolveClassName("java.awt.Object", compiler));
076: }
077:
078: public void testPackages() {
079: assertNull("Unexpectedly found handler for java.awt.JButton",
080: TagManager.getTagHandler(null, "java.awt.JButton",
081: compiler));
082: assertNotNull(
083: "Did not find handler for JButton with default namespace of java.awt.*",
084: TagManager.getTagHandler("java.awt.*", "JButton",
085: compiler));
086: assertNull("Unexpectedly found handler for java.awt.*:JButton",
087: TagManager.getTagHandler("java.awt.*", "JButton", true,
088: compiler));
089: assertNotNull("Did not find handler for javax.swing.JButton",
090: TagManager.getTagHandler(null, "javax.swing.JButton",
091: compiler));
092: assertNotNull(
093: "Did not find handler for JButton with default namespace of java.swing.*",
094: TagManager.getTagHandler("java.swing.*", "JButton",
095: compiler));
096: assertNotNull("Did not find handler for javax.swing.*:JButton",
097: TagManager.getTagHandler("javax.swing.*", "JButton",
098: true, compiler));
099: }
100:
101: public void testImport() {
102: assertNull(
103: "Found handler for ActionListener despite no java.awt.event.* import",
104: TagManager.getTagHandler(null, "ActionListener",
105: compiler));
106:
107: compiler.addImport("java.awt.event.*");
108:
109: assertNotNull(
110: "Did not find ActionListener with java.awt.event.* import",
111: TagManager.getTagHandler(null, "ActionListener",
112: compiler));
113: }
114:
115: public void testAmbiguousImport() throws Exception {
116: compiler.addImport("java.sql.*");
117: assertNotNull(
118: "Did not find java.sql.Date with only java.sql.* imported",
119: TagManager.getTagHandler(null, "Date", compiler));
120:
121: PrintStream oldErr = System.err;
122: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
123: System.setErr(new PrintStream(buffer));
124: compiler.addImport("java.util.*");
125: TagManager.reset();
126: assertNull(
127: "Still found a handler for Date with an ambiguous import",
128: TagManager.getTagHandler(null, "Date", compiler));
129: System.setErr(oldErr);
130: assertTrue("No errors were produced with an ambiguous import",
131: buffer.size() > 0);
132:
133: compiler.addImport("java.util.Date");
134: assertNotNull(
135: "Did not find java.util.Date with a disambiguating import",
136: TagManager.getTagHandler(null, "Date", compiler));
137: }
138:
139: public void testInnerClass() {
140: TagHandler handler = TagManager.getTagHandler(null,
141: "JPopupMenu.Separator", compiler);
142: assertTrue("Unable to resolve tag <JPopupMenu.Separator>",
143: handler instanceof DefaultComponentHandler);
144: assertTrue(((DefaultComponentHandler) handler).getBeanClass()
145: .getName().equals(JPopupMenu.Separator.class.getName()));
146:
147: handler = TagManager.getTagHandler(null,
148: "javax.swing.JPopupMenu.Separator", compiler);
149: assertTrue(
150: "Unable to resolve tag <javax.swing.JPopupMenu.Separator>",
151: handler instanceof DefaultComponentHandler);
152: assertTrue(((DefaultComponentHandler) handler).getBeanClass()
153: .getName().equals(JPopupMenu.Separator.class.getName()));
154: }
155:
156: public void testWrongCase() {
157: assertNull("Unexpectedly found handler for 'object'",
158: TagManager.getTagHandler(null, "object", compiler));
159: assertNull("Unexpectedly found handler for 'tagmanagertest'",
160: TagManager.getTagHandler(null, "tagmanagertest",
161: compiler));
162: }
163:
164: public void testAliasing() {
165: assertEquals(
166: "JComboBox is not aliased to jaxx.runtime.swing.JAXXComboBox",
167: "jaxx.runtime.swing.JAXXComboBox", TagManager
168: .resolveClassName("JComboBox", compiler));
169: assertEquals(
170: "javax.swing.JComboBox is not aliased to jaxx.runtime.swing.JAXXComboBox",
171: "jaxx.runtime.swing.JAXXComboBox", TagManager
172: .resolveClassName("javax.swing.JComboBox",
173: compiler));
174: }
175: }
|