KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > arooa > DesignIHTest


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.arooa;
5
6 import junit.framework.TestCase;
7
8 import org.oddjob.designer.elements.SimpleDE;
9 import org.oddjob.designer.model.DesignElementType;
10 import org.xml.sax.Attributes JavaDoc;
11
12 /**
13  *
14  */

15 public class DesignIHTest extends TestCase {
16
17     public void testText() {
18         class Test {
19             public String JavaDoc text() { return "test"; }
20         }
21         Test test = new Test();
22         DesignIH dih = DesignIH.getHelper(test.getClass());
23         assertEquals("text", "test", dih.text(test));
24     }
25
26     public void testUnknown() {
27         class Test {
28             String JavaDoc unknown;
29             public void unknown(String JavaDoc unknown) {
30                 this.unknown = unknown;
31             }
32             public String JavaDoc unknown() {
33                 return unknown;
34             }
35         }
36         Test test = new Test();
37         DesignIH dih = DesignIH.getHelper(test.getClass());
38         dih.unknown(test, "test");
39         assertEquals("text", "test", dih.unknown(test));
40     }
41
42     public void testAttributes() {
43         class Test {
44             public String JavaDoc getThisTest() {
45                 return "test1";
46             }
47             public String JavaDoc getThatTest() {
48                 return "test2";
49             }
50         }
51         Test test = new Test();
52         DesignIH dih = DesignIH.getHelper(test.getClass());
53         Attributes JavaDoc atts = dih.attributes(test);
54         assertEquals("size", 2, atts.getLength());
55         assertEquals("name 1", "thisTest", atts.getLocalName(0));
56         assertEquals("value 1", "test1", atts.getValue(0));
57         assertEquals("name 2", "thatTest", atts.getLocalName(1));
58         assertEquals("value 2", "test2", atts.getValue(1));
59     }
60
61     public void testDEAttributes() {
62         class Test {
63             // an attribute
64
public SimpleDE getThisTest() {
65                 SimpleDE a = new SimpleDE();
66                 a.attribute("test1");
67                 return a;
68             }
69             // not an attribute
70
public SimpleDE getThatTest() {
71                 return new SimpleDE();
72             }
73             public SimpleDE getNullTest() {
74                 return null;
75             }
76         }
77         Test test = new Test();
78         DesignIH dih = DesignIH.getHelper(test.getClass());
79         Attributes JavaDoc atts = dih.attributes(test);
80         assertEquals("size", 1, atts.getLength());
81         assertEquals("name 1", "thisTest", atts.getLocalName(0));
82         assertEquals("value 1", "test1", atts.getValue(0));
83         
84         ElementWrapper[] ews = dih.elements(test);
85         assertEquals("elements", 0, ews.length);
86     }
87
88     public void testElements() {
89         class Test {
90             public ElementWrapper[] elements() {
91                 return new ElementWrapper[] {
92                         new ElementWrapper("thistest",
93                             new Object JavaDoc() {
94                                 public String JavaDoc toString() {
95                                     return "test1";
96                                 }
97                             }),
98                         new ElementWrapper( ("thattest"),
99                             new Object JavaDoc() {
100                                 public String JavaDoc toString() {
101                                     return "test2";
102                                 }
103                             })
104                     };
105             }
106         }
107         Test test = new Test();
108         DesignIH dih = DesignIH.getHelper(test.getClass());
109         ElementWrapper[] ews = dih.elements(test);
110         assertEquals("value 1", "test1", ews[0].getElement().toString());
111         assertEquals("value 2", "test2", ews[1].getElement().toString());
112     }
113
114     /**
115      * Test named elements. This test the elementXXX methods.
116      *
117      */

118     public void testNamedElements() {
119         class Test {
120             public Object JavaDoc[] elementsThisTest() {
121                 return new Object JavaDoc[] {
122                     new Object JavaDoc() {
123                         public String JavaDoc toString() {
124                             return "test1";
125                         }
126                     } };
127             }
128             public Object JavaDoc[] elementsThatTest() {
129                 return new Object JavaDoc[] {
130                     new Object JavaDoc() {
131                         public String JavaDoc toString() {
132                             return "test2";
133                         }
134                     } };
135             }
136         }
137         Test test = new Test();
138         DesignIH dih = DesignIH.getHelper(test.getClass());
139         ElementWrapper[] ews = dih.elements(test);
140
141         assertEquals(2, ews.length);
142         
143         assertEquals("name 1", "thisTest", ews[0].getName());
144         assertEquals("value 1", "test1", ews[0].getElement().toString());
145
146         assertEquals("name 2", "thatTest", ews[1].getName());
147         assertEquals("value 2", "test2", ews[1].getElement().toString());
148     }
149
150     /**
151      * Has a populated attribute.
152      */

153     public void testHasDetailData1() {
154         class Test extends DesignElementType {
155             public Object JavaDoc elementFoo() {
156                 return new Object JavaDoc();
157             }
158         }
159         Test test = new Test();
160         DesignIH dih = DesignIH.getHelper(test.getClass());
161         assertTrue("detail data", dih.hasDetailData(test));
162     }
163
164     /**
165      * Has an empty element
166      *
167      */

168     public void testHasDetailData2() {
169         class Test extends DesignElementType {
170             public Object JavaDoc elementFoo() {
171                 return null;
172             }
173         }
174         Test test = new Test();
175         DesignIH dih = DesignIH.getHelper(test.getClass());
176         assertFalse("detail data", dih.hasDetailData(test));
177     }
178     
179     /**
180      * Has an empty element
181      *
182      */

183     public void testHasDetailData3() {
184         class Test extends DesignElementType {
185             public Object JavaDoc getFoo() {
186                 return null;
187             }
188         }
189         Test test = new Test();
190         DesignIH dih = DesignIH.getHelper(test.getClass());
191         assertFalse("detail data", dih.hasDetailData(test));
192     }
193 }
194
Popular Tags