1+ /*
2+ * Copyright 2007 the original author or authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package org .springframework .oxm .support ;
18+
19+ import java .util .HashMap ;
20+ import java .util .Map ;
21+ import javax .servlet .ServletException ;
22+ import javax .xml .transform .stream .StreamResult ;
23+
24+ import junit .framework .Assert ;
25+ import junit .framework .TestCase ;
26+ import org .easymock .MockControl ;
27+
28+ import org .springframework .mock .web .MockHttpServletRequest ;
29+ import org .springframework .mock .web .MockHttpServletResponse ;
30+ import org .springframework .oxm .Marshaller ;
31+
32+ public class MarshallingViewTest extends TestCase {
33+
34+ private MarshallingView view ;
35+
36+ private MockControl control ;
37+
38+ private Marshaller marshallerMock ;
39+
40+ protected void setUp () throws Exception {
41+ control = MockControl .createControl (Marshaller .class );
42+ marshallerMock = (Marshaller ) control .getMock ();
43+ view = new MarshallingView (marshallerMock );
44+ }
45+
46+ public void testGetContentType () {
47+ Assert .assertEquals ("Invalid content type" , "application/xml" , view .getContentType ());
48+ }
49+
50+ public void testRenderModelKey () throws Exception {
51+ Object toBeMarshalled = new Object ();
52+ String modelKey = "key" ;
53+ view .setModelKey (modelKey );
54+ Map model = new HashMap ();
55+ model .put (modelKey , toBeMarshalled );
56+
57+ MockHttpServletRequest request = new MockHttpServletRequest ();
58+ MockHttpServletResponse response = new MockHttpServletResponse ();
59+
60+ control .expectAndReturn (marshallerMock .supports (Object .class ), true );
61+ marshallerMock .marshal (toBeMarshalled , new StreamResult (response .getOutputStream ()));
62+ control .setMatcher (MockControl .ALWAYS_MATCHER );
63+
64+ control .replay ();
65+ view .render (model , request , response );
66+ Assert .assertEquals ("Invalid content type" , "application/xml" , response .getContentType ());
67+ Assert .assertEquals ("Invalid content length" , 0 , response .getContentLength ());
68+ control .verify ();
69+ }
70+
71+ public void testRenderModelKeyUnsupported () throws Exception {
72+ Object toBeMarshalled = new Object ();
73+ String modelKey = "key" ;
74+ view .setModelKey (modelKey );
75+ Map model = new HashMap ();
76+ model .put (modelKey , toBeMarshalled );
77+
78+ MockHttpServletRequest request = new MockHttpServletRequest ();
79+ MockHttpServletResponse response = new MockHttpServletResponse ();
80+
81+ control .expectAndReturn (marshallerMock .supports (Object .class ), false );
82+
83+ control .replay ();
84+ try {
85+ view .render (model , request , response );
86+ fail ("ServletException expected" );
87+ }
88+ catch (ServletException ex ) {
89+ // expected
90+ }
91+ control .verify ();
92+ }
93+
94+ public void testRenderNoModelKey () throws Exception {
95+ Object toBeMarshalled = new Object ();
96+ String modelKey = "key" ;
97+ Map model = new HashMap ();
98+ model .put (modelKey , toBeMarshalled );
99+
100+ MockHttpServletRequest request = new MockHttpServletRequest ();
101+ MockHttpServletResponse response = new MockHttpServletResponse ();
102+
103+ control .expectAndReturn (marshallerMock .supports (Object .class ), true );
104+ marshallerMock .marshal (toBeMarshalled , new StreamResult (response .getOutputStream ()));
105+ control .setMatcher (MockControl .ALWAYS_MATCHER );
106+
107+ control .replay ();
108+ view .render (model , request , response );
109+ Assert .assertEquals ("Invalid content type" , "application/xml" , response .getContentType ());
110+ Assert .assertEquals ("Invalid content length" , 0 , response .getContentLength ());
111+ control .verify ();
112+ }
113+
114+ public void testRenderUnsupportedModel () throws Exception {
115+ Object toBeMarshalled = new Object ();
116+ String modelKey = "key" ;
117+ Map model = new HashMap ();
118+ model .put (modelKey , toBeMarshalled );
119+
120+ MockHttpServletRequest request = new MockHttpServletRequest ();
121+ MockHttpServletResponse response = new MockHttpServletResponse ();
122+
123+ control .expectAndReturn (marshallerMock .supports (Object .class ), false );
124+
125+ control .replay ();
126+ try {
127+ view .render (model , request , response );
128+ fail ("ServletException expected" );
129+ }
130+ catch (ServletException ex ) {
131+ // expected
132+ }
133+ control .verify ();
134+ }
135+ }
0 commit comments