vmassol     01/05/05 07:37:18

  Modified:    cactus/src/framework/share/org/apache/commons/cactus/util
                        AssertUtils.java
               cactus/src/sample/share/org/apache/commons/cactus/sample
                        SampleServlet.java TestSampleServlet.java
               cactus/src/sample/share/org/apache/commons/cactus/sample/unit
                        TestServletTestCase2.java
  Log:
  fixed bug #1610 + new test for AssertUtils.getResponseAsString() + new tests for new 
method AssertUtils.getResponseAsStringArray()
  
  Revision  Changes    Path
  1.2       +26 -3     
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/util/AssertUtils.java
  
  Index: AssertUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/util/AssertUtils.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AssertUtils.java  2001/04/09 11:52:38     1.1
  +++ AssertUtils.java  2001/05/05 14:37:16     1.2
  @@ -74,13 +74,36 @@
       {
           StringBuffer sb = new StringBuffer();
           BufferedReader input = new BufferedReader(new 
InputStreamReader(theConnection.getInputStream()));
  -        String str;
  -        while (null != ((str = input.readLine()))) {
  -            sb.append(str);
  +        char[] buffer = new char[2048];
  +        int nb;
  +        while (-1 != (nb = input.read(buffer, 0, 2048))) {
  +            sb.append(buffer, 0, nb);
           }
           input.close ();
   
           return sb.toString();
  +    }
  +
  +    /**
  +     * @param theConnection the connection object used to connect to the server
  +     *                      redirector.
  +     * @return the servlet output stream bytes as an array of string (each
  +     *         string is a separate line from the output stream).
  +     */
  +    public static String[] getResponseAsStringArray(HttpURLConnection 
theConnection) throws IOException
  +    {
  +        BufferedReader input = new BufferedReader(new 
InputStreamReader(theConnection.getInputStream()));
  +        Vector lines = new Vector();
  +        String str;
  +        while (null != (str = input.readLine())) {
  +            lines.addElement(str);
  +        }
  +        input.close ();
  +
  +        // Fixme: I don't know why but if I don't use this dummy stuff I get a
  +        // ClassCastException !
  +        String[] dummy = new String[lines.size()];
  +        return (String[])(lines.toArray(dummy));
       }
   
       /**
  
  
  
  1.2       +6 -3      
jakarta-commons/cactus/src/sample/share/org/apache/commons/cactus/sample/SampleServlet.java
  
  Index: SampleServlet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/sample/share/org/apache/commons/cactus/sample/SampleServlet.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SampleServlet.java        2001/04/09 11:52:38     1.1
  +++ SampleServlet.java        2001/05/05 14:37:17     1.2
  @@ -82,9 +82,12 @@
   
           theResponse.setContentType("text/html");
   
  -        pw.println("<html><head/><body>");
  -        pw.println("A GET request");
  -        pw.println("</body></html>");
  +        // Note: We send the text in one line only because some servlet engines
  +        // (like Tomcat 3.2) add some characters at the end of the line
  +        // ('\x0D' + '\x0A') even though we use the print() method and not
  +        // println() ....
  +
  +        pw.print("<html><head/><body>A GET request</body></html>");
       }
   
       /**
  
  
  
  1.2       +3 -2      
jakarta-commons/cactus/src/sample/share/org/apache/commons/cactus/sample/TestSampleServlet.java
  
  Index: TestSampleServlet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/sample/share/org/apache/commons/cactus/sample/TestSampleServlet.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestSampleServlet.java    2001/04/09 11:52:38     1.1
  +++ TestSampleServlet.java    2001/05/05 14:37:17     1.2
  @@ -122,8 +122,9 @@
        */
       public void endReadServletOutputStream(HttpURLConnection theConnection) throws 
IOException
       {
  -        assertEquals("<html><head/><body>A GET request</body></html>",
  -            AssertUtils.getResponseAsString(theConnection));
  +        String expected = "<html><head/><body>A GET request</body></html>";
  +        String result = AssertUtils.getResponseAsString(theConnection);
  +        assertEquals(expected, result);            
       }
   
       //-------------------------------------------------------------------------
  
  
  
  1.3       +71 -0     
jakarta-commons/cactus/src/sample/share/org/apache/commons/cactus/sample/unit/TestServletTestCase2.java
  
  Index: TestServletTestCase2.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/sample/share/org/apache/commons/cactus/sample/unit/TestServletTestCase2.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestServletTestCase2.java 2001/04/14 18:27:26     1.2
  +++ TestServletTestCase2.java 2001/05/05 14:37:18     1.3
  @@ -63,6 +63,7 @@
   import junit.framework.*;
   
   import org.apache.commons.cactus.*;
  +import org.apache.commons.cactus.util.*;
   
   /**
    * Some Cactus unit tests for testing <code>ServletTestCase</code>.
  @@ -289,6 +290,76 @@
           }
           assertEquals("Should have received 2 values for header [testheader]", 2, 
count);
           */
  +    }
  +
  +    //-------------------------------------------------------------------------
  +
  +    /**
  +     * Verify that the <code>AsertUtils.getResponseAsString()</code> method
  +     * works with output text sent on multiple lines.
  +     */
  +    public void testGetResponseAsStringMultiLines() throws IOException
  +    {
  +        PrintWriter pw = response.getWriter();
  +        response.setContentType("text/html");
  +        pw.println("<html><head/>");
  +        pw.println("<body>A GET request</body>");
  +        pw.println("</html>");
  +    }
  +
  +    /**
  +     * Verify that the <code>AsertUtils.getResponseAsString()</code> method
  +     * works with output text sent on multiple lines.
  +     *
  +     * @param theConnection the HTTP connection that was used to call the
  +     *                      server redirector. It contains the returned HTTP
  +     *                      response.
  +     */
  +    public void endGetResponseAsStringMultiLines(HttpURLConnection theConnection) 
throws IOException
  +    {
  +        StringWriter sw = new StringWriter();
  +        PrintWriter pw = new PrintWriter(sw);
  +        pw.println("<html><head/>");
  +        pw.println("<body>A GET request</body>");
  +        pw.println("</html>");
  +
  +        String result = AssertUtils.getResponseAsString(theConnection);
  +        assertEquals(sw.toString(), result);
  +
  +        pw.close();
  +    }
  +
  +    //-------------------------------------------------------------------------
  +
  +    /**
  +     * Verify that the <code>AsertUtils.getResponseAsStringArray()</code> method
  +     * works with output text sent on multiple lines.
  +     */
  +    public void testGetResponseAsStringArrayMultiLines() throws IOException
  +    {
  +        PrintWriter pw = response.getWriter();
  +        response.setContentType("text/html");
  +        pw.println("<html><head/>");
  +        pw.println("<body>A GET request</body>");
  +        pw.println("</html>");
  +    }
  +
  +    /**
  +     * Verify that the <code>AsertUtils.getResponseAsStringArray()</code> method
  +     * works with output text sent on multiple lines.
  +     *
  +     * @param theConnection the HTTP connection that was used to call the
  +     *                      server redirector. It contains the returned HTTP
  +     *                      response.
  +     */
  +    public void endGetResponseAsStringArrayMultiLines(HttpURLConnection 
theConnection) throws IOException
  +    {
  +        String[] results = AssertUtils.getResponseAsStringArray(theConnection);
  +
  +        assert("Should have returned 3 lines of text", results.length == 3);
  +        assertEquals("<html><head/>", results[0]);
  +        assertEquals("<body>A GET request</body>", results[1]);
  +        assertEquals("</html>", results[2]);
       }
   
   }
  
  
  

Reply via email to