Moved tests over from testsuite to test

This commit is contained in:
Arjen Poutsma
2008-10-29 13:58:15 +00:00
parent 10f09e6298
commit 58dbd04cec
3 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mock.web;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
/**
* @author Rick Evans
* @author Mark Fisher
*/
public class MockHttpServletRequestTests extends TestCase {
public void testHttpHeaderNameCasingIsPreserved() throws Exception {
String headerName = "Header1";
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(headerName, "value1");
Enumeration requestHeaders = request.getHeaderNames();
assertNotNull(requestHeaders);
assertEquals("HTTP header casing not being preserved", headerName, requestHeaders.nextElement());
}
public void testSetMultipleParameters() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter("key1", "value1");
request.setParameter("key2", "value2");
Map params = new HashMap(2);
params.put("key1", "newValue1");
params.put("key3", new String[] { "value3A", "value3B" });
request.setParameters(params);
String[] values1 = request.getParameterValues("key1");
assertEquals(1, values1.length);
assertEquals("newValue1", request.getParameter("key1"));
assertEquals("value2", request.getParameter("key2"));
String[] values3 = request.getParameterValues("key3");
assertEquals(2, values3.length);
assertEquals("value3A", values3[0]);
assertEquals("value3B", values3[1]);
}
public void testAddMultipleParameters() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter("key1", "value1");
request.setParameter("key2", "value2");
Map params = new HashMap(2);
params.put("key1", "newValue1");
params.put("key3", new String[] { "value3A", "value3B" });
request.addParameters(params);
String[] values1 = request.getParameterValues("key1");
assertEquals(2, values1.length);
assertEquals("value1", values1[0]);
assertEquals("newValue1", values1[1]);
assertEquals("value2", request.getParameter("key2"));
String[] values3 = request.getParameterValues("key3");
assertEquals(2, values3.length);
assertEquals("value3A", values3[0]);
assertEquals("value3B", values3[1]);
}
public void testRemoveAllParameters() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter("key1", "value1");
Map params = new HashMap(2);
params.put("key2", "value2");
params.put("key3", new String[] { "value3A", "value3B" });
request.addParameters(params);
assertEquals(3, request.getParameterMap().size());
request.removeAllParameters();
assertEquals(0, request.getParameterMap().size());
}
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mock.web;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import junit.framework.TestCase;
import org.springframework.web.util.WebUtils;
/**
* @author Juergen Hoeller
* @author Rick Evans
* @since 19.02.2006
*/
public class MockHttpServletResponseTests extends TestCase {
public void testSetContentTypeWithNoEncoding() {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setContentType("test/plain");
assertEquals("Character encoding should be the default",
WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
}
public void testSetContentTypeWithUTF8() {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setContentType("test/plain; charset=UTF-8");
assertEquals("Character encoding should be 'UTF-8'", "UTF-8", response.getCharacterEncoding());
}
public void testHttpHeaderNameCasingIsPreserved() throws Exception {
final String headerName = "Header1";
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader(headerName, "value1");
Set responseHeaders = response.getHeaderNames();
assertNotNull(responseHeaders);
assertEquals(1, responseHeaders.size());
assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());
}
public void testServletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
assertFalse(response.isCommitted());
response.getOutputStream().write('X');
assertFalse(response.isCommitted());
int size = response.getBufferSize();
response.getOutputStream().write(new byte[size]);
assertTrue(response.isCommitted());
assertEquals(size + 1, response.getContentAsByteArray().length);
}
public void testServletOutputStreamCommittedOnFlushBuffer() throws IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
assertFalse(response.isCommitted());
response.getOutputStream().write('X');
assertFalse(response.isCommitted());
response.flushBuffer();
assertTrue(response.isCommitted());
assertEquals(1, response.getContentAsByteArray().length);
}
public void testServletWriterCommittedWhenBufferSizeExceeded() throws IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
assertFalse(response.isCommitted());
response.getWriter().write("X");
assertFalse(response.isCommitted());
int size = response.getBufferSize();
char[] data = new char[size];
Arrays.fill(data, 'p');
response.getWriter().write(data);
assertTrue(response.isCommitted());
assertEquals(size + 1, response.getContentAsByteArray().length);
}
public void testServletOutputStreamCommittedOnOutputStreamFlush() throws IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
assertFalse(response.isCommitted());
response.getOutputStream().write('X');
assertFalse(response.isCommitted());
response.getOutputStream().flush();
assertTrue(response.isCommitted());
assertEquals(1, response.getContentAsByteArray().length);
}
public void testServletWriterCommittedOnWriterFlush() throws IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
assertFalse(response.isCommitted());
response.getWriter().write("X");
assertFalse(response.isCommitted());
response.getWriter().flush();
assertTrue(response.isCommitted());
assertEquals(1, response.getContentAsByteArray().length);
}
public void testServletWriterAutoFlushedForString() throws IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
response.getWriter().write("X");
assertEquals("X", response.getContentAsString());
}
public void testServletWriterAutoFlushedForChar() throws IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
response.getWriter().write('X');
assertEquals("X", response.getContentAsString());
}
public void testServletWriterAutoFlushedForCharArray() throws IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
response.getWriter().write("XY".toCharArray());
assertEquals("XY", response.getContentAsString());
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mock.web;
import junit.framework.TestCase;
import javax.servlet.jsp.PageContext;
/**
* Unit tests for the <code>MockPageContext</code> class.
*
* @author Rick Evans
*/
public final class MockPageContextTests extends TestCase {
public void testSetAttributeWithNoScopeUsesPageScope() throws Exception {
String key = "foo";
String value = "bar";
MockPageContext ctx = new MockPageContext();
ctx.setAttribute(key, value);
assertEquals(value, ctx.getAttribute(key, PageContext.PAGE_SCOPE));
assertNull(ctx.getAttribute(key, PageContext.APPLICATION_SCOPE));
assertNull(ctx.getAttribute(key, PageContext.REQUEST_SCOPE));
assertNull(ctx.getAttribute(key, PageContext.SESSION_SCOPE));
}
public void testRemoveAttributeWithNoScopeSpecifiedRemovesValueFromAllScopes() throws Exception {
String key = "foo";
String value = "bar";
MockPageContext ctx = new MockPageContext();
ctx.setAttribute(key, value, PageContext.APPLICATION_SCOPE);
ctx.removeAttribute(key);
assertNull(ctx.getAttribute(key, PageContext.PAGE_SCOPE));
assertNull(ctx.getAttribute(key, PageContext.APPLICATION_SCOPE));
assertNull(ctx.getAttribute(key, PageContext.REQUEST_SCOPE));
assertNull(ctx.getAttribute(key, PageContext.SESSION_SCOPE));
}
}