KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > describe > TestHTMLDescriptionReceiver


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.describe;
16
17 import java.util.Arrays JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20
21 import org.apache.hivemind.Registry;
22 import org.apache.hivemind.impl.RegistryBuilder;
23 import org.apache.tapestry.IMarkupWriter;
24
25 /**
26  * Tests for {@link org.apache.tapestry.describe.HTMLDescriptionReceiver}and
27  * {@link org.apache.tapestry.describe.HTMLDescriberImpl}.
28  *
29  * @author Howard M. Lewis Ship
30  * @since 4.0
31  */

32 public class TestHTMLDescriptionReceiver extends BaseDescribeTestCase
33 {
34     protected DescribableStrategy newStrategy()
35     {
36         return (DescribableStrategy) newMock(DescribableStrategy.class);
37     }
38
39     public static class NoOpStrategy implements DescribableStrategy
40     {
41         public void describeObject(Object JavaDoc object, DescriptionReceiver receiver)
42         {
43             // Does nothing; sufficient for the tests.
44
}
45     }
46
47     private void trainForTitle(IMarkupWriter writer, String JavaDoc title)
48     {
49         writer.begin("div");
50         writer.attribute("class", "described-object-title");
51         writer.print(title);
52         writer.end();
53         writer.println();
54
55         writer.begin("table");
56         writer.attribute("class", "described-object");
57         writer.println();
58     }
59
60     private void trainForSection(IMarkupWriter writer, String JavaDoc section)
61     {
62         writer.begin("tr");
63         writer.attribute("class", "section");
64         writer.begin("th");
65         writer.attribute("colspan", 2);
66         writer.print(section);
67         writer.end("tr");
68         writer.println();
69     }
70
71     private void trainForKeyValue(IMarkupWriter writer, String JavaDoc key, String JavaDoc value)
72     {
73         writer.begin("tr");
74         writer.begin("th");
75         if (key != null)
76             writer.print(key);
77         writer.end();
78         writer.begin("td");
79         writer.print(value);
80         writer.end("tr");
81         writer.println();
82     }
83
84     private void trainForNestedKeyValue(IMarkupWriter writer, String JavaDoc key, String JavaDoc value)
85     {
86         writer.begin("tr");
87         writer.begin("th");
88         if (key != null)
89             writer.print(key);
90         writer.end();
91         writer.begin("td");
92         writer.print(value);
93         writer.println();
94         writer.end("tr");
95         writer.println();
96     }
97
98     public void testSetTitleTwiceFails()
99     {
100         IMarkupWriter writer = newWriter();
101         DescribableStrategy adapter = newStrategy();
102
103         replayControls();
104
105         DescriptionReceiver dr = new HTMLDescriptionReceiver(writer, adapter);
106
107         dr.title("First Title");
108
109         try
110         {
111             dr.title("Second Title");
112             unreachable();
113
114         }
115         catch (IllegalStateException JavaDoc ex)
116         {
117             assertEquals(DescribeMessages.setTitleOnce(), ex.getMessage());
118         }
119
120         verifyControls();
121     }
122
123     public void testSetSectionBeforeTitleFails()
124     {
125         IMarkupWriter writer = newWriter();
126         DescribableStrategy adapter = newStrategy();
127
128         replayControls();
129
130         DescriptionReceiver dr = new HTMLDescriptionReceiver(writer, adapter);
131
132         try
133         {
134             dr.section("Section");
135             unreachable();
136         }
137         catch (IllegalStateException JavaDoc ex)
138         {
139             assertEquals(DescribeMessages.mustSetTitleBeforeSection(), ex.getMessage());
140         }
141
142         verifyControls();
143     }
144
145     public void testIntProperty()
146     {
147         IMarkupWriter writer = newWriter();
148         DescribableStrategy adapter = newStrategy();
149
150         trainForTitle(writer, "Object Title");
151         trainForSection(writer, "Section");
152         trainForKeyValue(writer, "intProperty", "97");
153
154         replayControls();
155
156         DescriptionReceiver dr = new HTMLDescriptionReceiver(writer, adapter);
157
158         dr.title("Object Title");
159         dr.section("Section");
160         dr.property("intProperty", 97);
161
162         verifyControls();
163     }
164
165     public void testPropertiesWithoutSection()
166     {
167         IMarkupWriter writer = newWriter();
168         DescribableStrategy adapter = newStrategy();
169
170         trainForTitle(writer, "Object Title");
171         trainForKeyValue(writer, "first", "1");
172         trainForKeyValue(writer, "second", "2");
173
174         replayControls();
175
176         DescriptionReceiver dr = new HTMLDescriptionReceiver(writer, adapter);
177
178         dr.title("Object Title");
179         dr.property("first", 1);
180         dr.property("second", 2);
181
182         verifyControls();
183
184         trainForSection(writer, "Section 1");
185         trainForKeyValue(writer, "s1", "1");
186         trainForKeyValue(writer, "s2", "2");
187
188         replayControls();
189
190         dr.section("Section 1");
191         dr.property("s1", 1);
192         dr.property("s2", 2);
193
194         verifyControls();
195     }
196
197     public void testFinishWithProperties()
198     {
199         IMarkupWriter writer = newWriter();
200         DescribableStrategy adapter = newStrategy();
201
202         trainForTitle(writer, "Object Title");
203         trainForKeyValue(writer, "first", "1");
204
205         replayControls();
206
207         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, adapter);
208
209         dr.title("Object Title");
210         dr.property("first", 1);
211
212         verifyControls();
213
214         writer.end("table");
215         writer.println();
216
217         replayControls();
218
219         dr.finishUp(null);
220
221         verifyControls();
222     }
223
224     public void testFinishNoPropertiesNoTitle()
225     {
226         IMarkupWriter writer = newWriter();
227         DescribableStrategy adapter = newStrategy();
228
229         String JavaDoc object = "Fred";
230
231         writer.print("Fred");
232         writer.println();
233
234         replayControls();
235
236         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, adapter);
237
238         dr.finishUp(object);
239
240         verifyControls();
241     }
242
243     public void testFinishNoPropertiesWithTitle()
244     {
245         IMarkupWriter writer = newWriter();
246         DescribableStrategy adapter = newStrategy();
247
248         String JavaDoc object = "Fred";
249
250         writer.print("Fred Title");
251         writer.println();
252
253         replayControls();
254
255         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, adapter);
256
257         dr.title("Fred Title");
258
259         dr.finishUp(object);
260
261         verifyControls();
262     }
263
264     public void testArray()
265     {
266         IMarkupWriter writer = newWriter();
267         DescribableStrategy adapter = new NoOpStrategy();
268
269         Object JavaDoc[] array = new Object JavaDoc[]
270         { "Fred", "Barney" };
271
272         trainForTitle(writer, "Array");
273         trainForNestedKeyValue(writer, "list", "Fred");
274         trainForNestedKeyValue(writer, null, "Barney");
275
276         replayControls();
277
278         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, adapter);
279
280         dr.title("Array");
281         dr.array("list", array);
282
283         verifyControls();
284     }
285
286     public void testCollection()
287     {
288         IMarkupWriter writer = newWriter();
289         DescribableStrategy adapter = new NoOpStrategy();
290
291         Object JavaDoc[] array = new Object JavaDoc[]
292         { "Fred", "Barney" };
293         Collection JavaDoc collection = Arrays.asList(array);
294
295         trainForTitle(writer, "Collection");
296         trainForNestedKeyValue(writer, "list", "Fred");
297         trainForNestedKeyValue(writer, null, "Barney");
298
299         replayControls();
300
301         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, adapter);
302
303         dr.title("Collection");
304         dr.collection("list", collection);
305
306         verifyControls();
307     }
308
309     public void testArrayNullAndEmpty()
310     {
311         IMarkupWriter writer = newWriter();
312         DescribableStrategy strategy = newStrategy();
313
314         replayControls();
315
316         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, strategy);
317
318         dr.title("Array");
319         dr.array("null", null);
320         dr.array("empty", new Object JavaDoc[0]);
321
322         verifyControls();
323     }
324
325     public void testCollectionNullAndEmpty()
326     {
327         IMarkupWriter writer = newWriter();
328         DescribableStrategy strategy = newStrategy();
329
330         replayControls();
331
332         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, strategy);
333
334         dr.title("Collection");
335         dr.collection("null", null);
336         dr.collection("empty", Collections.EMPTY_LIST);
337
338         verifyControls();
339     }
340
341     public void testScalarProperties()
342     {
343         IMarkupWriter writer = newWriter();
344         DescribableStrategy strategy = newStrategy();
345
346         trainForTitle(writer, "Scalars");
347         trainForKeyValue(writer, "boolean", "true");
348         trainForKeyValue(writer, "byte", "22");
349         trainForKeyValue(writer, "char", "*");
350         trainForKeyValue(writer, "short", "-8");
351         trainForKeyValue(writer, "int", "900");
352         trainForKeyValue(writer, "long", "200020");
353         trainForKeyValue(writer, "float", "3.14");
354         trainForKeyValue(writer, "double", "-2.7");
355
356         replayControls();
357
358         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, strategy);
359
360         dr.title("Scalars");
361         dr.property("boolean", true);
362         dr.property("byte", (byte) 22);
363         dr.property("char", '*');
364         dr.property("short", (short) -8);
365         dr.property("int", 900);
366         dr.property("long", 200020l);
367         dr.property("float", (float) 3.14);
368         dr.property("double", -2.7);
369
370         verifyControls();
371     }
372
373     public void testNullRoot()
374     {
375         IMarkupWriter writer = newWriter();
376         DescribableStrategy strategy = newStrategy();
377
378         writer.print(HTMLDescriptionReceiver.NULL_VALUE);
379
380         replayControls();
381
382         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, strategy);
383
384         dr.describe(null);
385
386         verifyControls();
387     }
388
389     public void testNullProperty()
390     {
391         IMarkupWriter writer = newWriter();
392         DescribableStrategy strategy = newStrategy();
393
394         trainForTitle(writer, "Null Property");
395         trainForKeyValue(writer, "null", HTMLDescriptionReceiver.NULL_VALUE);
396
397         replayControls();
398
399         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, strategy);
400
401         dr.title("Null Property");
402         dr.property("null", null);
403
404         verifyControls();
405
406     }
407
408     public void testHTMLDescriber()
409     {
410         IMarkupWriter writer = newWriter();
411         DescribableStrategy strategy = new NoOpStrategy();
412
413         String JavaDoc object = "Tapestry";
414
415         writer.print("Tapestry");
416         writer.println();
417
418         replayControls();
419
420         HTMLDescriberImpl d = new HTMLDescriberImpl();
421         d.setStrategy(strategy);
422
423         d.describeObject(object, writer);
424
425         verifyControls();
426     }
427
428     public void testDescribeAlternate()
429     {
430         IMarkupWriter writer = newWriter();
431         DescribableStrategy strategy = newStrategy();
432
433         Object JavaDoc alternate = new Object JavaDoc();
434
435         HTMLDescriptionReceiver dr = new HTMLDescriptionReceiver(writer, strategy);
436
437         strategy.describeObject(alternate, dr);
438
439         replayControls();
440
441         dr.describeAlternate(alternate);
442
443         verifyControls();
444     }
445
446     public void testIntegration() throws Exception JavaDoc
447     {
448         IMarkupWriter writer = newWriter();
449
450         Registry r = RegistryBuilder.constructDefaultRegistry();
451         // The Portlet code, which may be in the classpath under Eclipse, adds a second
452
// implementation.
453
HTMLDescriber d = (HTMLDescriber) r.getService(
454                 "tapestry.describe.HTMLDescriber",
455                 HTMLDescriber.class);
456
457         writer.print("Tapestry");
458         writer.println();
459
460         replayControls();
461
462         d.describeObject("Tapestry", writer);
463
464         verifyControls();
465
466         writer.print("Anonymous Describable");
467         writer.println();
468
469         replayControls();
470
471         d.describeObject(new Describable()
472         {
473             public void describeTo(DescriptionReceiver receiver)
474             {
475                 receiver.title("Anonymous Describable");
476             }
477         }, writer);
478
479         verifyControls();
480     }
481 }
Popular Tags