package de.simonscholz.junit4converter.converters;

import static de.simonscholz.junit4converter.converters.JTestCaseConverter.DBRULE_VARIABLENAME;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;

public class RedirectCallsOfSuperClassToRuleMethodBodyVisitor extends
		ASTVisitor {
	private ASTRewrite rewriter;
	private Map<String, Boolean> toBeReplacedMethodCalls;

	public RedirectCallsOfSuperClassToRuleMethodBodyVisitor(ASTRewrite rewriter) {
		this.rewriter = rewriter;
		createListOfToBeReplacedMethods();
	}

	private void createListOfToBeReplacedMethods() {
		toBeReplacedMethodCalls = new HashMap<>();
		toBeReplacedMethodCalls.put("getSession()", Boolean.FALSE);
		toBeReplacedMethodCalls.put("addImplementation(", Boolean.TRUE);
		toBeReplacedMethodCalls.put("createMockView(", Boolean.TRUE);
		toBeReplacedMethodCalls.put("addDatatypeProvider(", Boolean.TRUE);
	}

	@Override
	public boolean visit(Block methodBody) {
		for (Statement currentStatement : getStatements(methodBody)) {
			String statement = currentStatement.toString();
			for (Entry<String, Boolean> entry : toBeReplacedMethodCalls
					.entrySet()) {
				Boolean replaceOnlyStartsWith = entry.getValue();
				String toBeReplacedMethodCall = entry.getKey();

				boolean shouldReplace = false;
				if (replaceOnlyStartsWith) {
					if (statement.startsWith(toBeReplacedMethodCall)) {
						shouldReplace = true;
					}
				} else if (statement.contains(toBeReplacedMethodCall)) {
					shouldReplace = true;
				}

				if (shouldReplace) {
					String newStatement = createReplacingStatement(statement,
							toBeReplacedMethodCall);
					ASTNode delegateToRule = rewriter.createStringPlaceholder(
							newStatement, ASTNode.EMPTY_STATEMENT);
					rewriter.replace(currentStatement, delegateToRule, null);
				}
			}
		}
		return true;
	}

	private String createReplacingStatement(String currentStatement,
			String toReplacingMethodCall) {
		String newMethodCall = DBRULE_VARIABLENAME + '.'
				+ toReplacingMethodCall;
		String newStatement = currentStatement.replace(toReplacingMethodCall,
				newMethodCall);
		return newStatement;
	}

	@SuppressWarnings("unchecked")
	private List<Statement> getStatements(Block methodBody) {
		return methodBody.statements();
	}

}

@@ -159,23 +159,25 @@
 va.util.
-HashMap
+ArrayList
 ;%0Aimport
@@ -196,58 +196,8 @@
 ist;
-%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;
 %0A%0Aim
@@ -542,27 +542,19 @@
 ate 
-Map
+List
 %3CString
-, Boolean
 %3E to
@@ -812,15 +812,17 @@
 new 
-HashMap
+ArrayList
 %3C%3E()
@@ -849,19 +849,19 @@
 odCalls.
-put
+add
 (%22getSes
@@ -871,23 +871,8 @@
 n()%22
-, Boolean.FALSE
 );%0A%09
@@ -892,27 +892,27 @@
 MethodCalls.
-put
+add
 (%22addImpleme
@@ -920,30 +920,16 @@
 tation(%22
-, Boolean.TRUE
 );%0A%09%09toB
@@ -949,19 +949,19 @@
 odCalls.
-put
+add
 (%22create
@@ -970,30 +970,69 @@
 ckView(%22
-, Boolean.TRUE
+);%0A%09%09toBeReplacedMethodCalls.add(%22addImplementation(%22
 );%0A%09%09toB
@@ -1056,11 +1056,11 @@
 lls.
-put
+add
 (%22ad
@@ -1082,22 +1082,8 @@
 er(%22
-, Boolean.TRUE
 );%0A%09
@@ -1266,466 +1266,117 @@
 or (
-Entry%3CString, Boolean%3E entry : toBeReplacedMethodCalls%0A%09%09%09%09%09.entrySet()) %7B%0A%09%09%09%09Boolean replaceOnlyStartsWith = entry.getValue();%0A%09%09%09%09String toBeReplacedMethodCall = entry.getKey();%0A%0A%09%09%09%09boolean shouldReplace = false;%0A%09%09%09%09if (replaceOnlyStartsWith) %7B%0A%09%09%09%09%09if (statement.startsWith(toBeReplacedMethodCall)) %7B%0A%09%09%09%09%09%09shouldReplace = true;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D else if (statement.contains(toBeReplacedMethodCall)) %7B%0A%09%09%09%09%09shouldReplace = true;%0A%09%09%09%09%7D%0A%0A%09%09%09%09if (shouldReplace
+String toBeReplacedMethodCall : toBeReplacedMethodCalls) %7B%0A%09%09%09%09if (statement.contains(toBeReplacedMethodCall)
 ) %7B%0A

Revert "test stricter replacing"