-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add methods().should().{be,notBe}{Static,Final} syntax
Signed-off-by: Manfred Hanke <[email protected]>
- Loading branch information
Showing
6 changed files
with
206 additions
and
1 deletion.
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
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/MethodsShould.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 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(); | ||
} |
41 changes: 41 additions & 0 deletions
41
...nit/src/main/java/com/tngtech/archunit/lang/syntax/elements/MethodsShouldConjunction.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,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(); | ||
} |
63 changes: 63 additions & 0 deletions
63
archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/MethodsShouldTest.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,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; | ||
} | ||
} | ||
} |