/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2004, University of Maryland
 * 
 * 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 edu.umd.cs.findbugs;

import junit.framework.Assert;
import junit.framework.TestCase;

public class ClassScreenerTest extends TestCase {
	private ClassScreener emptyScreener;
	private ClassScreener particularClassScreener;
	private ClassScreener particularPackageScreener;

	private static String makeFileName(String className) {
		return className.replace('.', '/') + ".class";
	}

	private static String makeJarURL(String fileName) {
		return "jar:http://foo.com/bar.jar!/" + fileName;
	}

	private static final String FOOBAR_PACKAGE = "com.foobar";
	private static final String FURRYLEMUR_PACKAGE = "org.furrylemur";

	private static final String SOME_CLASS = FOOBAR_PACKAGE + ".SomeClass";
	private static final String SOME_OTHER_CLASS = FOOBAR_PACKAGE + ".SomeOtherClass";
	private static final String UNRELATED_THING_CLASS = FURRYLEMUR_PACKAGE + ".UnrelatedThing";

	private static final String SOME_CLASS_FILENAME = makeFileName(SOME_CLASS);
	private static final String SOME_OTHER_CLASS_FILENAME = makeFileName(SOME_OTHER_CLASS);
	private static final String UNRELATED_THING_CLASS_FILENAME = makeFileName(UNRELATED_THING_CLASS);

	private static final String SOME_CLASS_JARFILENAME = makeJarURL(SOME_CLASS_FILENAME);
	private static final String SOME_OTHER_CLASS_JARFILENAME = makeJarURL(SOME_OTHER_CLASS_FILENAME);
	private static final String UNRELATED_THING_CLASS_JARFILENAME = makeJarURL(UNRELATED_THING_CLASS_FILENAME);

	protected void setUp() {
		emptyScreener = new ClassScreener();

		particularClassScreener = new ClassScreener();
		particularClassScreener.addAllowedClass(SOME_CLASS);

		particularPackageScreener = new ClassScreener();
		particularPackageScreener.addAllowedPackage(FOOBAR_PACKAGE);
	}

	public void testEmptyClassScreener() {
		Assert.assertTrue(emptyScreener.matches(SOME_CLASS_FILENAME));
		Assert.assertTrue(emptyScreener.matches(SOME_OTHER_CLASS_FILENAME));
		Assert.assertTrue(emptyScreener.matches(UNRELATED_THING_CLASS_FILENAME));

		Assert.assertTrue(emptyScreener.matches(SOME_CLASS_JARFILENAME));
		Assert.assertTrue(emptyScreener.matches(SOME_OTHER_CLASS_JARFILENAME));
		Assert.assertTrue(emptyScreener.matches(UNRELATED_THING_CLASS_JARFILENAME));
	}

	public void testParticularClassScreener() {
		Assert.assertTrue(particularClassScreener.matches(SOME_CLASS_FILENAME));
		Assert.assertFalse(particularClassScreener.matches(SOME_OTHER_CLASS_FILENAME));
		Assert.assertFalse(particularClassScreener.matches(UNRELATED_THING_CLASS_FILENAME));

		Assert.assertTrue(particularClassScreener.matches(SOME_CLASS_JARFILENAME));
		Assert.assertFalse(particularClassScreener.matches(SOME_OTHER_CLASS_JARFILENAME));
		Assert.assertFalse(particularClassScreener.matches(UNRELATED_THING_CLASS_JARFILENAME));
	}

	public void testParticularPackageScreener() {
		Assert.assertTrue(particularPackageScreener.matches(SOME_CLASS_FILENAME));
		Assert.assertTrue(particularPackageScreener.matches(SOME_OTHER_CLASS_FILENAME));
		Assert.assertFalse(particularPackageScreener.matches(UNRELATED_THING_CLASS_FILENAME));

		Assert.assertTrue(particularPackageScreener.matches(SOME_CLASS_JARFILENAME));
		Assert.assertTrue(particularPackageScreener.matches(SOME_OTHER_CLASS_JARFILENAME));
		Assert.assertFalse(particularPackageScreener.matches(UNRELATED_THING_CLASS_JARFILENAME));
	}
}

// vim:ts=4

@@ -1110,16 +1110,67 @@
 creener;
+%0A%09private ClassScreener particularPackageScreener2;
 %0A%0A%09priva
@@ -1441,16 +1441,95 @@
 oobar%22;%0A
+%09private static final String FOOBAR_PACKAGE_WITH_TRAILING_DOT = %22com.foobar.%22;%0A
 %09private
@@ -1588,16 +1588,17 @@
 lemur%22;%0A
+%09
 %0A%09privat
@@ -2688,16 +2688,153 @@
 CKAGE);%0A
+%09%09%0A%09%09particularPackageScreener2 = new ClassScreener();%0A%09%09particularPackageScreener2.addAllowedPackage(FOOBAR_PACKAGE_WITH_TRAILING_DOT);%0A
 %09%7D%0A%0A%09pub
@@ -3907,44 +3907,192 @@
 %7B%0A%09%09
-Assert.assertTrue(particularPackageS
+testPackageScreener(particularPackageScreener);%0A%09%09testPackageScreener(particularPackageScreener2);%0A%09%7D%0A%0A%09private void testPackageScreener(ClassScreener screener) %7B%0A%09%09Assert.assertTrue(s
 cree
@@ -4146,34 +4146,17 @@
 ertTrue(
-particularPackageS
+s
 creener.
@@ -4213,34 +4213,17 @@
 rtFalse(
-particularPackageS
+s
 creener.
@@ -4256,33 +4256,32 @@
 ASS_FILENAME));%0A
-%0A
 %09%09Assert.assertT
@@ -4284,34 +4284,17 @@
 ertTrue(
-particularPackageS
+s
 creener.
@@ -4347,34 +4347,17 @@
 ertTrue(
-particularPackageS
+s
 creener.
@@ -4417,34 +4417,17 @@
 rtFalse(
-particularPackageS
+s
 creener.

Test packages which have a trailing dot.