/*
 * Copyright (C) 2004-2007  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.copernica;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class ResourceTest extends AbstractWebTest
{

	public void testError() throws Exception
	{
		final String prefix = "http://localhost:8080/copetest-hsqldb/init.jsp/";
		final URL stylesheet = new URL(prefix + "resource-test.txt");

		final long stylesheetLastModified = assertURL(stylesheet);
		assertEquals(stylesheetLastModified, assertURL(stylesheet));
		assertEquals(stylesheetLastModified, assertURL(stylesheet, stylesheetLastModified-1, false));
		assertEquals(stylesheetLastModified, assertURL(stylesheet, stylesheetLastModified, true));
		assertEquals(stylesheetLastModified, assertURL(stylesheet, stylesheetLastModified+5000, true));

	}
	
	private long assertURL(final URL url) throws IOException
	{
		return assertURL(url, -1, false);
	}
	
	private long assertURL(final URL url, final String contentType) throws IOException
	{
		return assertURL(url, contentType, -1, false);
	}
	
	private long assertURL(final URL url, final long ifModifiedSince, final boolean expectNotModified) throws IOException
	{
		return assertURL(url, "text/plain", ifModifiedSince, expectNotModified);
	}

	private long assertURL(final URL url, final String contentType, final long ifModifiedSince, final boolean expectNotModified) throws IOException
	{
		final Date before = new Date();
		final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setFollowRedirects(false);
		if(ifModifiedSince>=0)
			conn.setIfModifiedSince(ifModifiedSince);
		conn.connect();
		assertEquals(expectNotModified ? conn.HTTP_NOT_MODIFIED : conn.HTTP_OK, conn.getResponseCode());
		assertEquals(expectNotModified ? "Not Modified" : "OK", conn.getResponseMessage());
		final long date = conn.getDate();
		final Date after = new Date();
		//System.out.println("Date: "+new Date(date));
		assertWithinHttpDate(before, after, new Date(date));
		final long lastModified = conn.getLastModified();
		//System.out.println("LastModified: "+new Date(lastModified));
		assertTrue((date+1000)>=lastModified);
		assertEquals(expectNotModified ? null : contentType, conn.getContentType()); // TODO: content type should be set on 304
		//System.out.println("Expires: "+new Date(textConn.getExpiration()));
		assertWithin(new Date(date+(4*60*1000)), new Date(date+(6*60*1000)), new Date(conn.getExpiration()));
		assertEquals(expectNotModified ? -1 : 43, conn.getContentLength());
		
		final BufferedReader is = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		if(!expectNotModified)
		{
			assertEquals("This is the test file", is.readLine());
			assertEquals("for the ResourceTest", is.readLine());
			assertEquals(null, is.readLine());
		}
		is.close();
		
		return lastModified;
	}

}

@@ -1186,25 +1186,19 @@
 nal URL 
-styleshee
+tex
 t = new 
@@ -1246,25 +1246,19 @@
 al long 
-styleshee
+tex
 tLastMod
@@ -1275,25 +1275,19 @@
 sertURL(
-styleshee
+tex
 t);%0A%09%09as
@@ -1293,33 +1293,27 @@
 ssertEquals(
-styleshee
+tex
 tLastModifie
@@ -1325,25 +1325,19 @@
 sertURL(
-styleshee
+tex
 t));%0A%09%09a
@@ -1344,33 +1344,27 @@
 ssertEquals(
-styleshee
+tex
 tLastModifie
@@ -1376,37 +1376,25 @@
 sertURL(
-stylesheet, styleshee
+text, tex
 tLastMod
@@ -1422,33 +1422,27 @@
 ssertEquals(
-styleshee
+tex
 tLastModifie
@@ -1454,37 +1454,25 @@
 sertURL(
-stylesheet, styleshee
+text, tex
 tLastMod
@@ -1501,25 +1501,19 @@
 tEquals(
-styleshee
+tex
 tLastMod
@@ -1533,29 +1533,17 @@
 URL(
-stylesheet, styleshee
+text, tex
 tLas

rename local variable