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
18
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
36
37
38
39
40
41
42
43
44
45
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
59
60
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
72
73
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 }