Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide basic rule definitions for fields, methods, constructors... #38

Closed
codecholeric opened this issue Oct 24, 2017 · 8 comments
Closed
Assignees
Milestone

Comments

@codecholeric
Copy link
Collaborator

We should add to ArchRuleDefinition the factory methods

  • GivenFields fields()
  • GivenMethods methods()
  • GivenConstructors constructors()
  • GivenCodeUnits codeUnits()
  • GivenMembers members()

that provide an easy entrance to speak about the respective objects, i.e. provide a that(predicate) and should(condition) fluent API. At some later point, this API can be extended for generally useful methods like declaredIn(clazz) or similar.

@stefluhh
Copy link

stefluhh commented Nov 9, 2017

Hi @codecholeric ,

do you already have ideas how to design the DSL for fields(), methods(), constructors() etc. when it is desired to relate to the class the fields, methods, constructors reside in?

My first idea would be something newly specific for that like this:

fields().whoseClasses()

This would enable the opportunity to reuse all thats() on classes level right? :)
Concrete usage in my use case would be:

fields()
     .that().haveName("userId")
     .and().whoseClasses().areAnnotatedWith(Aggregate.class)
     .should().beAnnotatedWith(Indexed.class)
     .because("UserId is an aggregate reference and we have finders on UserId.");

This would also lead to my next question, I would like to write ArchTests for specific classes only to make sure, that a certain constructor is annotated with @PersistenceConstructor or that only particular fields in that class are annotated with Spring Data MongoDb Indexed annotation.

This would probably be possible with something like this:

@RunWith(ArchUnitRunner.class)
@AnalyzeClass(Order.class)
public class RepositoryArchTest {

   @ArchTest
   public void enforceThat_UserId_IsIndexedForMongoDb(JavaClasses javaClasses) throws Exception {
      field()
              .that().hasName("userId")
              .should().beAnnotatedWith(Indexed.class)
              .check(javaClasses);
   }
}

However one could argument that this is also easily possible with java.lang.Class even:

Field userId = Order.class.getDeclaredField("userId");
assertTrue("UserId must be indexed in Mongo because there is a finder implemented for UserId", 
        userId.isAnnotationPresent(Indexed.class));

However only the existence of ArchUnit made me think of such test cases for the first time.

@codecholeric
Copy link
Collaborator Author

I pretty much had the same idea about reusing the class predicates for member assertions, my idea was, to call it

fields().that().areDeclaredIn() (.classesThat()/.packagesThat()/...)

😉
You are right about your observations in the second part, ArchUnit only provides an advantage over directly using reflection, if

  • You need non-local information (2+ classes in connection)
  • You want to be independent of the classpath
  • You can use some of the succinct syntax

Other than that, ArchUnit was created with a strong idea of providing an "extension" of reflection, so if you only use a subset, it might be well possible to assert all those things with reflection alone.

However, I would use ArchUnit anyway, simply for having all tests together in a consistent way. (only talking from my own experience, I used to write those tests with a whole arsenal of tools from reflection to AspectJ, and it certainly doesn't make it easier to understand, where certain new tests should go and which places you have to observe in the CI build)

Anyway, this issue will unfortunately only provide the basic entry point for fields, methods, ..., you'll have to be patient to get the full fluent API 😉

@codecholeric codecholeric self-assigned this Nov 19, 2017
@codecholeric codecholeric modified the milestones: Release 0.5.0, 0.6.0 Nov 22, 2017
@boidolr boidolr self-assigned this Nov 26, 2017
@codecholeric codecholeric modified the milestones: 0.6.0, 1.0.0 Feb 23, 2018
@boidolr boidolr removed their assignment Aug 24, 2018
codecholeric added a commit that referenced this issue Nov 25, 2018
…te) / should(predicate) methods to syntax. Unfortunately anonymous instantiations of abstract classes with varargs constructors cause a Javadoc bug up to JDK 11, thus all anonymous instantiations of ArchCondition had to be replaced by inner classes :-( This should not be a common problem for users of ArchUnit though, since usually there will be no Javadoc created for test code.

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Nov 25, 2018
…ion test for members syntax.

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
@raspacorp
Copy link

raspacorp commented Jan 30, 2019

@codecholeric I saw your commits where you are adding support for fields and methods in archrules, it looks great, I am currently working on a task of verifying that methods annotated with a specific annotation have an attribute set up. Do you know if these changes will be part of the next release? Thanks.

@codecholeric
Copy link
Collaborator Author

@raspacorp Yes, this is planned for the next release. However I don't have a fixed date except for sometimes within the next 2 months 😉
Other than that using AbstractClassesTransformer to transform classes to methods (all(methods()).that(..).should(..)) should also not be terribly much code I hope (there are a lot of predefined predicates for JavaMethod and co., by convention always in JavaXXX.Predicates).

codecholeric added a commit that referenced this issue Feb 2, 2019
…te) / should(predicate) methods to syntax. Unfortunately anonymous instantiations of abstract classes with varargs constructors cause a Javadoc bug up to JDK 11, thus all anonymous instantiations of ArchCondition had to be replaced by inner classes :-( This should not be a common problem for users of ArchUnit though, since usually there will be no Javadoc created for test code.

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 2, 2019
…ion test for members syntax.

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 2, 2019
…there for classes and match properties of members as well, like visibility or names)

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 2, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 2, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 2, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 10, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 11, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 11, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 17, 2019
…te) / should(predicate) methods to syntax. Unfortunately anonymous instantiations of abstract classes with varargs constructors cause a Javadoc bug up to JDK 11, thus all anonymous instantiations of ArchCondition had to be replaced by inner classes :-( This should not be a common problem for users of ArchUnit though, since usually there will be no Javadoc created for test code.

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 17, 2019
…ion test for members syntax.

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Mar 15, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Mar 15, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Mar 15, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Mar 15, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Mar 31, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Mar 31, 2019
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
…te) / should(predicate) methods to syntax. Unfortunately anonymous instantiations of abstract classes with varargs constructors cause a Javadoc bug up to JDK 11, thus all anonymous instantiations of ArchCondition had to be replaced by inner classes :-( This should not be a common problem for users of ArchUnit though, since usually there will be no Javadoc created for test code.

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
…ion test for members syntax.

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
…there for classes and match properties of members as well, like visibility or names)

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
…terface types instead of resolving runtime types via TypeToken (necessary to support more extended generics for complex members syntax with inheritance; we need to pass information about actual type parameters of generic interface types on, instead of using TypeToken on runtime types which will in parts yield <? extends #99...> types instead of the correctly bounded declared type available via public API)

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
…escription should be concern of PartialStep, not Parameters.

Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric added a commit that referenced this issue Feb 21, 2021
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants