001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.ows;
006:
007: import java.io.ByteArrayInputStream;
008: import java.io.IOException;
009: import java.io.InputStream;
010:
011: import javax.xml.parsers.DocumentBuilderFactory;
012:
013: import junit.framework.TestCase;
014:
015: import org.apache.xpath.XPathAPI;
016: import org.geoserver.platform.Service;
017: import org.geoserver.platform.ServiceException;
018: import org.geotools.util.Version;
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Node;
021:
022: import com.mockobjects.servlet.MockHttpServletRequest;
023: import com.mockobjects.servlet.MockHttpServletResponse;
024: import com.mockobjects.servlet.MockServletOutputStream;
025:
026: public class DefaultServiceExceptionHandlerTest extends TestCase {
027:
028: private DefaultServiceExceptionHandler handler;
029: private Service service;
030: private MockHttpServletRequest request;
031: private MockHttpServletResponse response;
032:
033: protected void setUp() throws Exception {
034: super .setUp();
035:
036: HelloWorld helloWorld = new HelloWorld();
037: service = new Service("hello", helloWorld, new Version("1.0.0"));
038:
039: request = new MockHttpServletRequest() {
040: public int getServerPort() {
041: return 8080;
042: }
043: };
044:
045: request.setupScheme("http");
046: request.setupServerName("localhost");
047:
048: request.setupGetContextPath("geoserver");
049:
050: MockServletOutputStream output = new MockServletOutputStream();
051: response = new MockHttpServletResponse();
052: response.setupOutputStream(output);
053:
054: handler = new DefaultServiceExceptionHandler();
055: }
056:
057: public void testHandleServiceException() throws Exception {
058: ServiceException exception = new ServiceException(
059: "hello service exception");
060: exception.setCode("helloCode");
061: exception.setLocator("helloLocator");
062: exception.getExceptionText().add("helloText");
063: handler.handleServiceException(exception, service, request,
064: response);
065:
066: InputStream input = new ByteArrayInputStream(response
067: .getOutputStreamContents().getBytes());
068:
069: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
070: .newInstance();
071: docBuilderFactory.setNamespaceAware(true);
072:
073: Document doc = docBuilderFactory.newDocumentBuilder().parse(
074: input);
075:
076: assertEquals("ows:ExceptionReport", doc.getDocumentElement()
077: .getNodeName());
078: }
079:
080: public void testHandleServiceExceptionCauses() throws Exception {
081: // create a stack of three exceptions
082: IllegalArgumentException illegalArgument = new IllegalArgumentException(
083: "Illegal argument here");
084: IOException ioException = new IOException("I/O exception here");
085: ioException.initCause(illegalArgument);
086: ServiceException serviceException = new ServiceException(
087: "hello service exception");
088: serviceException.setCode("helloCode");
089: serviceException.setLocator("helloLocator");
090: serviceException.getExceptionText().add("helloText");
091: serviceException.initCause(ioException);
092: handler.handleServiceException(serviceException, service,
093: request, response);
094:
095: InputStream input = new ByteArrayInputStream(response
096: .getOutputStreamContents().getBytes());
097: System.out.println(response.getOutputStreamContents());
098:
099: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
100: .newInstance();
101: docBuilderFactory.setNamespaceAware(true);
102:
103: Document doc = docBuilderFactory.newDocumentBuilder().parse(
104: input);
105: Node exceptionText = XPathAPI
106: .selectSingleNode(doc,
107: "ows:ExceptionReport/ows:Exception/ows:ExceptionText/text()");
108: assertNotNull(exceptionText);
109: assertTrue(exceptionText.getNodeValue().indexOf(
110: illegalArgument.getMessage()) != -1);
111: assertTrue(exceptionText.getNodeValue().indexOf(
112: ioException.getMessage()) != -1);
113: assertTrue(exceptionText.getNodeValue().indexOf(
114: serviceException.getMessage()) != -1);
115: }
116: }
|