View Javadoc
1   package com.ziesemer.utils.web;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.fail;
5   
6   import java.io.DataInputStream;
7   import java.io.File;
8   import java.io.InputStream;
9   import java.util.zip.ZipEntry;
10  import java.util.zip.ZipFile;
11  
12  import org.eclipse.jetty.servlet.ServletContextHandler;
13  import org.slf4j.LoggerFactory;
14  
15  /**
16   * <p>Requires that &quot;<code>yui_2.9.0.zip</code>&quot; is downloaded from <a href=
17   * 	"http://yui.github.io/yui2/">http://yui.github.io/yui2/</a>
18   * 	and placed underneath <code>TestWebContent/WEB-INF/</code>.</p>
19   * @author Mark A. Ziesemer
20   * <a href="http://www.ziesemer.com.">&lt;www.ziesemer.com&gt;</a>
21   */
22  public class ZipServletTestSupport extends ServletTester{
23  	public static final String WEB_CONTENT = "TestWebContent";
24  	public static final String PREFIX = "yui_2.9.0";
25  	
26  	public static final String TEST_SINGLE_PATH =
27  		"build/yahoo/yahoo.js";
28  	public static final String TEST_COMBO_PATH =
29  		"build/yahoo/yahoo-min.js&build/dom/dom-min.js&build/event/event-min.js";
30  	
31  	protected static final File ZIP_FILE_FILE;
32  	
33  	static{
34  		ZIP_FILE_FILE = new File(WEB_CONTENT + "/WEB-INF/" + PREFIX + ".zip");
35  		if(!ZIP_FILE_FILE.exists()){
36  			final String err = ZIP_FILE_FILE
37  				+ " must be provided to run unit tests.  Please download from http://yui.github.io/yui2/ .";
38  			LoggerFactory.getLogger(ZipServletTestSupport.class).error(err);
39  			throw new ExceptionInInitializerError(err);
40  		}
41  	}
42  	
43  	protected final ZipFile zipFile;
44  	
45  	public ZipServletTestSupport() throws Exception{
46  		zipFile = new ZipFile(ZIP_FILE_FILE);
47  	}
48  	
49  	@Override
50  	protected ServletContextHandler initHandler(){
51  		final ServletContextHandler c = initHandlerClass();
52  		c.setResourceBase(WEB_CONTENT);
53  		return c;
54  	}
55  	
56  	protected ServletContextHandler initHandlerClass(){
57  		return new ServletContextHandler();
58  	}
59  	
60  	public ZipFile getZipFile(){
61  		return zipFile;
62  	}
63  	
64  	public File getZipFileFile(){
65  		return ZIP_FILE_FILE;
66  	}
67  	
68  	/**
69  	 * Does not close or check for EOF on the InputStream.
70  	 * Can be called repeatedly for testing combo mode.
71  	 * Finish by calling {@link #testEndOfStream(InputStream)}.
72  	 */
73  	public void testStreamsEqual(final ZipEntry ze, final InputStream is) throws Exception{
74  		long readSum = 0;
75  		final InputStream zeIs = getZipFile().getInputStream(ze);
76  		try{
77  			final DataInputStream srvIs = new DataInputStream(is);
78  			final byte[] zeBuffer = new byte[1 << 10];
79  			final byte[] srvBuffer = new byte[zeBuffer.length];
80  			int read;
81  			while((read = zeIs.read(zeBuffer)) >= 0){
82  				srvIs.readFully(srvBuffer, 0, read);
83  				for(int i=0; i < read; i++){
84  					if(zeBuffer[i] != srvBuffer[i]){
85  						fail("Unmatched bytes at position: " + (readSum + i)
86  							+ ".  expected:<" + zeBuffer[i] + "> but was:<" + srvBuffer[i] + ">");
87  					}
88  				}
89  				readSum += read;
90  			}
91  		}finally{
92  			zeIs.close();
93  		}
94  		
95  		assertEquals("Didn't read all bytes.",
96  			ze.getSize(), readSum);
97  	}
98  	
99  	public void testEndOfStream(final InputStream is) throws Exception{
100 		assertEquals(-1, is.read());
101 	}
102 
103 	@Override
104 	public void close() throws Exception{
105 		zipFile.close();
106 		super.close();
107 	}
108 }