-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add methods().that().are[Not]{Static,Final} syntax
Signed-off-by: Manfred Hanke <Manfred.Hanke@tngtech.com>
- Loading branch information
Showing
6 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
archunit/src/main/java/com/tngtech/archunit/lang/syntax/MethodsThatInternal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/MethodsThat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
62 changes: 62 additions & 0 deletions
62
archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |