Skip to content

Commit

Permalink
Added JavaCodeUnit.getCallsFromSelf() and JavaCodeUnit.getAccessesFro…
Browse files Browse the repository at this point in the history
…mSelf() for convenience.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric committed Mar 16, 2019
1 parent 4a413be commit 5025a5c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.base.ChainableFunction;
import com.tngtech.archunit.base.DescribedPredicate;
Expand Down Expand Up @@ -120,6 +121,22 @@ public Set<JavaConstructorCall> getConstructorCallsFromSelf() {
return constructorCalls;
}

@PublicAPI(usage = ACCESS)
public Set<JavaCall<?>> getCallsFromSelf() {
return ImmutableSet.<JavaCall<?>>builder()
.addAll(getMethodCallsFromSelf())
.addAll(getConstructorCallsFromSelf())
.build();
}

@PublicAPI(usage = ACCESS)
public Set<JavaAccess<?>> getAccessesFromSelf() {
return ImmutableSet.<JavaAccess<?>>builder()
.addAll(getCallsFromSelf())
.addAll(getFieldAccesses())
.build();
}

@PublicAPI(usage = ACCESS)
public boolean isConstructor() {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.tngtech.archunit.core.domain;

import org.junit.Test;

import static com.google.common.collect.Sets.union;
import static com.tngtech.archunit.core.domain.TestUtils.importClassWithContext;
import static org.assertj.core.api.Assertions.assertThat;

public class JavaCodeUnitTest {

@Test
public void offers_all_calls_from_Self() {
JavaMethod method = importClassWithContext(ClassAccessingOtherClass.class).getMethod("access", ClassBeingAccessed.class);

assertThat(method.getCallsFromSelf())
.hasSize(4)
.containsOnlyElementsOf(union(method.getConstructorCallsFromSelf(), method.getMethodCallsFromSelf()));
}

@Test
public void offers_all_accesses_from_Self() {
JavaMethod method = importClassWithContext(ClassAccessingOtherClass.class).getMethod("access", ClassBeingAccessed.class);

assertThat(method.getAccessesFromSelf())
.hasSize(6)
.containsOnlyElementsOf(union(union(
method.getConstructorCallsFromSelf(),
method.getMethodCallsFromSelf()),
method.getFieldAccesses()));
}

@SuppressWarnings("unused")
private static class ClassAccessingOtherClass {
void access(ClassBeingAccessed classBeingAccessed) {
new ClassBeingAccessed();
new ClassBeingAccessed("");
classBeingAccessed.field1 = "";
classBeingAccessed.field2 = null;
classBeingAccessed.method1();
classBeingAccessed.method2();
}
}

private static class ClassBeingAccessed {
String field1;
Object field2;

ClassBeingAccessed() {
}

ClassBeingAccessed(String field1) {
this.field1 = field1;
}

void method1() {
}

void method2() {
}
}
}

0 comments on commit 5025a5c

Please sign in to comment.