Skip to content

Commit

Permalink
Added negations to CodeUnitsShould.*(..)
Browse files Browse the repository at this point in the history
Issue: #38
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric committed Mar 31, 2019
1 parent 1a49eb1 commit 11a3847
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.tngtech.archunit.lang.syntax.elements.CodeUnitsShould;
import com.tngtech.archunit.lang.syntax.elements.CodeUnitsShouldConjunction;

import static com.tngtech.archunit.lang.conditions.ArchConditions.not;

abstract class AbstractCodeUnitsShouldInternal<CODE_UNIT extends JavaCodeUnit, SELF extends AbstractCodeUnitsShouldInternal<CODE_UNIT, SELF>>
extends AbstractMembersShouldInternal<CODE_UNIT, SELF>
implements CodeUnitsShould<SELF>, CodeUnitsShouldConjunction<CODE_UNIT> {
Expand Down Expand Up @@ -61,46 +63,91 @@ public SELF haveRawParameterTypes(Class<?>... parameterTypes) {
return addCondition(ArchConditions.haveRawParameterTypes(parameterTypes));
}

@Override
public SELF notHaveRawParameterTypes(Class<?>... parameterTypes) {
return addCondition(not(ArchConditions.haveRawParameterTypes(parameterTypes)));
}

@Override
public SELF haveRawParameterTypes(String... parameterTypeNames) {
return addCondition(ArchConditions.haveRawParameterTypes(parameterTypeNames));
}

@Override
public SELF notHaveRawParameterTypes(String... parameterTypeNames) {
return addCondition(not(ArchConditions.haveRawParameterTypes(parameterTypeNames)));
}

@Override
public SELF haveRawParameterTypes(DescribedPredicate<List<JavaClass>> predicate) {
return addCondition(ArchConditions.haveRawParameterTypes(predicate));
}

@Override
public SELF notHaveRawParameterTypes(DescribedPredicate<List<JavaClass>> predicate) {
return addCondition(not(ArchConditions.haveRawParameterTypes(predicate)));
}

@Override
public SELF haveRawReturnType(Class<?> type) {
return addCondition(ArchConditions.haveRawReturnType(type));
}

@Override
public SELF notHaveRawReturnType(Class<?> type) {
return addCondition(not(ArchConditions.haveRawReturnType(type)));
}

@Override
public SELF haveRawReturnType(String typeName) {
return addCondition(ArchConditions.haveRawReturnType(typeName));
}

@Override
public SELF notHaveRawReturnType(String typeName) {
return addCondition(not(ArchConditions.haveRawReturnType(typeName)));
}

@Override
public SELF haveRawReturnType(DescribedPredicate<JavaClass> predicate) {
return addCondition(ArchConditions.haveRawReturnType(predicate));
}

@Override
public SELF notHaveRawReturnType(DescribedPredicate<JavaClass> predicate) {
return addCondition(not(ArchConditions.haveRawReturnType(predicate)));
}

@Override
public SELF declareThrowableOfType(Class<? extends Throwable> type) {
return addCondition(ArchConditions.declareThrowableOfType(type));
}

@Override
public SELF notDeclareThrowableOfType(Class<? extends Throwable> type) {
return addCondition(not(ArchConditions.declareThrowableOfType(type)));
}

@Override
public SELF declareThrowableOfType(String typeName) {
return addCondition(ArchConditions.declareThrowableOfType(typeName));
}

@Override
public SELF notDeclareThrowableOfType(String typeName) {
return addCondition(not(ArchConditions.declareThrowableOfType(typeName)));
}

@Override
public SELF declareThrowableOfType(DescribedPredicate<JavaClass> predicate) {
return addCondition(ArchConditions.declareThrowableOfType(predicate));
}

@Override
public SELF notDeclareThrowableOfType(DescribedPredicate<JavaClass> predicate) {
return addCondition(not(ArchConditions.declareThrowableOfType(predicate)));
}

static class CodeUnitsShouldInternal extends AbstractCodeUnitsShouldInternal<JavaCodeUnit, CodeUnitsShouldInternal> {

CodeUnitsShouldInternal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ public interface CodeUnitsShould<CONJUNCTION extends CodeUnitsShouldConjunction<
@PublicAPI(usage = ACCESS)
CONJUNCTION haveRawParameterTypes(Class<?>... parameterTypes);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} do not have the specified raw parameter types.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#notHaveRawParameterTypes(Class[]) notHaveRawParameterTypes(String.class, int.class)}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
* <pre><code>
* class Example {
* void someMethod(String stringParam, int intParam) {...}
* }
* </code></pre>
*
* @param parameterTypes Parameter types {@link JavaCodeUnit JavaCodeUnits} should not have
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notHaveRawParameterTypes(Class<?>... parameterTypes);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} have the specified fully qualified raw parameter type names.
* <br><br>
Expand All @@ -69,6 +90,27 @@ public interface CodeUnitsShould<CONJUNCTION extends CodeUnitsShouldConjunction<
@PublicAPI(usage = ACCESS)
CONJUNCTION haveRawParameterTypes(String... parameterTypeNames);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} do not have the specified fully qualified raw parameter type names.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#notHaveRawParameterTypes(String[]) notHaveRawParameterTypes(String.class.getName(), int.class.getName())}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
* <pre><code>
* class Example {
* void someMethod(String stringParam, int intParam) {...}
* }
* </code></pre>
*
* @param parameterTypeNames Fully qualified names of parameter types {@link JavaCodeUnit JavaCodeUnits} should not have
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notHaveRawParameterTypes(String... parameterTypeNames);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} have raw parameter types matching the given predicate.
* <br><br>
Expand All @@ -91,7 +133,28 @@ public interface CodeUnitsShould<CONJUNCTION extends CodeUnitsShouldConjunction<
CONJUNCTION haveRawParameterTypes(DescribedPredicate<List<JavaClass>> predicate);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} have the specified raw return types.
* Asserts that {@link JavaCodeUnit JavaCodeUnits} do not have raw parameter types matching the given predicate.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#notHaveRawParameterTypes(DescribedPredicate) notHaveRawParameterTypes(whereFirstTypeIs(String.class))}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
* <pre><code>
* class Example {
* void someMethod(String stringParam, int intParam) {...}
* }
* </code></pre>
*
* @param predicate A {@link DescribedPredicate} that determines, which {@link JavaCodeUnit JavaCodeUnits} do not match by their raw parameter types
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notHaveRawParameterTypes(DescribedPredicate<List<JavaClass>> predicate);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} have the specified raw return type.
* <br><br>
* E.g.
* <pre><code>
Expand All @@ -111,6 +174,27 @@ public interface CodeUnitsShould<CONJUNCTION extends CodeUnitsShouldConjunction<
@PublicAPI(usage = ACCESS)
CONJUNCTION haveRawReturnType(Class<?> type);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} do not have the specified raw return type.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#notHaveRawReturnType(Class) notHaveRawReturnType(String.class)}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
* <pre><code>
* class Example {
* String someMethod() {...}
* }
* </code></pre>
*
* @param type Return type {@link JavaCodeUnit JavaCodeUnits} should not have
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notHaveRawReturnType(Class<?> type);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} have the specified fully qualified raw return type name.
* <br><br>
Expand All @@ -132,6 +216,27 @@ public interface CodeUnitsShould<CONJUNCTION extends CodeUnitsShouldConjunction<
@PublicAPI(usage = ACCESS)
CONJUNCTION haveRawReturnType(String typeName);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} do not have the specified fully qualified raw return type name.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#notHaveRawReturnType(String) notHaveRawReturnType(String.class.getName())}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
* <pre><code>
* class Example {
* String someMethod() {...}
* }
* </code></pre>
*
* @param typeName Fully qualified name of a return type {@link JavaCodeUnit JavaCodeUnits} should not have
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notHaveRawReturnType(String typeName);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} have raw return types matching the given predicate.
* <br><br>
Expand All @@ -153,6 +258,27 @@ public interface CodeUnitsShould<CONJUNCTION extends CodeUnitsShouldConjunction<
@PublicAPI(usage = ACCESS)
CONJUNCTION haveRawReturnType(DescribedPredicate<JavaClass> predicate);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} do not have raw return types matching the given predicate.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#notHaveRawReturnType(DescribedPredicate) notHaveRawReturnType(assignableTo(Serializable.class))}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
* <pre><code>
* class Example {
* String someMethod() {...}
* }
* </code></pre>
*
* @param predicate A {@link DescribedPredicate} that determines, which {@link JavaCodeUnit JavaCodeUnits} do not match by their raw return types
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notHaveRawReturnType(DescribedPredicate<JavaClass> predicate);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} declare a {@link Throwable} of the specified type in their throws clause.
* <br><br>
Expand All @@ -174,6 +300,27 @@ public interface CodeUnitsShould<CONJUNCTION extends CodeUnitsShouldConjunction<
@PublicAPI(usage = ACCESS)
CONJUNCTION declareThrowableOfType(Class<? extends Throwable> type);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} do not declare a {@link Throwable} of the specified type in their throws clause.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#notDeclareThrowableOfType(Class) notDeclareThrowableOfType(SomeException.class)}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
* <pre><code>
* class Example {
* void someMethod() throws SomeException {...}
* }
* </code></pre>
*
* @param type Type of a declared {@link Throwable} {@link JavaCodeUnit JavaCodeUnits} should not have
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notDeclareThrowableOfType(Class<? extends Throwable> type);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} declare a {@link Throwable} of the specified fully qualified type name in their throws clause.
* <br><br>
Expand All @@ -195,12 +342,33 @@ public interface CodeUnitsShould<CONJUNCTION extends CodeUnitsShouldConjunction<
@PublicAPI(usage = ACCESS)
CONJUNCTION declareThrowableOfType(String typeName);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} do not declare a {@link Throwable} of the specified fully qualified type name in their throws clause.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#notDeclareThrowableOfType(String) notDeclareThrowableOfType(SomeException.class.getName())}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
* <pre><code>
* class Example {
* void someMethod() throws SomeException {...}
* }
* </code></pre>
*
* @param typeName Fully qualified name of a type of a declared {@link Throwable} {@link JavaCodeUnit JavaCodeUnits} should not have
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notDeclareThrowableOfType(String typeName);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} declare a {@link Throwable} which matches the given predicate.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#declareThrowableOfType(DescribedPredicate) declareThrowableOfType(nameStartingWith("First"))}
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#declareThrowableOfType(DescribedPredicate) declareThrowableOfType(nameStartingWith("Some"))}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
Expand All @@ -215,4 +383,25 @@ public interface CodeUnitsShould<CONJUNCTION extends CodeUnitsShouldConjunction<
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION declareThrowableOfType(DescribedPredicate<JavaClass> predicate);

/**
* Asserts that {@link JavaCodeUnit JavaCodeUnits} do not declare a {@link Throwable} which matches the given predicate.
* <br><br>
* E.g.
* <pre><code>
* {@link ArchRuleDefinition#codeUnits() codeUnits()}.{@link GivenCodeUnits#should() should()}.{@link CodeUnitsShould#notDeclareThrowableOfType(DescribedPredicate) notDeclareThrowableOfType(nameStartingWith("Some"))}
* </code></pre>
* would be violated by <code>someMethod</code> in
*
* <pre><code>
* class Example {
* void someMethod() throws SomeException {...}
* }
* </code></pre>
*
* @param predicate A {@link DescribedPredicate} that determines, which {@link JavaCodeUnit JavaCodeUnits} do not match by their declared {@link Throwable}
* @return A syntax conjunction element, which can be completed to form a full rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notDeclareThrowableOfType(DescribedPredicate<JavaClass> predicate);
}
Loading

0 comments on commit 11a3847

Please sign in to comment.