package kuleuven.group2.rewrite;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import kuleuven.group2.compile.NameUtils;
import kuleuven.group2.rewrite.OssRewriterLoader;

import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Predicate;

import be.kuleuven.cs.ossrewriter.Monitor;

public class OssRewriterLoaderTest {

	protected A a;
	protected static OssRewriterLoader ossRewriterLoader = OssRewriterLoader.getInstance();

	@Before
	public void setUp() throws Exception {
		a = new A();
	}

	public class A {

		public A() {

		}

		private boolean visited = false;

		public void visit() {
			visited = true;
		}

		public boolean getVisited() {
			return visited;
		}
	}

	public class VisitedMethodsTracker {

		private List<String> visitedMethodNames = new ArrayList<String>();

		public VisitedMethodsTracker() {

		}

		public boolean methodIsVisited(String method) {
			return visitedMethodNames.contains(method);
		}

		public void addVisitedMethod(String visitedMethod) {
			visitedMethodNames.add(visitedMethod);
		}

	}

	@Test
	public void methodIsVisitedTest() {
		final VisitedMethodsTracker visitedMethodsTracker = new VisitedMethodsTracker();
		Monitor monitor = new Monitor() {

			@Override
			public void enterMethod(String arg0) {
				visitedMethodsTracker.addVisitedMethod(arg0);
			}
		};
		ossRewriterLoader.setClassTransformFilter(new Predicate<String>() {
			@Override
			public boolean apply(String input) {
				return input.contains("kuleuven/group2/rewrite/OssRewriterLoaderTest$A");
			}
		});
		ossRewriterLoader.registerMonitor(monitor);
		ossRewriterLoader.enable(false);

		a.visit();

		ossRewriterLoader.disable();
		ossRewriterLoader.unregisterMonitor(monitor);

		assertTrue(visitedMethodsTracker.methodIsVisited(NameUtils.toInternalName(A.class.getName()) + ".visit()V"));
		assertEquals(1, visitedMethodsTracker.visitedMethodNames.size());
	}

}

@@ -1967,71 +1967,327 @@
 %0D%0A%09%09
-assertEquals(1, visitedMethodsTracker.visitedMethodNames.size()
+int actualMethodCount = 0;%0D%0A%09%09for (String methodName : visitedMethodsTracker.visitedMethodNames) %7B%0D%0A%09%09%09if (! methodName.contains(%22kuleuven/group2/rewrite/OssRewriterLoaderTest$A%22)) %7B%0D%0A%09%09%09%09fail();%0D%0A%09%09%09%7D%0D%0A%09%09%09if (methodName.contains(%22.visit()V%22)) %7B%0D%0A%09%09%09%09actualMethodCount++;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09assertEquals(1, actualMethodCount
 );%0D%0A

test fix