KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > markup > RealizerUnitTest


1 /*
2  * $Id: RealizerUnitTest.java,v 1.2 2005/01/27 13:32:03 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc.markup;
9
10 import java.awt.Component JavaDoc;
11 import java.awt.BorderLayout JavaDoc;
12
13 import java.beans.Expression JavaDoc;
14
15 import java.io.File JavaDoc;
16
17 import java.lang.reflect.Constructor JavaDoc;
18
19 import java.util.Date JavaDoc;
20
21 import java.util.logging.Level JavaDoc;
22
23 import java.net.URL JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25
26 import java.text.SimpleDateFormat JavaDoc;
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 JavaDoc baseURL;
51
52     public RealizerUnitTest(String JavaDoc name) {
53         super(name);
54         try {
55             baseURL = new File JavaDoc(".").toURL();
56         }
57         catch (Exception JavaDoc ex) {
58             throw new RuntimeException JavaDoc(ex);
59         }
60     }
61
62     /**
63      * This method creates an implementation of the object realizer.
64      * It uses the System property: openmarkup.realizer.impl
65      */

66     public static ObjectRealizer createObjectRealizer() {
67     // This system property contains the object realizer implementation.
68
String JavaDoc realizerImplName = System.getProperty("openmarkup.realizer.impl");
69     assertNotNull(realizerImplName + " is null", realizerImplName);
70
71     ObjectRealizer realizer = null;
72     try {
73         Class JavaDoc cls = Class.forName(realizerImplName);
74         Object JavaDoc or = cls.newInstance();
75         
76         // XXX this is a big hack since the Sun ObjectRealizer doesn't
77
// use net.openmarkup.Document. This is similar to RealizationUtils.
78
try {
79         realizer = (ObjectRealizer)(new Expression JavaDoc(or, "getInstance",
80                                new Object JavaDoc[0])).getValue();
81         } catch (Exception JavaDoc ex) {
82         // error
83
}
84
85         if (realizer == null) {
86         realizer = (ObjectRealizer)or;
87         }
88
89     } catch (Exception JavaDoc ex) {
90         fail(ex.toString());
91     }
92     assertNotNull(realizer);
93     
94     // Set the log level to the value of the system property
95
String JavaDoc level = System.getProperty("openmarkup.logging.level");
96     if (level != null) {
97         setLogLevel(level);
98     }
99
100     return realizer;
101     }
102
103     // Set the level of the logger.
104
static void setLogLevel(Level JavaDoc level) {
105     Scribe.getLogger().setLevel(level == null ? Level.WARNING : level);
106     }
107
108     static void setLogLevel(String JavaDoc level) {
109     Level JavaDoc lvl = null;
110     if (level != null) {
111         try {
112         lvl = Level.parse(level.toUpperCase());
113         } catch (Exception JavaDoc ex) {
114         // level is invalid, ignore
115
}
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     /* XXX - this doesn't seem to work since the enode vocabulary is the only
132        one presented.
133          public void testRealizedObjects() {
134       for (int i = 1; i <= 11; i++) {
135      Object obj = realizer.getObject(baseURL, "test/test" + i + ".xml");
136      System.out.println("Realized " + obj + ".");
137       }
138          }
139      */

140
141     /**
142      * Test to see if the toolbar, menu and status bar elements are created.
143      */

144     public void testRootPane() throws Exception JavaDoc {
145         URL JavaDoc url = RealizerUnitTest.class.getResource("resources/rootpane1.xml");
146         assertNotNull(url);
147
148         Object JavaDoc 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 JavaDoc {
160         URL JavaDoc url = RealizerUnitTest.class.getResource("resources/editor1.xml");
161         assertNotNull(url);
162
163         Object JavaDoc 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 JavaDoc 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 JavaDoc {
174         URL JavaDoc url = RealizerUnitTest.class.getResource("resources/form1.xml");
175         assertNotNull(url);
176
177         Object JavaDoc 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     /*
185         public void testTabularDataMetaData() {
186             URL url = RealizerUnitTest.class.getResource("./resources/tabulardatametadata.xml");
187             assertNotNull(url);
188             Object obj = realizer.getObject(url);
189             assertNotNull(obj);
190          assertTrue(obj.getClass().toString() + " is not a DefaultTableModelExt",
191                        obj instanceof DefaultTableModelExt);
192             DefaultTableModelExt data = (DefaultTableModelExt)obj;
193             assertEquals(8, data.getColumnCount());
194             MetaData metaData = data.getColumnMetaData(0);
195             assertNotNull(metaData);
196             MetaDataUnitTest.verifyMetaDataProperties(metaData, "bugid",
197                                                       String.class, "BugID",
198                                                       true, true,
199          Converters.get(String.class),
200                                                       null, null);
201          NumberMetaData numberMetaData = (NumberMetaData)data.getColumnMetaData(1);
202             assertNotNull(numberMetaData);
203          MetaDataUnitTest.verifyMetaDataProperties(numberMetaData, "priority",
204                                                       String.class, "Priority",
205                                                       true, false,
206          Converters.get(Integer.class),
207                                                       null, null);
208             MetaDataUnitTest.verifyNumberMetaDataProperties(numberMetaData,
209                 new Integer(1),
210                 new Integer(5), false);
211             numberMetaData = (NumberMetaData) data.getColumnMetaData(2);
212             assertNotNull(numberMetaData);
213          MetaDataUnitTest.verifyMetaDataProperties(numberMetaData, "severity",
214                                                       String.class, "Severity",
215                                                       true, true,
216          Converters.get(Integer.class),
217                                                       null, null);
218             MetaDataUnitTest.verifyNumberMetaDataProperties(numberMetaData,
219                                                       null, null, false);
220             metaData = data.getColumnMetaData(3);
221             assertNotNull(metaData);
222             MetaDataUnitTest.verifyMetaDataProperties(metaData, "engineer",
223                                                       String.class, "Engineer",
224                                                       false, false,
225          Converters.get(String.class),
226                                                       null, null);
227             metaData = data.getColumnMetaData(4);
228             assertNotNull(metaData);
229             MetaDataUnitTest.verifyMetaDataProperties(metaData, "dispatchdate",
230          String.class, "Dispatched",
231                                                       true, false,
232          Converters.get(Date.class),
233          new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"),
234          new SimpleDateFormat("MMM dd yyyy"));
235             metaData = data.getColumnMetaData(5);
236             assertNotNull(metaData);
237             MetaDataUnitTest.verifyMetaDataProperties(metaData, "jdcvotes",
238          String.class, "JDC Votes",
239                                                               true, true,
240          Converters.get(String.class),
241                                                               null, null);
242             metaData = data.getColumnMetaData(6);
243             assertNotNull(metaData);
244             MetaDataUnitTest.verifyMetaDataProperties(metaData, "synopsis",
245                                                       String.class, "Synopsis",
246                                                       false, true,
247          Converters.get(String.class),
248                                                       null, null);
249          EnumeratedMetaData enumMetaData = (EnumeratedMetaData)data.getColumnMetaData(7);
250             assertNotNull(enumMetaData);
251             MetaDataUnitTest.verifyMetaDataProperties(metaData, "state",
252                                                       String.class, "State",
253                                                       false, true,
254          Converters.get(String.class),
255                                                       null, null);
256             String stateValues[] = {"dispatched", "accepted", "evaluated",
257          "fixed", "integrated", "verified", "closed"};
258             MetaDataUnitTest.verifyEnumeratedMetaDataProperties(enumMetaData,
259                                                       stateValues);
260         }
261      */

262
263     /**
264      * Displays the jdnc application in a frame.
265      * @param demo a string which represents the relative path to a jdnc file.
266      */

267     public static void showFrame(String JavaDoc demo) {
268         URL JavaDoc url = RealizerUnitTest.class.getResource(demo);
269         if (url == null) {
270             throw new RuntimeException JavaDoc("url \"" + demo + "\" resource is null");
271         }
272         showFrame(url);
273     }
274
275     /**
276      * Displays the jdnc application in a frame.
277      * @param url a url which represents a jdnc file
278      */

279     public static void showFrame(URL JavaDoc url) {
280         if (url == null) {
281             throw new NullPointerException JavaDoc("url is null");
282         }
283     Application app = new Application(url, Level.FINE);
284     }
285
286     /**
287      * Original unit tests.
288      */

289     public static void runTests() throws Exception JavaDoc {
290         ObjectRealizer realizer = RealizerUnitTest.createObjectRealizer();
291         realizer.add(ElementTypes.get());
292
293         final Date JavaDoc start = new Date JavaDoc();
294
295         for (int i = 1; i <= 11; i++) {
296             Object JavaDoc obj = realizer.getObject("test/test" + i + ".xml");
297             System.out.println("Realized " + obj + ".");
298         }
299
300         final Date JavaDoc end = new Date JavaDoc();
301         System.out.println( (end.getTime() - start.getTime()) + " ms");
302     }
303
304     public static void main(String JavaDoc[] args) {
305         // String app = "resources/form2.xml";
306
// showFrame("resources/action3.xml");
307
//String app = "resources/action5.xml";
308
//showFrame("resources/action5.xml");
309
// showFrame("resources/form1.xml");
310
//showFrame("resources/form2.xml");
311
//showFrame("resources/form3.xml");
312
showFrame("resources/table2.xml");
313         //showFrame("resources/error3-1.xml");
314
//showFrame("resources/rootpane2.xml");
315
//showFrame(app);
316
}
317 }
318
Popular Tags