KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > components > TestInsert


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.components;
16
17 import java.text.DateFormat JavaDoc;
18 import java.text.Format JavaDoc;
19 import java.util.Date JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.Location;
23 import org.apache.tapestry.IBinding;
24 import org.apache.tapestry.IMarkupWriter;
25 import org.apache.tapestry.IPage;
26 import org.apache.tapestry.IRequestCycle;
27 import org.apache.tapestry.html.BasePage;
28 import org.apache.tapestry.spec.IComponentSpecification;
29 import org.easymock.MockControl;
30
31 /**
32  * Tests for the {@link org.apache.tapestry.components.Insert}  component.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 4.0
36  */

37 public class TestInsert extends BaseComponentTestCase
38 {
39     /**
40      * Returns a new page instance (not a mock page).
41      */

42
43     private IPage newBasePage(String JavaDoc name)
44     {
45         BasePage page = (BasePage) newInstance(BasePage.class);
46
47         page.setPageName(name);
48
49         return page;
50     }
51
52     public void testRewinding()
53     {
54         IRequestCycle cycle = newCycle(true);
55
56         replayControls();
57
58         Insert insert = (Insert) newInstance(Insert.class);
59
60         insert.render(null, cycle);
61
62         verifyControls();
63     }
64
65     public void testNullValue()
66     {
67         IRequestCycle cycle = newCycle(false);
68
69         replayControls();
70
71         Insert insert = (Insert) newInstance(Insert.class, new Object JavaDoc[]
72         { "value", null });
73
74         insert.render(null, cycle);
75
76         verifyControls();
77     }
78
79     public void testNoFormat()
80     {
81         IRequestCycle cycle = newCycle(false);
82         IMarkupWriter writer = newWriter();
83
84         writer.print("42", false);
85
86         replayControls();
87
88         Insert insert = (Insert) newInstance(Insert.class, new Object JavaDoc[]
89         { "value", new Integer JavaDoc(42) });
90
91         insert.render(writer, cycle);
92
93         verifyControls();
94     }
95
96     public void testFormat()
97     {
98         IRequestCycle cycle = newCycle(false);
99         IMarkupWriter writer = newWriter();
100
101         Date JavaDoc date = new Date JavaDoc();
102         DateFormat JavaDoc format = DateFormat.getDateInstance();
103         String JavaDoc expected = format.format(date);
104
105         writer.print(expected, false);
106
107         replayControls();
108
109         Insert insert = (Insert) newInstance(Insert.class, new Object JavaDoc[]
110         { "value", date, "format", format });
111
112         insert.render(writer, cycle);
113
114         verifyControls();
115     }
116
117     public void testUnableToFormat()
118     {
119         Object JavaDoc value = "xyzzyx";
120         Location l = fabricateLocation(87);
121         IBinding binding = newBinding(l);
122
123         Format JavaDoc format = DateFormat.getInstance();
124
125         IRequestCycle cycle = newCycle(false);
126         IMarkupWriter writer = newWriter();
127
128         IPage page = newBasePage("Flintstone");
129
130         replayControls();
131
132         Insert insert = (Insert) newInstance(Insert.class, new Object JavaDoc[]
133         { "value", value, "format", format });
134         insert.setBinding("format", binding);
135         insert.setId("fred");
136         insert.setPage(page);
137         insert.setContainer(page);
138
139         try
140         {
141             insert.render(writer, cycle);
142             unreachable();
143         }
144         catch (ApplicationRuntimeException ex)
145         {
146             assertEquals(
147                     "Flintstone/fred unable to format value 'xyzzyx': Cannot format given Object as a Date",
148                     ex.getMessage());
149             assertSame(insert, ex.getComponent());
150             assertSame(l, ex.getLocation());
151         }
152
153         verifyControls();
154     }
155
156     public void testRaw()
157     {
158         IRequestCycle cycle = newCycle(false);
159         IMarkupWriter writer = newWriter();
160
161         writer.print("42", true);
162
163         replayControls();
164
165         Insert insert = (Insert) newInstance(Insert.class, new Object JavaDoc[]
166         { "value", new Integer JavaDoc(42), "raw", Boolean.TRUE });
167
168         insert.render(writer, cycle);
169
170         verifyControls();
171     }
172
173     public void testStyleClass()
174     {
175         IBinding informal = newBinding("informal-value");
176
177         IRequestCycle cycle = newCycle(false);
178         IMarkupWriter writer = newWriter();
179         IComponentSpecification spec = newSpec("informal", null);
180
181         writer.begin("span");
182         writer.attribute("class", "paisley");
183         writer.attribute("informal", "informal-value");
184         writer.print("42", false);
185         writer.end();
186
187         replayControls();
188
189         Insert insert = (Insert) newInstance(Insert.class, new Object JavaDoc[]
190         { "value", "42", "specification", spec, "styleClass", "paisley" });
191
192         insert.setBinding("informal", informal);
193
194         insert.render(writer, cycle);
195
196         verifyControls();
197     }
198 }
Popular Tags