package de.simonscholz.junit4converter.converters;

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

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

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 List<String> toBeReplacedMethodCalls;

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

	private void createListOfToBeReplacedMethods() {
		toBeReplacedMethodCalls = new ArrayList<>();
		toBeReplacedMethodCalls.add("getSession()");
		toBeReplacedMethodCalls.add("addImplementation(");
		toBeReplacedMethodCalls.add("createMockView(");
		toBeReplacedMethodCalls.add("addImplementation(");
		toBeReplacedMethodCalls.add("addDatatypeProvider(");
	}

	@Override
	public boolean visit(Block methodBody) {
		for (Statement currentStatement : getStatements(methodBody)) {
			String statement = currentStatement.toString();
			for (String toBeReplacedMethodCall : toBeReplacedMethodCalls) {
				if (statement.contains(toBeReplacedMethodCall)) {
					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,25 +159,23 @@
 va.util.
-ArrayList
+HashMap
 ;%0Aimport
@@ -190,16 +190,66 @@
 il.List;
+%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;
 %0A%0Aimport
@@ -590,19 +590,27 @@
 ate 
-List
+Map
 %3CString
+, Boolean
 %3E to
@@ -868,17 +868,15 @@
 new 
-ArrayList
+HashMap
 %3C%3E()
@@ -903,19 +903,19 @@
 odCalls.
-add
+put
 (%22getSes
@@ -921,16 +921,31 @@
 ssion()%22
+, Boolean.FALSE
 );%0A%09%09toB
@@ -957,35 +957,35 @@
 acedMethodCalls.
-add
+put
 (%22addImplementat
@@ -981,32 +981,46 @@
 Implementation(%22
+, Boolean.TRUE
 );%0A%09%09toBeReplace
@@ -1032,19 +1032,19 @@
 odCalls.
-add
+put
 (%22create
@@ -1057,61 +1057,22 @@
 ew(%22
-);%0A%09%09toBeReplacedMethodCalls.add(%22addImplementation(%22
+, Boolean.TRUE
 );%0A%09
@@ -1096,19 +1096,19 @@
 odCalls.
-add
+put
 (%22addDat
@@ -1122,16 +1122,30 @@
 ovider(%22
+, Boolean.TRUE
 );%0A%09%7D%0A%0A%09
@@ -1324,117 +1324,466 @@
 or (
-String toBeReplacedMethodCall : toBeReplacedMethodCalls) %7B%0A%09%09%09%09if (statement.contains(toBeReplacedMethodCall)
+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
 ) %7B%0A

test stricter replacing