/*
 * Copyright 2006-2008 Web Cohesion
 *
 * 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.codehaus.enunciate.webapp;

import static org.easymock.EasyMock.*;

import junit.framework.TestCase;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author Ryan Heaton
 */
public class TestWSDLFilter extends TestCase {

  /**
   * tests the filtering.
   */
  public void testFiltering() throws Exception {
    final ServletContext context = createMock(ServletContext.class);
    WSDLFilter filter = new WSDLFilter() {
      @Override
      public ServletContext getServletContext() {
        return context;
      }
    };
    filter.setAssumedBaseAddress("http://localhost:8080/full");
    String wsdl = "<soap:address location=\"http://localhost:8080/full/soap-services/PersonServiceService\"/>\n" +
      "<somexml xmlns=\"http://localhost:8080/full/myns\"/>";
    HttpServletRequest req = createMock(HttpServletRequest.class);
    HttpServletResponse res = createMock(HttpServletResponse.class);
    FilterChain chain = createMock(FilterChain.class);
    expect(req.getRequestURL()).andReturn(new StringBuffer("http://myhost.com/mycontext/something/test.wsdl"));
    expect(req.getContextPath()).andReturn("/mycontext");
    expect(context.getResourceAsStream("/something/test.wsdl")).andReturn(new ByteArrayInputStream(wsdl.getBytes("utf-8")));
    StringWriter writer = new StringWriter();
    res.setContentType("application/xml");
    expect(res.getWriter()).andReturn(new PrintWriter(writer));
    replay(context, req, res, chain);
    filter.doFilter(req, res, chain);
    verify(context, req, res, chain);
    reset(context, req, res, chain);
    assertEquals("<soap:address location=\"http://myhost.com/mycontext/soap-services/PersonServiceService\"/>\n<somexml xmlns=\"http://localhost:8080/full/myns\"/>", writer.toString().trim());
  }

}

@@ -1044,19 +1044,487 @@
 tCase %7B%0A
-%0A
   
+private static final String EXPECTED_HEADER_LINE_1 = %22%3Csoap:address location=%5C%22http://myhost.com/mycontext/soap-services/PersonServiceService%5C%22/%3E%22; %0A  private static final String EXPECTED_HEADER_LINE_2 = %22%3Csomexml xmlns=%5C%22http://localhost:8080/full/myns%5C%22/%3E%22;%0A%0A  private static final String WSDL_HEADER = %22%3Csoap:address location=%5C%22http://localhost:8080/full/soap-services/PersonServiceService%5C%22/%3E%5Cn%22 +%0A%09      %22%3Csomexml xmlns=%5C%22http://localhost:8080/full/myns%5C%22/%3E%22;%0A  %0A%0A
 /**%0A   *
@@ -1817,16 +1817,21 @@
 %0A    %7D;%0A
+    %0A
     filt
@@ -1890,185 +1890,8 @@
 %22);%0A
-    String wsdl = %22%3Csoap:address location=%5C%22http://localhost:8080/full/soap-services/PersonServiceService%5C%22/%3E%5Cn%22 +%0A      %22%3Csomexml xmlns=%5C%22http://localhost:8080/full/myns%5C%22/%3E%22;%0A
     
@@ -2346,20 +2346,27 @@
 tStream(
-wsdl
+WSDL_HEADER
 .getByte
@@ -2379,20 +2379,17 @@
 -8%22)));%0A
-    
+%09
 StringWr
@@ -2684,199 +2684,791 @@
 n);%0A
-    assertEquals(%22%3Csoap:address location=%5C%22http://myhost.com/mycontext/soap-services/PersonServiceService%5C%22/%3E%5Cn%3Csomexml xmlns=%5C%22http://localhost:8080/full/myns%5C%22/%3E%22, writer.toString().trim())
+%0A%09%0A%09String actualHeader = writer.toString().trim();%0A%09String expectedOut = buildExpectedOut();%0A%09assertEquals(expectedOut, actualHeader);%0A  %7D%0A  %0A  /**%0A   * Windows uses /r/n instead of only /n to write new lines. %0A   * This builds the expected string so it works right across systems. %0A   * Or so I hope i didn't break anything%0A   * %0A   * @return the expected output using PrintWriter, like the filter does it%0A   */%0A  private String buildExpectedOut() %7B%0A%09  StringWriter expectedStringWriter = new StringWriter();%0A%09  PrintWriter expectedWriter = new PrintWriter(expectedStringWriter);%0A%09  %0A%09  expectedWriter.println(EXPECTED_HEADER_LINE_1);%0A%09  expectedWriter.println(EXPECTED_HEADER_LINE_2);%0A%09  String expectedHeader = expectedStringWriter.toString().trim();%0A%09  %0A%09  return expectedHeader
 ;%0A  

Test breaks on windows systems due to use of /r/n on new line characters. Fixed by running through the PrintWriter like the filter does