01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: **/package org.araneaframework.tests.servlet.util;
16:
17: import javax.servlet.http.Cookie;
18: import junit.framework.TestCase;
19: import org.araneaframework.http.core.StandardServletOutputData;
20: import org.araneaframework.http.util.AtomicResponseHelper;
21: import org.springframework.mock.web.MockHttpServletRequest;
22: import org.springframework.mock.web.MockHttpServletResponse;
23:
24: /**
25: *
26: * @author "Toomas Römer" <toomas@webmedia.ee>
27: */
28: public class AtomicResponseHelperTests extends TestCase {
29: private AtomicResponseHelper atomic;
30: private StandardServletOutputData output;
31: private MockHttpServletResponse res;
32:
33: public void setUp() throws Exception {
34: res = new MockHttpServletResponse();
35: res.setContentType("text/html; charset=UTF-8");
36:
37: output = new StandardServletOutputData(
38: new MockHttpServletRequest(), res);
39:
40: atomic = new AtomicResponseHelper(output);
41: }
42:
43: public void testCommitWriter() throws Exception {
44: res.getWriter().write("Hello, World!");
45: atomic.commit();
46: assertTrue(res.getContentAsByteArray().length > 0);
47: }
48:
49: /*
50: * MockHttpServletResponse.getContentAsByteArray() commits the
51: * PrintWriter and OutputStream. The reset method does not reset
52: * the PrintWriter nor OutputStream, just the internal buffer. But after
53: * resetting and then flushing, the old contents of the PrintWriter or OutputStream
54: * is copied to the internal buffer. It doesn't seem correct but don't have time
55: * to start making changes to the mock of Spring. Anyways this test will stay empty, until
56: * i find a solution.
57: */
58: public void testRollbackWriter() throws Exception {
59: fail();
60: /*
61: res.getWriter().write("Hello, World!");
62: atomic.rollback();
63: System.out.println(res.getContentAsByteArray().length+" "+res.getContentAsString());
64: assertTrue(res.getContentAsByteArray().length==0);*/
65: }
66:
67: public void testRollBackHeaders() throws Exception {
68: res.setHeader("key", "value");
69: assertEquals("value", res.getHeader("key"));
70:
71: Cookie cookie = new Cookie("key", "value");
72: res.addCookie(cookie);
73: assertEquals(cookie, res.getCookie("key"));
74:
75: atomic.rollback();
76: assertEquals(null, res.getHeader("key"));
77: assertEquals(null, res.getCookie("key"));
78: }
79:
80: public void testDoubleCommit() throws Exception {
81: try {
82: atomic.commit();
83: atomic.commit();
84: fail();
85: } catch (IllegalStateException e) {
86: //success
87: }
88: }
89: }
|