View Javadoc
1   package com.ziesemer.utils.web;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.net.HttpURLConnection;
6   import java.net.URL;
7   import java.util.HashSet;
8   import java.util.Iterator;
9   import java.util.Set;
10  
11  import org.junit.After;
12  import org.junit.AfterClass;
13  
14  import com.ziesemer.utils.junit.BaseTest;
15  
16  /**
17   * @author Mark A. Ziesemer
18   * 	<a href="http://www.ziesemer.com.">&lt;www.ziesemer.com&gt;</a>
19   */
20  public class BaseWebTest extends BaseTest{
21  	
22  	protected static ZipServletTestSupport ZSTS;
23  	
24  	protected Set<HttpURLConnection> connections = new HashSet<HttpURLConnection>();
25  	
26  	@AfterClass
27  	public static void afterClass() throws Exception{
28  		if(ZSTS != null){
29  			ZSTS.close();
30  			ZSTS = null;
31  		}
32  	}
33  	
34  	/**
35  	 * Read everything remaining in streams before closing to eliminate server-side warnings.  Otherwise:
36  	 * <ul>
37  	 * <li>One issue is that Jetty, at least if using blocking connectors, will throw an
38  	 * 	{@link InterruptedException} (logged as a warning) while still trying to write data if the client
39  	 * 	closes its connection before reading everything.</li>
40  	 * <li>Another issue in {@link com.ziesemer.utils.web.zipServlet.ZipServletConfigTests} is that a
41  	 * 		non-thread-safe call to {@link javax.servlet.http.HttpServlet#destroy()} is called with the
42  	 * 		next call to {@link com.ziesemer.utils.web.zipServlet.ZipServletConfigTests#resetServletHandler},
43  	 * 		and then {@link com.ziesemer.utils.web.zipServlet.ZipServlet} attempts to continue to read
44  	 * 		from a closed {@link java.util.zip.ZipFile}.</li>
45  	 * </ul>
46  	 */
47  	@After
48  	public void emptyAndCloseOpenConnections() throws Exception{
49  		logger.debug("Finishing reading streams...");
50  		final byte[] buffer = new byte[1 << 12];
51  		long readTotal = 0;
52  		int connectionsTotal = 0;
53  		for(final Iterator<HttpURLConnection> iter = connections.iterator(); iter.hasNext();){
54  			final HttpURLConnection huc = iter.next();
55  			iter.remove();
56  			long readConnection = 0;
57  			try{
58  				// TODO: Should check the getErrorStream() here as well, but there should currently be no
59  				// 	need for it within the current tests, and it currently results in a hang when read on a
60  				// 	HTTP 404 response - at least with the current version of Jetty.
61  				if(huc.getResponseCode() == HttpURLConnection.HTTP_OK){
62  					final InputStream is = huc.getInputStream();
63  					try{
64  						int read;
65  						while((read = is.read(buffer)) >= 0){
66  							readConnection += read;
67  							readTotal += read;
68  						}
69  					}catch(final IOException ioe){
70  						if(readConnection == 0){
71  							// Java 5, 6, and 7 don't have a problem re-reading from a closed HttpURLConnection.getInputStream().
72  							// However, Java 8 throws a "stream is closed" IOException - with no other way currently available to
73  							//   determine if it is already closed or not.
74  						}else{
75  							throw ioe;
76  						}
77  					}finally{
78  						is.close();
79  					}
80  				}
81  			}finally{
82  				huc.disconnect();
83  			}
84  			connectionsTotal++;
85  		}
86  		logger.debug("Finished reading {} bytes from {} connections.", readTotal, connectionsTotal);
87  	}
88  	
89  	protected HttpURLConnection getTestConnection() throws Exception{
90  		return getTestConnection("/" + ZipServletTestSupport.TEST_SINGLE_PATH);
91  	}
92  	
93  	protected HttpURLConnection getTestConnection(final String href) throws Exception{
94  		final HttpURLConnection huc = (HttpURLConnection)new URL(
95  			ZSTS.getBaseUrl() + ZipServletTestSupport.PREFIX + href).openConnection();
96  		connections.add(huc);
97  		return huc;
98  	}
99  	
100 }