private AsmMutabilityChecker checkerWithHardcodedAsImmutable( Class<ImmutableContainer> containerClass) { return new CollectionWithMutableElementTypeToFieldChecker( mutableTypeInfo, testingVerifierFactory(), ImmutableSet.of(fromClass(containerClass)), AnalysisInProgress.noAnalysisUnderway()); }
public class CollectionWithMutableElementTypeToFieldCheckerTest { private final MutableTypeInformation mutableTypeInfo = new MutableTypeInformation( testAnalysisSession(), Configurations.NO_CONFIGURATION, CyclicReferences.newEmptyMutableInstance()); private final AsmMutabilityChecker checker = new CollectionWithMutableElementTypeToFieldChecker( mutableTypeInfo, testingVerifierFactory(), ImmutableSet.<Dotted>of(), AnalysisInProgress.noAnalysisUnderway()); @Test public void safelyWrappedCollectionsAreStillMutableIfTheTypeOfListElementsIsMutable() throws Exception { AnalysisResult result = runChecker(checker, SafelyCopiedMapGenericOnMutableTypeForKey.class); assertThat(result, areNotImmutable()); assertThat(checker, hasReasons(COLLECTION_FIELD_WITH_MUTABLE_ELEMENT_TYPE)); } @Test public void doesNotRaiseErrorWhenElementTypeIsImmutable() throws Exception { AnalysisResult result = runChecker(checker, SafelyCopiedMapGenericOnImmutableTypeForKey_ManyFields.class); assertThat(result, areImmutable()); assertThat(checker, hasNoReasons()); } @Test public void supportsNestedGenericTypes() throws Exception { AnalysisResult result = runChecker(checker, NestedGenericTypes.class); assertThat(result, areNotImmutable()); assertThat(checker, hasReasons(COLLECTION_FIELD_WITH_MUTABLE_ELEMENT_TYPE)); } @Test public void raisesErrorWhenCollectionFieldHasElementTypeUsingGenericTypeOfClass() { AnalysisResult result = runChecker(checker, SafelyCopiedMap_UsesGenericTypeOfClass.class); assertThat(result, areNotImmutable()); assertThat(checker, hasReasons(COLLECTION_FIELD_WITH_MUTABLE_ELEMENT_TYPE)); assertThat( checker.checkerResult().reasons.iterator().next().message(), containsString("<org.mutabilitydetector.benchmarks.ImmutableExample, SOME_GENERIC_TYPE>")); } @Test public void raisesErrorForHardcodedImmutableContainerTypeThatIsGenericWithMutableType() { AsmMutabilityChecker checker = checkerWithHardcodedAsImmutable(ImmutableContainer.class); AnalysisResult result = runChecker(checker, HasImmutableContainerOfMutableType.class); assertThat(result, areNotImmutable()); } @Test public void raisesErrorForHardcodedImmutableContainerTypeThatIsGenericWithVariableGenericParameter() { AsmMutabilityChecker checker = checkerWithHardcodedAsImmutable(ImmutableContainer.class); AnalysisResult result = runChecker(checker, HasImmutableContainerOfGenericType.class); assertThat(result, areNotImmutable()); } @Test public void doesNotRaiseErrorForHardcodedImmutableContainerTypeThatIsGenericWithImmutableType() { AsmMutabilityChecker checker = checkerWithHardcodedAsImmutable(ImmutableContainer.class); AnalysisResult result = runChecker(checker, HasImmutableContainerOfImmutableType.class); assertThat(result, areImmutable()); } private AsmMutabilityChecker checkerWithHardcodedAsImmutable( Class<ImmutableContainer> containerClass) { return new CollectionWithMutableElementTypeToFieldChecker( mutableTypeInfo, testingVerifierFactory(), ImmutableSet.of(fromClass(containerClass)), AnalysisInProgress.noAnalysisUnderway()); } @Test public void descriptionOfCollectionWithMutableElementType() throws Exception { AnalysisResult result = runChecker(checker, SafelyCopiedMapGenericOnMutableTypeForKey.class); assertThat(result, areNotImmutable()); MutableReasonDetail reasonDetail = result.reasons.iterator().next(); assertThat( reasonDetail.message(), is( "Field can have collection with mutable element type " + "(java.util.Map<java.util.Date, org.mutabilitydetector.benchmarks.ImmutableExample>) assigned to it.")); } }