Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Jan 20, 2019
1 parent 82293c1 commit ebbc89b
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 56 deletions.
18 changes: 11 additions & 7 deletions integration-test/src/main/resources/META-INF/deptective.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{
"packages" : [
"components" : [
{
"name" : "com.example.ui",
"name" : "ui",
"contains" : [ "com.example.ui" ],
"reads" : [
"com.example.persistence",
"com.example.service"
"persistence",
"service"
]
},
{
"name" : "com.example.persistence"
"name" : "persistence",
"contains" : [ "com.example.persistence" ]
},
{
"name" : "com.example.service"
"name" : "service",
"contains" : [ "com.example.service" ]
},
{
"name" : "com.example.rest"
"name" : "rest",
"contains" : [ "com.example.rest" ]
}
],
"whitelisted" : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ public class JsonSerializer implements ModelSerializer {
private final ObjectMapper mapper;

private final ObjectNode root;
private final ArrayNode packages;
private final ArrayNode components;
private final ArrayNode whitelisted;

public JsonSerializer() {
mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);

root = mapper.createObjectNode();
packages = root.putArray("packages");
components = root.putArray("components");
whitelisted = root.putArray("whitelisted");
}

@Override
public void addComponent(Component component) {
packages.add(toJsonNode(component, mapper));
components.add(toJsonNode(component, mapper));
}

@Override
Expand All @@ -72,6 +72,14 @@ private JsonNode toJsonNode(Component component, ObjectMapper mapper) {

node.put("name", component.getName());

if (!component.getContained().isEmpty()) {
ArrayNode reads = node.putArray("contains");
component.getContained()
.stream()
.sorted()
.forEach(r -> reads.add(r.toString()));
}

if (!component.getReads().isEmpty()) {
ArrayNode reads = node.putArray("reads");
component.getReads()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public boolean onEnteringCompilationUnit(CompilationUnitTree tree) {

currentPackageName = packageNameTree.toString();
packagesOfCurrentCompilation.add(currentPackageName);

builder.addContains(currentPackageName, PackagePattern.getPattern(currentPackageName));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.moditect.deptective.internal.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -57,11 +58,16 @@ public Builder addRead(String read, ReadKind readKind) {
return this;
}

public Builder addContains(List<PackagePattern> contains) {
public Builder addContains(Collection<PackagePattern> contains) {
contained.addAll(contains);
return this;
}

public Builder addContains(PackagePattern contains) {
contained.add(contains);
return this;
}

public Component build() {
return new Component(name, contained, reads);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -71,23 +70,23 @@ public PackageDependencies getPackageDependencies() {

private PackageDependencies parseConfig(InputStream config) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
return parsePackages(objectMapper.readTree(config));
return parseComponents(objectMapper.readTree(config));
}

private PackageDependencies parseConfig(String config) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
return parsePackages(objectMapper.readTree(config));
return parseComponents(objectMapper.readTree(config));
}

private PackageDependencies parsePackages(JsonNode config) throws IOException {
private PackageDependencies parseComponents(JsonNode config) throws IOException {
PackageDependencies.Builder builder = PackageDependencies.builder();

ArrayNode packages = (ArrayNode) config.get("packages");
ArrayNode components = (ArrayNode) config.get("components");

if (packages != null) {
Iterator<JsonNode> it = packages.iterator();
if (components != null) {
Iterator<JsonNode> it = components.iterator();
while (it.hasNext()) {
parsePackage(it.next(), builder);
parseComponent(it.next(), builder);
}
}

Expand All @@ -104,11 +103,12 @@ private PackageDependencies parsePackages(JsonNode config) throws IOException {
return builder.build();
}

private void parsePackage(JsonNode pakkage, Builder builder) {
String name = pakkage.get("name").asText();
List<String> reads = parseReads((ArrayNode) (pakkage.get("reads")));
private void parseComponent(JsonNode component, Builder builder) {
String name = component.get("name").asText();
List<PackagePattern> contains = parseContains((ArrayNode) (component.get("contains")));
List<String> reads = parseReads((ArrayNode) (component.get("reads")));

builder.addComponent(name, Arrays.asList(PackagePattern.getPattern(name)), reads);
builder.addComponent(name, contains, reads);
}

private List<String> parseReads(ArrayNode arrayNode) {
Expand All @@ -125,4 +125,19 @@ private List<String> parseReads(ArrayNode arrayNode) {

return packages;
}

private List<PackagePattern> parseContains(ArrayNode arrayNode) {
if (arrayNode == null) {
return Collections.emptyList();
}

Iterator<JsonNode> it = arrayNode.iterator();
List<PackagePattern> components = new ArrayList<>();

while (it.hasNext()) {
components.add(PackagePattern.getPattern(it.next().asText()));
}

return components;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public void addComponent(String name, List<PackagePattern> contains, List<String
);
}

public void addContains(String componentName, PackagePattern contained) {
Component.Builder builder = componentsByName.computeIfAbsent(componentName, n -> Component.builder(n));
builder.addContains(contained);
}

public void addRead(String name, String readComponent, ReadKind readKind) {
Component.Builder builder = componentsByName.computeIfAbsent(name, n -> Component.builder(n));
builder.addRead(readComponent, readKind);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,25 @@ public void shouldLoadConfig() throws Exception {
PackageDependencies dependencies = new ConfigParser(
lines(
"{",
" \"packages\" : [",
" \"components\" : [",
" {",
" \"name\" : \"com.example.ui\",",
" \"name\" : \"ui\",",
" \"contains\" : [ \"com.example.ui\" ],",
" \"reads\" : [",
" \"com.example.service\",",
" \"com.example.persistence\"",
" \"service\",",
" \"persistence\"",
" ]",
" },",
" {",
" \"name\" : \"com.example.service\",",
" \"name\" : \"service\",",
" \"contains\" : [ \"com.example.service\" ],",
" \"reads\" : [",
" \"com.example.persistence\"",
" \"persistence\"",
" ]",
" },",
" {",
" \"name\" : \"persistence\",",
" \"contains\" : [ \"com.example.persistence\" ]",
" }",
" ],",
" \"whitelisted\" : [",
Expand All @@ -56,14 +62,17 @@ public void shouldLoadConfig() throws Exception {

Component ui = dependencies.getComponentByPackage("com.example.ui");
assertThat(ui).isNotNull();
assertThat(ui.allowedToRead(component("com.example.service"))).isTrue();
assertThat(ui.allowedToRead(component("com.example.persistence"))).isTrue();
assertThat(ui.getName()).isEqualTo("ui");
assertThat(ui.containsPackage("com.example.ui")).isTrue();
assertThat(ui.allowedToRead(component("service"))).isTrue();
assertThat(ui.allowedToRead(component("persistence"))).isTrue();

Component service = dependencies.getComponentByPackage("com.example.service");
assertThat(service).isNotNull();
assertThat(service).isNotNull();
assertThat(service.allowedToRead(component("com.example.ui"))).isFalse();
assertThat(service.allowedToRead(component("com.example.persistence"))).isTrue();
assertThat(service.getName()).isEqualTo("service");
assertThat(service.containsPackage("com.example.service")).isTrue();
assertThat(service.allowedToRead(component("ui"))).isFalse();
assertThat(service.allowedToRead(component("persistence"))).isTrue();

assertThat(dependencies.isWhitelisted("java.awt")).isTrue();
assertThat(dependencies.isWhitelisted("java.awt.color")).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,21 @@ public void shouldGenerateConfig() throws Exception {

String expectedConfig = lines(
"{",
" \"packages\" : [ {",
" \"components\" : [ {",
" \"name\" : \"org.moditect.deptective.plugintest.analyze.bar\",",
" \"contains\" : [ \"org.moditect.deptective.plugintest.analyze.bar\" ],",
" \"reads\" : [ \"org.moditect.deptective.plugintest.analyze.qux\" ]",
" }, {",
" \"name\" : \"org.moditect.deptective.plugintest.analyze.foo\",",
" \"contains\" : [ \"org.moditect.deptective.plugintest.analyze.foo\" ],",
" \"reads\" : [",
" \"org.moditect.deptective.plugintest.analyze.qux\",",
" \"org.moditect.deptective.plugintest.analyze.bar\" ]\n",
" } ],",
" \"whitelisted\" : [ \"java.math\" ]",
" }]"
" \"org.moditect.deptective.plugintest.analyze.bar\" ]",
" }, {",
" \"name\" : \"org.moditect.deptective.plugintest.analyze.qux\",",
" \"contains\" : [ \"org.moditect.deptective.plugintest.analyze.qux\" ]",
" } ]",
"}"
);

Optional<JavaFileObject> deptectiveFile = compilation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public void shouldGenerateConfig() throws Exception {

String expectedConfig = lines(
"{",
" \"packages\" : [ {",
" \"name\" : \"org.moditect.deptective.plugintest.analyzewhitelistallexternal.bar\"",
" \"components\" : [ {",
" \"name\" : \"org.moditect.deptective.plugintest.analyzewhitelistallexternal.bar\",",
" \"contains\" : [ \"org.moditect.deptective.plugintest.analyzewhitelistallexternal.bar\" ]",
" }, {",
" \"name\" : \"org.moditect.deptective.plugintest.analyzewhitelistallexternal.foo\",",
" \"contains\" : [ \"org.moditect.deptective.plugintest.analyzewhitelistallexternal.foo\" ],",
" \"reads\" : [ \"org.moditect.deptective.plugintest.analyzewhitelistallexternal.bar\" ]",
" } ],",
" \"whitelisted\" : [ \"java.io\", \"java.math\", \"java.net\" ]",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
{
"packages" : [
"components" : [
{
"name" : "org.moditect.deptective.plugintest.basic.barctorcall"
"name" : "org.moditect.deptective.plugintest.basic.barctorcall",
"contains" : [ "org.moditect.deptective.plugintest.basic.barctorcall" ]
},
{
"name" : "org.moditect.deptective.plugintest.basic.barfield"
"name" : "org.moditect.deptective.plugintest.basic.barfield",
"contains" : [ "org.moditect.deptective.plugintest.basic.barfield" ]
},
{
"name" : "org.moditect.deptective.plugintest.basic.barlocalvar"
"name" : "org.moditect.deptective.plugintest.basic.barlocalvar",
"contains" : [ "org.moditect.deptective.plugintest.basic.barlocalvar" ]
},
{
"name" : "org.moditect.deptective.plugintest.basic.barloopvar"
"name" : "org.moditect.deptective.plugintest.basic.barloopvar",
"contains" : [ "org.moditect.deptective.plugintest.basic.barloopvar" ]
},
{
"name" : "org.moditect.deptective.plugintest.basic.barparameter"
"name" : "org.moditect.deptective.plugintest.basic.barparameter",
"contains" : [ "org.moditect.deptective.plugintest.basic.barparameter" ]
},
{
"name" : "org.moditect.deptective.plugintest.basic.barretval"
"name" : "org.moditect.deptective.plugintest.basic.barretval",
"contains" : [ "org.moditect.deptective.plugintest.basic.barretval" ]
},
{
"name" : "org.moditect.deptective.plugintest.basic.bartypearg"
"name" : "org.moditect.deptective.plugintest.basic.bartypearg",
"contains" : [ "org.moditect.deptective.plugintest.basic.bartypearg" ]
},
{
"name" : "org.moditect.deptective.plugintest.basic.foo",
"contains" : [ "org.moditect.deptective.plugintest.basic.foo" ],
"reads" : [
"java.util"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"packages" : [
"components" : [
{
"name" : "com.example.foo"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"packages" : [
"components" : [
]
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"packages" : [
"components" : [
{
"name" : "org.moditect.deptective.plugintest.visualize.bar",
"contains" : [ "org.moditect.deptective.plugintest.visualize.bar" ],
"reads" : [
"org.moditect.deptective.plugintest.visualize.qux"
]
},
{
"name" : "org.moditect.deptective.plugintest.visualize.foo",
"contains" : [ "org.moditect.deptective.plugintest.visualize.foo" ],
"reads" : [
"org.moditect.deptective.plugintest.visualize.qux"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"packages" : [
"components" : [
{
"name" : "org.moditect.deptective.plugintest.whitelist.foo"
"name" : "org.moditect.deptective.plugintest.whitelist.foo",
"contains" : [ "org.moditect.deptective.plugintest.whitelist.foo" ]
},
{
"name" : "org.moditect.deptective.plugintest.whitelist.bar"
"name" : "org.moditect.deptective.plugintest.whitelist.bar",
"contains" : [ "org.moditect.deptective.plugintest.whitelist.bar" ]
}
],
"whitelisted" : [
Expand Down

0 comments on commit ebbc89b

Please sign in to comment.