package org.mutabilitydetector;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.mutabilitydetector.AnalysisResult.analysisResult;
import static org.mutabilitydetector.locations.Dotted.dotted;

import org.junit.Test;

public class ConfigurationBuilderTest {

    @Test
    public void canMergeResultsFromExistingConfiguration() throws Exception {
        final Configuration existing = new ConfigurationBuilder() {
            @Override public void configure() {
                hardcodeResult(AnalysisResult.definitelyImmutable("hardcoded.in.other.Configuration"));
            }
        }.build();
        
        final Configuration current = new ConfigurationBuilder() {
            @Override public void configure() {
                mergeHardcodedResultsFrom(existing);
            }
        }.build();
        
        assertThat(current.hardcodedResults(), hasKey(dotted("hardcoded.in.other.Configuration")));
    }

    @Test
    public void mergeReplacesExistingHardcodedResultForClassWithCurrentHardcodedResult() throws Exception {
        final Configuration existing = new ConfigurationBuilder() {
            @Override public void configure() {
                hardcodeResult(AnalysisResult.definitelyImmutable("hardcoded.in.both.Configurations"));
            }
        }.build();
        
        final AnalysisResult resultInCurrentConfig = analysisResult("hardcoded.in.both.Configurations", 
                                                              IsImmutable.NOT_IMMUTABLE, 
                                                              TestUtil.unusedMutableReasonDetail());
        final Configuration current = new ConfigurationBuilder() {
            @Override public void configure() {
                hardcodeResult(resultInCurrentConfig);

                mergeHardcodedResultsFrom(existing);
            }
        }.build();
        
        assertThat(current.hardcodedResults(), hasEntry(dotted("hardcoded.in.both.Configurations"),
                                                        resultInCurrentConfig));
    }
}

@@ -169,16 +169,56 @@
 hasKey;%0A
+import static org.hamcrest.Matchers.is;%0A
 import s
@@ -318,55 +318,199 @@
 tor.
-locations.Dotted.dotted;%0A%0Aimport org.junit.Test
+AnalysisResult.definitelyImmutable;%0Aimport static org.mutabilitydetector.locations.Dotted.dotted;%0A%0Aimport java.util.Map;%0A%0Aimport org.junit.Test;%0Aimport org.mutabilitydetector.locations.Dotted
 ;%0A%0Ap
@@ -780,39 +780,24 @@
 dcodeResult(
-AnalysisResult.
 definitelyIm
@@ -1466,31 +1466,16 @@
 eResult(
-AnalysisResult.
 definite
@@ -1520,24 +1520,111 @@
 rations%22));%0A
+                hardcodeResult(definitelyImmutable(%22only.in.existing.Configuration%22));%0A
             
@@ -1945,24 +1945,33 @@
 nDetail());%0A
+        %0A
         fina
@@ -2127,16 +2127,102 @@
 Config);
+%0A                hardcodeResult(definitelyImmutable(%22only.in.current.Configuration%22));
 %0A%0A      
@@ -2322,177 +2322,543 @@
     
-assertThat(current.hardcodedResults(), hasEntry(dotted(%22hardcoded.in.both.Configurations%22),%0A                                                        resultInC
+Map%3CDotted, AnalysisResult%3E hardcodedResults = current.hardcodedResults();%0A        %0A        assertThat(hardcodedResults.size(), is(3));%0A        assertThat(hardcodedResults, hasEntry(dotted(%22hardcoded.in.both.Configurations%22), resultInCurrentConfig));%0A        assertThat(hardcodedResults, hasEntry(dotted(%22only.in.existing.Configuration%22), definitelyImmutable(%22only.in.existing.Configuration%22)));%0A        assertThat(hardcodedResults, hasEntry(dotted(%22only.in.current.Configuration%22), definitelyImmutable(%22only.in.c
 urrent
+.
 Config
+uration%22)
 ));%0A

Add more data to the configurations to show merging behaviour.