Skip to content

Commit

Permalink
add methods().should().{be,notBe}{Static,Final} syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Hanke <[email protected]>
  • Loading branch information
hankem committed Mar 29, 2019
1 parent 6b13196 commit ca18b17
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@

import com.tngtech.archunit.base.Function;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ClassesTransformer;
import com.tngtech.archunit.lang.Priority;
import com.tngtech.archunit.lang.conditions.ArchConditions;
import com.tngtech.archunit.lang.syntax.elements.MethodsShould;
import com.tngtech.archunit.lang.syntax.elements.MethodsShouldConjunction;

class MethodsShouldInternal extends AbstractCodeUnitsShouldInternal<JavaMethod, MethodsShouldInternal> {
import static com.tngtech.archunit.lang.conditions.ArchConditions.not;

class MethodsShouldInternal
extends AbstractCodeUnitsShouldInternal<JavaMethod, MethodsShouldInternal>
implements MethodsShould<MethodsShouldInternal>, MethodsShouldConjunction {

MethodsShouldInternal(
ClassesTransformer<? extends JavaMethod> classesTransformer,
Expand Down Expand Up @@ -53,4 +61,24 @@ private MethodsShouldInternal(
MethodsShouldInternal copyWithNewCondition(ConditionAggregator<JavaMethod> newCondition) {
return new MethodsShouldInternal(classesTransformer, priority, newCondition, prepareCondition);
}

@Override
public MethodsShouldInternal beStatic() {
return addCondition(ArchConditions.haveModifier(JavaModifier.STATIC).as("be static"));
}

@Override
public MethodsShouldInternal notBeStatic() {
return addCondition(not(ArchConditions.haveModifier(JavaModifier.STATIC)).as("not be static"));
}

@Override
public MethodsShouldInternal beFinal() {
return addCondition(ArchConditions.haveModifier(JavaModifier.FINAL).as("be final"));
}

@Override
public MethodsShouldInternal notBeFinal() {
return addCondition(not(ArchConditions.haveModifier(JavaModifier.FINAL)).as("not be final"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.lang.ArchCondition;

import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

Expand All @@ -30,4 +31,12 @@ public interface GivenMethods extends GivenCodeUnits<JavaMethod> {
@Override
@PublicAPI(usage = ACCESS)
GivenMethodsConjunction that(DescribedPredicate<? super JavaMethod> predicate);

@Override
@PublicAPI(usage = ACCESS)
MethodsShould<?> should();

@Override
@PublicAPI(usage = ACCESS)
MethodsShouldConjunction should(ArchCondition<? super JavaMethod> condition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.lang.ArchCondition;

import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

Expand All @@ -38,4 +39,12 @@ public interface GivenMethodsConjunction extends GivenCodeUnitsConjunction<JavaM
@Override
@PublicAPI(usage = ACCESS)
GivenMethodsConjunction or(DescribedPredicate<? super JavaMethod> predicate);

@Override
@PublicAPI(usage = ACCESS)
MethodsShould<?> should();

@Override
@PublicAPI(usage = ACCESS)
MethodsShouldConjunction should(ArchCondition<? super JavaMethod> condition);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2019 TNG Technology Consulting GmbH
*
* 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.
*/
package com.tngtech.archunit.lang.syntax.elements;

import com.tngtech.archunit.PublicAPI;

import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

public interface MethodsShould<CONJUNCTION extends MethodsShouldConjunction> extends CodeUnitsShould<CONJUNCTION> {

/**
* Asserts that methods are static.
*
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION beStatic();

/**
* Asserts that methods are non-static.
*
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notBeStatic();

/**
* Asserts that methods are final.
*
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION beFinal();

/**
* Asserts that methods are non-final.
*
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notBeFinal();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2019 TNG Technology Consulting GmbH
*
* 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.
*/
package com.tngtech.archunit.lang.syntax.elements;

import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.lang.ArchCondition;

import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

public interface MethodsShouldConjunction extends CodeUnitsShouldConjunction<JavaMethod> {

@Override
@PublicAPI(usage = ACCESS)
MethodsShouldConjunction andShould(ArchCondition<? super JavaMethod> condition);

@Override
@PublicAPI(usage = ACCESS)
MethodsShould<?> andShould();

@Override
@PublicAPI(usage = ACCESS)
MethodsShouldConjunction orShould(ArchCondition<? super JavaMethod> condition);

@Override
@PublicAPI(usage = ACCESS)
MethodsShould<?> orShould();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.tngtech.archunit.lang.syntax.elements;

import com.google.common.collect.ImmutableSet;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.EvaluationResult;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Collection;
import java.util.Set;

import static com.tngtech.archunit.core.domain.TestUtils.importClasses;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;
import static com.tngtech.archunit.lang.syntax.elements.MembersShouldTest.parseMembers;
import static com.tngtech.java.junit.dataprovider.DataProviders.$;
import static com.tngtech.java.junit.dataprovider.DataProviders.$$;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith(DataProviderRunner.class)
public class MethodsShouldTest {

@DataProvider
public static Object[][] restricted_property_rule_ends() {
return $$(
$(methods().should().beFinal(), ImmutableSet.of(METHOD_C, METHOD_D)),
$(methods().should().notBeFinal(), ImmutableSet.of(METHOD_A, METHOD_B)),
$(methods().should().beStatic(), ImmutableSet.of(METHOD_A, METHOD_C)),
$(methods().should().notBeStatic(), ImmutableSet.of(METHOD_B, METHOD_D)),
$(methods().should().notBeFinal().andShould().notBeStatic(), ImmutableSet.of(METHOD_A, METHOD_B, METHOD_D)),
$(methods().should().notBeFinal().orShould().notBeStatic(), ImmutableSet.of(METHOD_B))
);
}

@Test
@UseDataProvider("restricted_property_rule_ends")
public void property_predicates(ArchRule ruleStart, Collection<String> expectedMembers) {
EvaluationResult result = ruleStart.evaluate(importClasses(ClassWithVariousMembers.class));

Set<String> actualMethods = parseMembers(ClassWithVariousMembers.class, result.getFailureReport().getDetails());
assertThat(actualMethods).containsOnlyElementsOf(expectedMembers);
}

private static final String METHOD_A = "methodA([I)";
private static final String METHOD_B = "methodB(boolean)";
private static final String METHOD_C = "methodC(char)";
private static final String METHOD_D = "methodD()";

@SuppressWarnings({"unused"})
private static class ClassWithVariousMembers {
public final void methodA(int[] array) {
}
protected static final void methodB(boolean flag) {
}
private void methodC(char ch) {
}
static int methodD() {
return 0;
}
}
}

0 comments on commit ca18b17

Please sign in to comment.