1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, Steve Meyfroidt
4 * All rights reserved.
5 * Email: smeyfroi@users.sourceforge.net
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name "Scope" nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
37 * $Id: TestServletView.java,v 1.4 2002/08/05 13:17:17 ludovicc Exp $
38 */
39
40
41 package test.view.servlet;
42
43 import java.io.ByteArrayOutputStream;
44 import java.io.OutputStream;
45 import java.io.StringWriter;
46 import java.io.Writer;
47 import junit.framework.TestCase;
48 import org.scopemvc.controller.basic.ViewContext;
49 import org.scopemvc.controller.servlet.ServletContext;
50 import org.scopemvc.controller.basic.BasicController;
51 import org.scopemvc.core.Control;
52 import org.scopemvc.core.ControlException;
53 import org.scopemvc.core.View;
54 import org.scopemvc.view.servlet.Page;
55 import org.scopemvc.view.servlet.ServletView;
56
57
58 /***
59 *
60 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
61 * @version $Revision: 1.4 $ $Date: 2002/08/05 13:17:17 $
62 */
63 public final class TestServletView extends TestCase {
64
65
66 public TestServletView(String inName) {
67 super(inName);
68 }
69
70
71 private ServletView view;
72 private Object model;
73 private ServletTestController controller;
74
75
76 protected void setUp() {
77 view = new ServletView();
78 model = new Object();
79 controller = new ServletTestController();
80 }
81
82
83 public void testGetSetBoundModel() throws Exception {
84 assertNull(view.getBoundModel());
85 view.setBoundModel(model);
86 assertSame(model, view.getBoundModel());
87 }
88
89
90 public void testGetSetController() throws Exception {
91 assertNull(view.getController());
92 view.setController(controller);
93 assertSame(controller, view.getController());
94 }
95
96
97 public void testControlIssue() {
98 ViewContext.setThreadContext(new TestServletContext());
99 view.setController(controller);
100
101 Control control = new Control("TestControl");
102 view.issueControl(control);
103 assertTrue("Control not issued through parent view", controller.handledControl);
104 }
105
106
107 public void testPages() {
108 Page page1 = new ServletTestPage("1");
109 Page page2 = new ServletTestPage("2");
110
111 view.addPage(page1);
112 view.addPage(page2);
113
114 assertSame(view, page1.getParent());
115 assertSame(view, page2.getParent());
116
117 assertSame(page1, view.findPageByID("1"));
118 assertSame(page2, view.findPageByID("2"));
119
120 assertSame(page1, view.getVisible());
121 assertSame(page1, view.getFirstPage());
122
123 view.setVisible("2");
124 assertSame(page2, view.getVisible());
125 assertSame(page1, view.getFirstPage());
126
127 view.setVisible("1");
128 assertSame(page1, view.getVisible());
129 assertSame(page1, view.getFirstPage());
130
131 try {
132 view.setVisible("rubbish");
133 fail("Set visible to an ID that doesn't exist");
134 }
135 catch (UnsupportedOperationException e) {
136 // expected
137 }
138 }
139
140
141 public void testStreamView() throws Exception {
142 Page page1 = new ServletTestPage("1");
143 Page page2 = new ServletTestPage("2");
144
145 view.addPage(page1);
146 view.addPage(page2);
147
148 StringWriter writer = new StringWriter();
149 ((ServletTestPage)view.getVisible()).streamView(writer);
150 assertEquals("1", writer.toString());
151
152 writer = new StringWriter();
153 view.setVisible("2");
154 ((ServletTestPage)view.getVisible()).streamView(writer);
155 assertEquals("2", writer.toString());
156
157 writer = new StringWriter();
158 view.setVisible("1");
159 ((ServletTestPage)view.getVisible()).streamView(writer);
160 assertEquals("1", writer.toString());
161 }
162
163
164 public void testStreamViewError() throws Exception {
165 StringWriter writer = new StringWriter();
166 try {
167 ((ServletTestPage)view.getVisible()).streamView(writer);
168 fail("Streamed a view when no Pages exist.");
169 } catch (NullPointerException e) {
170 // expected
171 }
172 }
173
174
175 public void testGetContentType() throws Exception {
176 ServletTestPage page1 = new ServletTestPage("1");
177 ServletTestPage page2 = new ServletTestPage("2");
178
179 view.addPage(page1);
180 view.addPage(page2);
181
182 page1.setContentType("abc");
183 page2.setContentType("xyz");
184
185 assertEquals("abc", ((ServletTestPage)view.getVisible()).getContentType());
186
187 view.setVisible("2");
188 assertEquals("xyz", ((ServletTestPage)view.getVisible()).getContentType());
189 }
190
191 static class TestServletContext extends ServletContext {
192 public TestServletContext() {
193 super(null, null, null, null);
194 }
195 public void showView(View inView) {}
196 }
197 }
198
199
This page was automatically generated by Maven