Skip to content

Commit

Permalink
Add tests to verify that Java6 Assertions are in sync with its BDD an…
Browse files Browse the repository at this point in the history
…d Soft assertions

Add sync test between BDD and BDD soft assertions.
  • Loading branch information
joel-costigliola committed Mar 4, 2017
1 parent 3876b60 commit 0d586a5
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 31 deletions.
Expand Up @@ -12,7 +12,7 @@
*/
package org.assertj.core.api;

import org.assertj.core.util.CheckReturnValue;
import static org.assertj.core.api.Assertions.catchThrowable;

import java.io.File;
import java.io.InputStream;
Expand All @@ -38,7 +38,7 @@
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.atomic.AtomicStampedReference;

import static org.assertj.core.api.Assertions.catchThrowable;
import org.assertj.core.util.CheckReturnValue;

/**
* AbstractBDDSoftAssertions compatible with Android. Duplicated from {@link AbstractBDDSoftAssertions}.
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/assertj/core/api/Java6Assertions.java
Expand Up @@ -121,7 +121,7 @@ public static AtomicIntegerArrayAssert assertThat(AtomicIntegerArray actual) {
*/
@CheckReturnValue
public static <OBJECT> AtomicIntegerFieldUpdaterAssert<OBJECT> assertThat(AtomicIntegerFieldUpdater<OBJECT> actual) {
return new AtomicIntegerFieldUpdaterAssert<OBJECT>(actual);
return new AtomicIntegerFieldUpdaterAssert<>(actual);
}

/**
Expand Down Expand Up @@ -161,7 +161,7 @@ public static AtomicLongArrayAssert assertThat(AtomicLongArray actual) {
*/
@CheckReturnValue
public static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT> assertThat(AtomicLongFieldUpdater<OBJECT> actual) {
return new AtomicLongFieldUpdaterAssert<OBJECT>(actual);
return new AtomicLongFieldUpdaterAssert<>(actual);
}

/**
Expand All @@ -175,7 +175,7 @@ public static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT> assertThat(AtomicLon
*/
@CheckReturnValue
public static <VALUE> AtomicReferenceAssert<VALUE> assertThat(AtomicReference<VALUE> actual) {
return new AtomicReferenceAssert<VALUE>(actual);
return new AtomicReferenceAssert<>(actual);
}

/**
Expand Down Expand Up @@ -409,7 +409,7 @@ public static <T extends Comparable<? super T>> AbstractComparableAssert<?, T> a
* @return the created assertion object.
*/
@CheckReturnValue
public static <T> IterableAssert<T> assertThat(Iterable<? extends T> actual) {
public static <T> AbstractIterableAssert<?, Iterable<? extends T>, T, ObjectAssert<T>> assertThat(Iterable<? extends T> actual) {
return new IterableAssert<>(actual);
}

Expand All @@ -424,7 +424,7 @@ public static <T> IterableAssert<T> assertThat(Iterable<? extends T> actual) {
* @return the created assertion object.
*/
@CheckReturnValue
public static <T> IterableAssert<T> assertThat(Iterator<? extends T> actual) {
public static <T> AbstractIterableAssert<?, Iterable<? extends T>, T, ObjectAssert<T>> assertThat(Iterator<? extends T> actual) {
return new IterableAssert<>(actual);
}

Expand Down Expand Up @@ -482,7 +482,7 @@ public static AbstractFileAssert<?> assertThat(File actual) {
* @since 2.7.0 / 3.7.0
*/
@CheckReturnValue
public static <RESULT> FutureAssert<RESULT> assertThat(Future<RESULT> actual) {
public static <RESULT> AbstractFutureAssert<?, ? extends Future<? extends RESULT>, RESULT> assertThat(Future<RESULT> actual) {
return new FutureAssert<>(actual);
}

Expand Down Expand Up @@ -570,7 +570,7 @@ public static AbstractIntegerAssert<?> assertThat(Integer actual) {
* @return the created assertion object.
*/
@CheckReturnValue
public static <T> ListAssert<T> assertThat(List<? extends T> actual) {
public static <T> AbstractListAssert<?, List<? extends T>, T, ObjectAssert<T>> assertThat(List<? extends T> actual) {
return new ListAssert<>(actual);
}

Expand Down
Expand Up @@ -21,10 +21,10 @@
/**
* @author Filip Hrisafov
*/
public class AssertionsTest extends BaseAssertionsTest {
public class Assertions_sync_assertThat_with_BDD_and_Soft_variants_Test extends BaseAssertionsTest {

@Test
public void should_have_the_same_methods_as_in_bdd_assertions() {
public void standard_assertions_and_bdd_assertions_should_have_the_same_assertions_methods() {
Method[] assertThatMethods = findMethodsWithName(Assertions.class, "assertThat");
Method[] thenMethods = findMethodsWithName(BDDAssertions.class, "then");

Expand All @@ -33,15 +33,28 @@ public void should_have_the_same_methods_as_in_bdd_assertions() {
}

@Test
public void should_have_the_same_methods_as_in_standard_soft_assertions() {
public void standard_assertions_and_soft_assertions_should_have_the_same_assertions_methods() {
// Until the SpecialIgnoredReturnTypes like AssertProvider, XXXNavigableXXXAssert are implemented for
// the soft assertions we need to ignore them
Method[] assertThatMethods = findMethodsWithName(Assertions.class, "assertThat", SPECIAL_IGNORED_RETURN_TYPES);
Method[] assertThatSoftMethods = findMethodsWithName(AbstractStandardSoftAssertions.class, "assertThat");

// ignore the return type of soft assertions until they have the same as the Assertions
// ignore the return type of soft assertions until they have the same as the Assertions
assertThat(assertThatMethods).usingElementComparator(IGNORING_DECLARING_CLASS_AND_RETURN_TYPE)
.containsExactlyInAnyOrder(assertThatSoftMethods);

}

@Test
public void bdd_assertions_and_bdd_soft_assertions_should_have_the_same_assertions_methods() {
// Until the SpecialIgnoredReturnTypes like AssertProvider, XXXNavigableXXXAssert are implemented for
// the soft assertions we need to ignore them
Method[] thenMethods = findMethodsWithName(BDDAssertions.class, "then", SPECIAL_IGNORED_RETURN_TYPES);
Method[] thenSoftMethods = findMethodsWithName(AbstractBDDSoftAssertions.class, "then");

// ignore the return type of soft assertions until they have the same as the Assertions
assertThat(thenMethods).usingElementComparator(IGNORING_DECLARING_CLASS_AND_RETURN_TYPE)
.containsExactlyInAnyOrder(thenSoftMethods);

}
}
37 changes: 19 additions & 18 deletions src/test/java/org/assertj/core/api/BaseAssertionsTest.java
Expand Up @@ -12,33 +12,42 @@
*/
package org.assertj.core.api;

import static org.assertj.core.util.Arrays.array;
import static org.assertj.core.util.Sets.newLinkedHashSet;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import org.assertj.core.util.Sets;

/**
* @author Filip Hrisafov
*/
public class BaseAssertionsTest {

private static final int ACCESS_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;

protected static final Comparator<Method> IGNORING_DECLARING_CLASS_AND_METHOD_NAME = internalMethodComparator(true, false, true);
protected static final Comparator<Method> IGNORING_DECLARING_CLASS_AND_RETURN_TYPE = internalMethodComparator(true, true, false);

//Object is ignored because of the AssertProvider
protected static final Class<?>[] SPECIAL_IGNORED_RETURN_TYPES = new Class[] { AssertDelegateTarget.class,
FactoryBasedNavigableListAssert.class, FactoryBasedNavigableIterableAssert.class,
ClassBasedNavigableListAssert.class, ClassBasedNavigableIterableAssert.class, Object.class};
protected static final Comparator<Method> IGNORING_DECLARING_CLASS_AND_METHOD_NAME = internalMethodComparator(true,
false,
true);

protected static final Comparator<Method> IGNORING_DECLARING_CLASS_AND_RETURN_TYPE = internalMethodComparator(true,
true,
false);

// Object is ignored because of the AssertProvider
protected static final Class<?>[] SPECIAL_IGNORED_RETURN_TYPES = array(AssertDelegateTarget.class,
FactoryBasedNavigableListAssert.class,
FactoryBasedNavigableIterableAssert.class,
ClassBasedNavigableListAssert.class,
ClassBasedNavigableIterableAssert.class,
Object.class);

protected static Method[] findMethodsWithName(Class<?> clazz, String name, Class<?>... ignoredReturnTypes) {
List<Method> matchingMethods = new ArrayList<>();
Set<Class<?>> ignoredReturnTypesSet = Sets.newLinkedHashSet(ignoredReturnTypes);
Set<Class<?>> ignoredReturnTypesSet = newLinkedHashSet(ignoredReturnTypes);
for (Method method : clazz.getMethods()) {
if (!ignoredReturnTypesSet.contains(method.getReturnType()) && method.getName().equals(name)) {
matchingMethods.add(method);
Expand All @@ -47,14 +56,6 @@ protected static Method[] findMethodsWithName(Class<?> clazz, String name, Class
return matchingMethods.toArray(new Method[matchingMethods.size()]);
}

protected static Comparator<Method> ignoringDeclaringClassAndMethodName() {
return internalMethodComparator(true, false, true);
}

protected static Comparator<Method> ignoringDeclaringClassAndReturnType() {
return internalMethodComparator(true, true, false);
}

private static Comparator<Method> internalMethodComparator(final boolean ignoreDeclaringClass,
final boolean ignoreReturnType,
final boolean ignoreMethodName) {
Expand Down
@@ -0,0 +1,60 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2017 the original author or authors.
*/
package org.assertj.core.api;

import static org.assertj.core.api.Assertions.assertThat;

import java.lang.reflect.Method;

import org.junit.Test;

/**
* @author Filip Hrisafov
*/
public class Java6Assertions_sync_assertThat_with_BDD_and_Soft_variants_Test extends BaseAssertionsTest {

@Test
public void standard_assertions_and_bdd_assertions_should_have_the_same_assertions_methods() {
Method[] assertThatMethods = findMethodsWithName(Java6Assertions.class, "assertThat");
Method[] thenMethods = findMethodsWithName(Java6BDDAssertions.class, "then");

assertThat(assertThatMethods).usingElementComparator(IGNORING_DECLARING_CLASS_AND_METHOD_NAME)
.containsExactlyInAnyOrder(thenMethods);
}

@Test
public void standard_assertions_and_soft_assertions_should_have_the_same_assertions_methods() {
// Until the SpecialIgnoredReturnTypes like AssertProvider, XXXNavigableXXXAssert are implemented for
// the soft assertions we need to ignore them
Method[] assertThatMethods = findMethodsWithName(Java6Assertions.class, "assertThat", SPECIAL_IGNORED_RETURN_TYPES);
Method[] assertThatSoftMethods = findMethodsWithName(Java6AbstractStandardSoftAssertions.class, "assertThat");

// ignore the return type of soft assertions until they have the same as the Assertions
assertThat(assertThatMethods).usingElementComparator(IGNORING_DECLARING_CLASS_AND_RETURN_TYPE)
.containsExactlyInAnyOrder(assertThatSoftMethods);

}

@Test
public void bdd_assertions_and_bdd_soft_assertions_should_have_the_same_assertions_methods() {
// Until the SpecialIgnoredReturnTypes like AssertProvider, XXXNavigableXXXAssert are implemented for
// the soft assertions we need to ignore them
Method[] thenMethods = findMethodsWithName(Java6BDDAssertions.class, "then", SPECIAL_IGNORED_RETURN_TYPES);
Method[] thenSoftMethods = findMethodsWithName(Java6AbstractBDDSoftAssertions.class, "then");

// ignore the return type of soft assertions until they have the same as the Assertions
assertThat(thenMethods).usingElementComparator(IGNORING_DECLARING_CLASS_AND_RETURN_TYPE)
.containsExactlyInAnyOrder(thenSoftMethods);

}
}

0 comments on commit 0d586a5

Please sign in to comment.