|
8 | 8 | import java.util.Set; |
9 | 9 |
|
10 | 10 | import com.google.common.collect.ImmutableList; |
| 11 | +import com.tngtech.archunit.base.DescribedPredicate; |
11 | 12 | import com.tngtech.archunit.core.importer.ClassFileImporter; |
12 | 13 | import com.tngtech.java.junit.dataprovider.DataProvider; |
13 | 14 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; |
|
18 | 19 | import static com.google.common.collect.Lists.newArrayList; |
19 | 20 | import static com.google.common.collect.Sets.union; |
20 | 21 | import static com.tngtech.archunit.core.domain.JavaClass.Predicates.equivalentTo; |
| 22 | +import static com.tngtech.archunit.core.domain.JavaCodeUnit.Predicates.allParameters; |
| 23 | +import static com.tngtech.archunit.core.domain.JavaCodeUnit.Predicates.anyParameterThat; |
21 | 24 | import static com.tngtech.archunit.core.domain.JavaParameter.startWithLowercase; |
22 | 25 | import static com.tngtech.archunit.core.domain.TestUtils.importClassWithContext; |
23 | 26 | import static com.tngtech.archunit.core.domain.properties.HasType.Functions.GET_RAW_TYPE; |
| 27 | +import static com.tngtech.archunit.lang.conditions.ArchPredicates.are; |
| 28 | +import static com.tngtech.archunit.lang.conditions.ArchPredicates.is; |
24 | 29 | import static com.tngtech.archunit.testutil.Assertions.assertThat; |
25 | 30 | import static com.tngtech.archunit.testutil.Assertions.assertThatAnnotation; |
26 | 31 | import static com.tngtech.archunit.testutil.Assertions.assertThatTypes; |
@@ -326,6 +331,32 @@ void method(@SomeParameterAnnotation("test") String param) { |
326 | 331 | assertThat(parameter.tryGetAnnotationOfType(Deprecated.class.getName())).isEmpty(); |
327 | 332 | } |
328 | 333 |
|
| 334 | + @Test |
| 335 | + public void predicate_on_any_parameter() { |
| 336 | + JavaClass someClass = importClassWithContext(SomeClass.class); |
| 337 | + DescribedPredicate<JavaParameter> stringTyped = equivalentTo(String.class).onResultOf(JavaParameter::getRawType); |
| 338 | + |
| 339 | + DescribedPredicate<JavaCodeUnit> predicate = anyParameterThat(is(stringTyped)); |
| 340 | + |
| 341 | + assertThat(predicate).hasDescription("any parameter that is equivalent to java.lang.String") |
| 342 | + .accepts(someClass.getMethod("method", String.class, String.class)) |
| 343 | + .accepts(someClass.getMethod("method", String.class, Integer.class)) |
| 344 | + .rejects(someClass.getMethod("method", Integer.class, Integer.class)); |
| 345 | + } |
| 346 | + |
| 347 | + @Test |
| 348 | + public void predicate_on_all_parameters() { |
| 349 | + JavaClass someClass = importClassWithContext(SomeClass.class); |
| 350 | + DescribedPredicate<JavaParameter> stringTyped = equivalentTo(String.class).onResultOf(JavaParameter::getRawType); |
| 351 | + |
| 352 | + DescribedPredicate<JavaCodeUnit> predicate = allParameters(are(stringTyped)); |
| 353 | + |
| 354 | + assertThat(predicate).hasDescription("all parameters are equivalent to java.lang.String") |
| 355 | + .accepts(someClass.getMethod("method", String.class, String.class)) |
| 356 | + .rejects(someClass.getMethod("method", String.class, Integer.class)) |
| 357 | + .rejects(someClass.getMethod("method", Integer.class, Integer.class)); |
| 358 | + } |
| 359 | + |
329 | 360 | @SuppressWarnings("unused") |
330 | 361 | private static class ClassWithVariousCodeUnitParameters { |
331 | 362 | ClassWithVariousCodeUnitParameters(Object simple, String noParameterizedTypes) { |
@@ -379,6 +410,18 @@ enum NonTrivialEnum { |
379 | 410 | } |
380 | 411 | } |
381 | 412 |
|
| 413 | + @SuppressWarnings("unused") |
| 414 | + private static class SomeClass { |
| 415 | + void method(Integer a, Integer b) { |
| 416 | + } |
| 417 | + |
| 418 | + void method(String l, String r) { |
| 419 | + } |
| 420 | + |
| 421 | + void method(String s, Integer i) { |
| 422 | + } |
| 423 | + } |
| 424 | + |
382 | 425 | @interface SomeMetaAnnotation { |
383 | 426 | } |
384 | 427 |
|
|
0 commit comments