1 7 8 package org.jdesktop.jdnc.markup; 9 10 import java.awt.Component ; 11 import java.awt.BorderLayout ; 12 13 import java.beans.Expression ; 14 15 import java.io.File ; 16 17 import java.lang.reflect.Constructor ; 18 19 import java.util.Date ; 20 21 import java.util.logging.Level ; 22 23 import java.net.URL ; 24 import java.net.MalformedURLException ; 25 26 import java.text.SimpleDateFormat ; 27 28 import javax.swing.*; 29 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 import org.jdesktop.jdnc.markup.ElementTypes; 35 36 import org.jdesktop.jdnc.runner.Application; 37 38 import net.openmarkup.ObjectRealizer; 39 import net.openmarkup.Scribe; 40 41 import org.jdesktop.swing.JXRootPane; 42 43 import org.jdesktop.jdnc.JNEditor; 44 import org.jdesktop.jdnc.JNForm; 45 46 public class RealizerUnitTest extends TestCase { 47 48 private ObjectRealizer realizer; 49 50 private URL baseURL; 51 52 public RealizerUnitTest(String name) { 53 super(name); 54 try { 55 baseURL = new File (".").toURL(); 56 } 57 catch (Exception ex) { 58 throw new RuntimeException (ex); 59 } 60 } 61 62 66 public static ObjectRealizer createObjectRealizer() { 67 String realizerImplName = System.getProperty("openmarkup.realizer.impl"); 69 assertNotNull(realizerImplName + " is null", realizerImplName); 70 71 ObjectRealizer realizer = null; 72 try { 73 Class cls = Class.forName(realizerImplName); 74 Object or = cls.newInstance(); 75 76 try { 79 realizer = (ObjectRealizer)(new Expression (or, "getInstance", 80 new Object [0])).getValue(); 81 } catch (Exception ex) { 82 } 84 85 if (realizer == null) { 86 realizer = (ObjectRealizer)or; 87 } 88 89 } catch (Exception ex) { 90 fail(ex.toString()); 91 } 92 assertNotNull(realizer); 93 94 String level = System.getProperty("openmarkup.logging.level"); 96 if (level != null) { 97 setLogLevel(level); 98 } 99 100 return realizer; 101 } 102 103 static void setLogLevel(Level level) { 105 Scribe.getLogger().setLevel(level == null ? Level.WARNING : level); 106 } 107 108 static void setLogLevel(String level) { 109 Level lvl = null; 110 if (level != null) { 111 try { 112 lvl = Level.parse(level.toUpperCase()); 113 } catch (Exception ex) { 114 } 116 } 117 setLogLevel(lvl); 118 } 119 120 121 122 protected void setUp() { 123 realizer = createObjectRealizer(); 124 realizer.add(ElementTypes.get()); 125 } 126 127 protected void tearDown() { 128 realizer = null; 129 } 130 131 140 141 144 public void testRootPane() throws Exception { 145 URL url = RealizerUnitTest.class.getResource("resources/rootpane1.xml"); 146 assertNotNull(url); 147 148 Object obj = realizer.getObject(url); 149 assertNotNull(obj); 150 assertTrue(obj.getClass().toString() + " is not a JRootPane", 151 obj instanceof JRootPane); 152 JXRootPane root = (JXRootPane) obj; 153 154 assertNotNull(root.getToolBar()); 155 assertNotNull(root.getJMenuBar()); 156 assertNotNull(root.getStatusBar()); 157 } 158 159 public void testEditor() throws Exception { 160 URL url = RealizerUnitTest.class.getResource("resources/editor1.xml"); 161 assertNotNull(url); 162 163 Object obj = realizer.getObject(url); 164 assertNotNull(obj); 165 assertTrue(obj.getClass().toString() + " is not a JNEditor", 166 obj instanceof JNEditor); 167 JNEditor editor = (JNEditor) obj; 168 URL bike = RealizerUnitTest.class.getResource("resources/bike.html"); 169 assertEquals(bike.toString(), editor.getInputURL()); 170 assertEquals(bike, editor.getEditor().getPage()); 171 } 172 173 public void testForm() throws Exception { 174 URL url = RealizerUnitTest.class.getResource("resources/form1.xml"); 175 assertNotNull(url); 176 177 Object obj = realizer.getObject(url); 178 assertNotNull(obj); 179 assertTrue(obj.getClass().toString() + " is not a JNForm", 180 obj instanceof JNForm); 181 JNForm editor = (JNForm) obj; 182 } 183 184 262 263 267 public static void showFrame(String demo) { 268 URL url = RealizerUnitTest.class.getResource(demo); 269 if (url == null) { 270 throw new RuntimeException ("url \"" + demo + "\" resource is null"); 271 } 272 showFrame(url); 273 } 274 275 279 public static void showFrame(URL url) { 280 if (url == null) { 281 throw new NullPointerException ("url is null"); 282 } 283 Application app = new Application(url, Level.FINE); 284 } 285 286 289 public static void runTests() throws Exception { 290 ObjectRealizer realizer = RealizerUnitTest.createObjectRealizer(); 291 realizer.add(ElementTypes.get()); 292 293 final Date start = new Date (); 294 295 for (int i = 1; i <= 11; i++) { 296 Object obj = realizer.getObject("test/test" + i + ".xml"); 297 System.out.println("Realized " + obj + "."); 298 } 299 300 final Date end = new Date (); 301 System.out.println( (end.getTime() - start.getTime()) + " ms"); 302 } 303 304 public static void main(String [] args) { 305 showFrame("resources/table2.xml"); 313 } 317 } 318 | Popular Tags |