package org.sahagin.runlib.external.adapter.junit4;

import javassist.CtMethod;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.junit.Test;
import org.sahagin.runlib.additionaltestdoc.AdditionalTestDocs;
import org.sahagin.runlib.external.adapter.Adapter;
import org.sahagin.runlib.external.adapter.AdapterContainer;
import org.sahagin.runlib.external.adapter.AdditionalTestDocsAdapter;
import org.sahagin.runlib.external.adapter.RootFunctionAdapter;
import org.sahagin.runlib.srctreegen.ASTUtils;

public class JUnit4Adapter implements Adapter {

    @Override
    public void initialSetAdapter() {
        AdapterContainer container = AdapterContainer.globalInstance();
        container.setRootFunctionAdapter(new RootFunctionAdapterImpl());
        container.addAdditionalTestDocsAdapter(new AdditionalTestDocsAdapterImpl());
    }

    private static class RootFunctionAdapterImpl implements RootFunctionAdapter {

        @Override
        public boolean isRootFunction(IMethodBinding methodBinding) {
            return ASTUtils.getAnnotationBinding(methodBinding.getAnnotations(), Test.class) != null;
        }

        @Override
        public boolean isRootFunction(CtMethod method) {
            try {
                return method.getAnnotation(Test.class) != null;
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

    }

    private static class AdditionalTestDocsAdapterImpl implements AdditionalTestDocsAdapter {

        @Override
        public void classAdd(AdditionalTestDocs docs) {
            docs.classAdd("org.junit.Assert", "値チェック");
        }

        @Override
        public void funcAdd(AdditionalTestDocs docs) {
            // TODO cannot handle methods defined on subclass
            // TODO multiple language support

            // alphabetical order
            docs.methodAdd("org.hamcrest.core.Is", "is", "「{0}」に等しい");
            docs.methodAdd("org.junit.Assert", "assertEquals", "「{0}」が「{1}」に等しいことをチェック");
            docs.methodAdd("org.junit.Assert", "assertThat", "「{0}」が{1}ことをチェック");
        }

    }

}

@@ -1931,24 +1931,100 @@
 %7B0%7D%E3%80%8D%E3%81%AB%E7%AD%89%E3%81%97%E3%81%84%22);%0A
+            docs.methodAdd(%22org.hamcrest.CoreMatchers%22, %22is%22, %22%E3%80%8C%7B0%7D%E3%80%8D%E3%81%AB%E7%AD%89%E3%81%97%E3%81%84%22);%0A
             

Add testDoc for org.hamcrest.CoreMatchers.is