Sunday, April 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Java

ArchUnit Verifies Architecture Rules for Java Applications

learningcode_x1mckf by learningcode_x1mckf
October 17, 2022
in Java
0
ArchUnit Verifies Architecture Rules for Java Applications
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

So why did they decide to call it Java? – InfoWorld

Senior Java Developer – IT-Online

West Java to provide simultaneous polio vaccinations from Apr 3 – ANTARA English

ArchUnit permits builders to implement structure guidelines similar to naming conventions, class entry to different lessons, and the prevention of cycles. The library was initially created in 2017 by Peter Gafert and model 1.0.0 was released in October.

ArchUnit works with all Java check frameworks and gives particular dependencies for JUnit. The next dependency needs to be used for JUnit 5:


<dependency>
    <groupId>com.tngtech.archunit</groupId>
    <artifactId>archunit-junit5</artifactId>
    <model>1.0.0</model>
    <scope>check</scope>
</dependency>

Now the ClassFileImporter can be utilized to import Java bytecode into Java code. For instance, to import all lessons within the org.instance bundle:


JavaClasses javaClasses = new ClassFileImporter().importPackages("org.instance");

Now the ArchRule class could also be used to outline architectural guidelines for the imported Java lessons in a Area Particular Language (DSL). There are numerous forms of checks obtainable, the primary one is for bundle dependencies. The verify specifies that no lessons inside repository packages ought to use lessons inside controller packages:


ArchRule rule = noClasses()
    .that().resideInAPackage("..repository..")
    .ought to().dependOnClassesThat().resideInAPackage("..controller..");

Two lessons are used to confirm the foundations, a CourseController class inside a controller bundle and a CourseRepository class inside a repository bundle:


public class CourseController 
	non-public CourseRepository courseRepository;


public class CourseRepository 
	CourseController courseController;

This isn’t allowed by the ArchRule outlined earlier than, which might be examined robotically with JUnit:


AssertionError assertionError =

    Assertions.assertThrows(AssertionError.class, () -> 
        rule.verify(javaClasses);
);

String expectedMessage = """
	Structure Violation [Priority: MEDIUM] - 
        Rule 'no lessons that reside in a bundle 
        '..repository..' ought to rely on lessons that reside in a bundle 
        '..controller..'' was violated (1 instances):
	Subject <org.instance.repository.CourseRepository.courseController> has sort 
        <org.instance.controller.CourseController> in (CourseRepository.java:0)""";

assertEquals(expectedMessage, assertionError.getMessage());

The CourseController and CourseRepository rely on one another, which regularly is a design flaw. The cycle verify detects cycles between lessons and packages:


ArchRule rule = slices()

    .matching("org.instance.(*)..")
    .ought to().beFreeOfCycles();

AssertionError assertionError =     
    Assertions.assertThrows(AssertionError.class, () -> 
        rule.verify(javaClasses);
);

String expectedMessage = """
	Structure Violation [Priority: MEDIUM] - Rule 'slices matching 
        'org.instance.(*)..' needs to be freed from cycles' was violated (1 instances):
	Cycle detected: Slice controller ->s
                	Slice repository ->s
                	Slice controller
  	1. Dependencies of Slice controller
    	- Subject <org.instance.controller.CourseController.courseRepository> has sort 
            <org.instance.repository.CourseRepository> in (CourseController.java:0)
  	2. Dependencies of Slice repository
    	- Subject <org.instance.repository.CourseRepository.courseController> has sort 
            <org.instance.controller.CourseController> in (CourseRepository.java:0)""";

assertEquals(expectedMessage, assertionError.getMessage());

Class and Bundle containment checks permit the verification of naming and site conventions. For instance, to confirm that no interfaces are positioned inside implementation packages:


noClasses()
    .that().resideInAPackage("..implementation..")
    .ought to().beInterfaces().verify(lessons);

Or to confirm that each one interfaces have a reputation containing “Interface”:


noClasses()
    .that().areInterfaces()
    .ought to().haveSimpleNameContaining("Interface").verify(lessons);

These containment checks could also be mixed with an annotation verify. For instance, to confirm that each one lessons within the controller bundle with a RestController annotation have a reputation ending with Controller:


lessons()
    .that().resideInAPackage("..controller..")
    .and().areAnnotatedWith(RestController.class)
    .ought to().haveSimpleNameEndingWith("Controller");

Inheritance checks permit, for instance, to confirm that each one lessons implementing the Repository interface have a reputation ending with Repository:


lessons().that().implement(Repository.class)
    .ought to().haveSimpleNameEndingWith("Repository")

With the layer checks, it is doable to outline the structure layers of an utility after which outline the foundations between the layers:


Architectures.LayeredArchitecture rule = layeredArchitecture()
    .consideringAllDependencies()
    // Outline layers
    .layer("Controller").definedBy("..controller..")
    .layer("Repository").definedBy("..Repository..")
    // Add constraints
    .whereLayer("Controller").mayNotBeAccessedByAnyLayer()
    .whereLayer("Repository").mayOnlyBeAccessedByLayers("Controller");

AssertionError assertionError = 
    Assertions.assertThrows(AssertionError.class, () -> 
        rule.verify(javaClasses);
);

String expectedMessage = """
	Structure Violation [Priority: MEDIUM] - Rule 'Layered structure 
        contemplating all dependencies, consisting of
	layer 'Controller' ('..controller..')
	layer 'Repository' ('..Repository..')
	the place layer 'Controller' might not be accessed by any layer
	the place layer 'Repository' could solely be accessed by layers ['Controller']' 
        was violated (2 instances):
	Subject <org.instance.repository.CourseRepository.courseController> has sort 
        <org.instance.controller.CourseController> in (CourseRepository.java:0)
	Layer 'Repository' is empty""";

assertEquals(expectedMessage, assertionError.getMessage());

Extra data might be discovered within the intensive user guide and official examples from ArchUnit can be found on GitHub.





Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

So why did they decide to call it Java? – InfoWorld

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

So why did they decide to call it Java?  InfoWorld Source link

Read more

Senior Java Developer – IT-Online

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Senior Java Developer  IT-On-line Source link

Read more

West Java to provide simultaneous polio vaccinations from Apr 3 – ANTARA English

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

West Java to provide simultaneous polio vaccinations from Apr 3  ANTARA English Source link

Read more

COBOL programming skills gap thwarts modernization to Java – TechTarget

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

COBOL programming skills gap thwarts modernization to Java  TechTarget Source link

Read more

User input with a Java JOptionPane example – TheServerSide.com

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

User input with a Java JOptionPane example  TheServerSide.com Source link

Read more
Next Post
A Practical Introduction to Web Scraping in Python – Real Python

A Practical Introduction to Web Scraping in Python – Real Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

8 Java frameworks for embedded development

8 Java frameworks for embedded development

November 1, 2022
Better Date and Time Management in JavaScript

Better Date and Time Management in JavaScript

September 22, 2022
Strings and String Methods – Real Python

Strings and String Methods – Real Python

October 4, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • So why did they decide to call it Java? – InfoWorld
  • Senior Java Developer – IT-Online
  • 4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?