/*
 * Copyright (C) 2004-2012  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.cope.servletutil;

import static com.exedio.cope.servletutil.ServletSource.create;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import com.exedio.cope.junit.CopeAssert;
import com.exedio.cope.util.Properties.Source;

public class ServletSourceFileTest extends CopeAssert
{
	private File file;

	@Override
	protected void tearDown() throws Exception
	{
		if(file!=null)
		{
			file.delete();
			file = null;
		}

		super.tearDown();
	}

	public void testNormal()
	{
		final Source s = create(new TestContext("/testContextPath", "testContextPath.", file()));
		assertKey(s);
		assertEquals("v1", s.get("p1"));
		assertEquals("v2", s.get("p2"));
		assertEquals(null, s.get("p3"));
		assertEquals(null, s.get("top"));
		assertEquals("/testContextPath", s.get("contextPath"));
		assertContainsUnmodifiable("p1", "p2", "contextPath", s.keySet());
		assertEquals(file.getAbsolutePath(), s.getDescription());
		assertEquals(file.getAbsolutePath(), s.toString());
	}

	public void testRoot()
	{
		final Source s = create(new TestContext("", "root.", file()));
		assertKey(s);
		assertEquals("v1", s.get("p1"));
		assertEquals("v2", s.get("p2"));
		assertEquals(null, s.get("p3"));
		assertEquals(null, s.get("top"));
		assertEquals("", s.get("contextPath"));
		assertContainsUnmodifiable("p1", "p2", "contextPath", s.keySet());
		assertEquals(file.getAbsolutePath(), s.getDescription());
		assertEquals(file.getAbsolutePath(), s.toString());
	}

	public void testWithoutSlash()
	{
		final Source s = create(new TestContext("ding", "ding.", file()));
		assertKey(s);
		assertEquals("v1", s.get("p1"));
		assertEquals("v2", s.get("p2"));
		assertEquals(null, s.get("p3"));
		assertEquals(null, s.get("top"));
		assertEquals("ding", s.get("contextPath"));
		assertContainsUnmodifiable("p1", "p2", "contextPath", s.keySet());
		assertEquals(file.getAbsolutePath(), s.getDescription());
		assertEquals(file.getAbsolutePath(), s.toString());
	}

	public void testNull()
	{
		final Source s = create(new TestContext(null, "", file()));
		assertKey(s);
		assertEquals("v1", s.get("p1"));
		assertEquals("v2", s.get("p2"));
		assertEquals(null, s.get("p3"));
		assertEquals(null, s.get("top"));
		assertEquals(null, s.get("contextPath"));
		assertContainsUnmodifiable("p1", "p2", "contextPath", s.keySet());
		assertEquals(file.getAbsolutePath(), s.getDescription());
		assertEquals(file.getAbsolutePath(), s.toString());
	}

	private File file()
	{
		assertNull(file);
		final Properties props = new Properties();
		props.setProperty("p1", "v1");
		props.setProperty("p2", "v2");
		try
		{
			file = File.createTempFile(ServletSourceFileTest.class.getName(), ".properties");
			final FileOutputStream out = new FileOutputStream(file);
			try
			{
				props.store(out, null);
			}
			finally
			{
				out.close();
			}
			return file;
		}
		catch(final IOException e)
		{
			throw new RuntimeException(e);
		}
	}

	private static class TestContext extends AssertionFailedServletContext
	{
		private final String contextPath;
		private final String prefix;
		private final File file;

		TestContext(final String contextPath, final String prefix, final File file)
		{
			this.contextPath = contextPath;
			this.prefix = prefix;
			this.file = file;
		}

		@Override
		public String getInitParameter(final String name)
		{
			if((prefix + "com.exedio.cope.servletutil.ServletSource.propertiesFile").equals(name))
				return file.getAbsolutePath();
			else
				throw new IllegalArgumentException(name);
		}

		@Override
		public Enumeration<String> getInitParameterNames()
		{
			return new Vector<String>(Arrays.asList(prefix+"p1", prefix+"p2", "top")).elements();
		}

		@Override
		public String getContextPath()
		{
			return contextPath;
		}
	}
}

@@ -1181,16 +1181,76 @@
 ource;%0A%0A
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;%0A%0A
 public c
@@ -1329,16 +1329,77 @@
 verride%0A
+%09@SuppressFBWarnings(%22RV_RETURN_VALUE_IGNORED_BAD_PRACTICE%22)%0A
 %09protect

fix findbugs after r24616: ServletSource supports com.exedio.cope.servletutil.ServletSource.propertiesFile