-
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.
Allow defining Onion Architecture components by predicates #894
Similar to `LayeredArchitecture` this will extend `OnionArchitecture` to not only allow defining components via package identifiers (e.g. `domainModels("..some.pkg..")`) but also via predicates (e.g. `domainModels(annotatedWith(DomainModel.class))` or `domainModels(simpleNameEndingWith("Model"))`). This will allow users that follow a different convention than packages to identify their Onion Architecture components to also use `OnionArchitecture` to assert their architecture.
- Loading branch information
Showing
53 changed files
with
902 additions
and
146 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ple-junit4/src/test/java/com/tngtech/archunit/exampletest/junit4/ControllerRulesTest.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
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
2 changes: 1 addition & 1 deletion
2
...ple-junit5/src/test/java/com/tngtech/archunit/exampletest/junit5/ControllerRulesTest.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
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
5 changes: 5 additions & 0 deletions
5
...va/com/tngtech/archunit/example/onionarchitecture_by_annotations/annotations/Adapter.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,5 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations; | ||
|
||
public @interface Adapter { | ||
String value(); | ||
} |
4 changes: 4 additions & 0 deletions
4
...om/tngtech/archunit/example/onionarchitecture_by_annotations/annotations/Application.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,4 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations; | ||
|
||
public @interface Application { | ||
} |
4 changes: 4 additions & 0 deletions
4
...om/tngtech/archunit/example/onionarchitecture_by_annotations/annotations/DomainModel.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,4 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations; | ||
|
||
public @interface DomainModel { | ||
} |
4 changes: 4 additions & 0 deletions
4
.../tngtech/archunit/example/onionarchitecture_by_annotations/annotations/DomainService.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,4 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations; | ||
|
||
public @interface DomainService { | ||
} |
8 changes: 8 additions & 0 deletions
8
...m/tngtech/archunit/example/onionarchitecture_by_annotations/onion/AdministrationPort.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,8 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.Application; | ||
|
||
@Application | ||
public interface AdministrationPort { | ||
<T> T getInstanceOf(Class<T> type); | ||
} |
19 changes: 19 additions & 0 deletions
19
.../tngtech/archunit/example/onionarchitecture_by_annotations/onion/ShoppingApplication.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,19 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.Application; | ||
|
||
@Application | ||
public class ShoppingApplication { | ||
public static void main(String[] args) { | ||
// start the whole application / provide IOC features | ||
} | ||
|
||
public static AdministrationPort openAdministrationPort() { | ||
return new AdministrationPort() { | ||
@Override | ||
public <T> T getInstanceOf(Class<T> type) { | ||
throw new UnsupportedOperationException("Not yet implemented"); | ||
} | ||
}; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...unit/example/onionarchitecture_by_annotations/onion/administration/AdministrationCLI.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,22 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.administration; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.Adapter; | ||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.AdministrationPort; | ||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.ShoppingApplication; | ||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.product.ProductRepository; | ||
|
||
@Adapter("cli") | ||
@SuppressWarnings("unused") | ||
public class AdministrationCLI { | ||
public static void main(String[] args) { | ||
AdministrationPort port = ShoppingApplication.openAdministrationPort(); | ||
handle(args, port); | ||
} | ||
|
||
private static void handle(String[] args, AdministrationPort port) { | ||
// violates the pairwise independence of adapters | ||
ProductRepository repository = port.getInstanceOf(ProductRepository.class); | ||
long count = repository.getTotalCount(); | ||
// parse arguments and re-configure application according to count through port | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../com/tngtech/archunit/example/onionarchitecture_by_annotations/onion/order/OrderItem.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,26 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.order; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.DomainModel; | ||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.product.Product; | ||
|
||
@DomainModel | ||
public class OrderItem { | ||
private final Product product; | ||
private final OrderQuantity quantity; | ||
|
||
public OrderItem(Product product, OrderQuantity quantity) { | ||
if (product == null) { | ||
throw new IllegalArgumentException("Product must not be null"); | ||
} | ||
if (quantity == null) { | ||
throw new IllegalArgumentException("Quantity not be null"); | ||
} | ||
this.product = product; | ||
this.quantity = quantity; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "{product=" + product + ", quantity=" + quantity + '}'; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
.../tngtech/archunit/example/onionarchitecture_by_annotations/onion/order/OrderQuantity.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,21 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.order; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.DomainService; | ||
|
||
@DomainService | ||
@SuppressWarnings("unused") | ||
public class OrderQuantity { | ||
private final int quantity; | ||
|
||
public OrderQuantity(int quantity) { | ||
if (quantity <= 0) { | ||
throw new IllegalArgumentException("Quantity must be positive"); | ||
} | ||
this.quantity = quantity; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "{quantity=" + quantity + '}'; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
.../tngtech/archunit/example/onionarchitecture_by_annotations/onion/order/PaymentMethod.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,7 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.order; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.DomainModel; | ||
|
||
@DomainModel | ||
public class PaymentMethod { | ||
} |
27 changes: 27 additions & 0 deletions
27
.../com/tngtech/archunit/example/onionarchitecture_by_annotations/onion/product/Product.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,27 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.product; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.DomainModel; | ||
|
||
@DomainModel | ||
public class Product { | ||
// Dependency on ProductId violates the architecture, since ProductId resides with persistence adapter | ||
private final ProductId id; | ||
// Dependency on ProductName violates the architecture, since ProductName is located in the DomainService layer | ||
private final ProductName name; | ||
|
||
public Product(ProductId id, ProductName name) { | ||
if (id == null) { | ||
throw new IllegalArgumentException("Product id must not be null"); | ||
} | ||
if (name == null) { | ||
throw new IllegalArgumentException("Product name must not be null"); | ||
} | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "{id=" + id + ", name=" + name + '}'; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...om/tngtech/archunit/example/onionarchitecture_by_annotations/onion/product/ProductId.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,23 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.product; | ||
|
||
import java.util.UUID; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.Adapter; | ||
|
||
@Adapter("persistence") | ||
@SuppressWarnings("unused") | ||
public class ProductId { | ||
private final UUID id; | ||
|
||
public ProductId(UUID id) { | ||
if (id == null) { | ||
throw new IllegalArgumentException("ID must not be null"); | ||
} | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "{id=" + id + '}'; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...archunit/example/onionarchitecture_by_annotations/onion/product/ProductJpaRepository.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,17 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.product; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.Adapter; | ||
|
||
@Adapter("persistence") | ||
@SuppressWarnings("unused") | ||
public class ProductJpaRepository implements ProductRepository { | ||
@Override | ||
public Product read(ProductId id) { | ||
return new Product(id, new ProductName("would normally be read")); | ||
} | ||
|
||
@Override | ||
public long getTotalCount() { | ||
return 0; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
.../tngtech/archunit/example/onionarchitecture_by_annotations/onion/product/ProductName.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,21 @@ | ||
package com.tngtech.archunit.example.onionarchitecture_by_annotations.onion.product; | ||
|
||
import com.tngtech.archunit.example.onionarchitecture_by_annotations.annotations.DomainService; | ||
|
||
@DomainService | ||
@SuppressWarnings("unused") | ||
public class ProductName { | ||
private final String name; | ||
|
||
public ProductName(String name) { | ||
if (name == null || name.isEmpty()) { | ||
throw new IllegalArgumentException("Name must not be empty"); | ||
} | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "{name='" + name + '\'' + '}'; | ||
} | ||
} |
Oops, something went wrong.