Skip to content

Commit

Permalink
add methods().that().are[Not]{Static,Final} syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Hanke <Manfred.Hanke@tngtech.com>
  • Loading branch information
hankem committed Mar 29, 2019
1 parent efbbc32 commit 6b13196
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ private GivenMethodsInternal(
super(factory, priority, classesTransformer, prepareCondition, relevantObjectsPredicates, overriddenDescription);
}

@Override
public MethodsThatInternal that() {
return new MethodsThatInternal(this, currentPredicate());
}

@Override
public MethodsThatInternal and() {
return new MethodsThatInternal(this, currentPredicate().thatANDs());
}

@Override
public MethodsThatInternal or() {
return new MethodsThatInternal(this, currentPredicate().thatORs());
}

@Override
public MethodsShouldInternal should() {
return new MethodsShouldInternal(finishedClassesTransformer(), priority, prepareCondition);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;

import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.lang.syntax.elements.MethodsThat;

class MethodsThatInternal
extends CodeUnitsThatInternal<JavaMethod, GivenMethodsInternal>
implements MethodsThat<GivenMethodsInternal> {

MethodsThatInternal(GivenMethodsInternal givenMethods, PredicateAggregator<JavaMethod> currentPredicate) {
super(givenMethods, currentPredicate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

public interface GivenMethods extends GivenCodeUnits<JavaMethod> {

@Override
@PublicAPI(usage = ACCESS)
MethodsThat<?> that();

@Override
@PublicAPI(usage = ACCESS)
GivenMethodsConjunction that(DescribedPredicate<? super JavaMethod> predicate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@

public interface GivenMethodsConjunction extends GivenCodeUnitsConjunction<JavaMethod> {

@Override
@PublicAPI(usage = ACCESS)
MethodsThat<?> and();

@Override
@PublicAPI(usage = ACCESS)
MethodsThat<?> or();

@Override
@PublicAPI(usage = ACCESS)
GivenMethodsConjunction and(DescribedPredicate<? super JavaMethod> predicate);
Expand Down
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 MethodsThat<CONJUNCTION extends GivenMethodsConjunction> extends CodeUnitsThat<CONJUNCTION> {

/**
* Matches static methods.
*
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION areStatic();

/**
* Matches non-static methods.
*
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION areNotStatic();

/**
* Matches final methods.
*
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION areFinal();

/**
* Matches non-final methods.
*
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION areNotFinal();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.tngtech.archunit.lang.syntax.elements;

import com.google.common.collect.ImmutableSet;
import com.tngtech.archunit.lang.EvaluationResult;
import com.tngtech.archunit.lang.syntax.elements.GivenMembersTest.*;
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 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.GivenMembersTest.*;
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 GivenMethodsTest {

@DataProvider
public static Object[][] restricted_property_rule_starts() {
return $$(
$(described(methods().that().areFinal()), ImmutableSet.of(METHOD_A, METHOD_B)),
$(described(methods().that().areNotFinal()), ImmutableSet.of(METHOD_C, METHOD_D)),
$(described(methods().that().areStatic()), ImmutableSet.of(METHOD_B, METHOD_D)),
$(described(methods().that().areNotStatic()), ImmutableSet.of(METHOD_A, METHOD_C)),
$(described(methods().that().areFinal().and().areStatic()), ImmutableSet.of(METHOD_B)),
$(described(methods().that().areFinal().or().areStatic()), ImmutableSet.of(METHOD_A, METHOD_B, METHOD_D))
);
}

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

assertThat(result.getFailureReport().getDetails()).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 6b13196

Please sign in to comment.