package de.is24.deadcode4j.analyzer;

import de.is24.deadcode4j.Analyzer;
import de.is24.deadcode4j.CodeContext;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;

import static com.google.common.collect.Iterables.concat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public final class A_HibernateAnnotationsAnalyzer extends AnAnalyzer {

    private Analyzer objectUnderTest;

    @Before
    public void setUp() throws Exception {
        objectUnderTest = new HibernateAnnotationsAnalyzer();
    }

    @Test
    public void shouldRecognizeDependencyFromClassWithTypeAnnotatedFieldToTypeDefAnnotatedClass() {
        CodeContext codeContext = new CodeContext();
        objectUnderTest.doAnalysis(codeContext, getFile("de/is24/deadcode4j/analyzer/hibernateannotations/ClassWithTypeDef.class"));
        objectUnderTest.doAnalysis(codeContext, getFile("de/is24/deadcode4j/analyzer/hibernateannotations/ClassUsingTypeAtField.class"));
        objectUnderTest.finishAnalysis(codeContext);


        Map<String, ? extends Iterable<String>> codeDependencies = codeContext.getAnalyzedCode().getCodeDependencies();
        assertThat("Should have analyzed the class files!", codeContext.getAnalyzedCode().getAnalyzedClasses(), hasSize(2));
        assertThat(codeDependencies.keySet(), contains("de.is24.deadcode4j.analyzer.hibernateannotations.ClassUsingTypeAtField"));
        assertThat(concat(codeDependencies.values()), contains("de.is24.deadcode4j.analyzer.hibernateannotations.ClassWithTypeDef"));
    }

    @Test
    public void shouldRecognizeDependencyFromClassWithTypeAnnotatedMethodToTypeDefAnnotatedClass() {
        CodeContext codeContext = new CodeContext();
        objectUnderTest.doAnalysis(codeContext, getFile("de/is24/deadcode4j/analyzer/hibernateannotations/ClassWithTypeDef.class"));
        objectUnderTest.doAnalysis(codeContext, getFile("de/is24/deadcode4j/analyzer/hibernateannotations/ClassUsingTypeAtMethod.class"));
        objectUnderTest.finishAnalysis(codeContext);


        Map<String, ? extends Iterable<String>> codeDependencies = codeContext.getAnalyzedCode().getCodeDependencies();
        assertThat("Should have analyzed the class files!", codeContext.getAnalyzedCode().getAnalyzedClasses(), hasSize(2));
        assertThat(codeDependencies.keySet(), contains("de.is24.deadcode4j.analyzer.hibernateannotations.ClassUsingTypeAtMethod"));
        assertThat(concat(codeDependencies.values()), contains("de.is24.deadcode4j.analyzer.hibernateannotations.ClassWithTypeDef"));
    }

    @Test
    public void shouldRecognizeDependencyFromTypeAnnotatedClassesToTypeDefsAnnotatedPackage() {
        CodeContext codeContext = new CodeContext();
        objectUnderTest.doAnalysis(codeContext, getFile("de/is24/deadcode4j/analyzer/hibernateannotations/Entity.class"));
        objectUnderTest.doAnalysis(codeContext, getFile("de/is24/deadcode4j/analyzer/hibernateannotations/package-info.class"));
        objectUnderTest.doAnalysis(codeContext, getFile("de/is24/deadcode4j/analyzer/hibernateannotations/AnotherEntity.class"));
        objectUnderTest.finishAnalysis(codeContext);


        Map<String, ? extends Iterable<String>> codeDependencies = codeContext.getAnalyzedCode().getCodeDependencies();
        assertThat("Should have analyzed the class files!", codeContext.getAnalyzedCode().getAnalyzedClasses(), hasSize(3));
        assertThat(codeDependencies.keySet(), containsInAnyOrder(
                "de.is24.deadcode4j.analyzer.hibernateannotations.Entity",
                "de.is24.deadcode4j.analyzer.hibernateannotations.AnotherEntity"));
        assertThat(concat(codeDependencies.values()), contains(
                "de.is24.deadcode4j.analyzer.hibernateannotations.package-info",
                "de.is24.deadcode4j.analyzer.hibernateannotations.package-info"));
    }

    @Test
    public void shouldRecognizeDependencyFromClassWithTypeAnnotatedMethodToReferencedClass() {
        CodeContext codeContext = new CodeContext();
        objectUnderTest.doAnalysis(codeContext, getFile("de/is24/deadcode4j/analyzer/hibernateannotations/ClassUsingTypeWithoutTypeDef.class"));
        objectUnderTest.doAnalysis(codeContext, getFile("IndependentClass.class"));
        objectUnderTest.finishAnalysis(codeContext);


        Map<String, ? extends Iterable<String>> codeDependencies = codeContext.getAnalyzedCode().getCodeDependencies();
        assertThat("Should have analyzed the class files!", codeContext.getAnalyzedCode().getAnalyzedClasses(), hasSize(2));
        assertThat(codeDependencies.keySet(), hasItem("de.is24.deadcode4j.analyzer.hibernateannotations.ClassUsingTypeWithoutTypeDef"));
        assertThat(concat(codeDependencies.values()), contains("IndependentClass"));
    }

}

@@ -586,39 +586,33 @@
 public void 
-shouldR
+r
 ecognize
 DependencyFr
@@ -591,32 +591,33 @@
 c void recognize
+s
 DependencyFromCl
@@ -1587,39 +1587,33 @@
 public void 
-shouldR
+r
 ecognize
 DependencyFr
@@ -1592,32 +1592,33 @@
 c void recognize
+s
 DependencyFromCl
@@ -2591,39 +2591,33 @@
 public void 
-shouldR
+r
 ecognize
 DependencyFr
@@ -2596,32 +2596,33 @@
 c void recognize
+s
 DependencyFromTy
@@ -3895,23 +3895,17 @@
 oid 
-shouldR
+r
 ecognize
 Depe
@@ -3900,16 +3900,17 @@
 ecognize
+s
 Dependen

no should