Re: Requirement of a new ServletRunner constructor in HttpUnit 1.7

Xiaofeng Guo <[email protected]> Wed, 12 Aug 2009 15:34:56 +0800
Newsgroups gmane.comp.web.httpunit.devel
Message-ID <[email protected]>
Hi Wolfgang,

I attached my patch, it includes:
- META-INF support for servlettest.
- Unit test error on cookie expiration time.
- Unit test error of head time conversion.
- Add/Modify some ServletRunner constructors.

Would you help me to take a look? If OK, would you help me check in
the code? Thanks!

Best Regards,
Xiaofeng



On Sat, Aug 8, 2009 at 5:23 PM, Wolfgang Fahl<[email protected]> wrote:
> Xiaofeng,
> thank you for your mail,
> you wrote:
>> I am trying to use the servlet unittest framework of httpunit. My case
>> is a bit special, I need to read file in the _contextDir, which
>> construct in com.meterware.servletunit.WebApplication. However,
>> ServletRunner doesn't provide a way for me to set this variable. I am
>> wondering whether it is suitable to add constructors as below in
>> ServletRunner?
>>
>>     public ServletRunner(InputStream webXml, File contextDir, String
>> contextPath) throws SAXException, IOException {
>>       InputSource inputSource=new InputSource( webXml );
>>       Document doc=HttpUnitUtils.parse(inputSource);
>>       _application = new WebApplication( doc, contextDir, contextPath );
>>       completeInitialization( contextPath );
>>     }
>>
>> I both attached a draft change of this file. I am wondering about the
>> check-in criteria of HttpUnit, including questions as below:
>> - How many unit tests I need to pass before checking the code?
> All the ones that passed before your modifications/additions + at least one
> test that uses/checks the modified/added code.
>> - How about the coding style of the code?
> Please stick to what you find in the existing code. If we only talk about a
> few lines I'll do the style-fixing before checking in.
>> I'll follow that and check in the code. Is it OK to you? Thanks for
>> your comments!
> Yes - adding a constructor won't harm too much.
> Beware of the other changes you are suggesting (cleaning up the code). It
> takes a lot of time to check such changes which might hold up the check in
> for many weeks or months. Currently there is a backlog of changes of a few
> month sincce  I don't get to checking in things very often. The best chances
> are for small clear patches which are accompanied by a simple JUnit test.
> Thanks for your cooperation! I'm looking forward to your patch.
> Wolfgang
> BITPlan - smart solutions
> Pater-Delp-Str. 1, D-47877 Willich Schiefbahn
> Tel. +49 2154 811-480, Fax +49 2154 811-481
> Web: http://www.bitplan.de
> bitplan GmbH, Willich - HRB 6820 Krefeld, VAT-ID: 10258040548,
> Geschäftsführer: Wolfgang Fahl
>
> The following section of this message contains a file attachment
> prepared for transmission using the Internet MIME message format.
> If you are using Pegasus Mail, or any other MIME-compliant system,
> you should be able to save it or view it from within your mailer.
> If you cannot, please ask your system administrator for assistance.
>
>   ---- File information -----------
>     File:  ServletRunner.java
>     Date:  8 Aug 2009, 11:06
>     Size:  11111 bytes.
>     Type:  Unknown
>
>

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july

_______________________________________________
Httpunit-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/httpunit-develop
httpunit.patch (application/octet-stream, 5.7 KB)
Index: trunk/httpunit/build.xml
===================================================================
--- trunk/httpunit/build.xml	(revision 1025)
+++ trunk/httpunit/build.xml	(working copy)
@@ -223,6 +223,7 @@
          <classpath>
              <path refid="base.classpath" />
              <pathelement location="${build.classes}" />
+             <pathelement location="META-INF" />
              <pathelement location="${test.classes}" />
          </classpath>
     </java>
Index: trunk/httpunit/src/com/meterware/httpunit/cookies/Cookie.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/cookies/Cookie.java	(revision 1025)
+++ trunk/httpunit/src/com/meterware/httpunit/cookies/Cookie.java	(working copy)
@@ -43,7 +43,7 @@
 
     private String _domain;
     
-    private long _expiredTime;
+    private long _expiredTime = Long.MAX_VALUE;
 
 
     /**
@@ -102,15 +102,21 @@
               _path = attributeValue;
             } else if (key.equalsIgnoreCase( "domain" )) {
               _domain = attributeValue;
+            } else if (key.equalsIgnoreCase("expires")) {
+              setExpiredTime(getAgeInMsecFromDate( attributeValue ));
             } else if (key.equalsIgnoreCase( "max-age" )) {
-              _expiredTime = System.currentTimeMillis() + getAgeInMsec( attributeValue );
-            } else if (key.equalsIgnoreCase( "expires" )) {
-            	_expiredTime = getAgeInMsecFromDate( attributeValue );
+              setExpiredTime(System.currentTimeMillis() + getAgeInMsec(attributeValue));
             }            
         }
     }
 
 
+    private void setExpiredTime(long expiredTime) {
+      if (_expiredTime > expiredTime) {
+        _expiredTime = expiredTime;
+      }
+    }
+
     /**
      * get the age of the cookie in Milliseconds from a string representaiton in seconds
      * @param maxAgeValue - the string with the age in seconds
Index: trunk/httpunit/src/com/meterware/servletunit/ServletRunner.java
===================================================================
--- trunk/httpunit/src/com/meterware/servletunit/ServletRunner.java	(revision 1025)
+++ trunk/httpunit/src/com/meterware/servletunit/ServletRunner.java	(working copy)
@@ -20,6 +20,7 @@
 *
 *******************************************************************************************************************/
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
@@ -103,8 +104,7 @@
      * @since 1.6
      */
     public ServletRunner( File webXml, String contextPath ) throws IOException, SAXException {
-        _application = new WebApplication( HttpUnitUtils.newParser().parse( webXml ), webXml.getParentFile().getParentFile(), contextPath );
-        completeInitialization( contextPath );
+      this(new FileInputStream(webXml), webXml.getParentFile().getParentFile(), contextPath);
     }
     
     /**
@@ -119,10 +119,9 @@
     public ServletRunner( String webXMLFileSpec,EntityResolver resolver )	throws IOException, SAXException	{
   		DocumentBuilder parser = HttpUnitUtils.newParser();
   		parser.setEntityResolver(resolver);
-  		_application = new WebApplication( parser.parse(
-  		webXMLFileSpec ) );
+  		_application = new WebApplication( parser.parse(webXMLFileSpec ) );
   		completeInitialization( null );
- 		}
+    }
 
     /**
      * Constructor which expects an input stream containing the web.xml for the application.
@@ -138,16 +137,16 @@
      * @throws IOException
      * @throws SAXException
      */
-    public ServletRunner( InputStream webXML, String contextPath ) throws IOException, SAXException {
- 			InputSource inputSource=new InputSource( webXML );
- 			Document doc=HttpUnitUtils.parse(inputSource);
- 			try {
- 	 			_application = new WebApplication( doc, contextPath );
- 	 			completeInitialization( contextPath );
- 			} catch (java.net.MalformedURLException mue) {
- 				throw mue;
- 			}
+    public ServletRunner( InputStream webXml, String contextPath ) throws IOException, SAXException {
+      this(webXml, null, contextPath);
     }
+    
+    public ServletRunner(InputStream webXml, File contextDir, String contextPath) throws SAXException, IOException {
+      InputSource inputSource = new InputSource(webXml);
+      Document doc = HttpUnitUtils.parse(inputSource);
+      _application = new WebApplication(doc, contextDir, contextPath);
+      completeInitialization(contextPath);
+    }
 
 
     /**
Index: trunk/httpunit/test/com/meterware/servletunit/HttpServletResponseTest.java
===================================================================
--- trunk/httpunit/test/com/meterware/servletunit/HttpServletResponseTest.java	(revision 1025)
+++ trunk/httpunit/test/com/meterware/servletunit/HttpServletResponseTest.java	(working copy)
@@ -217,7 +217,7 @@
         Date d = df.parse( "12/9/1969 GMT" );
         servletResponse.setDateHeader( "date", d.getTime() );
         headerValue = servletResponse.getHeaderField( "date" );
-        assertEquals( "date header is wrong", "Tue, 09 Dec 1969 12:00:00 GMT", headerValue );
+        assertEquals( "date header is wrong", "Tue, 09 Dec 1969 00:00:00 GMT", headerValue );
     }
 
 
@@ -235,7 +235,7 @@
         String[] headerList = servletResponse.getHeaderFields( "list" );
         assertEquals( "header is wrong", "foo", headerList[ 0 ] );
         assertEquals( "header is wrong", "3", headerList[ 1 ] );
-        assertEquals( "header is wrong", "Tue, 09 Dec 1969 12:00:00 GMT", headerList[ 2 ] );
+        assertEquals( "header is wrong", "Tue, 09 Dec 1969 00:00:00 GMT", headerList[ 2 ] );
 
         servletResponse.setHeader( "list", "monkeyboy" );
         headerList = servletResponse.getHeaderFields( "list" );