package com.crawljax.core.plugin;

import java.io.File;
import java.util.List;

import junit.framework.Assert;

import org.apache.commons.configuration.ConfigurationException;
import org.junit.BeforeClass;
import org.junit.Test;

import com.crawljax.core.CandidateElement;
import com.crawljax.core.CrawlSession;
import com.crawljax.core.CrawljaxController;
import com.crawljax.core.CrawljaxException;
import com.crawljax.core.configuration.CrawlSpecification;
import com.crawljax.core.configuration.CrawljaxConfiguration;
import com.crawljax.core.state.Eventable;

/**
 * Test case for bug #8.
 * 
 * @author Stefan Lenselink <S.R.Lenselink@student.tudelft.nl>
 * @version $Id$
 */
public class OnFireEventFailedPluginTest {
	private static CrawljaxController controller;

	private static int hit;

	@BeforeClass
	public static void setup() throws ConfigurationException {

		CrawlSpecification spec =
		        new CrawlSpecification("file://"
		                + new File("src/test/site/crawler/index.html").getAbsolutePath());
		spec.clickDefaultElements();

		CrawljaxConfiguration config = new CrawljaxConfiguration();
		config.setCrawlSpecification(spec);
		config.addPlugin(new PreStateCrawlingPlugin() {

			@Override
			public void preStateCrawling(CrawlSession session,
			        List<CandidateElement> candidateElement) {
				for (CandidateElement candidate : candidateElement) {
					candidate.getIdentification().setValue("/HTML[1]/BODY[1]/FAILED[1]/A[1]");
				}
			}
		});
		config.addPlugin(new OnFireEventFailedPlugin() {
			@Override
			public void onFireEventFailed(Eventable eventable, List<Eventable> pathToFailure) {
				hit++;
			}
		});

		controller = new CrawljaxController(config);
	}

	@Test
	public void testFireEventFaildHasBeenExecuted() throws ConfigurationException,
	        CrawljaxException {
		controller.run();
		Assert.assertEquals(
		        "The FireEventFaild Plugin has been executed the correct amount of times", hit,
		        controller.getElementChecker().numberOfExaminedElements());
	}
}

@@ -165,24 +165,53 @@
 nException;%0A
+import org.junit.AfterClass;%0A
 import org.j
@@ -224,24 +224,24 @@
 eforeClass;%0A
-
 import org.j
@@ -794,17 +794,62 @@
 roller;%0A
+%09private static CrawljaxConfiguration config;
 %0A
-
 %09private
@@ -1131,38 +1131,16 @@
 s();%0A%0A%09%09
-CrawljaxConfiguration 
 config =
@@ -2078,13 +2078,104 @@
 nts());%0A
-
 %09%7D%0A
+%0A%09@AfterClass%0A%09public static void cleanUp() %7B%0A%09%09CrawljaxPluginsUtil.loadPlugins(null);%0A%09%7D%0A%0A
 %7D%0A

remove the plugins added.