org.mutabilitydetector.unittesting.MutabilityAsserter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of MutabilityDetector Show documentation
Show all versions of MutabilityDetector Show documentation
Lightweight analysis tool for detecting mutability in Java
classes.
package org.mutabilitydetector.unittesting;
/*
* #%L
* MutabilityDetector
* %%
* Copyright (C) 2008 - 2014 Graham Allan
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import static java.util.Arrays.asList;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import static org.mutabilitydetector.unittesting.matchers.reasons.WithAllowedReasonsMatcher.withAllowedReasons;
import static org.mutabilitydetector.unittesting.matchers.reasons.WithAllowedReasonsMatcher.withNoAllowedReasons;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.Matcher;
import org.mutabilitydetector.AnalysisResult;
import org.mutabilitydetector.AnalysisSession;
import org.mutabilitydetector.Configuration;
import org.mutabilitydetector.ConfigurationBuilder;
import org.mutabilitydetector.Configurations;
import org.mutabilitydetector.MutableReasonDetail;
import org.mutabilitydetector.ThreadUnsafeAnalysisSession;
import org.mutabilitydetector.locations.Dotted;
import org.mutabilitydetector.unittesting.internal.AssertionReporter;
import org.mutabilitydetector.unittesting.matchers.reasons.WithAllowedReasonsMatcher;
import com.google.common.collect.Lists;
/**
* Performs Mutability Detector's analysis and produces unit-test-friendly
* {@link AssertionError} if the result is not as expected.
*
* Instances of this class provide the methods accessed by
* {@link MutabilityAssert}. More detailed documentation can be found there.
*/
public class MutabilityAsserter {
private final AssertionReporter reporter;
private final AnalysisSession analysisSession;
private MutabilityAsserter(AssertionReporter reporter, AnalysisSession analysisSession) {
this.reporter = reporter;
this.analysisSession = analysisSession;
}
/**
* Create a new asserter with an existing {@link Configuration}.
*
* Example:
*
* MutabilityAsserter.configured(MyConfigurations.DEFAULT_CONFIGURATIONS);
*
* @see Configurations
* @see Configurations#JDK_CONFIGURATION
* @see Configurations#NO_CONFIGURATION
* @see Configurations#OUT_OF_THE_BOX_CONFIGURATION
*/
public static MutabilityAsserter configured(Configuration configuration) {
return new MutabilityAsserter(new AssertionReporter(),
ThreadUnsafeAnalysisSession.createWithCurrentClassPath(configuration));
}
/**
* Create a new asserter with a {@link Configuration} as built by the given
* {@link ConfigurationBuilder}.
*
* Use this method when you want to build a one-time Configuration inline..
*
* Example:
*
*
*
* MutabilityAsserter.configured(new ConfigurationBuilder() {
* @Override public void configure() {
* hardcodeAsDefinitelyImmutable(ActuallyImmutable.class);
* }
* });
*
*
*/
public static MutabilityAsserter configured(ConfigurationBuilder configuration) {
return new MutabilityAsserter(new AssertionReporter(),
ThreadUnsafeAnalysisSession.createWithCurrentClassPath(configuration.build()));
}
/**
* @see MutabilityAssert#assertImmutable(Class)
*/
public void assertImmutable(Class> expectedImmutableClass) {
reporter.assertThat(getResultFor(expectedImmutableClass), withNoAllowedReasons(areImmutable()));
}
/**
* @see MutabilityAssert#assertInstancesOf(Class, Matcher)
*/
public void assertInstancesOf(Class> clazz, Matcher mutabilityMatcher) {
reporter.assertThat(getResultFor(clazz), withNoAllowedReasons(mutabilityMatcher));
}
/**
* @see MutabilityAssert#assertInstancesOf(Class, Matcher, Matcher)
*/
@SuppressWarnings("unchecked")
public void assertInstancesOf(Class> clazz, Matcher mutabilityMatcher,
Matcher allowing) {
WithAllowedReasonsMatcher areImmutable_withReasons = withAllowedReasons(mutabilityMatcher, asList((allowing)));
reporter.assertThat(getResultFor(clazz), areImmutable_withReasons);
}
/**
* @see MutabilityAssert#assertInstancesOf(Class, Matcher, Matcher, Matcher)
*/
@SuppressWarnings("unchecked")
public void assertInstancesOf(Class> clazz, Matcher mutabilityMatcher,
Matcher allowingFirst,
Matcher allowingSecond) {
WithAllowedReasonsMatcher areImmutable_withReasons = withAllowedReasons(mutabilityMatcher,
asList(allowingFirst, allowingSecond));
reporter.assertThat(getResultFor(clazz), areImmutable_withReasons);
}
/**
* @see MutabilityAssert#assertInstancesOf(Class, Matcher, Matcher, Matcher, Matcher)
*/
@SuppressWarnings("unchecked")
public void assertInstancesOf(Class> clazz, Matcher mutabilityMatcher,
Matcher allowingFirst,
Matcher allowingSecond,
Matcher allowingThird) {
WithAllowedReasonsMatcher areImmutable_withReasons = withAllowedReasons(mutabilityMatcher,
asList(allowingFirst,
allowingSecond,
allowingThird));
reporter.assertThat(getResultFor(clazz), areImmutable_withReasons);
}
/**
* @see MutabilityAssert#assertInstancesOf(Class, Matcher, Matcher, Matcher, Matcher, Matcher...)
*/
public void assertInstancesOf(Class> clazz, Matcher mutabilityMatcher,
Matcher allowingFirst,
Matcher allowingSecond,
Matcher allowingThird,
Matcher... allowingRest) {
List> allowedReasonMatchers = new ArrayList>();
allowedReasonMatchers.add(allowingFirst);
allowedReasonMatchers.add(allowingSecond);
allowedReasonMatchers.add(allowingThird);
allowedReasonMatchers.addAll(asList(allowingRest));
WithAllowedReasonsMatcher areImmutable_withReasons = withAllowedReasons(mutabilityMatcher, allowedReasonMatchers);
reporter.assertThat(getResultFor(clazz), areImmutable_withReasons);
}
/**
* @see MutabilityAssert#assertInstancesOf(Class, Matcher, Iterable)
*/
public void assertInstancesOf(Class> clazz,
Matcher mutabilityMatcher,
Iterable> allowingAll) {
Iterable> allowedReasonMatchers = Lists.newArrayList(allowingAll);
WithAllowedReasonsMatcher areImmutable_withReasons = withAllowedReasons(mutabilityMatcher, allowedReasonMatchers);
reporter.assertThat(getResultFor(clazz), areImmutable_withReasons);
}
private AnalysisResult getResultFor(Class> clazz) {
return analysisSession.resultFor(Dotted.fromClass(clazz));
}
}