/*
 * test-driven-detectors4findbugs. Copyright (c) 2011 youDevise, Ltd.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
*/

package com.youdevise.fbplugins.tdd4fb;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Collection;

import org.hamcrest.Matcher;

import com.youdevise.fbplugins.tdd4fb.TestingBugReporter.TddBugReporter;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;

class BugsReportedAsserter {

	public void assertNoBugsReported(BugReporter bugReporter) {
		Collection<BugInstance> bugsReported = bugsFrom(bugReporter);
		assertThat(bugsReported.isEmpty(), is(true));
	}

	public void assertBugReported(BugReporter bugReporter) {
		Collection<BugInstance> bugsReported = bugsFrom(bugReporter);
		assertThat(bugsReported.isEmpty(), is(false));
	}

	public void assertBugReported(BugReporter bugReporter, Matcher<BugInstance> matches) {
		Collection<BugInstance> bugsReported = bugsFrom(bugReporter);
		assertThat(bugsReported, hasItem(matches));
	}
	
	public void assertAllBugsReported(BugReporter bugReporter, Matcher<BugInstance>... matches) {
		Collection<BugInstance> bugsReported = bugsFrom(bugReporter);
		assertThat("Expected a certain number of bugs to be reported.", bugsReported.size(), is(matches.length));
		assertThat(bugsReported, hasItems(matches));
	}
	
	private Collection<BugInstance> bugsFrom(BugReporter bugReporter) {
		if(bugReporter instanceof TddBugReporter) {
			return ((TddBugReporter)bugReporter).getReportedBugs();
		} else {
			throw new AssertionError(
					"An invalid BugReporter has been used. " +
					"Please use the BugReporter given by DetectorAssert.bugReporterForTesting(), passing " +
					"the same BugReporter to your custom detector, and any DetectorAssert assertion methods.");
		}
	}
}

@@ -1339,91 +1339,100 @@
 is;%0A
+%0A
 import 
-static org.hamcrest.MatcherAssert.assertThat;%0A%0Aimport java.util.Collection;%0A
+java.util.Collection;%0A%0Aimport org.hamcrest.Description;%0Aimport org.hamcrest.Matcher;
 %0Aimp
@@ -1448,23 +1448,33 @@
 amcrest.
-Matcher
+StringDescription
 ;%0A%0Aimpor
@@ -2729,22 +2729,31 @@
 new 
-AssertionError
+DetectorAssertException
 (%0A%09%09
@@ -2993,13 +2993,792 @@
 %22);%0A%09%09%7D%0A
-
 %09%7D%0A
+%09%0A%09/*%0A%09 * Replace with MatcherAssert.assertThat() if/when only supporting Hamcrest 1.2+%0A%09 */%0A    private static %3CT%3E void assertThat(String reason, T actual, Matcher%3C? super T%3E matcher) %7B%0A        if (!matcher.matches(actual)) %7B%0A            Description description = new StringDescription();%0A            description.appendText(reason)%0A                       .appendText(%22%5CnExpected: %22)%0A                       .appendDescriptionOf(matcher)%0A                       .appendText(%22%5Cn     but: %22);%0A            matcher.describeMismatch(actual, description);%0A            %0A            throw new DetectorAssertException(description.toString());%0A        %7D%0A    %7D%0A    %0A    private static %3CT%3E void assertThat(T actual, Matcher%3C? super T%3E matcher) %7B%0A        assertThat(%22%22, actual, matcher);%0A    %7D%0A
 %7D%0A

Use a private version of assertThat which will not require Hamcrest 1.2